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
96262801Sdim    explicit wstring_convert(Codecvt* pcvt = new Codecvt);          // explicit in C++14
97227825Stheraven    wstring_convert(Codecvt* pcvt, state_type state);
98262801Sdim    explicit wstring_convert(const byte_string& byte_err,           // explicit in C++14
99227825Stheraven                    const wide_string& wide_err = wide_string());
100262801Sdim    wstring_convert(const wstring_convert&) = delete;               // C++14
101262801Sdim    wstring_convert & operator=(const wstring_convert &) = delete;  // C++14
102227825Stheraven    ~wstring_convert();
103227825Stheraven
104227825Stheraven    wide_string from_bytes(char byte);
105227825Stheraven    wide_string from_bytes(const char* ptr);
106227825Stheraven    wide_string from_bytes(const byte_string& str);
107227825Stheraven    wide_string from_bytes(const char* first, const char* last);
108227825Stheraven
109227825Stheraven    byte_string to_bytes(Elem wchar);
110227825Stheraven    byte_string to_bytes(const Elem* wptr);
111227825Stheraven    byte_string to_bytes(const wide_string& wstr);
112227825Stheraven    byte_string to_bytes(const Elem* first, const Elem* last);
113227825Stheraven
114262801Sdim    size_t converted() const; // noexcept in C++14
115227825Stheraven    state_type state() const;
116227825Stheraven};
117227825Stheraven
118227825Stheraventemplate <class Codecvt, class Elem = wchar_t, class Tr = char_traits<Elem>>
119227825Stheravenclass wbuffer_convert
120227825Stheraven    : public basic_streambuf<Elem, Tr>
121227825Stheraven{
122227825Stheravenpublic:
123227825Stheraven    typedef typename Tr::state_type state_type;
124227825Stheraven
125262801Sdim    explicit wbuffer_convert(streambuf* bytebuf = 0, Codecvt* pcvt = new Codecvt,
126262801Sdim                    state_type state = state_type());       // explicit in C++14
127262801Sdim    wbuffer_convert(const wbuffer_convert&) = delete;               // C++14
128262801Sdim    wbuffer_convert & operator=(const wbuffer_convert &) = delete;  // C++14
129262801Sdim    ~wbuffer_convert();                                             // C++14
130262801Sdim    
131227825Stheraven    streambuf* rdbuf() const;
132227825Stheraven    streambuf* rdbuf(streambuf* bytebuf);
133227825Stheraven
134227825Stheraven    state_type state() const;
135227825Stheraven};
136227825Stheraven
137227825Stheraven// 22.4.1 and 22.4.1.3, ctype:
138227825Stheravenclass ctype_base;
139227825Stheraventemplate <class charT> class ctype;
140227825Stheraventemplate <> class ctype<char>; // specialization
141227825Stheraventemplate <class charT> class ctype_byname;
142227825Stheraventemplate <> class ctype_byname<char>; // specialization
143227825Stheraven
144227825Stheravenclass codecvt_base;
145227825Stheraventemplate <class internT, class externT, class stateT> class codecvt;
146227825Stheraventemplate <class internT, class externT, class stateT> class codecvt_byname;
147227825Stheraven
148227825Stheraven// 22.4.2 and 22.4.3, numeric:
149227825Stheraventemplate <class charT, class InputIterator> class num_get;
150227825Stheraventemplate <class charT, class OutputIterator> class num_put;
151227825Stheraventemplate <class charT> class numpunct;
152227825Stheraventemplate <class charT> class numpunct_byname;
153227825Stheraven
154227825Stheraven// 22.4.4, col lation:
155227825Stheraventemplate <class charT> class collate;
156227825Stheraventemplate <class charT> class collate_byname;
157227825Stheraven
158227825Stheraven// 22.4.5, date and time:
159227825Stheravenclass time_base;
160227825Stheraventemplate <class charT, class InputIterator> class time_get;
161227825Stheraventemplate <class charT, class InputIterator> class time_get_byname;
162227825Stheraventemplate <class charT, class OutputIterator> class time_put;
163227825Stheraventemplate <class charT, class OutputIterator> class time_put_byname;
164227825Stheraven
165227825Stheraven// 22.4.6, money:
166227825Stheravenclass money_base;
167227825Stheraventemplate <class charT, class InputIterator> class money_get;
168227825Stheraventemplate <class charT, class OutputIterator> class money_put;
169227825Stheraventemplate <class charT, bool Intl> class moneypunct;
170227825Stheraventemplate <class charT, bool Intl> class moneypunct_byname;
171227825Stheraven
172227825Stheraven// 22.4.7, message retrieval:
173227825Stheravenclass messages_base;
174227825Stheraventemplate <class charT> class messages;
175227825Stheraventemplate <class charT> class messages_byname;
176227825Stheraven
177227825Stheraven}  // std
178227825Stheraven
179227825Stheraven*/
180227825Stheraven
181227825Stheraven#include <__config>
182227825Stheraven#include <__locale>
183227825Stheraven#include <algorithm>
184227825Stheraven#include <memory>
185227825Stheraven#include <ios>
186227825Stheraven#include <streambuf>
187227825Stheraven#include <iterator>
188227825Stheraven#include <limits>
189249998Sdim#ifndef __APPLE__
190227825Stheraven#include <cstdarg>
191227825Stheraven#endif
192227825Stheraven#include <cstdlib>
193227825Stheraven#include <ctime>
194262801Sdim#if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
195227825Stheraven#include <support/win32/locale_win32.h>
196278724Sdim#elif defined(_NEWLIB_VERSION)
197278724Sdim// FIXME: replace all the uses of _NEWLIB_VERSION with __NEWLIB__ preceded by an
198278724Sdim// include of <sys/cdefs.h> once https://sourceware.org/ml/newlib-cvs/2014-q3/msg00038.html
199278724Sdim// has had a chance to bake for a bit
200278724Sdim#include <support/newlib/xlocale.h>
201278724Sdim#elif !defined(__ANDROID__)
202227825Stheraven#include <nl_types.h>
203278724Sdim#endif
204227825Stheraven
205249998Sdim#ifdef __APPLE__
206243673Stheraven#include <Availability.h>
207243673Stheraven#endif
208243673Stheraven
209232950Stheraven#include <__undef_min_max>
210232950Stheraven
211227825Stheraven#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
212227825Stheraven#pragma GCC system_header
213227825Stheraven#endif
214227825Stheraven
215227825Stheraven_LIBCPP_BEGIN_NAMESPACE_STD
216227825Stheraven
217249998Sdim#if defined(__APPLE__) || defined(__FreeBSD__)
218227825Stheraven#  define _LIBCPP_GET_C_LOCALE 0
219253159Stheraven#elif defined(__NetBSD__)
220253159Stheraven#  define _LIBCPP_GET_C_LOCALE LC_C_LOCALE
221227825Stheraven#else
222227825Stheraven#  define _LIBCPP_GET_C_LOCALE __cloc()
223227825Stheraven   // Get the C locale object
224262801Sdim   _LIBCPP_FUNC_VIS locale_t __cloc();
225227825Stheraven#define __cloc_defined
226227825Stheraven#endif
227227825Stheraven
228227825Stheraventypedef _VSTD::remove_pointer<locale_t>::type __locale_struct;
229227825Stheraventypedef _VSTD::unique_ptr<__locale_struct, decltype(&freelocale)> __locale_unique_ptr;
230232950Stheraven#ifndef _LIBCPP_LOCALE__L_EXTENSIONS
231227825Stheraventypedef _VSTD::unique_ptr<__locale_struct, decltype(&uselocale)> __locale_raii;
232232950Stheraven#endif
233227825Stheraven
234227825Stheraven// OSX has nice foo_l() functions that let you turn off use of the global
235227825Stheraven// locale.  Linux, not so much.  The following functions avoid the locale when
236227825Stheraven// that's possible and otherwise do the wrong thing.  FIXME.
237278724Sdim#if defined(__linux__) || defined(__EMSCRIPTEN__) || defined(_AIX) || \
238278724Sdim    defined(_NEWLIB_VERSION)
239227825Stheraven
240227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
241227825Stheravendecltype(MB_CUR_MAX_L(_VSTD::declval<locale_t>()))
242227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
243227825Stheraven__mb_cur_max_l(locale_t __l)
244227825Stheraven{
245227825Stheraven  return MB_CUR_MAX_L(__l);
246227825Stheraven}
247227825Stheraven#else  // _LIBCPP_LOCALE__L_EXTENSIONS
248262801Sdiminline _LIBCPP_ALWAYS_INLINE
249227825Stheravendecltype(MB_CUR_MAX) __mb_cur_max_l(locale_t __l)
250227825Stheraven{
251227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
252227825Stheraven  return MB_CUR_MAX;
253227825Stheraven}
254227825Stheraven#endif // _LIBCPP_LOCALE__L_EXTENSIONS
255227825Stheraven
256262801Sdiminline _LIBCPP_ALWAYS_INLINE
257227825Stheravenwint_t __btowc_l(int __c, locale_t __l)
258227825Stheraven{
259227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
260227825Stheraven  return btowc_l(__c, __l);
261227825Stheraven#else
262227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
263227825Stheraven  return btowc(__c);
264227825Stheraven#endif
265227825Stheraven}
266227825Stheraven
267262801Sdiminline _LIBCPP_ALWAYS_INLINE
268227825Stheravenint __wctob_l(wint_t __c, locale_t __l)
269227825Stheraven{
270227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
271227825Stheraven  return wctob_l(__c, __l);
272227825Stheraven#else
273227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
274227825Stheraven  return wctob(__c);
275227825Stheraven#endif
276227825Stheraven}
277227825Stheraven
278262801Sdiminline _LIBCPP_ALWAYS_INLINE
279227825Stheravensize_t __wcsnrtombs_l(char *__dest, const wchar_t **__src, size_t __nwc,
280227825Stheraven                      size_t __len, mbstate_t *__ps, locale_t __l)
281227825Stheraven{
282227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
283227825Stheraven  return wcsnrtombs_l(__dest, __src, __nwc, __len, __ps, __l);
284227825Stheraven#else
285227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
286227825Stheraven  return wcsnrtombs(__dest, __src, __nwc, __len, __ps);
287227825Stheraven#endif
288227825Stheraven}
289227825Stheraven
290262801Sdiminline _LIBCPP_ALWAYS_INLINE
291227825Stheravensize_t __wcrtomb_l(char *__s, wchar_t __wc, mbstate_t *__ps, locale_t __l)
292227825Stheraven{
293227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
294227825Stheraven  return wcrtomb_l(__s, __wc, __ps, __l);
295227825Stheraven#else
296227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
297227825Stheraven  return wcrtomb(__s, __wc, __ps);
298227825Stheraven#endif
299227825Stheraven}
300227825Stheraven
301262801Sdiminline _LIBCPP_ALWAYS_INLINE
302227825Stheravensize_t __mbsnrtowcs_l(wchar_t * __dest, const char **__src, size_t __nms,
303227825Stheraven                      size_t __len, mbstate_t *__ps, locale_t __l)
304227825Stheraven{
305227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
306227825Stheraven  return mbsnrtowcs_l(__dest, __src, __nms, __len, __ps, __l);
307227825Stheraven#else
308227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
309227825Stheraven  return mbsnrtowcs(__dest, __src, __nms, __len, __ps);
310227825Stheraven#endif
311227825Stheraven}
312227825Stheraven
313262801Sdiminline _LIBCPP_ALWAYS_INLINE
314227825Stheravensize_t __mbrtowc_l(wchar_t *__pwc, const char *__s, size_t __n,
315227825Stheraven                   mbstate_t *__ps, locale_t __l)
316227825Stheraven{
317227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
318227825Stheraven  return mbrtowc_l(__pwc, __s, __n, __ps, __l);
319227825Stheraven#else
320227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
321227825Stheraven  return mbrtowc(__pwc, __s, __n, __ps);
322227825Stheraven#endif
323227825Stheraven}
324227825Stheraven
325262801Sdiminline _LIBCPP_ALWAYS_INLINE
326227825Stheravenint __mbtowc_l(wchar_t *__pwc, const char *__pmb, size_t __max, locale_t __l)
327227825Stheraven{
328227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
329227825Stheraven  return mbtowc_l(__pwc, __pmb, __max, __l);
330227825Stheraven#else
331227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
332227825Stheraven  return mbtowc(__pwc, __pmb, __max);
333227825Stheraven#endif
334227825Stheraven}
335227825Stheraven
336262801Sdiminline _LIBCPP_ALWAYS_INLINE
337227825Stheravensize_t __mbrlen_l(const char *__s, size_t __n, mbstate_t *__ps, locale_t __l)
338227825Stheraven{
339227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
340227825Stheraven  return mbrlen_l(__s, __n, __ps, __l);
341227825Stheraven#else
342227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
343227825Stheraven  return mbrlen(__s, __n, __ps);
344227825Stheraven#endif
345227825Stheraven}
346227825Stheraven
347262801Sdiminline _LIBCPP_ALWAYS_INLINE
348227825Stheravenlconv *__localeconv_l(locale_t __l)
349227825Stheraven{
350227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
351227825Stheraven  return localeconv_l(__l);
352227825Stheraven#else
353227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
354227825Stheraven  return localeconv();
355227825Stheraven#endif
356227825Stheraven}
357227825Stheraven
358262801Sdiminline _LIBCPP_ALWAYS_INLINE
359227825Stheravensize_t __mbsrtowcs_l(wchar_t *__dest, const char **__src, size_t __len,
360227825Stheraven                     mbstate_t *__ps, locale_t __l)
361227825Stheraven{
362227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
363227825Stheraven  return mbsrtowcs_l(__dest, __src, __len, __ps, __l);
364227825Stheraven#else
365227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
366227825Stheraven  return mbsrtowcs(__dest, __src, __len, __ps);
367227825Stheraven#endif
368227825Stheraven}
369227825Stheraven
370246487Stheraveninline
371227825Stheravenint __snprintf_l(char *__s, size_t __n, locale_t __l, const char *__format, ...) {
372227825Stheraven  va_list __va;
373227825Stheraven  va_start(__va, __format);
374227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
375227825Stheraven  int __res = vsnprintf_l(__s, __n, __l, __format, __va);
376227825Stheraven#else
377227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
378227825Stheraven  int __res = vsnprintf(__s, __n, __format, __va);
379227825Stheraven#endif
380227825Stheraven  va_end(__va);
381227825Stheraven  return __res;
382227825Stheraven}
383227825Stheraven
384246487Stheraveninline
385227825Stheravenint __asprintf_l(char **__s, locale_t __l, const char *__format, ...) {
386227825Stheraven  va_list __va;
387227825Stheraven  va_start(__va, __format);
388227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
389227825Stheraven  int __res = vasprintf_l(__s, __l, __format, __va);
390227825Stheraven#else
391227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
392227825Stheraven  int __res = vasprintf(__s, __format, __va);
393227825Stheraven#endif
394227825Stheraven  va_end(__va);
395227825Stheraven  return __res;
396227825Stheraven}
397227825Stheraven
398246487Stheraveninline
399227825Stheravenint __sscanf_l(const char *__s, locale_t __l, const char *__format, ...) {
400227825Stheraven  va_list __va;
401227825Stheraven  va_start(__va, __format);
402227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
403227825Stheraven  int __res = vsscanf_l(__s, __l, __format, __va);
404227825Stheraven#else
405227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
406227825Stheraven  int __res = vsscanf(__s, __format, __va);
407227825Stheraven#endif
408227825Stheraven  va_end(__va);
409227825Stheraven  return __res;
410227825Stheraven}
411227825Stheraven
412227825Stheraven#endif  // __linux__
413227825Stheraven
414227825Stheraven// __scan_keyword
415227825Stheraven// Scans [__b, __e) until a match is found in the basic_strings range
416227825Stheraven//  [__kb, __ke) or until it can be shown that there is no match in [__kb, __ke).
417227825Stheraven//  __b will be incremented (visibly), consuming CharT until a match is found
418227825Stheraven//  or proved to not exist.  A keyword may be "", in which will match anything.
419227825Stheraven//  If one keyword is a prefix of another, and the next CharT in the input
420227825Stheraven//  might match another keyword, the algorithm will attempt to find the longest
421227825Stheraven//  matching keyword.  If the longer matching keyword ends up not matching, then
422227825Stheraven//  no keyword match is found.  If no keyword match is found, __ke is returned
423227825Stheraven//  and failbit is set in __err.
424227825Stheraven//  Else an iterator pointing to the matching keyword is found.  If more than
425227825Stheraven//  one keyword matches, an iterator to the first matching keyword is returned.
426278724Sdim//  If on exit __b == __e, eofbit is set in __err.  If __case_sensitive is false,
427227825Stheraven//  __ct is used to force to lower case before comparing characters.
428227825Stheraven//  Examples:
429227825Stheraven//  Keywords:  "a", "abb"
430227825Stheraven//  If the input is "a", the first keyword matches and eofbit is set.
431227825Stheraven//  If the input is "abc", no match is found and "ab" are consumed.
432227825Stheraventemplate <class _InputIterator, class _ForwardIterator, class _Ctype>
433227825Stheraven_LIBCPP_HIDDEN
434227825Stheraven_ForwardIterator
435227825Stheraven__scan_keyword(_InputIterator& __b, _InputIterator __e,
436227825Stheraven               _ForwardIterator __kb, _ForwardIterator __ke,
437227825Stheraven               const _Ctype& __ct, ios_base::iostate& __err,
438227825Stheraven               bool __case_sensitive = true)
439227825Stheraven{
440227825Stheraven    typedef typename iterator_traits<_InputIterator>::value_type _CharT;
441232950Stheraven    size_t __nkw = static_cast<size_t>(_VSTD::distance(__kb, __ke));
442227825Stheraven    const unsigned char __doesnt_match = '\0';
443227825Stheraven    const unsigned char __might_match = '\1';
444227825Stheraven    const unsigned char __does_match = '\2';
445227825Stheraven    unsigned char __statbuf[100];
446227825Stheraven    unsigned char* __status = __statbuf;
447227825Stheraven    unique_ptr<unsigned char, void(*)(void*)> __stat_hold(0, free);
448227825Stheraven    if (__nkw > sizeof(__statbuf))
449227825Stheraven    {
450227825Stheraven        __status = (unsigned char*)malloc(__nkw);
451227825Stheraven        if (__status == 0)
452227825Stheraven            __throw_bad_alloc();
453227825Stheraven        __stat_hold.reset(__status);
454227825Stheraven    }
455227825Stheraven    size_t __n_might_match = __nkw;  // At this point, any keyword might match
456227825Stheraven    size_t __n_does_match = 0;       // but none of them definitely do
457227825Stheraven    // Initialize all statuses to __might_match, except for "" keywords are __does_match
458227825Stheraven    unsigned char* __st = __status;
459278724Sdim    for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
460227825Stheraven    {
461227825Stheraven        if (!__ky->empty())
462227825Stheraven            *__st = __might_match;
463227825Stheraven        else
464227825Stheraven        {
465227825Stheraven            *__st = __does_match;
466227825Stheraven            --__n_might_match;
467227825Stheraven            ++__n_does_match;
468227825Stheraven        }
469227825Stheraven    }
470227825Stheraven    // While there might be a match, test keywords against the next CharT
471227825Stheraven    for (size_t __indx = 0; __b != __e && __n_might_match > 0; ++__indx)
472227825Stheraven    {
473227825Stheraven        // Peek at the next CharT but don't consume it
474227825Stheraven        _CharT __c = *__b;
475227825Stheraven        if (!__case_sensitive)
476227825Stheraven            __c = __ct.toupper(__c);
477227825Stheraven        bool __consume = false;
478227825Stheraven        // For each keyword which might match, see if the __indx character is __c
479227825Stheraven        // If a match if found, consume __c
480227825Stheraven        // If a match is found, and that is the last character in the keyword,
481227825Stheraven        //    then that keyword matches.
482227825Stheraven        // If the keyword doesn't match this character, then change the keyword
483227825Stheraven        //    to doesn't match
484227825Stheraven        __st = __status;
485278724Sdim        for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
486227825Stheraven        {
487227825Stheraven            if (*__st == __might_match)
488227825Stheraven            {
489227825Stheraven                _CharT __kc = (*__ky)[__indx];
490227825Stheraven                if (!__case_sensitive)
491227825Stheraven                    __kc = __ct.toupper(__kc);
492227825Stheraven                if (__c == __kc)
493227825Stheraven                {
494227825Stheraven                    __consume = true;
495227825Stheraven                    if (__ky->size() == __indx+1)
496227825Stheraven                    {
497227825Stheraven                        *__st = __does_match;
498227825Stheraven                        --__n_might_match;
499227825Stheraven                        ++__n_does_match;
500227825Stheraven                    }
501227825Stheraven                }
502227825Stheraven                else
503227825Stheraven                {
504227825Stheraven                    *__st = __doesnt_match;
505227825Stheraven                    --__n_might_match;
506227825Stheraven                }
507227825Stheraven            }
508227825Stheraven        }
509227825Stheraven        // consume if we matched a character
510227825Stheraven        if (__consume)
511227825Stheraven        {
512227825Stheraven            ++__b;
513227825Stheraven            // If we consumed a character and there might be a matched keyword that
514227825Stheraven            //   was marked matched on a previous iteration, then such keywords
515227825Stheraven            //   which are now marked as not matching.
516227825Stheraven            if (__n_might_match + __n_does_match > 1)
517227825Stheraven            {
518227825Stheraven                __st = __status;
519278724Sdim                for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
520227825Stheraven                {
521227825Stheraven                    if (*__st == __does_match && __ky->size() != __indx+1)
522227825Stheraven                    {
523227825Stheraven                        *__st = __doesnt_match;
524227825Stheraven                        --__n_does_match;
525227825Stheraven                    }
526227825Stheraven                }
527227825Stheraven            }
528227825Stheraven        }
529227825Stheraven    }
530227825Stheraven    // We've exited the loop because we hit eof and/or we have no more "might matches".
531227825Stheraven    if (__b == __e)
532227825Stheraven        __err |= ios_base::eofbit;
533227825Stheraven    // Return the first matching result
534278724Sdim    for (__st = __status; __kb != __ke; ++__kb, (void) ++__st)
535227825Stheraven        if (*__st == __does_match)
536227825Stheraven            break;
537227825Stheraven    if (__kb == __ke)
538227825Stheraven        __err |= ios_base::failbit;
539227825Stheraven    return __kb;
540227825Stheraven}
541227825Stheraven
542262801Sdimstruct _LIBCPP_TYPE_VIS __num_get_base
543227825Stheraven{
544227825Stheraven    static const int __num_get_buf_sz = 40;
545227825Stheraven
546227825Stheraven    static int __get_base(ios_base&);
547227825Stheraven    static const char __src[33];
548227825Stheraven};
549227825Stheraven
550262801Sdim_LIBCPP_FUNC_VIS
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
701262801Sdim_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_TYPE_VIS __num_get<char>)
702262801Sdim_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_TYPE_VIS __num_get<wchar_t>)
703227825Stheraven
704227825Stheraventemplate <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
705262801Sdimclass _LIBCPP_TYPE_VIS_ONLY 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
800262801Sdim    template <class _Fp>
801262801Sdim    iter_type __do_get_floating_point
802262801Sdim                            (iter_type __b, iter_type __e, ios_base& __iob,
803262801Sdim                             ios_base::iostate& __err, _Fp& __v) const;
804262801Sdim
805262801Sdim    template <class _Signed>
806262801Sdim    iter_type __do_get_signed
807262801Sdim                            (iter_type __b, iter_type __e, ios_base& __iob,
808262801Sdim                             ios_base::iostate& __err, _Signed& __v) const;
809262801Sdim
810262801Sdim    template <class _Unsigned>
811262801Sdim    iter_type __do_get_unsigned
812262801Sdim                            (iter_type __b, iter_type __e, ios_base& __iob,
813262801Sdim                             ios_base::iostate& __err, _Unsigned& __v) const;
814262801Sdim
815262801Sdim
816227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
817227825Stheraven                             ios_base::iostate& __err, bool& __v) const;
818262801Sdim
819227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
820262801Sdim                             ios_base::iostate& __err, long& __v) const
821262801Sdim    { return this->__do_get_signed ( __b, __e, __iob, __err, __v ); }
822262801Sdim
823227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
824262801Sdim                             ios_base::iostate& __err, long long& __v) const
825262801Sdim    { return this->__do_get_signed ( __b, __e, __iob, __err, __v ); }
826262801Sdim
827227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
828262801Sdim                             ios_base::iostate& __err, unsigned short& __v) const
829262801Sdim    { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
830262801Sdim
831227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
832262801Sdim                             ios_base::iostate& __err, unsigned int& __v) const
833262801Sdim    { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
834262801Sdim
835227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
836262801Sdim                             ios_base::iostate& __err, unsigned long& __v) const
837262801Sdim    { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
838262801Sdim
839227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
840262801Sdim                             ios_base::iostate& __err, unsigned long long& __v) const
841262801Sdim    { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
842262801Sdim
843227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
844262801Sdim                             ios_base::iostate& __err, float& __v) const
845262801Sdim    { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
846262801Sdim
847227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
848262801Sdim                             ios_base::iostate& __err, double& __v) const
849262801Sdim    { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
850262801Sdim
851227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
852262801Sdim                             ios_base::iostate& __err, long double& __v) const
853262801Sdim    { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
854262801Sdim
855227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
856227825Stheraven                             ios_base::iostate& __err, void*& __v) const;
857227825Stheraven};
858227825Stheraven
859227825Stheraventemplate <class _CharT, class _InputIterator>
860227825Stheravenlocale::id
861227825Stheravennum_get<_CharT, _InputIterator>::id;
862227825Stheraven
863227825Stheraventemplate <class _Tp>
864227825Stheraven_Tp
865227825Stheraven__num_get_signed_integral(const char* __a, const char* __a_end,
866227825Stheraven                          ios_base::iostate& __err, int __base)
867227825Stheraven{
868227825Stheraven    if (__a != __a_end)
869227825Stheraven    {
870246487Stheraven        typename remove_reference<decltype(errno)>::type __save_errno = errno;
871227825Stheraven        errno = 0;
872227825Stheraven        char *__p2;
873227825Stheraven        long long __ll = strtoll_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
874246487Stheraven        typename remove_reference<decltype(errno)>::type __current_errno = errno;
875227825Stheraven        if (__current_errno == 0)
876227825Stheraven            errno = __save_errno;
877227825Stheraven        if (__p2 != __a_end)
878227825Stheraven        {
879227825Stheraven            __err = ios_base::failbit;
880227825Stheraven            return 0;
881227825Stheraven        }
882227825Stheraven        else if (__current_errno == ERANGE         ||
883227825Stheraven                 __ll < numeric_limits<_Tp>::min() ||
884227825Stheraven                 numeric_limits<_Tp>::max() < __ll)
885227825Stheraven        {
886227825Stheraven            __err = ios_base::failbit;
887227825Stheraven            if (__ll > 0)
888227825Stheraven                return numeric_limits<_Tp>::max();
889227825Stheraven            else
890227825Stheraven                return numeric_limits<_Tp>::min();
891227825Stheraven        }
892227825Stheraven        return static_cast<_Tp>(__ll);
893227825Stheraven    }
894227825Stheraven    __err = ios_base::failbit;
895227825Stheraven    return 0;
896227825Stheraven}
897227825Stheraven
898227825Stheraventemplate <class _Tp>
899227825Stheraven_Tp
900227825Stheraven__num_get_unsigned_integral(const char* __a, const char* __a_end,
901227825Stheraven                            ios_base::iostate& __err, int __base)
902227825Stheraven{
903227825Stheraven    if (__a != __a_end)
904227825Stheraven    {
905227825Stheraven        if (*__a == '-')
906227825Stheraven        {
907227825Stheraven            __err = ios_base::failbit;
908227825Stheraven            return 0;
909227825Stheraven        }
910246487Stheraven        typename remove_reference<decltype(errno)>::type __save_errno = errno;
911227825Stheraven        errno = 0;
912227825Stheraven        char *__p2;
913227825Stheraven        unsigned long long __ll = strtoull_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
914246487Stheraven        typename remove_reference<decltype(errno)>::type __current_errno = errno;
915227825Stheraven        if (__current_errno == 0)
916227825Stheraven            errno = __save_errno;
917227825Stheraven        if (__p2 != __a_end)
918227825Stheraven        {
919227825Stheraven            __err = ios_base::failbit;
920227825Stheraven            return 0;
921227825Stheraven        }
922227825Stheraven        else if (__current_errno == ERANGE ||
923227825Stheraven                 numeric_limits<_Tp>::max() < __ll)
924227825Stheraven        {
925227825Stheraven            __err = ios_base::failbit;
926227825Stheraven            return numeric_limits<_Tp>::max();
927227825Stheraven        }
928227825Stheraven        return static_cast<_Tp>(__ll);
929227825Stheraven    }
930227825Stheraven    __err = ios_base::failbit;
931227825Stheraven    return 0;
932227825Stheraven}
933227825Stheraven
934227825Stheraventemplate <class _Tp>
935227825Stheraven_Tp
936227825Stheraven__num_get_float(const char* __a, const char* __a_end, ios_base::iostate& __err)
937227825Stheraven{
938227825Stheraven    if (__a != __a_end)
939227825Stheraven    {
940249998Sdim        typename remove_reference<decltype(errno)>::type __save_errno = errno;
941249998Sdim        errno = 0;
942227825Stheraven        char *__p2;
943227825Stheraven        long double __ld = strtold_l(__a, &__p2, _LIBCPP_GET_C_LOCALE);
944249998Sdim        typename remove_reference<decltype(errno)>::type __current_errno = errno;
945249998Sdim        if (__current_errno == 0)
946249998Sdim            errno = __save_errno;
947227825Stheraven        if (__p2 != __a_end)
948227825Stheraven        {
949227825Stheraven            __err = ios_base::failbit;
950227825Stheraven            return 0;
951227825Stheraven        }
952249998Sdim        else if (__current_errno == ERANGE)
953249998Sdim            __err = ios_base::failbit;
954227825Stheraven        return static_cast<_Tp>(__ld);
955227825Stheraven    }
956227825Stheraven    __err = ios_base::failbit;
957227825Stheraven    return 0;
958227825Stheraven}
959227825Stheraven
960227825Stheraventemplate <class _CharT, class _InputIterator>
961227825Stheraven_InputIterator
962227825Stheravennum_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
963227825Stheraven                                        ios_base& __iob,
964227825Stheraven                                        ios_base::iostate& __err,
965227825Stheraven                                        bool& __v) const
966227825Stheraven{
967227825Stheraven    if ((__iob.flags() & ios_base::boolalpha) == 0)
968227825Stheraven    {
969227825Stheraven        long __lv = -1;
970227825Stheraven        __b = do_get(__b, __e, __iob, __err, __lv);
971227825Stheraven        switch (__lv)
972227825Stheraven        {
973227825Stheraven        case 0:
974227825Stheraven            __v = false;
975227825Stheraven            break;
976227825Stheraven        case 1:
977227825Stheraven            __v = true;
978227825Stheraven            break;
979227825Stheraven        default:
980227825Stheraven            __v = true;
981227825Stheraven            __err = ios_base::failbit;
982227825Stheraven            break;
983227825Stheraven        }
984227825Stheraven        return __b;
985227825Stheraven    }
986227825Stheraven    const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__iob.getloc());
987227825Stheraven    const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__iob.getloc());
988227825Stheraven    typedef typename numpunct<_CharT>::string_type string_type;
989227825Stheraven    const string_type __names[2] = {__np.truename(), __np.falsename()};
990227825Stheraven    const string_type* __i = __scan_keyword(__b, __e, __names, __names+2,
991227825Stheraven                                            __ct, __err);
992227825Stheraven    __v = __i == __names;
993227825Stheraven    return __b;
994227825Stheraven}
995227825Stheraven
996262801Sdim// signed
997227825Stheraven
998227825Stheraventemplate <class _CharT, class _InputIterator>
999262801Sdimtemplate <class _Signed>
1000227825Stheraven_InputIterator
1001262801Sdimnum_get<_CharT, _InputIterator>::__do_get_signed(iter_type __b, iter_type __e,
1002227825Stheraven                                        ios_base& __iob,
1003227825Stheraven                                        ios_base::iostate& __err,
1004262801Sdim                                        _Signed& __v) const
1005227825Stheraven{
1006227825Stheraven    // Stage 1
1007227825Stheraven    int __base = this->__get_base(__iob);
1008227825Stheraven    // Stage 2
1009227825Stheraven    char_type __atoms[26];
1010227825Stheraven    char_type __thousands_sep;
1011227825Stheraven    string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
1012249998Sdim    string __buf;
1013249998Sdim    __buf.resize(__buf.capacity());
1014249998Sdim    char* __a = &__buf[0];
1015227825Stheraven    char* __a_end = __a;
1016227825Stheraven    unsigned __g[__num_get_base::__num_get_buf_sz];
1017227825Stheraven    unsigned* __g_end = __g;
1018227825Stheraven    unsigned __dc = 0;
1019227825Stheraven    for (; __b != __e; ++__b)
1020249998Sdim    {
1021262801Sdim        if (__a_end == __a + __buf.size())
1022249998Sdim        {
1023249998Sdim            size_t __tmp = __buf.size();
1024249998Sdim            __buf.resize(2*__buf.size());
1025249998Sdim            __buf.resize(__buf.capacity());
1026249998Sdim            __a = &__buf[0];
1027249998Sdim            __a_end = __a + __tmp;
1028249998Sdim        }
1029227825Stheraven        if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1030227825Stheraven                                    __thousands_sep, __grouping, __g, __g_end,
1031227825Stheraven                                    __atoms))
1032227825Stheraven            break;
1033249998Sdim    }
1034227825Stheraven    if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1035227825Stheraven        *__g_end++ = __dc;
1036227825Stheraven    // Stage 3
1037262801Sdim    __v = __num_get_signed_integral<_Signed>(__a, __a_end, __err, __base);
1038227825Stheraven    // Digit grouping checked
1039227825Stheraven    __check_grouping(__grouping, __g, __g_end, __err);
1040227825Stheraven    // EOF checked
1041227825Stheraven    if (__b == __e)
1042227825Stheraven        __err |= ios_base::eofbit;
1043227825Stheraven    return __b;
1044227825Stheraven}
1045227825Stheraven
1046262801Sdim// unsigned
1047227825Stheraven
1048227825Stheraventemplate <class _CharT, class _InputIterator>
1049262801Sdimtemplate <class _Unsigned>
1050227825Stheraven_InputIterator
1051262801Sdimnum_get<_CharT, _InputIterator>::__do_get_unsigned(iter_type __b, iter_type __e,
1052227825Stheraven                                        ios_base& __iob,
1053227825Stheraven                                        ios_base::iostate& __err,
1054262801Sdim                                        _Unsigned& __v) const
1055227825Stheraven{
1056227825Stheraven    // Stage 1
1057227825Stheraven    int __base = this->__get_base(__iob);
1058227825Stheraven    // Stage 2
1059227825Stheraven    char_type __atoms[26];
1060227825Stheraven    char_type __thousands_sep;
1061227825Stheraven    string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
1062249998Sdim    string __buf;
1063249998Sdim    __buf.resize(__buf.capacity());
1064249998Sdim    char* __a = &__buf[0];
1065227825Stheraven    char* __a_end = __a;
1066227825Stheraven    unsigned __g[__num_get_base::__num_get_buf_sz];
1067227825Stheraven    unsigned* __g_end = __g;
1068227825Stheraven    unsigned __dc = 0;
1069227825Stheraven    for (; __b != __e; ++__b)
1070249998Sdim    {
1071262801Sdim        if (__a_end == __a + __buf.size())
1072249998Sdim        {
1073249998Sdim            size_t __tmp = __buf.size();
1074249998Sdim            __buf.resize(2*__buf.size());
1075249998Sdim            __buf.resize(__buf.capacity());
1076249998Sdim            __a = &__buf[0];
1077249998Sdim            __a_end = __a + __tmp;
1078249998Sdim        }
1079227825Stheraven        if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1080227825Stheraven                                    __thousands_sep, __grouping, __g, __g_end,
1081227825Stheraven                                    __atoms))
1082227825Stheraven            break;
1083249998Sdim    }
1084227825Stheraven    if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1085227825Stheraven        *__g_end++ = __dc;
1086227825Stheraven    // Stage 3
1087262801Sdim    __v = __num_get_unsigned_integral<_Unsigned>(__a, __a_end, __err, __base);
1088227825Stheraven    // Digit grouping checked
1089227825Stheraven    __check_grouping(__grouping, __g, __g_end, __err);
1090227825Stheraven    // EOF checked
1091227825Stheraven    if (__b == __e)
1092227825Stheraven        __err |= ios_base::eofbit;
1093227825Stheraven    return __b;
1094227825Stheraven}
1095227825Stheraven
1096262801Sdim// floating point
1097227825Stheraven
1098227825Stheraventemplate <class _CharT, class _InputIterator>
1099262801Sdimtemplate <class _Fp>
1100227825Stheraven_InputIterator
1101262801Sdimnum_get<_CharT, _InputIterator>::__do_get_floating_point(iter_type __b, iter_type __e,
1102227825Stheraven                                        ios_base& __iob,
1103227825Stheraven                                        ios_base::iostate& __err,
1104262801Sdim                                        _Fp& __v) const
1105227825Stheraven{
1106227825Stheraven    // Stage 1, nothing to do
1107227825Stheraven    // Stage 2
1108227825Stheraven    char_type __atoms[32];
1109227825Stheraven    char_type __decimal_point;
1110227825Stheraven    char_type __thousands_sep;
1111227825Stheraven    string __grouping = this->__stage2_float_prep(__iob, __atoms,
1112227825Stheraven                                                  __decimal_point,
1113227825Stheraven                                                  __thousands_sep);
1114249998Sdim    string __buf;
1115249998Sdim    __buf.resize(__buf.capacity());
1116249998Sdim    char* __a = &__buf[0];
1117227825Stheraven    char* __a_end = __a;
1118227825Stheraven    unsigned __g[__num_get_base::__num_get_buf_sz];
1119227825Stheraven    unsigned* __g_end = __g;
1120227825Stheraven    unsigned __dc = 0;
1121227825Stheraven    bool __in_units = true;
1122227825Stheraven    char __exp = 'E';
1123227825Stheraven    for (; __b != __e; ++__b)
1124249998Sdim    {
1125262801Sdim        if (__a_end == __a + __buf.size())
1126249998Sdim        {
1127249998Sdim            size_t __tmp = __buf.size();
1128249998Sdim            __buf.resize(2*__buf.size());
1129249998Sdim            __buf.resize(__buf.capacity());
1130249998Sdim            __a = &__buf[0];
1131249998Sdim            __a_end = __a + __tmp;
1132249998Sdim        }
1133227825Stheraven        if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1134227825Stheraven                                      __decimal_point, __thousands_sep,
1135227825Stheraven                                      __grouping, __g, __g_end,
1136227825Stheraven                                      __dc, __atoms))
1137227825Stheraven            break;
1138249998Sdim    }
1139227825Stheraven    if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1140227825Stheraven        *__g_end++ = __dc;
1141227825Stheraven    // Stage 3
1142262801Sdim    __v = __num_get_float<_Fp>(__a, __a_end, __err);
1143227825Stheraven    // Digit grouping checked
1144227825Stheraven    __check_grouping(__grouping, __g, __g_end, __err);
1145227825Stheraven    // EOF checked
1146227825Stheraven    if (__b == __e)
1147227825Stheraven        __err |= ios_base::eofbit;
1148227825Stheraven    return __b;
1149227825Stheraven}
1150227825Stheraven
1151227825Stheraventemplate <class _CharT, class _InputIterator>
1152227825Stheraven_InputIterator
1153227825Stheravennum_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1154227825Stheraven                                        ios_base& __iob,
1155227825Stheraven                                        ios_base::iostate& __err,
1156227825Stheraven                                        void*& __v) const
1157227825Stheraven{
1158227825Stheraven    // Stage 1
1159227825Stheraven    int __base = 16;
1160227825Stheraven    // Stage 2
1161227825Stheraven    char_type __atoms[26];
1162232950Stheraven    char_type __thousands_sep = 0;
1163227825Stheraven    string __grouping;
1164227825Stheraven    use_facet<ctype<_CharT> >(__iob.getloc()).widen(__num_get_base::__src,
1165227825Stheraven                                                    __num_get_base::__src + 26, __atoms);
1166249998Sdim    string __buf;
1167249998Sdim    __buf.resize(__buf.capacity());
1168249998Sdim    char* __a = &__buf[0];
1169227825Stheraven    char* __a_end = __a;
1170227825Stheraven    unsigned __g[__num_get_base::__num_get_buf_sz];
1171227825Stheraven    unsigned* __g_end = __g;
1172227825Stheraven    unsigned __dc = 0;
1173227825Stheraven    for (; __b != __e; ++__b)
1174249998Sdim    {
1175262801Sdim        if (__a_end == __a + __buf.size())
1176249998Sdim        {
1177249998Sdim            size_t __tmp = __buf.size();
1178249998Sdim            __buf.resize(2*__buf.size());
1179249998Sdim            __buf.resize(__buf.capacity());
1180249998Sdim            __a = &__buf[0];
1181249998Sdim            __a_end = __a + __tmp;
1182249998Sdim        }
1183227825Stheraven        if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1184227825Stheraven                                    __thousands_sep, __grouping,
1185227825Stheraven                                    __g, __g_end, __atoms))
1186227825Stheraven            break;
1187249998Sdim    }
1188227825Stheraven    // Stage 3
1189278724Sdim    __buf.resize(__a_end - __a);
1190227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1191278724Sdim    if (sscanf_l(__buf.c_str(), _LIBCPP_GET_C_LOCALE, "%p", &__v) != 1)
1192227825Stheraven#else
1193278724Sdim    if (__sscanf_l(__buf.c_str(), __cloc(), "%p", &__v) != 1)
1194227825Stheraven#endif
1195227825Stheraven        __err = ios_base::failbit;
1196227825Stheraven    // EOF checked
1197227825Stheraven    if (__b == __e)
1198227825Stheraven        __err |= ios_base::eofbit;
1199227825Stheraven    return __b;
1200227825Stheraven}
1201227825Stheraven
1202262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS num_get<char>)
1203262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS num_get<wchar_t>)
1204227825Stheraven
1205262801Sdimstruct _LIBCPP_TYPE_VIS __num_put_base
1206227825Stheraven{
1207227825Stheravenprotected:
1208227825Stheraven    static void __format_int(char* __fmt, const char* __len, bool __signd,
1209227825Stheraven                             ios_base::fmtflags __flags);
1210227825Stheraven    static bool __format_float(char* __fmt, const char* __len,
1211227825Stheraven                               ios_base::fmtflags __flags);
1212227825Stheraven    static char* __identify_padding(char* __nb, char* __ne,
1213227825Stheraven                                    const ios_base& __iob);
1214227825Stheraven};
1215227825Stheraven
1216227825Stheraventemplate <class _CharT>
1217227825Stheravenstruct __num_put
1218227825Stheraven    : protected __num_put_base
1219227825Stheraven{
1220227825Stheraven    static void __widen_and_group_int(char* __nb, char* __np, char* __ne,
1221227825Stheraven                                      _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1222227825Stheraven                                      const locale& __loc);
1223227825Stheraven    static void __widen_and_group_float(char* __nb, char* __np, char* __ne,
1224227825Stheraven                                        _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1225227825Stheraven                                        const locale& __loc);
1226227825Stheraven};
1227227825Stheraven
1228227825Stheraventemplate <class _CharT>
1229227825Stheravenvoid
1230227825Stheraven__num_put<_CharT>::__widen_and_group_int(char* __nb, char* __np, char* __ne,
1231227825Stheraven                                         _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1232227825Stheraven                                         const locale& __loc)
1233227825Stheraven{
1234227825Stheraven    const ctype<_CharT>&    __ct = use_facet<ctype<_CharT> >   (__loc);
1235227825Stheraven    const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1236227825Stheraven    string __grouping = __npt.grouping();
1237227825Stheraven    if (__grouping.empty())
1238227825Stheraven    {
1239227825Stheraven        __ct.widen(__nb, __ne, __ob);
1240227825Stheraven        __oe = __ob + (__ne - __nb);
1241227825Stheraven    }
1242227825Stheraven    else
1243227825Stheraven    {
1244227825Stheraven        __oe = __ob;
1245227825Stheraven        char* __nf = __nb;
1246227825Stheraven        if (*__nf == '-' || *__nf == '+')
1247227825Stheraven            *__oe++ = __ct.widen(*__nf++);
1248227825Stheraven        if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1249227825Stheraven                                                   __nf[1] == 'X'))
1250227825Stheraven        {
1251227825Stheraven            *__oe++ = __ct.widen(*__nf++);
1252227825Stheraven            *__oe++ = __ct.widen(*__nf++);
1253227825Stheraven        }
1254227825Stheraven        reverse(__nf, __ne);
1255227825Stheraven        _CharT __thousands_sep = __npt.thousands_sep();
1256227825Stheraven        unsigned __dc = 0;
1257227825Stheraven        unsigned __dg = 0;
1258227825Stheraven        for (char* __p = __nf; __p < __ne; ++__p)
1259227825Stheraven        {
1260227825Stheraven            if (static_cast<unsigned>(__grouping[__dg]) > 0 &&
1261227825Stheraven                __dc == static_cast<unsigned>(__grouping[__dg]))
1262227825Stheraven            {
1263227825Stheraven                *__oe++ = __thousands_sep;
1264227825Stheraven                __dc = 0;
1265227825Stheraven                if (__dg < __grouping.size()-1)
1266227825Stheraven                    ++__dg;
1267227825Stheraven            }
1268227825Stheraven            *__oe++ = __ct.widen(*__p);
1269227825Stheraven            ++__dc;
1270227825Stheraven        }
1271227825Stheraven        reverse(__ob + (__nf - __nb), __oe);
1272227825Stheraven    }
1273227825Stheraven    if (__np == __ne)
1274227825Stheraven        __op = __oe;
1275227825Stheraven    else
1276227825Stheraven        __op = __ob + (__np - __nb);
1277227825Stheraven}
1278227825Stheraven
1279227825Stheraventemplate <class _CharT>
1280227825Stheravenvoid
1281227825Stheraven__num_put<_CharT>::__widen_and_group_float(char* __nb, char* __np, char* __ne,
1282227825Stheraven                                           _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1283227825Stheraven                                           const locale& __loc)
1284227825Stheraven{
1285227825Stheraven    const ctype<_CharT>&    __ct = use_facet<ctype<_CharT> >   (__loc);
1286227825Stheraven    const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1287227825Stheraven    string __grouping = __npt.grouping();
1288227825Stheraven    __oe = __ob;
1289227825Stheraven    char* __nf = __nb;
1290227825Stheraven    if (*__nf == '-' || *__nf == '+')
1291227825Stheraven        *__oe++ = __ct.widen(*__nf++);
1292227825Stheraven    char* __ns;
1293227825Stheraven    if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1294227825Stheraven                                               __nf[1] == 'X'))
1295227825Stheraven    {
1296227825Stheraven        *__oe++ = __ct.widen(*__nf++);
1297227825Stheraven        *__oe++ = __ct.widen(*__nf++);
1298227825Stheraven        for (__ns = __nf; __ns < __ne; ++__ns)
1299227825Stheraven            if (!isxdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
1300227825Stheraven                break;
1301227825Stheraven    }
1302227825Stheraven    else
1303227825Stheraven    {
1304227825Stheraven        for (__ns = __nf; __ns < __ne; ++__ns)
1305227825Stheraven            if (!isdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
1306227825Stheraven                break;
1307227825Stheraven    }
1308227825Stheraven    if (__grouping.empty())
1309227825Stheraven    {
1310227825Stheraven        __ct.widen(__nf, __ns, __oe);
1311227825Stheraven        __oe += __ns - __nf;
1312227825Stheraven    }
1313227825Stheraven    else
1314227825Stheraven    {
1315227825Stheraven        reverse(__nf, __ns);
1316227825Stheraven        _CharT __thousands_sep = __npt.thousands_sep();
1317227825Stheraven        unsigned __dc = 0;
1318227825Stheraven        unsigned __dg = 0;
1319227825Stheraven        for (char* __p = __nf; __p < __ns; ++__p)
1320227825Stheraven        {
1321227825Stheraven            if (__grouping[__dg] > 0 && __dc == static_cast<unsigned>(__grouping[__dg]))
1322227825Stheraven            {
1323227825Stheraven                *__oe++ = __thousands_sep;
1324227825Stheraven                __dc = 0;
1325227825Stheraven                if (__dg < __grouping.size()-1)
1326227825Stheraven                    ++__dg;
1327227825Stheraven            }
1328227825Stheraven            *__oe++ = __ct.widen(*__p);
1329227825Stheraven            ++__dc;
1330227825Stheraven        }
1331227825Stheraven        reverse(__ob + (__nf - __nb), __oe);
1332227825Stheraven    }
1333227825Stheraven    for (__nf = __ns; __nf < __ne; ++__nf)
1334227825Stheraven    {
1335227825Stheraven        if (*__nf == '.')
1336227825Stheraven        {
1337227825Stheraven            *__oe++ = __npt.decimal_point();
1338227825Stheraven            ++__nf;
1339227825Stheraven            break;
1340227825Stheraven        }
1341227825Stheraven        else
1342227825Stheraven            *__oe++ = __ct.widen(*__nf);
1343227825Stheraven    }
1344227825Stheraven    __ct.widen(__nf, __ne, __oe);
1345227825Stheraven    __oe += __ne - __nf;
1346227825Stheraven    if (__np == __ne)
1347227825Stheraven        __op = __oe;
1348227825Stheraven    else
1349227825Stheraven        __op = __ob + (__np - __nb);
1350227825Stheraven}
1351227825Stheraven
1352262801Sdim_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_TYPE_VIS __num_put<char>)
1353262801Sdim_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_TYPE_VIS __num_put<wchar_t>)
1354227825Stheraven
1355227825Stheraventemplate <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
1356262801Sdimclass _LIBCPP_TYPE_VIS_ONLY num_put
1357227825Stheraven    : public locale::facet,
1358227825Stheraven      private __num_put<_CharT>
1359227825Stheraven{
1360227825Stheravenpublic:
1361227825Stheraven    typedef _CharT char_type;
1362227825Stheraven    typedef _OutputIterator iter_type;
1363227825Stheraven
1364227825Stheraven    _LIBCPP_ALWAYS_INLINE
1365227825Stheraven    explicit num_put(size_t __refs = 0)
1366227825Stheraven        : locale::facet(__refs) {}
1367227825Stheraven
1368227825Stheraven    _LIBCPP_ALWAYS_INLINE
1369227825Stheraven    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1370227825Stheraven                  bool __v) const
1371227825Stheraven    {
1372227825Stheraven        return do_put(__s, __iob, __fl, __v);
1373227825Stheraven    }
1374227825Stheraven
1375227825Stheraven    _LIBCPP_ALWAYS_INLINE
1376227825Stheraven    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1377227825Stheraven                  long __v) const
1378227825Stheraven    {
1379227825Stheraven        return do_put(__s, __iob, __fl, __v);
1380227825Stheraven    }
1381227825Stheraven
1382227825Stheraven    _LIBCPP_ALWAYS_INLINE
1383227825Stheraven    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1384227825Stheraven                  long long __v) const
1385227825Stheraven    {
1386227825Stheraven        return do_put(__s, __iob, __fl, __v);
1387227825Stheraven    }
1388227825Stheraven
1389227825Stheraven    _LIBCPP_ALWAYS_INLINE
1390227825Stheraven    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1391227825Stheraven                  unsigned long __v) const
1392227825Stheraven    {
1393227825Stheraven        return do_put(__s, __iob, __fl, __v);
1394227825Stheraven    }
1395227825Stheraven
1396227825Stheraven    _LIBCPP_ALWAYS_INLINE
1397227825Stheraven    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1398227825Stheraven                  unsigned long long __v) const
1399227825Stheraven    {
1400227825Stheraven        return do_put(__s, __iob, __fl, __v);
1401227825Stheraven    }
1402227825Stheraven
1403227825Stheraven    _LIBCPP_ALWAYS_INLINE
1404227825Stheraven    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1405227825Stheraven                  double __v) const
1406227825Stheraven    {
1407227825Stheraven        return do_put(__s, __iob, __fl, __v);
1408227825Stheraven    }
1409227825Stheraven
1410227825Stheraven    _LIBCPP_ALWAYS_INLINE
1411227825Stheraven    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1412227825Stheraven                  long double __v) const
1413227825Stheraven    {
1414227825Stheraven        return do_put(__s, __iob, __fl, __v);
1415227825Stheraven    }
1416227825Stheraven
1417227825Stheraven    _LIBCPP_ALWAYS_INLINE
1418227825Stheraven    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1419227825Stheraven                  const void* __v) const
1420227825Stheraven    {
1421227825Stheraven        return do_put(__s, __iob, __fl, __v);
1422227825Stheraven    }
1423227825Stheraven
1424227825Stheraven    static locale::id id;
1425227825Stheraven
1426227825Stheravenprotected:
1427227825Stheraven    _LIBCPP_ALWAYS_INLINE
1428227825Stheraven    ~num_put() {}
1429227825Stheraven
1430227825Stheraven    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1431227825Stheraven                             bool __v) const;
1432227825Stheraven    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1433227825Stheraven                             long __v) const;
1434227825Stheraven    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1435227825Stheraven                             long long __v) const;
1436227825Stheraven    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1437227825Stheraven                             unsigned long) const;
1438227825Stheraven    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1439227825Stheraven                             unsigned long long) const;
1440227825Stheraven    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1441227825Stheraven                             double __v) const;
1442227825Stheraven    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1443227825Stheraven                             long double __v) const;
1444227825Stheraven    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1445227825Stheraven                             const void* __v) const;
1446227825Stheraven};
1447227825Stheraven
1448227825Stheraventemplate <class _CharT, class _OutputIterator>
1449227825Stheravenlocale::id
1450227825Stheravennum_put<_CharT, _OutputIterator>::id;
1451227825Stheraven
1452227825Stheraventemplate <class _CharT, class _OutputIterator>
1453227825Stheraven_LIBCPP_HIDDEN
1454227825Stheraven_OutputIterator
1455227825Stheraven__pad_and_output(_OutputIterator __s,
1456227825Stheraven                 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1457227825Stheraven                 ios_base& __iob, _CharT __fl)
1458227825Stheraven{
1459227825Stheraven    streamsize __sz = __oe - __ob;
1460227825Stheraven    streamsize __ns = __iob.width();
1461227825Stheraven    if (__ns > __sz)
1462227825Stheraven        __ns -= __sz;
1463227825Stheraven    else
1464227825Stheraven        __ns = 0;
1465227825Stheraven    for (;__ob < __op; ++__ob, ++__s)
1466227825Stheraven        *__s = *__ob;
1467227825Stheraven    for (; __ns; --__ns, ++__s)
1468227825Stheraven        *__s = __fl;
1469227825Stheraven    for (; __ob < __oe; ++__ob, ++__s)
1470227825Stheraven        *__s = *__ob;
1471227825Stheraven    __iob.width(0);
1472227825Stheraven    return __s;
1473227825Stheraven}
1474227825Stheraven
1475243673Stheraven#if !defined(__APPLE__) || \
1476243673Stheraven    (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED > __MAC_10_8) || \
1477243673Stheraven    (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_6_0)
1478243673Stheraven
1479241903Sdimtemplate <class _CharT, class _Traits>
1480241903Sdim_LIBCPP_HIDDEN
1481241903Sdimostreambuf_iterator<_CharT, _Traits>
1482241903Sdim__pad_and_output(ostreambuf_iterator<_CharT, _Traits> __s,
1483241903Sdim                 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1484241903Sdim                 ios_base& __iob, _CharT __fl)
1485241903Sdim{
1486241903Sdim    if (__s.__sbuf_ == nullptr)
1487241903Sdim        return __s;
1488241903Sdim    streamsize __sz = __oe - __ob;
1489241903Sdim    streamsize __ns = __iob.width();
1490241903Sdim    if (__ns > __sz)
1491241903Sdim        __ns -= __sz;
1492241903Sdim    else
1493241903Sdim        __ns = 0;
1494241903Sdim    streamsize __np = __op - __ob;
1495241903Sdim    if (__np > 0)
1496241903Sdim    {
1497241903Sdim        if (__s.__sbuf_->sputn(__ob, __np) != __np)
1498241903Sdim        {
1499241903Sdim            __s.__sbuf_ = nullptr;
1500241903Sdim            return __s;
1501241903Sdim        }
1502241903Sdim    }
1503241903Sdim    if (__ns > 0)
1504241903Sdim    {
1505241903Sdim        basic_string<_CharT, _Traits> __sp(__ns, __fl);
1506241903Sdim        if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns)
1507241903Sdim        {
1508241903Sdim            __s.__sbuf_ = nullptr;
1509241903Sdim            return __s;
1510241903Sdim        }
1511241903Sdim    }
1512241903Sdim    __np = __oe - __op;
1513241903Sdim    if (__np > 0)
1514241903Sdim    {
1515241903Sdim        if (__s.__sbuf_->sputn(__op, __np) != __np)
1516241903Sdim        {
1517241903Sdim            __s.__sbuf_ = nullptr;
1518241903Sdim            return __s;
1519241903Sdim        }
1520241903Sdim    }
1521241903Sdim    __iob.width(0);
1522241903Sdim    return __s;
1523241903Sdim}
1524241903Sdim
1525243673Stheraven#endif
1526243673Stheraven
1527227825Stheraventemplate <class _CharT, class _OutputIterator>
1528227825Stheraven_OutputIterator
1529227825Stheravennum_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1530227825Stheraven                                         char_type __fl, bool __v) const
1531227825Stheraven{
1532227825Stheraven    if ((__iob.flags() & ios_base::boolalpha) == 0)
1533227825Stheraven        return do_put(__s, __iob, __fl, (unsigned long)__v);
1534227825Stheraven    const numpunct<char_type>& __np = use_facet<numpunct<char_type> >(__iob.getloc());
1535227825Stheraven    typedef typename numpunct<char_type>::string_type string_type;
1536262801Sdim#if _LIBCPP_DEBUG_LEVEL >= 2
1537262801Sdim    string_type __tmp(__v ? __np.truename() : __np.falsename());
1538262801Sdim    string_type __nm = _VSTD::move(__tmp);
1539262801Sdim#else
1540227825Stheraven    string_type __nm = __v ? __np.truename() : __np.falsename();
1541262801Sdim#endif
1542227825Stheraven    for (typename string_type::iterator __i = __nm.begin(); __i != __nm.end(); ++__i, ++__s)
1543227825Stheraven        *__s = *__i;
1544227825Stheraven    return __s;
1545227825Stheraven}
1546227825Stheraven
1547227825Stheraventemplate <class _CharT, class _OutputIterator>
1548227825Stheraven_OutputIterator
1549227825Stheravennum_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1550227825Stheraven                                         char_type __fl, long __v) const
1551227825Stheraven{
1552227825Stheraven    // Stage 1 - Get number in narrow char
1553227825Stheraven    char __fmt[6] = {'%', 0};
1554227825Stheraven    const char* __len = "l";
1555227825Stheraven    this->__format_int(__fmt+1, __len, true, __iob.flags());
1556227825Stheraven    const unsigned __nbuf = (numeric_limits<long>::digits / 3)
1557227825Stheraven                          + ((numeric_limits<long>::digits % 3) != 0)
1558227825Stheraven                          + 1;
1559227825Stheraven    char __nar[__nbuf];
1560227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1561253159Stheraven    int __nc = snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1562227825Stheraven#else
1563253159Stheraven    int __nc = __snprintf_l(__nar, sizeof(__nar), __cloc(), __fmt, __v);
1564227825Stheraven#endif
1565227825Stheraven    char* __ne = __nar + __nc;
1566227825Stheraven    char* __np = this->__identify_padding(__nar, __ne, __iob);
1567227825Stheraven    // Stage 2 - Widen __nar while adding thousands separators
1568227825Stheraven    char_type __o[2*(__nbuf-1) - 1];
1569227825Stheraven    char_type* __op;  // pad here
1570227825Stheraven    char_type* __oe;  // end of output
1571227825Stheraven    this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1572227825Stheraven    // [__o, __oe) contains thousands_sep'd wide number
1573227825Stheraven    // Stage 3 & 4
1574227825Stheraven    return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1575227825Stheraven}
1576227825Stheraven
1577227825Stheraventemplate <class _CharT, class _OutputIterator>
1578227825Stheraven_OutputIterator
1579227825Stheravennum_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1580227825Stheraven                                         char_type __fl, long long __v) const
1581227825Stheraven{
1582227825Stheraven    // Stage 1 - Get number in narrow char
1583227825Stheraven    char __fmt[8] = {'%', 0};
1584227825Stheraven    const char* __len = "ll";
1585227825Stheraven    this->__format_int(__fmt+1, __len, true, __iob.flags());
1586227825Stheraven    const unsigned __nbuf = (numeric_limits<long long>::digits / 3)
1587227825Stheraven                          + ((numeric_limits<long long>::digits % 3) != 0)
1588227825Stheraven                          + 1;
1589227825Stheraven    char __nar[__nbuf];
1590227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1591253159Stheraven    int __nc = snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1592227825Stheraven#else
1593253159Stheraven    int __nc = __snprintf_l(__nar, sizeof(__nar), __cloc(), __fmt, __v);
1594227825Stheraven#endif
1595227825Stheraven    char* __ne = __nar + __nc;
1596227825Stheraven    char* __np = this->__identify_padding(__nar, __ne, __iob);
1597227825Stheraven    // Stage 2 - Widen __nar while adding thousands separators
1598227825Stheraven    char_type __o[2*(__nbuf-1) - 1];
1599227825Stheraven    char_type* __op;  // pad here
1600227825Stheraven    char_type* __oe;  // end of output
1601227825Stheraven    this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1602227825Stheraven    // [__o, __oe) contains thousands_sep'd wide number
1603227825Stheraven    // Stage 3 & 4
1604227825Stheraven    return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1605227825Stheraven}
1606227825Stheraven
1607227825Stheraventemplate <class _CharT, class _OutputIterator>
1608227825Stheraven_OutputIterator
1609227825Stheravennum_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1610227825Stheraven                                         char_type __fl, unsigned long __v) const
1611227825Stheraven{
1612227825Stheraven    // Stage 1 - Get number in narrow char
1613227825Stheraven    char __fmt[6] = {'%', 0};
1614227825Stheraven    const char* __len = "l";
1615227825Stheraven    this->__format_int(__fmt+1, __len, false, __iob.flags());
1616227825Stheraven    const unsigned __nbuf = (numeric_limits<unsigned long>::digits / 3)
1617227825Stheraven                          + ((numeric_limits<unsigned long>::digits % 3) != 0)
1618227825Stheraven                          + 1;
1619227825Stheraven    char __nar[__nbuf];
1620227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1621253159Stheraven    int __nc = snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1622227825Stheraven#else
1623253159Stheraven    int __nc = __snprintf_l(__nar, sizeof(__nar), __cloc(), __fmt, __v);
1624227825Stheraven#endif
1625227825Stheraven    char* __ne = __nar + __nc;
1626227825Stheraven    char* __np = this->__identify_padding(__nar, __ne, __iob);
1627227825Stheraven    // Stage 2 - Widen __nar while adding thousands separators
1628227825Stheraven    char_type __o[2*(__nbuf-1) - 1];
1629227825Stheraven    char_type* __op;  // pad here
1630227825Stheraven    char_type* __oe;  // end of output
1631227825Stheraven    this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1632227825Stheraven    // [__o, __oe) contains thousands_sep'd wide number
1633227825Stheraven    // Stage 3 & 4
1634227825Stheraven    return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1635227825Stheraven}
1636227825Stheraven
1637227825Stheraventemplate <class _CharT, class _OutputIterator>
1638227825Stheraven_OutputIterator
1639227825Stheravennum_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1640227825Stheraven                                         char_type __fl, unsigned long long __v) const
1641227825Stheraven{
1642227825Stheraven    // Stage 1 - Get number in narrow char
1643227825Stheraven    char __fmt[8] = {'%', 0};
1644227825Stheraven    const char* __len = "ll";
1645227825Stheraven    this->__format_int(__fmt+1, __len, false, __iob.flags());
1646227825Stheraven    const unsigned __nbuf = (numeric_limits<unsigned long long>::digits / 3)
1647227825Stheraven                          + ((numeric_limits<unsigned long long>::digits % 3) != 0)
1648227825Stheraven                          + 1;
1649227825Stheraven    char __nar[__nbuf];
1650227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1651253159Stheraven    int __nc = snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1652227825Stheraven#else
1653253159Stheraven    int __nc = __snprintf_l(__nar, sizeof(__nar), __cloc(), __fmt, __v);
1654227825Stheraven#endif
1655227825Stheraven    char* __ne = __nar + __nc;
1656227825Stheraven    char* __np = this->__identify_padding(__nar, __ne, __iob);
1657227825Stheraven    // Stage 2 - Widen __nar while adding thousands separators
1658227825Stheraven    char_type __o[2*(__nbuf-1) - 1];
1659227825Stheraven    char_type* __op;  // pad here
1660227825Stheraven    char_type* __oe;  // end of output
1661227825Stheraven    this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1662227825Stheraven    // [__o, __oe) contains thousands_sep'd wide number
1663227825Stheraven    // Stage 3 & 4
1664227825Stheraven    return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1665227825Stheraven}
1666227825Stheraven
1667227825Stheraventemplate <class _CharT, class _OutputIterator>
1668227825Stheraven_OutputIterator
1669227825Stheravennum_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1670227825Stheraven                                         char_type __fl, double __v) const
1671227825Stheraven{
1672227825Stheraven    // Stage 1 - Get number in narrow char
1673227825Stheraven    char __fmt[8] = {'%', 0};
1674227825Stheraven    const char* __len = "";
1675227825Stheraven    bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1676227825Stheraven    const unsigned __nbuf = 30;
1677227825Stheraven    char __nar[__nbuf];
1678227825Stheraven    char* __nb = __nar;
1679227825Stheraven    int __nc;
1680227825Stheraven    if (__specify_precision)
1681227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1682227825Stheraven        __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
1683227825Stheraven                                   (int)__iob.precision(), __v);
1684227825Stheraven#else
1685227825Stheraven        __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt,
1686227825Stheraven                            (int)__iob.precision(), __v);
1687227825Stheraven#endif
1688227825Stheraven    else
1689227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1690227825Stheraven        __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1691227825Stheraven#else
1692227825Stheraven        __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt, __v);
1693227825Stheraven#endif
1694227825Stheraven    unique_ptr<char, void(*)(void*)> __nbh(0, free);
1695227825Stheraven    if (__nc > static_cast<int>(__nbuf-1))
1696227825Stheraven    {
1697227825Stheraven        if (__specify_precision)
1698227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1699227825Stheraven            __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
1700227825Stheraven#else
1701227825Stheraven            __nc = __asprintf_l(&__nb, __cloc(), __fmt,
1702227825Stheraven                              (int)__iob.precision(), __v);
1703227825Stheraven#endif
1704227825Stheraven        else
1705227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1706227825Stheraven            __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1707227825Stheraven#else
1708227825Stheraven            __nc = __asprintf_l(&__nb, __cloc(), __fmt, (int)__iob.precision(), __v);
1709227825Stheraven#endif
1710227825Stheraven        if (__nb == 0)
1711227825Stheraven            __throw_bad_alloc();
1712227825Stheraven        __nbh.reset(__nb);
1713227825Stheraven    }
1714227825Stheraven    char* __ne = __nb + __nc;
1715227825Stheraven    char* __np = this->__identify_padding(__nb, __ne, __iob);
1716227825Stheraven    // Stage 2 - Widen __nar while adding thousands separators
1717227825Stheraven    char_type __o[2*(__nbuf-1) - 1];
1718227825Stheraven    char_type* __ob = __o;
1719227825Stheraven    unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1720227825Stheraven    if (__nb != __nar)
1721227825Stheraven    {
1722232950Stheraven        __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
1723227825Stheraven        if (__ob == 0)
1724227825Stheraven            __throw_bad_alloc();
1725227825Stheraven        __obh.reset(__ob);
1726227825Stheraven    }
1727227825Stheraven    char_type* __op;  // pad here
1728227825Stheraven    char_type* __oe;  // end of output
1729227825Stheraven    this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1730227825Stheraven    // [__o, __oe) contains thousands_sep'd wide number
1731227825Stheraven    // Stage 3 & 4
1732227825Stheraven    __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1733227825Stheraven    return __s;
1734227825Stheraven}
1735227825Stheraven
1736227825Stheraventemplate <class _CharT, class _OutputIterator>
1737227825Stheraven_OutputIterator
1738227825Stheravennum_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1739227825Stheraven                                         char_type __fl, long double __v) const
1740227825Stheraven{
1741227825Stheraven    // Stage 1 - Get number in narrow char
1742227825Stheraven    char __fmt[8] = {'%', 0};
1743227825Stheraven    const char* __len = "L";
1744227825Stheraven    bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1745227825Stheraven    const unsigned __nbuf = 30;
1746227825Stheraven    char __nar[__nbuf];
1747227825Stheraven    char* __nb = __nar;
1748227825Stheraven    int __nc;
1749227825Stheraven    if (__specify_precision)
1750227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1751227825Stheraven        __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
1752227825Stheraven                                   (int)__iob.precision(), __v);
1753227825Stheraven#else
1754227825Stheraven        __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt,
1755227825Stheraven                            (int)__iob.precision(), __v);
1756227825Stheraven#endif
1757227825Stheraven    else
1758227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1759227825Stheraven        __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1760227825Stheraven#else
1761227825Stheraven        __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt, __v);
1762227825Stheraven#endif
1763227825Stheraven    unique_ptr<char, void(*)(void*)> __nbh(0, free);
1764227825Stheraven    if (__nc > static_cast<int>(__nbuf-1))
1765227825Stheraven    {
1766227825Stheraven        if (__specify_precision)
1767227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1768227825Stheraven            __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
1769227825Stheraven#else
1770227825Stheraven            __nc = __asprintf_l(&__nb, __cloc(), __fmt,
1771227825Stheraven                              (int)__iob.precision(), __v);
1772227825Stheraven#endif
1773227825Stheraven        else
1774227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1775227825Stheraven            __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1776227825Stheraven#else
1777227825Stheraven            __nc = __asprintf_l(&__nb, __cloc(), __fmt, __v);
1778227825Stheraven#endif
1779227825Stheraven        if (__nb == 0)
1780227825Stheraven            __throw_bad_alloc();
1781227825Stheraven        __nbh.reset(__nb);
1782227825Stheraven    }
1783227825Stheraven    char* __ne = __nb + __nc;
1784227825Stheraven    char* __np = this->__identify_padding(__nb, __ne, __iob);
1785227825Stheraven    // Stage 2 - Widen __nar while adding thousands separators
1786227825Stheraven    char_type __o[2*(__nbuf-1) - 1];
1787227825Stheraven    char_type* __ob = __o;
1788227825Stheraven    unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1789227825Stheraven    if (__nb != __nar)
1790227825Stheraven    {
1791232950Stheraven        __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
1792227825Stheraven        if (__ob == 0)
1793227825Stheraven            __throw_bad_alloc();
1794227825Stheraven        __obh.reset(__ob);
1795227825Stheraven    }
1796227825Stheraven    char_type* __op;  // pad here
1797227825Stheraven    char_type* __oe;  // end of output
1798227825Stheraven    this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1799227825Stheraven    // [__o, __oe) contains thousands_sep'd wide number
1800227825Stheraven    // Stage 3 & 4
1801227825Stheraven    __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1802227825Stheraven    return __s;
1803227825Stheraven}
1804227825Stheraven
1805227825Stheraventemplate <class _CharT, class _OutputIterator>
1806227825Stheraven_OutputIterator
1807227825Stheravennum_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1808227825Stheraven                                         char_type __fl, const void* __v) const
1809227825Stheraven{
1810227825Stheraven    // Stage 1 - Get pointer in narrow char
1811227825Stheraven    char __fmt[6] = "%p";
1812227825Stheraven    const unsigned __nbuf = 20;
1813227825Stheraven    char __nar[__nbuf];
1814227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1815253159Stheraven    int __nc = snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1816227825Stheraven#else
1817253159Stheraven    int __nc = __snprintf_l(__nar, sizeof(__nar), __cloc(), __fmt, __v);
1818227825Stheraven#endif
1819227825Stheraven    char* __ne = __nar + __nc;
1820227825Stheraven    char* __np = this->__identify_padding(__nar, __ne, __iob);
1821227825Stheraven    // Stage 2 - Widen __nar
1822227825Stheraven    char_type __o[2*(__nbuf-1) - 1];
1823227825Stheraven    char_type* __op;  // pad here
1824227825Stheraven    char_type* __oe;  // end of output
1825227825Stheraven    const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
1826227825Stheraven    __ct.widen(__nar, __ne, __o);
1827227825Stheraven    __oe = __o + (__ne - __nar);
1828227825Stheraven    if (__np == __ne)
1829227825Stheraven        __op = __oe;
1830227825Stheraven    else
1831227825Stheraven        __op = __o + (__np - __nar);
1832227825Stheraven    // [__o, __oe) contains wide number
1833227825Stheraven    // Stage 3 & 4
1834227825Stheraven    return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1835227825Stheraven}
1836227825Stheraven
1837262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS num_put<char>)
1838262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS num_put<wchar_t>)
1839227825Stheraven
1840227825Stheraventemplate <class _CharT, class _InputIterator>
1841227825Stheraven_LIBCPP_HIDDEN
1842227825Stheravenint
1843227825Stheraven__get_up_to_n_digits(_InputIterator& __b, _InputIterator __e,
1844227825Stheraven                     ios_base::iostate& __err, const ctype<_CharT>& __ct, int __n)
1845227825Stheraven{
1846227825Stheraven    // Precondition:  __n >= 1
1847227825Stheraven    if (__b == __e)
1848227825Stheraven    {
1849227825Stheraven        __err |= ios_base::eofbit | ios_base::failbit;
1850227825Stheraven        return 0;
1851227825Stheraven    }
1852227825Stheraven    // get first digit
1853227825Stheraven    _CharT __c = *__b;
1854227825Stheraven    if (!__ct.is(ctype_base::digit, __c))
1855227825Stheraven    {
1856227825Stheraven        __err |= ios_base::failbit;
1857227825Stheraven        return 0;
1858227825Stheraven    }
1859227825Stheraven    int __r = __ct.narrow(__c, 0) - '0';
1860278724Sdim    for (++__b, (void) --__n; __b != __e && __n > 0; ++__b, (void) --__n)
1861227825Stheraven    {
1862227825Stheraven        // get next digit
1863227825Stheraven        __c = *__b;
1864227825Stheraven        if (!__ct.is(ctype_base::digit, __c))
1865227825Stheraven            return __r;
1866227825Stheraven        __r = __r * 10 + __ct.narrow(__c, 0) - '0';
1867227825Stheraven    }
1868227825Stheraven    if (__b == __e)
1869227825Stheraven        __err |= ios_base::eofbit;
1870227825Stheraven    return __r;
1871227825Stheraven}
1872227825Stheraven
1873249998Sdimclass _LIBCPP_TYPE_VIS time_base
1874227825Stheraven{
1875227825Stheravenpublic:
1876227825Stheraven    enum dateorder {no_order, dmy, mdy, ymd, ydm};
1877227825Stheraven};
1878227825Stheraven
1879227825Stheraventemplate <class _CharT>
1880278724Sdimclass _LIBCPP_TYPE_VIS_ONLY __time_get_c_storage
1881227825Stheraven{
1882227825Stheravenprotected:
1883227825Stheraven    typedef basic_string<_CharT> string_type;
1884227825Stheraven
1885227825Stheraven    virtual const string_type* __weeks() const;
1886227825Stheraven    virtual const string_type* __months() const;
1887227825Stheraven    virtual const string_type* __am_pm() const;
1888227825Stheraven    virtual const string_type& __c() const;
1889227825Stheraven    virtual const string_type& __r() const;
1890227825Stheraven    virtual const string_type& __x() const;
1891227825Stheraven    virtual const string_type& __X() const;
1892227825Stheraven};
1893227825Stheraven
1894227825Stheraventemplate <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
1895262801Sdimclass _LIBCPP_TYPE_VIS_ONLY time_get
1896227825Stheraven    : public locale::facet,
1897227825Stheraven      public time_base,
1898227825Stheraven      private __time_get_c_storage<_CharT>
1899227825Stheraven{
1900227825Stheravenpublic:
1901227825Stheraven    typedef _CharT                  char_type;
1902227825Stheraven    typedef _InputIterator          iter_type;
1903227825Stheraven    typedef time_base::dateorder    dateorder;
1904227825Stheraven    typedef basic_string<char_type> string_type;
1905227825Stheraven
1906227825Stheraven    _LIBCPP_ALWAYS_INLINE
1907227825Stheraven    explicit time_get(size_t __refs = 0)
1908227825Stheraven        : locale::facet(__refs) {}
1909227825Stheraven
1910227825Stheraven    _LIBCPP_ALWAYS_INLINE
1911227825Stheraven    dateorder date_order() const
1912227825Stheraven    {
1913227825Stheraven        return this->do_date_order();
1914227825Stheraven    }
1915227825Stheraven
1916227825Stheraven    _LIBCPP_ALWAYS_INLINE
1917227825Stheraven    iter_type get_time(iter_type __b, iter_type __e, ios_base& __iob,
1918227825Stheraven                       ios_base::iostate& __err, tm* __tm) const
1919227825Stheraven    {
1920227825Stheraven        return do_get_time(__b, __e, __iob, __err, __tm);
1921227825Stheraven    }
1922227825Stheraven
1923227825Stheraven    _LIBCPP_ALWAYS_INLINE
1924227825Stheraven    iter_type get_date(iter_type __b, iter_type __e, ios_base& __iob,
1925227825Stheraven                       ios_base::iostate& __err, tm* __tm) const
1926227825Stheraven    {
1927227825Stheraven        return do_get_date(__b, __e, __iob, __err, __tm);
1928227825Stheraven    }
1929227825Stheraven
1930227825Stheraven    _LIBCPP_ALWAYS_INLINE
1931227825Stheraven    iter_type get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
1932227825Stheraven                          ios_base::iostate& __err, tm* __tm) const
1933227825Stheraven    {
1934227825Stheraven        return do_get_weekday(__b, __e, __iob, __err, __tm);
1935227825Stheraven    }
1936227825Stheraven
1937227825Stheraven    _LIBCPP_ALWAYS_INLINE
1938227825Stheraven    iter_type get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
1939227825Stheraven                            ios_base::iostate& __err, tm* __tm) const
1940227825Stheraven    {
1941227825Stheraven        return do_get_monthname(__b, __e, __iob, __err, __tm);
1942227825Stheraven    }
1943227825Stheraven
1944227825Stheraven    _LIBCPP_ALWAYS_INLINE
1945227825Stheraven    iter_type get_year(iter_type __b, iter_type __e, ios_base& __iob,
1946227825Stheraven                       ios_base::iostate& __err, tm* __tm) const
1947227825Stheraven    {
1948227825Stheraven        return do_get_year(__b, __e, __iob, __err, __tm);
1949227825Stheraven    }
1950227825Stheraven
1951227825Stheraven    _LIBCPP_ALWAYS_INLINE
1952227825Stheraven    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
1953227825Stheraven                  ios_base::iostate& __err, tm *__tm,
1954227825Stheraven                  char __fmt, char __mod = 0) const
1955227825Stheraven    {
1956227825Stheraven        return do_get(__b, __e, __iob, __err, __tm, __fmt, __mod);
1957227825Stheraven    }
1958227825Stheraven
1959227825Stheraven    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
1960227825Stheraven                  ios_base::iostate& __err, tm* __tm,
1961227825Stheraven                  const char_type* __fmtb, const char_type* __fmte) const;
1962227825Stheraven
1963227825Stheraven    static locale::id id;
1964227825Stheraven
1965227825Stheravenprotected:
1966227825Stheraven    _LIBCPP_ALWAYS_INLINE
1967227825Stheraven    ~time_get() {}
1968227825Stheraven
1969227825Stheraven    virtual dateorder do_date_order() const;
1970227825Stheraven    virtual iter_type do_get_time(iter_type __b, iter_type __e, ios_base& __iob,
1971227825Stheraven                                  ios_base::iostate& __err, tm* __tm) const;
1972227825Stheraven    virtual iter_type do_get_date(iter_type __b, iter_type __e, ios_base& __iob,
1973227825Stheraven                                  ios_base::iostate& __err, tm* __tm) const;
1974227825Stheraven    virtual iter_type do_get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
1975227825Stheraven                                     ios_base::iostate& __err, tm* __tm) const;
1976227825Stheraven    virtual iter_type do_get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
1977227825Stheraven                                       ios_base::iostate& __err, tm* __tm) const;
1978227825Stheraven    virtual iter_type do_get_year(iter_type __b, iter_type __e, ios_base& __iob,
1979227825Stheraven                                  ios_base::iostate& __err, tm* __tm) const;
1980227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
1981227825Stheraven                             ios_base::iostate& __err, tm* __tm,
1982227825Stheraven                             char __fmt, char __mod) const;
1983227825Stheravenprivate:
1984227825Stheraven    void __get_white_space(iter_type& __b, iter_type __e,
1985227825Stheraven                           ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1986227825Stheraven    void __get_percent(iter_type& __b, iter_type __e, ios_base::iostate& __err,
1987227825Stheraven                       const ctype<char_type>& __ct) const;
1988227825Stheraven
1989227825Stheraven    void __get_weekdayname(int& __m,
1990227825Stheraven                           iter_type& __b, iter_type __e,
1991227825Stheraven                           ios_base::iostate& __err,
1992227825Stheraven                           const ctype<char_type>& __ct) const;
1993227825Stheraven    void __get_monthname(int& __m,
1994227825Stheraven                         iter_type& __b, iter_type __e,
1995227825Stheraven                         ios_base::iostate& __err,
1996227825Stheraven                         const ctype<char_type>& __ct) const;
1997227825Stheraven    void __get_day(int& __d,
1998227825Stheraven                   iter_type& __b, iter_type __e,
1999227825Stheraven                   ios_base::iostate& __err,
2000227825Stheraven                   const ctype<char_type>& __ct) const;
2001227825Stheraven    void __get_month(int& __m,
2002227825Stheraven                     iter_type& __b, iter_type __e,
2003227825Stheraven                     ios_base::iostate& __err,
2004227825Stheraven                     const ctype<char_type>& __ct) const;
2005227825Stheraven    void __get_year(int& __y,
2006227825Stheraven                   iter_type& __b, iter_type __e,
2007227825Stheraven                   ios_base::iostate& __err,
2008227825Stheraven                   const ctype<char_type>& __ct) const;
2009227825Stheraven    void __get_year4(int& __y,
2010227825Stheraven                    iter_type& __b, iter_type __e,
2011227825Stheraven                    ios_base::iostate& __err,
2012227825Stheraven                    const ctype<char_type>& __ct) const;
2013227825Stheraven    void __get_hour(int& __d,
2014227825Stheraven                    iter_type& __b, iter_type __e,
2015227825Stheraven                    ios_base::iostate& __err,
2016227825Stheraven                    const ctype<char_type>& __ct) const;
2017227825Stheraven    void __get_12_hour(int& __h,
2018227825Stheraven                       iter_type& __b, iter_type __e,
2019227825Stheraven                       ios_base::iostate& __err,
2020227825Stheraven                       const ctype<char_type>& __ct) const;
2021227825Stheraven    void __get_am_pm(int& __h,
2022227825Stheraven                     iter_type& __b, iter_type __e,
2023227825Stheraven                     ios_base::iostate& __err,
2024227825Stheraven                     const ctype<char_type>& __ct) const;
2025227825Stheraven    void __get_minute(int& __m,
2026227825Stheraven                      iter_type& __b, iter_type __e,
2027227825Stheraven                      ios_base::iostate& __err,
2028227825Stheraven                      const ctype<char_type>& __ct) const;
2029227825Stheraven    void __get_second(int& __s,
2030227825Stheraven                      iter_type& __b, iter_type __e,
2031227825Stheraven                      ios_base::iostate& __err,
2032227825Stheraven                      const ctype<char_type>& __ct) const;
2033227825Stheraven    void __get_weekday(int& __w,
2034227825Stheraven                       iter_type& __b, iter_type __e,
2035227825Stheraven                       ios_base::iostate& __err,
2036227825Stheraven                       const ctype<char_type>& __ct) const;
2037227825Stheraven    void __get_day_year_num(int& __w,
2038227825Stheraven                            iter_type& __b, iter_type __e,
2039227825Stheraven                            ios_base::iostate& __err,
2040227825Stheraven                            const ctype<char_type>& __ct) const;
2041227825Stheraven};
2042227825Stheraven
2043227825Stheraventemplate <class _CharT, class _InputIterator>
2044227825Stheravenlocale::id
2045227825Stheraventime_get<_CharT, _InputIterator>::id;
2046227825Stheraven
2047278724Sdim// time_get primitives
2048227825Stheraven
2049227825Stheraventemplate <class _CharT, class _InputIterator>
2050227825Stheravenvoid
2051227825Stheraventime_get<_CharT, _InputIterator>::__get_weekdayname(int& __w,
2052227825Stheraven                                                    iter_type& __b, iter_type __e,
2053227825Stheraven                                                    ios_base::iostate& __err,
2054227825Stheraven                                                    const ctype<char_type>& __ct) const
2055227825Stheraven{
2056227825Stheraven    // Note:  ignoring case comes from the POSIX strptime spec
2057227825Stheraven    const string_type* __wk = this->__weeks();
2058232950Stheraven    ptrdiff_t __i = __scan_keyword(__b, __e, __wk, __wk+14, __ct, __err, false) - __wk;
2059227825Stheraven    if (__i < 14)
2060227825Stheraven        __w = __i % 7;
2061227825Stheraven}
2062227825Stheraven
2063227825Stheraventemplate <class _CharT, class _InputIterator>
2064227825Stheravenvoid
2065227825Stheraventime_get<_CharT, _InputIterator>::__get_monthname(int& __m,
2066227825Stheraven                                                  iter_type& __b, iter_type __e,
2067227825Stheraven                                                  ios_base::iostate& __err,
2068227825Stheraven                                                  const ctype<char_type>& __ct) const
2069227825Stheraven{
2070227825Stheraven    // Note:  ignoring case comes from the POSIX strptime spec
2071227825Stheraven    const string_type* __month = this->__months();
2072232950Stheraven    ptrdiff_t __i = __scan_keyword(__b, __e, __month, __month+24, __ct, __err, false) - __month;
2073227825Stheraven    if (__i < 24)
2074227825Stheraven        __m = __i % 12;
2075227825Stheraven}
2076227825Stheraven
2077227825Stheraventemplate <class _CharT, class _InputIterator>
2078227825Stheravenvoid
2079227825Stheraventime_get<_CharT, _InputIterator>::__get_day(int& __d,
2080227825Stheraven                                            iter_type& __b, iter_type __e,
2081227825Stheraven                                            ios_base::iostate& __err,
2082227825Stheraven                                            const ctype<char_type>& __ct) const
2083227825Stheraven{
2084227825Stheraven    int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2085227825Stheraven    if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 31)
2086227825Stheraven        __d = __t;
2087227825Stheraven    else
2088227825Stheraven        __err |= ios_base::failbit;
2089227825Stheraven}
2090227825Stheraven
2091227825Stheraventemplate <class _CharT, class _InputIterator>
2092227825Stheravenvoid
2093227825Stheraventime_get<_CharT, _InputIterator>::__get_month(int& __m,
2094227825Stheraven                                              iter_type& __b, iter_type __e,
2095227825Stheraven                                              ios_base::iostate& __err,
2096227825Stheraven                                              const ctype<char_type>& __ct) const
2097227825Stheraven{
2098227825Stheraven    int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2) - 1;
2099227825Stheraven    if (!(__err & ios_base::failbit) && __t <= 11)
2100227825Stheraven        __m = __t;
2101227825Stheraven    else
2102227825Stheraven        __err |= ios_base::failbit;
2103227825Stheraven}
2104227825Stheraven
2105227825Stheraventemplate <class _CharT, class _InputIterator>
2106227825Stheravenvoid
2107227825Stheraventime_get<_CharT, _InputIterator>::__get_year(int& __y,
2108227825Stheraven                                             iter_type& __b, iter_type __e,
2109227825Stheraven                                             ios_base::iostate& __err,
2110227825Stheraven                                             const ctype<char_type>& __ct) const
2111227825Stheraven{
2112227825Stheraven    int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
2113227825Stheraven    if (!(__err & ios_base::failbit))
2114227825Stheraven    {
2115227825Stheraven        if (__t < 69)
2116227825Stheraven            __t += 2000;
2117227825Stheraven        else if (69 <= __t && __t <= 99)
2118227825Stheraven            __t += 1900;
2119227825Stheraven        __y = __t - 1900;
2120227825Stheraven    }
2121227825Stheraven}
2122227825Stheraven
2123227825Stheraventemplate <class _CharT, class _InputIterator>
2124227825Stheravenvoid
2125227825Stheraventime_get<_CharT, _InputIterator>::__get_year4(int& __y,
2126227825Stheraven                                              iter_type& __b, iter_type __e,
2127227825Stheraven                                              ios_base::iostate& __err,
2128227825Stheraven                                              const ctype<char_type>& __ct) const
2129227825Stheraven{
2130227825Stheraven    int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
2131227825Stheraven    if (!(__err & ios_base::failbit))
2132227825Stheraven        __y = __t - 1900;
2133227825Stheraven}
2134227825Stheraven
2135227825Stheraventemplate <class _CharT, class _InputIterator>
2136227825Stheravenvoid
2137227825Stheraventime_get<_CharT, _InputIterator>::__get_hour(int& __h,
2138227825Stheraven                                             iter_type& __b, iter_type __e,
2139227825Stheraven                                             ios_base::iostate& __err,
2140227825Stheraven                                             const ctype<char_type>& __ct) const
2141227825Stheraven{
2142227825Stheraven    int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2143227825Stheraven    if (!(__err & ios_base::failbit) && __t <= 23)
2144227825Stheraven        __h = __t;
2145227825Stheraven    else
2146227825Stheraven        __err |= ios_base::failbit;
2147227825Stheraven}
2148227825Stheraven
2149227825Stheraventemplate <class _CharT, class _InputIterator>
2150227825Stheravenvoid
2151227825Stheraventime_get<_CharT, _InputIterator>::__get_12_hour(int& __h,
2152227825Stheraven                                                iter_type& __b, iter_type __e,
2153227825Stheraven                                                ios_base::iostate& __err,
2154227825Stheraven                                                const ctype<char_type>& __ct) const
2155227825Stheraven{
2156227825Stheraven    int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2157227825Stheraven    if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 12)
2158227825Stheraven        __h = __t;
2159227825Stheraven    else
2160227825Stheraven        __err |= ios_base::failbit;
2161227825Stheraven}
2162227825Stheraven
2163227825Stheraventemplate <class _CharT, class _InputIterator>
2164227825Stheravenvoid
2165227825Stheraventime_get<_CharT, _InputIterator>::__get_minute(int& __m,
2166227825Stheraven                                               iter_type& __b, iter_type __e,
2167227825Stheraven                                               ios_base::iostate& __err,
2168227825Stheraven                                               const ctype<char_type>& __ct) const
2169227825Stheraven{
2170227825Stheraven    int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2171227825Stheraven    if (!(__err & ios_base::failbit) && __t <= 59)
2172227825Stheraven        __m = __t;
2173227825Stheraven    else
2174227825Stheraven        __err |= ios_base::failbit;
2175227825Stheraven}
2176227825Stheraven
2177227825Stheraventemplate <class _CharT, class _InputIterator>
2178227825Stheravenvoid
2179227825Stheraventime_get<_CharT, _InputIterator>::__get_second(int& __s,
2180227825Stheraven                                               iter_type& __b, iter_type __e,
2181227825Stheraven                                               ios_base::iostate& __err,
2182227825Stheraven                                               const ctype<char_type>& __ct) const
2183227825Stheraven{
2184227825Stheraven    int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2185227825Stheraven    if (!(__err & ios_base::failbit) && __t <= 60)
2186227825Stheraven        __s = __t;
2187227825Stheraven    else
2188227825Stheraven        __err |= ios_base::failbit;
2189227825Stheraven}
2190227825Stheraven
2191227825Stheraventemplate <class _CharT, class _InputIterator>
2192227825Stheravenvoid
2193227825Stheraventime_get<_CharT, _InputIterator>::__get_weekday(int& __w,
2194227825Stheraven                                                iter_type& __b, iter_type __e,
2195227825Stheraven                                                ios_base::iostate& __err,
2196227825Stheraven                                                const ctype<char_type>& __ct) const
2197227825Stheraven{
2198227825Stheraven    int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 1);
2199227825Stheraven    if (!(__err & ios_base::failbit) && __t <= 6)
2200227825Stheraven        __w = __t;
2201227825Stheraven    else
2202227825Stheraven        __err |= ios_base::failbit;
2203227825Stheraven}
2204227825Stheraven
2205227825Stheraventemplate <class _CharT, class _InputIterator>
2206227825Stheravenvoid
2207227825Stheraventime_get<_CharT, _InputIterator>::__get_day_year_num(int& __d,
2208227825Stheraven                                                     iter_type& __b, iter_type __e,
2209227825Stheraven                                                     ios_base::iostate& __err,
2210227825Stheraven                                                     const ctype<char_type>& __ct) const
2211227825Stheraven{
2212227825Stheraven    int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 3);
2213227825Stheraven    if (!(__err & ios_base::failbit) && __t <= 365)
2214227825Stheraven        __d = __t;
2215227825Stheraven    else
2216227825Stheraven        __err |= ios_base::failbit;
2217227825Stheraven}
2218227825Stheraven
2219227825Stheraventemplate <class _CharT, class _InputIterator>
2220227825Stheravenvoid
2221227825Stheraventime_get<_CharT, _InputIterator>::__get_white_space(iter_type& __b, iter_type __e,
2222227825Stheraven                                                    ios_base::iostate& __err,
2223227825Stheraven                                                    const ctype<char_type>& __ct) const
2224227825Stheraven{
2225227825Stheraven    for (; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2226227825Stheraven        ;
2227227825Stheraven    if (__b == __e)
2228227825Stheraven        __err |= ios_base::eofbit;
2229227825Stheraven}
2230227825Stheraven
2231227825Stheraventemplate <class _CharT, class _InputIterator>
2232227825Stheravenvoid
2233227825Stheraventime_get<_CharT, _InputIterator>::__get_am_pm(int& __h,
2234227825Stheraven                                              iter_type& __b, iter_type __e,
2235227825Stheraven                                              ios_base::iostate& __err,
2236227825Stheraven                                              const ctype<char_type>& __ct) const
2237227825Stheraven{
2238227825Stheraven    const string_type* __ap = this->__am_pm();
2239227825Stheraven    if (__ap[0].size() + __ap[1].size() == 0)
2240227825Stheraven    {
2241227825Stheraven        __err |= ios_base::failbit;
2242227825Stheraven        return;
2243227825Stheraven    }
2244232950Stheraven    ptrdiff_t __i = __scan_keyword(__b, __e, __ap, __ap+2, __ct, __err, false) - __ap;
2245227825Stheraven    if (__i == 0 && __h == 12)
2246227825Stheraven        __h = 0;
2247227825Stheraven    else if (__i == 1 && __h < 12)
2248227825Stheraven        __h += 12;
2249227825Stheraven}
2250227825Stheraven
2251227825Stheraventemplate <class _CharT, class _InputIterator>
2252227825Stheravenvoid
2253227825Stheraventime_get<_CharT, _InputIterator>::__get_percent(iter_type& __b, iter_type __e,
2254227825Stheraven                                                ios_base::iostate& __err,
2255227825Stheraven                                                const ctype<char_type>& __ct) const
2256227825Stheraven{
2257227825Stheraven    if (__b == __e)
2258227825Stheraven    {
2259227825Stheraven        __err |= ios_base::eofbit | ios_base::failbit;
2260227825Stheraven        return;
2261227825Stheraven    }
2262227825Stheraven    if (__ct.narrow(*__b, 0) != '%')
2263227825Stheraven        __err |= ios_base::failbit;
2264227825Stheraven    else if(++__b == __e)
2265227825Stheraven        __err |= ios_base::eofbit;
2266227825Stheraven}
2267227825Stheraven
2268278724Sdim// time_get end primitives
2269227825Stheraven
2270227825Stheraventemplate <class _CharT, class _InputIterator>
2271227825Stheraven_InputIterator
2272227825Stheraventime_get<_CharT, _InputIterator>::get(iter_type __b, iter_type __e,
2273227825Stheraven                                      ios_base& __iob,
2274227825Stheraven                                      ios_base::iostate& __err, tm* __tm,
2275227825Stheraven                                      const char_type* __fmtb, const char_type* __fmte) const
2276227825Stheraven{
2277227825Stheraven    const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2278227825Stheraven    __err = ios_base::goodbit;
2279227825Stheraven    while (__fmtb != __fmte && __err == ios_base::goodbit)
2280227825Stheraven    {
2281227825Stheraven        if (__b == __e)
2282227825Stheraven        {
2283227825Stheraven            __err = ios_base::failbit;
2284227825Stheraven            break;
2285227825Stheraven        }
2286227825Stheraven        if (__ct.narrow(*__fmtb, 0) == '%')
2287227825Stheraven        {
2288227825Stheraven            if (++__fmtb == __fmte)
2289227825Stheraven            {
2290227825Stheraven                __err = ios_base::failbit;
2291227825Stheraven                break;
2292227825Stheraven            }
2293227825Stheraven            char __cmd = __ct.narrow(*__fmtb, 0);
2294227825Stheraven            char __opt = '\0';
2295227825Stheraven            if (__cmd == 'E' || __cmd == '0')
2296227825Stheraven            {
2297227825Stheraven                if (++__fmtb == __fmte)
2298227825Stheraven                {
2299227825Stheraven                    __err = ios_base::failbit;
2300227825Stheraven                    break;
2301227825Stheraven                }
2302227825Stheraven                __opt = __cmd;
2303227825Stheraven                __cmd = __ct.narrow(*__fmtb, 0);
2304227825Stheraven            }
2305227825Stheraven            __b = do_get(__b, __e, __iob, __err, __tm, __cmd, __opt);
2306227825Stheraven            ++__fmtb;
2307227825Stheraven        }
2308227825Stheraven        else if (__ct.is(ctype_base::space, *__fmtb))
2309227825Stheraven        {
2310227825Stheraven            for (++__fmtb; __fmtb != __fmte && __ct.is(ctype_base::space, *__fmtb); ++__fmtb)
2311227825Stheraven                ;
2312227825Stheraven            for (        ;    __b != __e    && __ct.is(ctype_base::space, *__b);    ++__b)
2313227825Stheraven                ;
2314227825Stheraven        }
2315227825Stheraven        else if (__ct.toupper(*__b) == __ct.toupper(*__fmtb))
2316227825Stheraven        {
2317227825Stheraven            ++__b;
2318227825Stheraven            ++__fmtb;
2319227825Stheraven        }
2320227825Stheraven        else
2321227825Stheraven            __err = ios_base::failbit;
2322227825Stheraven    }
2323227825Stheraven    if (__b == __e)
2324227825Stheraven        __err |= ios_base::eofbit;
2325227825Stheraven    return __b;
2326227825Stheraven}
2327227825Stheraven
2328227825Stheraventemplate <class _CharT, class _InputIterator>
2329227825Stheraventypename time_get<_CharT, _InputIterator>::dateorder
2330227825Stheraventime_get<_CharT, _InputIterator>::do_date_order() const
2331227825Stheraven{
2332227825Stheraven    return mdy;
2333227825Stheraven}
2334227825Stheraven
2335227825Stheraventemplate <class _CharT, class _InputIterator>
2336227825Stheraven_InputIterator
2337227825Stheraventime_get<_CharT, _InputIterator>::do_get_time(iter_type __b, iter_type __e,
2338227825Stheraven                                              ios_base& __iob,
2339227825Stheraven                                              ios_base::iostate& __err,
2340227825Stheraven                                              tm* __tm) const
2341227825Stheraven{
2342227825Stheraven    const char_type __fmt[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2343227825Stheraven    return get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt)/sizeof(__fmt[0]));
2344227825Stheraven}
2345227825Stheraven
2346227825Stheraventemplate <class _CharT, class _InputIterator>
2347227825Stheraven_InputIterator
2348227825Stheraventime_get<_CharT, _InputIterator>::do_get_date(iter_type __b, iter_type __e,
2349227825Stheraven                                              ios_base& __iob,
2350227825Stheraven                                              ios_base::iostate& __err,
2351227825Stheraven                                              tm* __tm) const
2352227825Stheraven{
2353227825Stheraven    const string_type& __fmt = this->__x();
2354227825Stheraven    return get(__b, __e, __iob, __err, __tm, __fmt.data(), __fmt.data() + __fmt.size());
2355227825Stheraven}
2356227825Stheraven
2357227825Stheraventemplate <class _CharT, class _InputIterator>
2358227825Stheraven_InputIterator
2359227825Stheraventime_get<_CharT, _InputIterator>::do_get_weekday(iter_type __b, iter_type __e,
2360227825Stheraven                                                 ios_base& __iob,
2361227825Stheraven                                                 ios_base::iostate& __err,
2362227825Stheraven                                                 tm* __tm) const
2363227825Stheraven{
2364227825Stheraven    const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2365227825Stheraven    __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2366227825Stheraven    return __b;
2367227825Stheraven}
2368227825Stheraven
2369227825Stheraventemplate <class _CharT, class _InputIterator>
2370227825Stheraven_InputIterator
2371227825Stheraventime_get<_CharT, _InputIterator>::do_get_monthname(iter_type __b, iter_type __e,
2372227825Stheraven                                                   ios_base& __iob,
2373227825Stheraven                                                   ios_base::iostate& __err,
2374227825Stheraven                                                   tm* __tm) const
2375227825Stheraven{
2376227825Stheraven    const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2377227825Stheraven    __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2378227825Stheraven    return __b;
2379227825Stheraven}
2380227825Stheraven
2381227825Stheraventemplate <class _CharT, class _InputIterator>
2382227825Stheraven_InputIterator
2383227825Stheraventime_get<_CharT, _InputIterator>::do_get_year(iter_type __b, iter_type __e,
2384227825Stheraven                                              ios_base& __iob,
2385227825Stheraven                                              ios_base::iostate& __err,
2386227825Stheraven                                              tm* __tm) const
2387227825Stheraven{
2388227825Stheraven    const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2389227825Stheraven    __get_year(__tm->tm_year, __b, __e, __err, __ct);
2390227825Stheraven    return __b;
2391227825Stheraven}
2392227825Stheraven
2393227825Stheraventemplate <class _CharT, class _InputIterator>
2394227825Stheraven_InputIterator
2395227825Stheraventime_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
2396227825Stheraven                                         ios_base& __iob,
2397227825Stheraven                                         ios_base::iostate& __err, tm* __tm,
2398227825Stheraven                                         char __fmt, char) const
2399227825Stheraven{
2400227825Stheraven    __err = ios_base::goodbit;
2401227825Stheraven    const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2402227825Stheraven    switch (__fmt)
2403227825Stheraven    {
2404227825Stheraven    case 'a':
2405227825Stheraven    case 'A':
2406227825Stheraven        __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2407227825Stheraven        break;
2408227825Stheraven    case 'b':
2409227825Stheraven    case 'B':
2410227825Stheraven    case 'h':
2411227825Stheraven        __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2412227825Stheraven        break;
2413227825Stheraven    case 'c':
2414227825Stheraven        {
2415232950Stheraven        const string_type& __fm = this->__c();
2416232950Stheraven        __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
2417227825Stheraven        }
2418227825Stheraven        break;
2419227825Stheraven    case 'd':
2420227825Stheraven    case 'e':
2421227825Stheraven        __get_day(__tm->tm_mday, __b, __e, __err, __ct);
2422227825Stheraven        break;
2423227825Stheraven    case 'D':
2424227825Stheraven        {
2425232950Stheraven        const char_type __fm[] = {'%', 'm', '/', '%', 'd', '/', '%', 'y'};
2426232950Stheraven        __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2427227825Stheraven        }
2428227825Stheraven        break;
2429227825Stheraven    case 'F':
2430227825Stheraven        {
2431232950Stheraven        const char_type __fm[] = {'%', 'Y', '-', '%', 'm', '-', '%', 'd'};
2432232950Stheraven        __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2433227825Stheraven        }
2434227825Stheraven        break;
2435227825Stheraven    case 'H':
2436227825Stheraven        __get_hour(__tm->tm_hour, __b, __e, __err, __ct);
2437227825Stheraven        break;
2438227825Stheraven    case 'I':
2439227825Stheraven        __get_12_hour(__tm->tm_hour, __b, __e, __err, __ct);
2440227825Stheraven        break;
2441227825Stheraven    case 'j':
2442227825Stheraven        __get_day_year_num(__tm->tm_yday, __b, __e, __err, __ct);
2443227825Stheraven        break;
2444227825Stheraven    case 'm':
2445227825Stheraven        __get_month(__tm->tm_mon, __b, __e, __err, __ct);
2446227825Stheraven        break;
2447227825Stheraven    case 'M':
2448227825Stheraven        __get_minute(__tm->tm_min, __b, __e, __err, __ct);
2449227825Stheraven        break;
2450227825Stheraven    case 'n':
2451227825Stheraven    case 't':
2452227825Stheraven        __get_white_space(__b, __e, __err, __ct);
2453227825Stheraven        break;
2454227825Stheraven    case 'p':
2455227825Stheraven        __get_am_pm(__tm->tm_hour, __b, __e, __err, __ct);
2456227825Stheraven        break;
2457227825Stheraven    case 'r':
2458227825Stheraven        {
2459232950Stheraven        const char_type __fm[] = {'%', 'I', ':', '%', 'M', ':', '%', 'S', ' ', '%', 'p'};
2460232950Stheraven        __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2461227825Stheraven        }
2462227825Stheraven        break;
2463227825Stheraven    case 'R':
2464227825Stheraven        {
2465232950Stheraven        const char_type __fm[] = {'%', 'H', ':', '%', 'M'};
2466232950Stheraven        __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2467227825Stheraven        }
2468227825Stheraven        break;
2469227825Stheraven    case 'S':
2470227825Stheraven        __get_second(__tm->tm_sec, __b, __e, __err, __ct);
2471227825Stheraven        break;
2472227825Stheraven    case 'T':
2473227825Stheraven        {
2474232950Stheraven        const char_type __fm[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2475232950Stheraven        __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2476227825Stheraven        }
2477227825Stheraven        break;
2478227825Stheraven    case 'w':
2479227825Stheraven        __get_weekday(__tm->tm_wday, __b, __e, __err, __ct);
2480227825Stheraven        break;
2481227825Stheraven    case 'x':
2482227825Stheraven        return do_get_date(__b, __e, __iob, __err, __tm);
2483227825Stheraven    case 'X':
2484227825Stheraven        {
2485232950Stheraven        const string_type& __fm = this->__X();
2486232950Stheraven        __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
2487227825Stheraven        }
2488227825Stheraven        break;
2489227825Stheraven    case 'y':
2490227825Stheraven        __get_year(__tm->tm_year, __b, __e, __err, __ct);
2491227825Stheraven        break;
2492227825Stheraven    case 'Y':
2493227825Stheraven        __get_year4(__tm->tm_year, __b, __e, __err, __ct);
2494227825Stheraven        break;
2495227825Stheraven    case '%':
2496227825Stheraven        __get_percent(__b, __e, __err, __ct);
2497227825Stheraven        break;
2498227825Stheraven    default:
2499227825Stheraven        __err |= ios_base::failbit;
2500227825Stheraven    }
2501227825Stheraven    return __b;
2502227825Stheraven}
2503227825Stheraven
2504262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_get<char>)
2505262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_get<wchar_t>)
2506227825Stheraven
2507262801Sdimclass _LIBCPP_TYPE_VIS __time_get
2508227825Stheraven{
2509227825Stheravenprotected:
2510227825Stheraven    locale_t __loc_;
2511227825Stheraven
2512227825Stheraven    __time_get(const char* __nm);
2513227825Stheraven    __time_get(const string& __nm);
2514227825Stheraven    ~__time_get();
2515227825Stheraven};
2516227825Stheraven
2517227825Stheraventemplate <class _CharT>
2518278724Sdimclass _LIBCPP_TYPE_VIS_ONLY __time_get_storage
2519227825Stheraven    : public __time_get
2520227825Stheraven{
2521227825Stheravenprotected:
2522227825Stheraven    typedef basic_string<_CharT> string_type;
2523227825Stheraven
2524227825Stheraven    string_type __weeks_[14];
2525227825Stheraven    string_type __months_[24];
2526227825Stheraven    string_type __am_pm_[2];
2527227825Stheraven    string_type __c_;
2528227825Stheraven    string_type __r_;
2529227825Stheraven    string_type __x_;
2530227825Stheraven    string_type __X_;
2531227825Stheraven
2532227825Stheraven    explicit __time_get_storage(const char* __nm);
2533227825Stheraven    explicit __time_get_storage(const string& __nm);
2534227825Stheraven
2535227825Stheraven    _LIBCPP_ALWAYS_INLINE ~__time_get_storage() {}
2536227825Stheraven
2537227825Stheraven    time_base::dateorder __do_date_order() const;
2538227825Stheraven
2539227825Stheravenprivate:
2540227825Stheraven    void init(const ctype<_CharT>&);
2541227825Stheraven    string_type __analyze(char __fmt, const ctype<_CharT>&);
2542227825Stheraven};
2543227825Stheraven
2544227825Stheraventemplate <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2545262801Sdimclass _LIBCPP_TYPE_VIS_ONLY time_get_byname
2546227825Stheraven    : public time_get<_CharT, _InputIterator>,
2547227825Stheraven      private __time_get_storage<_CharT>
2548227825Stheraven{
2549227825Stheravenpublic:
2550227825Stheraven    typedef time_base::dateorder    dateorder;
2551227825Stheraven    typedef _InputIterator          iter_type;
2552227825Stheraven    typedef _CharT                  char_type;
2553227825Stheraven    typedef basic_string<char_type> string_type;
2554227825Stheraven
2555227825Stheraven    _LIBCPP_INLINE_VISIBILITY
2556227825Stheraven    explicit time_get_byname(const char* __nm, size_t __refs = 0)
2557227825Stheraven        : time_get<_CharT, _InputIterator>(__refs),
2558227825Stheraven          __time_get_storage<_CharT>(__nm) {}
2559227825Stheraven    _LIBCPP_INLINE_VISIBILITY
2560227825Stheraven    explicit time_get_byname(const string& __nm, size_t __refs = 0)
2561227825Stheraven        : time_get<_CharT, _InputIterator>(__refs),
2562227825Stheraven          __time_get_storage<_CharT>(__nm) {}
2563227825Stheraven
2564227825Stheravenprotected:
2565227825Stheraven    _LIBCPP_INLINE_VISIBILITY
2566227825Stheraven    ~time_get_byname() {}
2567227825Stheraven
2568227825Stheraven    _LIBCPP_INLINE_VISIBILITY
2569227825Stheraven    virtual dateorder do_date_order() const {return this->__do_date_order();}
2570227825Stheravenprivate:
2571227825Stheraven    _LIBCPP_INLINE_VISIBILITY
2572227825Stheraven    virtual const string_type* __weeks() const  {return this->__weeks_;}
2573227825Stheraven    _LIBCPP_INLINE_VISIBILITY
2574227825Stheraven    virtual const string_type* __months() const {return this->__months_;}
2575227825Stheraven    _LIBCPP_INLINE_VISIBILITY
2576227825Stheraven    virtual const string_type* __am_pm() const  {return this->__am_pm_;}
2577227825Stheraven    _LIBCPP_INLINE_VISIBILITY
2578227825Stheraven    virtual const string_type& __c() const      {return this->__c_;}
2579227825Stheraven    _LIBCPP_INLINE_VISIBILITY
2580227825Stheraven    virtual const string_type& __r() const      {return this->__r_;}
2581227825Stheraven    _LIBCPP_INLINE_VISIBILITY
2582227825Stheraven    virtual const string_type& __x() const      {return this->__x_;}
2583227825Stheraven    _LIBCPP_INLINE_VISIBILITY
2584227825Stheraven    virtual const string_type& __X() const      {return this->__X_;}
2585227825Stheraven};
2586227825Stheraven
2587262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_get_byname<char>)
2588262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_get_byname<wchar_t>)
2589227825Stheraven
2590262801Sdimclass _LIBCPP_TYPE_VIS __time_put
2591227825Stheraven{
2592227825Stheraven    locale_t __loc_;
2593227825Stheravenprotected:
2594227825Stheraven    _LIBCPP_ALWAYS_INLINE __time_put() : __loc_(_LIBCPP_GET_C_LOCALE) {}
2595227825Stheraven    __time_put(const char* __nm);
2596227825Stheraven    __time_put(const string& __nm);
2597227825Stheraven    ~__time_put();
2598227825Stheraven    void __do_put(char* __nb, char*& __ne, const tm* __tm,
2599227825Stheraven                  char __fmt, char __mod) const;
2600227825Stheraven    void __do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm,
2601227825Stheraven                  char __fmt, char __mod) const;
2602227825Stheraven};
2603227825Stheraven
2604227825Stheraventemplate <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2605262801Sdimclass _LIBCPP_TYPE_VIS_ONLY time_put
2606227825Stheraven    : public locale::facet,
2607227825Stheraven      private __time_put
2608227825Stheraven{
2609227825Stheravenpublic:
2610227825Stheraven    typedef _CharT char_type;
2611227825Stheraven    typedef _OutputIterator iter_type;
2612227825Stheraven
2613227825Stheraven    _LIBCPP_ALWAYS_INLINE
2614227825Stheraven    explicit time_put(size_t __refs = 0)
2615227825Stheraven        : locale::facet(__refs) {}
2616227825Stheraven
2617227825Stheraven    iter_type put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm,
2618227825Stheraven                  const char_type* __pb, const char_type* __pe) const;
2619227825Stheraven
2620227825Stheraven    _LIBCPP_ALWAYS_INLINE
2621227825Stheraven    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
2622227825Stheraven                  const tm* __tm, char __fmt, char __mod = 0) const
2623227825Stheraven    {
2624227825Stheraven        return do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2625227825Stheraven    }
2626227825Stheraven
2627227825Stheraven    static locale::id id;
2628227825Stheraven
2629227825Stheravenprotected:
2630227825Stheraven    _LIBCPP_ALWAYS_INLINE
2631227825Stheraven    ~time_put() {}
2632227825Stheraven    virtual iter_type do_put(iter_type __s, ios_base&, char_type, const tm* __tm,
2633227825Stheraven                             char __fmt, char __mod) const;
2634227825Stheraven
2635227825Stheraven    _LIBCPP_ALWAYS_INLINE
2636227825Stheraven    explicit time_put(const char* __nm, size_t __refs)
2637227825Stheraven        : locale::facet(__refs),
2638227825Stheraven          __time_put(__nm) {}
2639227825Stheraven    _LIBCPP_ALWAYS_INLINE
2640227825Stheraven    explicit time_put(const string& __nm, size_t __refs)
2641227825Stheraven        : locale::facet(__refs),
2642227825Stheraven          __time_put(__nm) {}
2643227825Stheraven};
2644227825Stheraven
2645227825Stheraventemplate <class _CharT, class _OutputIterator>
2646227825Stheravenlocale::id
2647227825Stheraventime_put<_CharT, _OutputIterator>::id;
2648227825Stheraven
2649227825Stheraventemplate <class _CharT, class _OutputIterator>
2650227825Stheraven_OutputIterator
2651227825Stheraventime_put<_CharT, _OutputIterator>::put(iter_type __s, ios_base& __iob,
2652227825Stheraven                                       char_type __fl, const tm* __tm,
2653227825Stheraven                                       const char_type* __pb,
2654227825Stheraven                                       const char_type* __pe) const
2655227825Stheraven{
2656227825Stheraven    const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2657227825Stheraven    for (; __pb != __pe; ++__pb)
2658227825Stheraven    {
2659227825Stheraven        if (__ct.narrow(*__pb, 0) == '%')
2660227825Stheraven        {
2661227825Stheraven            if (++__pb == __pe)
2662227825Stheraven            {
2663227825Stheraven                *__s++ = __pb[-1];
2664227825Stheraven                break;
2665227825Stheraven            }
2666227825Stheraven            char __mod = 0;
2667227825Stheraven            char __fmt = __ct.narrow(*__pb, 0);
2668227825Stheraven            if (__fmt == 'E' || __fmt == 'O')
2669227825Stheraven            {
2670227825Stheraven                if (++__pb == __pe)
2671227825Stheraven                {
2672227825Stheraven                    *__s++ = __pb[-2];
2673227825Stheraven                    *__s++ = __pb[-1];
2674227825Stheraven                    break;
2675227825Stheraven                }
2676227825Stheraven                __mod = __fmt;
2677227825Stheraven                __fmt = __ct.narrow(*__pb, 0);
2678227825Stheraven            }
2679227825Stheraven            __s = do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2680227825Stheraven        }
2681227825Stheraven        else
2682227825Stheraven            *__s++ = *__pb;
2683227825Stheraven    }
2684227825Stheraven    return __s;
2685227825Stheraven}
2686227825Stheraven
2687227825Stheraventemplate <class _CharT, class _OutputIterator>
2688227825Stheraven_OutputIterator
2689232950Stheraventime_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base&,
2690227825Stheraven                                          char_type, const tm* __tm,
2691227825Stheraven                                          char __fmt, char __mod) const
2692227825Stheraven{
2693227825Stheraven    char_type __nar[100];
2694227825Stheraven    char_type* __nb = __nar;
2695227825Stheraven    char_type* __ne = __nb + 100;
2696227825Stheraven    __do_put(__nb, __ne, __tm, __fmt, __mod);
2697227825Stheraven    return _VSTD::copy(__nb, __ne, __s);
2698227825Stheraven}
2699227825Stheraven
2700262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_put<char>)
2701262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_put<wchar_t>)
2702227825Stheraven
2703227825Stheraventemplate <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2704262801Sdimclass _LIBCPP_TYPE_VIS_ONLY time_put_byname
2705227825Stheraven    : public time_put<_CharT, _OutputIterator>
2706227825Stheraven{
2707227825Stheravenpublic:
2708227825Stheraven    _LIBCPP_ALWAYS_INLINE
2709227825Stheraven    explicit time_put_byname(const char* __nm, size_t __refs = 0)
2710227825Stheraven        : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2711227825Stheraven
2712227825Stheraven    _LIBCPP_ALWAYS_INLINE
2713227825Stheraven    explicit time_put_byname(const string& __nm, size_t __refs = 0)
2714227825Stheraven        : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2715227825Stheraven
2716227825Stheravenprotected:
2717227825Stheraven    _LIBCPP_ALWAYS_INLINE
2718227825Stheraven    ~time_put_byname() {}
2719227825Stheraven};
2720227825Stheraven
2721262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_put_byname<char>)
2722262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_put_byname<wchar_t>)
2723227825Stheraven
2724227825Stheraven// money_base
2725227825Stheraven
2726249998Sdimclass _LIBCPP_TYPE_VIS money_base
2727227825Stheraven{
2728227825Stheravenpublic:
2729227825Stheraven    enum part {none, space, symbol, sign, value};
2730227825Stheraven    struct pattern {char field[4];};
2731227825Stheraven
2732227825Stheraven    _LIBCPP_ALWAYS_INLINE money_base() {}
2733227825Stheraven};
2734227825Stheraven
2735227825Stheraven// moneypunct
2736227825Stheraven
2737227825Stheraventemplate <class _CharT, bool _International = false>
2738262801Sdimclass _LIBCPP_TYPE_VIS_ONLY moneypunct
2739227825Stheraven    : public locale::facet,
2740227825Stheraven      public money_base
2741227825Stheraven{
2742227825Stheravenpublic:
2743227825Stheraven    typedef _CharT                  char_type;
2744227825Stheraven    typedef basic_string<char_type> string_type;
2745227825Stheraven
2746227825Stheraven    _LIBCPP_ALWAYS_INLINE
2747227825Stheraven    explicit moneypunct(size_t __refs = 0)
2748227825Stheraven        : locale::facet(__refs) {}
2749227825Stheraven
2750227825Stheraven    _LIBCPP_ALWAYS_INLINE char_type   decimal_point() const {return do_decimal_point();}
2751227825Stheraven    _LIBCPP_ALWAYS_INLINE char_type   thousands_sep() const {return do_thousands_sep();}
2752227825Stheraven    _LIBCPP_ALWAYS_INLINE string      grouping()      const {return do_grouping();}
2753227825Stheraven    _LIBCPP_ALWAYS_INLINE string_type curr_symbol()   const {return do_curr_symbol();}
2754227825Stheraven    _LIBCPP_ALWAYS_INLINE string_type positive_sign() const {return do_positive_sign();}
2755227825Stheraven    _LIBCPP_ALWAYS_INLINE string_type negative_sign() const {return do_negative_sign();}
2756227825Stheraven    _LIBCPP_ALWAYS_INLINE int         frac_digits()   const {return do_frac_digits();}
2757227825Stheraven    _LIBCPP_ALWAYS_INLINE pattern     pos_format()    const {return do_pos_format();}
2758227825Stheraven    _LIBCPP_ALWAYS_INLINE pattern     neg_format()    const {return do_neg_format();}
2759227825Stheraven
2760227825Stheraven    static locale::id id;
2761227825Stheraven    static const bool intl = _International;
2762227825Stheraven
2763227825Stheravenprotected:
2764227825Stheraven    _LIBCPP_ALWAYS_INLINE
2765227825Stheraven    ~moneypunct() {}
2766227825Stheraven
2767227825Stheraven    virtual char_type   do_decimal_point() const {return numeric_limits<char_type>::max();}
2768227825Stheraven    virtual char_type   do_thousands_sep() const {return numeric_limits<char_type>::max();}
2769227825Stheraven    virtual string      do_grouping()      const {return string();}
2770227825Stheraven    virtual string_type do_curr_symbol()   const {return string_type();}
2771227825Stheraven    virtual string_type do_positive_sign() const {return string_type();}
2772227825Stheraven    virtual string_type do_negative_sign() const {return string_type(1, '-');}
2773227825Stheraven    virtual int         do_frac_digits()   const {return 0;}
2774227825Stheraven    virtual pattern     do_pos_format()    const
2775241907Sdim        {pattern __p = {{symbol, sign, none, value}}; return __p;}
2776227825Stheraven    virtual pattern     do_neg_format()    const
2777241907Sdim        {pattern __p = {{symbol, sign, none, value}}; return __p;}
2778227825Stheraven};
2779227825Stheraven
2780227825Stheraventemplate <class _CharT, bool _International>
2781227825Stheravenlocale::id
2782227825Stheravenmoneypunct<_CharT, _International>::id;
2783227825Stheraven
2784246487Stheraventemplate <class _CharT, bool _International>
2785246487Stheravenconst bool
2786246487Stheravenmoneypunct<_CharT, _International>::intl;
2787246487Stheraven
2788262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct<char, false>)
2789262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct<char, true>)
2790262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct<wchar_t, false>)
2791262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct<wchar_t, true>)
2792227825Stheraven
2793227825Stheraven// moneypunct_byname
2794227825Stheraven
2795227825Stheraventemplate <class _CharT, bool _International = false>
2796262801Sdimclass _LIBCPP_TYPE_VIS_ONLY moneypunct_byname
2797227825Stheraven    : public moneypunct<_CharT, _International>
2798227825Stheraven{
2799227825Stheravenpublic:
2800227825Stheraven    typedef money_base::pattern  pattern;
2801227825Stheraven    typedef _CharT                  char_type;
2802227825Stheraven    typedef basic_string<char_type> string_type;
2803227825Stheraven
2804227825Stheraven    _LIBCPP_ALWAYS_INLINE
2805227825Stheraven    explicit moneypunct_byname(const char* __nm, size_t __refs = 0)
2806227825Stheraven        : moneypunct<_CharT, _International>(__refs) {init(__nm);}
2807227825Stheraven
2808227825Stheraven    _LIBCPP_ALWAYS_INLINE
2809227825Stheraven    explicit moneypunct_byname(const string& __nm, size_t __refs = 0)
2810227825Stheraven        : moneypunct<_CharT, _International>(__refs) {init(__nm.c_str());}
2811227825Stheraven
2812227825Stheravenprotected:
2813227825Stheraven    _LIBCPP_ALWAYS_INLINE
2814227825Stheraven    ~moneypunct_byname() {}
2815227825Stheraven
2816227825Stheraven    virtual char_type   do_decimal_point() const {return __decimal_point_;}
2817227825Stheraven    virtual char_type   do_thousands_sep() const {return __thousands_sep_;}
2818227825Stheraven    virtual string      do_grouping()      const {return __grouping_;}
2819227825Stheraven    virtual string_type do_curr_symbol()   const {return __curr_symbol_;}
2820227825Stheraven    virtual string_type do_positive_sign() const {return __positive_sign_;}
2821227825Stheraven    virtual string_type do_negative_sign() const {return __negative_sign_;}
2822227825Stheraven    virtual int         do_frac_digits()   const {return __frac_digits_;}
2823227825Stheraven    virtual pattern     do_pos_format()    const {return __pos_format_;}
2824227825Stheraven    virtual pattern     do_neg_format()    const {return __neg_format_;}
2825227825Stheraven
2826227825Stheravenprivate:
2827227825Stheraven    char_type   __decimal_point_;
2828227825Stheraven    char_type   __thousands_sep_;
2829227825Stheraven    string      __grouping_;
2830227825Stheraven    string_type __curr_symbol_;
2831227825Stheraven    string_type __positive_sign_;
2832227825Stheraven    string_type __negative_sign_;
2833227825Stheraven    int         __frac_digits_;
2834227825Stheraven    pattern     __pos_format_;
2835227825Stheraven    pattern     __neg_format_;
2836227825Stheraven
2837227825Stheraven    void init(const char*);
2838227825Stheraven};
2839227825Stheraven
2840227825Stheraventemplate<> void moneypunct_byname<char, false>::init(const char*);
2841227825Stheraventemplate<> void moneypunct_byname<char, true>::init(const char*);
2842227825Stheraventemplate<> void moneypunct_byname<wchar_t, false>::init(const char*);
2843227825Stheraventemplate<> void moneypunct_byname<wchar_t, true>::init(const char*);
2844227825Stheraven
2845262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct_byname<char, false>)
2846262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct_byname<char, true>)
2847262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct_byname<wchar_t, false>)
2848262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct_byname<wchar_t, true>)
2849227825Stheraven
2850227825Stheraven// money_get
2851227825Stheraven
2852227825Stheraventemplate <class _CharT>
2853227825Stheravenclass __money_get
2854227825Stheraven{
2855227825Stheravenprotected:
2856227825Stheraven    typedef _CharT                  char_type;
2857227825Stheraven    typedef basic_string<char_type> string_type;
2858227825Stheraven
2859227825Stheraven    _LIBCPP_ALWAYS_INLINE __money_get() {}
2860227825Stheraven
2861227825Stheraven    static void __gather_info(bool __intl, const locale& __loc,
2862227825Stheraven                              money_base::pattern& __pat, char_type& __dp,
2863227825Stheraven                              char_type& __ts, string& __grp,
2864227825Stheraven                              string_type& __sym, string_type& __psn,
2865227825Stheraven                              string_type& __nsn, int& __fd);
2866227825Stheraven};
2867227825Stheraven
2868227825Stheraventemplate <class _CharT>
2869227825Stheravenvoid
2870227825Stheraven__money_get<_CharT>::__gather_info(bool __intl, const locale& __loc,
2871227825Stheraven                                   money_base::pattern& __pat, char_type& __dp,
2872227825Stheraven                                   char_type& __ts, string& __grp,
2873227825Stheraven                                   string_type& __sym, string_type& __psn,
2874227825Stheraven                                   string_type& __nsn, int& __fd)
2875227825Stheraven{
2876227825Stheraven    if (__intl)
2877227825Stheraven    {
2878227825Stheraven        const moneypunct<char_type, true>& __mp =
2879227825Stheraven            use_facet<moneypunct<char_type, true> >(__loc);
2880227825Stheraven        __pat = __mp.neg_format();
2881227825Stheraven        __nsn = __mp.negative_sign();
2882227825Stheraven        __psn = __mp.positive_sign();
2883227825Stheraven        __dp = __mp.decimal_point();
2884227825Stheraven        __ts = __mp.thousands_sep();
2885227825Stheraven        __grp = __mp.grouping();
2886227825Stheraven        __sym = __mp.curr_symbol();
2887227825Stheraven        __fd = __mp.frac_digits();
2888227825Stheraven    }
2889227825Stheraven    else
2890227825Stheraven    {
2891227825Stheraven        const moneypunct<char_type, false>& __mp =
2892227825Stheraven            use_facet<moneypunct<char_type, false> >(__loc);
2893227825Stheraven        __pat = __mp.neg_format();
2894227825Stheraven        __nsn = __mp.negative_sign();
2895227825Stheraven        __psn = __mp.positive_sign();
2896227825Stheraven        __dp = __mp.decimal_point();
2897227825Stheraven        __ts = __mp.thousands_sep();
2898227825Stheraven        __grp = __mp.grouping();
2899227825Stheraven        __sym = __mp.curr_symbol();
2900227825Stheraven        __fd = __mp.frac_digits();
2901227825Stheraven    }
2902227825Stheraven}
2903227825Stheraven
2904262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS __money_get<char>)
2905262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS __money_get<wchar_t>)
2906227825Stheraven
2907227825Stheraventemplate <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2908262801Sdimclass _LIBCPP_TYPE_VIS_ONLY money_get
2909227825Stheraven    : public locale::facet,
2910227825Stheraven      private __money_get<_CharT>
2911227825Stheraven{
2912227825Stheravenpublic:
2913227825Stheraven    typedef _CharT                  char_type;
2914227825Stheraven    typedef _InputIterator          iter_type;
2915227825Stheraven    typedef basic_string<char_type> string_type;
2916227825Stheraven
2917227825Stheraven    _LIBCPP_ALWAYS_INLINE
2918227825Stheraven    explicit money_get(size_t __refs = 0)
2919227825Stheraven        : locale::facet(__refs) {}
2920227825Stheraven
2921227825Stheraven    _LIBCPP_ALWAYS_INLINE
2922227825Stheraven    iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
2923227825Stheraven                  ios_base::iostate& __err, long double& __v) const
2924227825Stheraven    {
2925227825Stheraven        return do_get(__b, __e, __intl, __iob, __err, __v);
2926227825Stheraven    }
2927227825Stheraven
2928227825Stheraven    _LIBCPP_ALWAYS_INLINE
2929227825Stheraven    iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
2930227825Stheraven                  ios_base::iostate& __err, string_type& __v) const
2931227825Stheraven    {
2932227825Stheraven        return do_get(__b, __e, __intl, __iob, __err, __v);
2933227825Stheraven    }
2934227825Stheraven
2935227825Stheraven    static locale::id id;
2936227825Stheraven
2937227825Stheravenprotected:
2938227825Stheraven
2939227825Stheraven    _LIBCPP_ALWAYS_INLINE
2940227825Stheraven    ~money_get() {}
2941227825Stheraven
2942227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
2943227825Stheraven                             ios_base& __iob, ios_base::iostate& __err,
2944227825Stheraven                             long double& __v) const;
2945227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
2946227825Stheraven                             ios_base& __iob, ios_base::iostate& __err,
2947227825Stheraven                             string_type& __v) const;
2948227825Stheraven
2949227825Stheravenprivate:
2950227825Stheraven    static bool __do_get(iter_type& __b, iter_type __e,
2951227825Stheraven                         bool __intl, const locale& __loc,
2952227825Stheraven                         ios_base::fmtflags __flags, ios_base::iostate& __err,
2953227825Stheraven                         bool& __neg, const ctype<char_type>& __ct,
2954227825Stheraven                         unique_ptr<char_type, void(*)(void*)>& __wb,
2955227825Stheraven                         char_type*& __wn, char_type* __we);
2956227825Stheraven};
2957227825Stheraven
2958227825Stheraventemplate <class _CharT, class _InputIterator>
2959227825Stheravenlocale::id
2960227825Stheravenmoney_get<_CharT, _InputIterator>::id;
2961227825Stheraven
2962262801Sdim_LIBCPP_FUNC_VIS void __do_nothing(void*);
2963227825Stheraven
2964227825Stheraventemplate <class _Tp>
2965227825Stheraven_LIBCPP_HIDDEN
2966227825Stheravenvoid
2967227825Stheraven__double_or_nothing(unique_ptr<_Tp, void(*)(void*)>& __b, _Tp*& __n, _Tp*& __e)
2968227825Stheraven{
2969227825Stheraven    bool __owns = __b.get_deleter() != __do_nothing;
2970232950Stheraven    size_t __cur_cap = static_cast<size_t>(__e-__b.get()) * sizeof(_Tp);
2971227825Stheraven    size_t __new_cap = __cur_cap < numeric_limits<size_t>::max() / 2 ?
2972227825Stheraven                       2 * __cur_cap : numeric_limits<size_t>::max();
2973278724Sdim    if (__new_cap == 0)
2974278724Sdim        __new_cap = sizeof(_Tp);
2975232950Stheraven    size_t __n_off = static_cast<size_t>(__n - __b.get());
2976227825Stheraven    _Tp* __t = (_Tp*)realloc(__owns ? __b.get() : 0, __new_cap);
2977227825Stheraven    if (__t == 0)
2978227825Stheraven        __throw_bad_alloc();
2979227825Stheraven    if (__owns)
2980227825Stheraven        __b.release();
2981227825Stheraven    __b = unique_ptr<_Tp, void(*)(void*)>(__t, free);
2982227825Stheraven    __new_cap /= sizeof(_Tp);
2983227825Stheraven    __n = __b.get() + __n_off;
2984227825Stheraven    __e = __b.get() + __new_cap;
2985227825Stheraven}
2986227825Stheraven
2987227825Stheraven// true == success
2988227825Stheraventemplate <class _CharT, class _InputIterator>
2989227825Stheravenbool
2990227825Stheravenmoney_get<_CharT, _InputIterator>::__do_get(iter_type& __b, iter_type __e,
2991227825Stheraven                                            bool __intl, const locale& __loc,
2992227825Stheraven                                            ios_base::fmtflags __flags,
2993227825Stheraven                                            ios_base::iostate& __err,
2994227825Stheraven                                            bool& __neg,
2995227825Stheraven                                            const ctype<char_type>& __ct,
2996227825Stheraven                                            unique_ptr<char_type, void(*)(void*)>& __wb,
2997227825Stheraven                                            char_type*& __wn, char_type* __we)
2998227825Stheraven{
2999227825Stheraven    const unsigned __bz = 100;
3000227825Stheraven    unsigned __gbuf[__bz];
3001227825Stheraven    unique_ptr<unsigned, void(*)(void*)> __gb(__gbuf, __do_nothing);
3002227825Stheraven    unsigned* __gn = __gb.get();
3003227825Stheraven    unsigned* __ge = __gn + __bz;
3004227825Stheraven    money_base::pattern __pat;
3005227825Stheraven    char_type __dp;
3006227825Stheraven    char_type __ts;
3007227825Stheraven    string __grp;
3008227825Stheraven    string_type __sym;
3009227825Stheraven    string_type __psn;
3010227825Stheraven    string_type __nsn;
3011232950Stheraven    // Capture the spaces read into money_base::{space,none} so they
3012232950Stheraven    // can be compared to initial spaces in __sym.
3013232950Stheraven    string_type __spaces;
3014227825Stheraven    int __fd;
3015227825Stheraven    __money_get<_CharT>::__gather_info(__intl, __loc, __pat, __dp, __ts, __grp,
3016227825Stheraven                                       __sym, __psn, __nsn, __fd);
3017227825Stheraven    const string_type* __trailing_sign = 0;
3018227825Stheraven    __wn = __wb.get();
3019227825Stheraven    for (unsigned __p = 0; __p < 4 && __b != __e; ++__p)
3020227825Stheraven    {
3021227825Stheraven        switch (__pat.field[__p])
3022227825Stheraven        {
3023227825Stheraven        case money_base::space:
3024227825Stheraven            if (__p != 3)
3025227825Stheraven            {
3026227825Stheraven                if (__ct.is(ctype_base::space, *__b))
3027232950Stheraven                    __spaces.push_back(*__b++);
3028227825Stheraven                else
3029227825Stheraven                {
3030227825Stheraven                    __err |= ios_base::failbit;
3031227825Stheraven                    return false;
3032227825Stheraven                }
3033227825Stheraven            }
3034227825Stheraven            // drop through
3035227825Stheraven        case money_base::none:
3036227825Stheraven            if (__p != 3)
3037227825Stheraven            {
3038227825Stheraven                while (__b != __e && __ct.is(ctype_base::space, *__b))
3039232950Stheraven                    __spaces.push_back(*__b++);
3040227825Stheraven            }
3041227825Stheraven            break;
3042227825Stheraven        case money_base::sign:
3043227825Stheraven            if (__psn.size() + __nsn.size() > 0)
3044227825Stheraven            {
3045227825Stheraven                if (__psn.size() == 0 || __nsn.size() == 0)
3046227825Stheraven                {   // sign is optional
3047227825Stheraven                    if (__psn.size() > 0)
3048227825Stheraven                    {   // __nsn.size() == 0
3049227825Stheraven                        if (*__b == __psn[0])
3050227825Stheraven                        {
3051227825Stheraven                            ++__b;
3052227825Stheraven                            if (__psn.size() > 1)
3053227825Stheraven                                __trailing_sign = &__psn;
3054227825Stheraven                        }
3055227825Stheraven                        else
3056227825Stheraven                            __neg = true;
3057227825Stheraven                    }
3058227825Stheraven                    else if (*__b == __nsn[0])  // __nsn.size() > 0 &&  __psn.size() == 0
3059227825Stheraven                    {
3060227825Stheraven                        ++__b;
3061227825Stheraven                        __neg = true;
3062227825Stheraven                        if (__nsn.size() > 1)
3063227825Stheraven                            __trailing_sign = &__nsn;
3064227825Stheraven                    }
3065227825Stheraven                }
3066227825Stheraven                else  // sign is required
3067227825Stheraven                {
3068227825Stheraven                    if (*__b == __psn[0])
3069227825Stheraven                    {
3070227825Stheraven                        ++__b;
3071227825Stheraven                        if (__psn.size() > 1)
3072227825Stheraven                            __trailing_sign = &__psn;
3073227825Stheraven                    }
3074227825Stheraven                    else if (*__b == __nsn[0])
3075227825Stheraven                    {
3076227825Stheraven                        ++__b;
3077227825Stheraven                        __neg = true;
3078227825Stheraven                        if (__nsn.size() > 1)
3079227825Stheraven                            __trailing_sign = &__nsn;
3080227825Stheraven                    }
3081227825Stheraven                    else
3082227825Stheraven                    {
3083227825Stheraven                        __err |= ios_base::failbit;
3084227825Stheraven                        return false;
3085227825Stheraven                    }
3086227825Stheraven                }
3087227825Stheraven            }
3088227825Stheraven            break;
3089227825Stheraven        case money_base::symbol:
3090227825Stheraven            {
3091227825Stheraven            bool __more_needed = __trailing_sign ||
3092227825Stheraven                                 (__p < 2)       ||
3093227825Stheraven                                 (__p == 2 && __pat.field[3] != static_cast<char>(money_base::none));
3094262801Sdim            bool __sb = (__flags & ios_base::showbase) != 0;
3095227825Stheraven            if (__sb || __more_needed)
3096227825Stheraven            {
3097232950Stheraven                typename string_type::const_iterator __sym_space_end = __sym.begin();
3098232950Stheraven                if (__p > 0 && (__pat.field[__p - 1] == money_base::none ||
3099232950Stheraven                                __pat.field[__p - 1] == money_base::space)) {
3100232950Stheraven                    // Match spaces we've already read against spaces at
3101232950Stheraven                    // the beginning of __sym.
3102232950Stheraven                    while (__sym_space_end != __sym.end() &&
3103232950Stheraven                           __ct.is(ctype_base::space, *__sym_space_end))
3104232950Stheraven                        ++__sym_space_end;
3105232950Stheraven                    const size_t __num_spaces = __sym_space_end - __sym.begin();
3106232950Stheraven                    if (__num_spaces > __spaces.size() ||
3107232950Stheraven                        !equal(__spaces.end() - __num_spaces, __spaces.end(),
3108232950Stheraven                               __sym.begin())) {
3109232950Stheraven                        // No match. Put __sym_space_end back at the
3110232950Stheraven                        // beginning of __sym, which will prevent a
3111232950Stheraven                        // match in the next loop.
3112232950Stheraven                        __sym_space_end = __sym.begin();
3113232950Stheraven                    }
3114232950Stheraven                }
3115232950Stheraven                typename string_type::const_iterator __sym_curr_char = __sym_space_end;
3116232950Stheraven                while (__sym_curr_char != __sym.end() && __b != __e &&
3117232950Stheraven                       *__b == *__sym_curr_char) {
3118232950Stheraven                    ++__b;
3119232950Stheraven                    ++__sym_curr_char;
3120232950Stheraven                }
3121232950Stheraven                if (__sb && __sym_curr_char != __sym.end())
3122227825Stheraven                {
3123227825Stheraven                    __err |= ios_base::failbit;
3124227825Stheraven                    return false;
3125227825Stheraven                }
3126227825Stheraven            }
3127227825Stheraven            }
3128227825Stheraven            break;
3129227825Stheraven        case money_base::value:
3130227825Stheraven            {
3131227825Stheraven            unsigned __ng = 0;
3132227825Stheraven            for (; __b != __e; ++__b)
3133227825Stheraven            {
3134227825Stheraven                char_type __c = *__b;
3135227825Stheraven                if (__ct.is(ctype_base::digit, __c))
3136227825Stheraven                {
3137227825Stheraven                    if (__wn == __we)
3138227825Stheraven                        __double_or_nothing(__wb, __wn, __we);
3139227825Stheraven                    *__wn++ = __c;
3140227825Stheraven                    ++__ng;
3141227825Stheraven                }
3142227825Stheraven                else if (__grp.size() > 0 && __ng > 0 && __c == __ts)
3143227825Stheraven                {
3144227825Stheraven                    if (__gn == __ge)
3145227825Stheraven                        __double_or_nothing(__gb, __gn, __ge);
3146227825Stheraven                    *__gn++ = __ng;
3147227825Stheraven                    __ng = 0;
3148227825Stheraven                }
3149227825Stheraven                else
3150227825Stheraven                    break;
3151227825Stheraven            }
3152227825Stheraven            if (__gb.get() != __gn && __ng > 0)
3153227825Stheraven            {
3154227825Stheraven                if (__gn == __ge)
3155227825Stheraven                    __double_or_nothing(__gb, __gn, __ge);
3156227825Stheraven                *__gn++ = __ng;
3157227825Stheraven            }
3158227825Stheraven            if (__fd > 0)
3159227825Stheraven            {
3160227825Stheraven                if (__b == __e || *__b != __dp)
3161227825Stheraven                {
3162227825Stheraven                    __err |= ios_base::failbit;
3163227825Stheraven                    return false;
3164227825Stheraven                }
3165227825Stheraven                for (++__b; __fd > 0; --__fd, ++__b)
3166227825Stheraven                {
3167227825Stheraven                    if (__b == __e || !__ct.is(ctype_base::digit, *__b))
3168227825Stheraven                    {
3169227825Stheraven                        __err |= ios_base::failbit;
3170227825Stheraven                        return false;
3171227825Stheraven                    }
3172227825Stheraven                    if (__wn == __we)
3173227825Stheraven                        __double_or_nothing(__wb, __wn, __we);
3174227825Stheraven                    *__wn++ = *__b;
3175227825Stheraven                }
3176227825Stheraven            }
3177227825Stheraven            if (__wn == __wb.get())
3178227825Stheraven            {
3179227825Stheraven                __err |= ios_base::failbit;
3180227825Stheraven                return false;
3181227825Stheraven            }
3182227825Stheraven            }
3183227825Stheraven            break;
3184227825Stheraven        }
3185227825Stheraven    }
3186227825Stheraven    if (__trailing_sign)
3187227825Stheraven    {
3188227825Stheraven        for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, ++__b)
3189227825Stheraven        {
3190227825Stheraven            if (__b == __e || *__b != (*__trailing_sign)[__i])
3191227825Stheraven            {
3192227825Stheraven                __err |= ios_base::failbit;
3193227825Stheraven                return false;
3194227825Stheraven            }
3195227825Stheraven        }
3196227825Stheraven    }
3197227825Stheraven    if (__gb.get() != __gn)
3198227825Stheraven    {
3199227825Stheraven        ios_base::iostate __et = ios_base::goodbit;
3200227825Stheraven        __check_grouping(__grp, __gb.get(), __gn, __et);
3201227825Stheraven        if (__et)
3202227825Stheraven        {
3203227825Stheraven            __err |= ios_base::failbit;
3204227825Stheraven            return false;
3205227825Stheraven        }
3206227825Stheraven    }
3207227825Stheraven    return true;
3208227825Stheraven}
3209227825Stheraven
3210227825Stheraventemplate <class _CharT, class _InputIterator>
3211227825Stheraven_InputIterator
3212227825Stheravenmoney_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3213227825Stheraven                                          bool __intl, ios_base& __iob,
3214227825Stheraven                                          ios_base::iostate& __err,
3215227825Stheraven                                          long double& __v) const
3216227825Stheraven{
3217232950Stheraven    const int __bz = 100;
3218227825Stheraven    char_type __wbuf[__bz];
3219227825Stheraven    unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3220227825Stheraven    char_type* __wn;
3221227825Stheraven    char_type* __we = __wbuf + __bz;
3222227825Stheraven    locale __loc = __iob.getloc();
3223227825Stheraven    const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3224227825Stheraven    bool __neg = false;
3225227825Stheraven    if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3226227825Stheraven                 __wb, __wn, __we))
3227227825Stheraven    {
3228227825Stheraven        const char __src[] = "0123456789";
3229227825Stheraven        char_type __atoms[sizeof(__src)-1];
3230227825Stheraven        __ct.widen(__src, __src + (sizeof(__src)-1), __atoms);
3231227825Stheraven        char __nbuf[__bz];
3232227825Stheraven        char* __nc = __nbuf;
3233227825Stheraven        unique_ptr<char, void(*)(void*)> __h(0, free);
3234227825Stheraven        if (__wn - __wb.get() > __bz-2)
3235227825Stheraven        {
3236232950Stheraven            __h.reset((char*)malloc(static_cast<size_t>(__wn - __wb.get() + 2)));
3237227825Stheraven            if (__h.get() == 0)
3238227825Stheraven                __throw_bad_alloc();
3239227825Stheraven            __nc = __h.get();
3240227825Stheraven        }
3241227825Stheraven        if (__neg)
3242227825Stheraven            *__nc++ = '-';
3243227825Stheraven        for (const char_type* __w = __wb.get(); __w < __wn; ++__w, ++__nc)
3244249998Sdim            *__nc = __src[find(__atoms, _VSTD::end(__atoms), *__w) - __atoms];
3245227825Stheraven        *__nc = char();
3246227825Stheraven        if (sscanf(__nbuf, "%Lf", &__v) != 1)
3247227825Stheraven            __throw_runtime_error("money_get error");
3248227825Stheraven    }
3249227825Stheraven    if (__b == __e)
3250227825Stheraven        __err |= ios_base::eofbit;
3251227825Stheraven    return __b;
3252227825Stheraven}
3253227825Stheraven
3254227825Stheraventemplate <class _CharT, class _InputIterator>
3255227825Stheraven_InputIterator
3256227825Stheravenmoney_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3257227825Stheraven                                          bool __intl, ios_base& __iob,
3258227825Stheraven                                          ios_base::iostate& __err,
3259227825Stheraven                                          string_type& __v) const
3260227825Stheraven{
3261232950Stheraven    const int __bz = 100;
3262227825Stheraven    char_type __wbuf[__bz];
3263227825Stheraven    unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3264227825Stheraven    char_type* __wn;
3265227825Stheraven    char_type* __we = __wbuf + __bz;
3266227825Stheraven    locale __loc = __iob.getloc();
3267227825Stheraven    const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3268227825Stheraven    bool __neg = false;
3269227825Stheraven    if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3270227825Stheraven                 __wb, __wn, __we))
3271227825Stheraven    {
3272227825Stheraven        __v.clear();
3273227825Stheraven        if (__neg)
3274227825Stheraven            __v.push_back(__ct.widen('-'));
3275227825Stheraven        char_type __z = __ct.widen('0');
3276227825Stheraven        char_type* __w;
3277227825Stheraven        for (__w = __wb.get(); __w < __wn-1; ++__w)
3278227825Stheraven            if (*__w != __z)
3279227825Stheraven                break;
3280227825Stheraven        __v.append(__w, __wn);
3281227825Stheraven    }
3282227825Stheraven    if (__b == __e)
3283227825Stheraven        __err |= ios_base::eofbit;
3284227825Stheraven    return __b;
3285227825Stheraven}
3286227825Stheraven
3287262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS money_get<char>)
3288262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS money_get<wchar_t>)
3289227825Stheraven
3290227825Stheraven// money_put
3291227825Stheraven
3292227825Stheraventemplate <class _CharT>
3293227825Stheravenclass __money_put
3294227825Stheraven{
3295227825Stheravenprotected:
3296227825Stheraven    typedef _CharT                  char_type;
3297227825Stheraven    typedef basic_string<char_type> string_type;
3298227825Stheraven
3299227825Stheraven    _LIBCPP_ALWAYS_INLINE __money_put() {}
3300227825Stheraven
3301227825Stheraven    static void __gather_info(bool __intl, bool __neg, const locale& __loc,
3302227825Stheraven                              money_base::pattern& __pat, char_type& __dp,
3303227825Stheraven                              char_type& __ts, string& __grp,
3304227825Stheraven                              string_type& __sym, string_type& __sn,
3305227825Stheraven                              int& __fd);
3306227825Stheraven    static void __format(char_type* __mb, char_type*& __mi, char_type*& __me,
3307227825Stheraven                         ios_base::fmtflags __flags,
3308227825Stheraven                         const char_type* __db, const char_type* __de,
3309227825Stheraven                         const ctype<char_type>& __ct, bool __neg,
3310227825Stheraven                         const money_base::pattern& __pat, char_type __dp,
3311227825Stheraven                         char_type __ts, const string& __grp,
3312227825Stheraven                         const string_type& __sym, const string_type& __sn,
3313227825Stheraven                         int __fd);
3314227825Stheraven};
3315227825Stheraven
3316227825Stheraventemplate <class _CharT>
3317227825Stheravenvoid
3318227825Stheraven__money_put<_CharT>::__gather_info(bool __intl, bool __neg, const locale& __loc,
3319227825Stheraven                                   money_base::pattern& __pat, char_type& __dp,
3320227825Stheraven                                   char_type& __ts, string& __grp,
3321227825Stheraven                                   string_type& __sym, string_type& __sn,
3322227825Stheraven                                   int& __fd)
3323227825Stheraven{
3324227825Stheraven    if (__intl)
3325227825Stheraven    {
3326227825Stheraven        const moneypunct<char_type, true>& __mp =
3327227825Stheraven            use_facet<moneypunct<char_type, true> >(__loc);
3328227825Stheraven        if (__neg)
3329227825Stheraven        {
3330227825Stheraven            __pat = __mp.neg_format();
3331227825Stheraven            __sn = __mp.negative_sign();
3332227825Stheraven        }
3333227825Stheraven        else
3334227825Stheraven        {
3335227825Stheraven            __pat = __mp.pos_format();
3336227825Stheraven            __sn = __mp.positive_sign();
3337227825Stheraven        }
3338227825Stheraven        __dp = __mp.decimal_point();
3339227825Stheraven        __ts = __mp.thousands_sep();
3340227825Stheraven        __grp = __mp.grouping();
3341227825Stheraven        __sym = __mp.curr_symbol();
3342227825Stheraven        __fd = __mp.frac_digits();
3343227825Stheraven    }
3344227825Stheraven    else
3345227825Stheraven    {
3346227825Stheraven        const moneypunct<char_type, false>& __mp =
3347227825Stheraven            use_facet<moneypunct<char_type, false> >(__loc);
3348227825Stheraven        if (__neg)
3349227825Stheraven        {
3350227825Stheraven            __pat = __mp.neg_format();
3351227825Stheraven            __sn = __mp.negative_sign();
3352227825Stheraven        }
3353227825Stheraven        else
3354227825Stheraven        {
3355227825Stheraven            __pat = __mp.pos_format();
3356227825Stheraven            __sn = __mp.positive_sign();
3357227825Stheraven        }
3358227825Stheraven        __dp = __mp.decimal_point();
3359227825Stheraven        __ts = __mp.thousands_sep();
3360227825Stheraven        __grp = __mp.grouping();
3361227825Stheraven        __sym = __mp.curr_symbol();
3362227825Stheraven        __fd = __mp.frac_digits();
3363227825Stheraven    }
3364227825Stheraven}
3365227825Stheraven
3366227825Stheraventemplate <class _CharT>
3367227825Stheravenvoid
3368227825Stheraven__money_put<_CharT>::__format(char_type* __mb, char_type*& __mi, char_type*& __me,
3369227825Stheraven                              ios_base::fmtflags __flags,
3370227825Stheraven                              const char_type* __db, const char_type* __de,
3371227825Stheraven                              const ctype<char_type>& __ct, bool __neg,
3372227825Stheraven                              const money_base::pattern& __pat, char_type __dp,
3373227825Stheraven                              char_type __ts, const string& __grp,
3374227825Stheraven                              const string_type& __sym, const string_type& __sn,
3375227825Stheraven                              int __fd)
3376227825Stheraven{
3377227825Stheraven    __me = __mb;
3378227825Stheraven    for (unsigned __p = 0; __p < 4; ++__p)
3379227825Stheraven    {
3380227825Stheraven        switch (__pat.field[__p])
3381227825Stheraven        {
3382227825Stheraven        case money_base::none:
3383227825Stheraven            __mi = __me;
3384227825Stheraven            break;
3385227825Stheraven        case money_base::space:
3386227825Stheraven            __mi = __me;
3387227825Stheraven            *__me++ = __ct.widen(' ');
3388227825Stheraven            break;
3389227825Stheraven        case money_base::sign:
3390227825Stheraven            if (!__sn.empty())
3391227825Stheraven                *__me++ = __sn[0];
3392227825Stheraven            break;
3393227825Stheraven        case money_base::symbol:
3394227825Stheraven            if (!__sym.empty() && (__flags & ios_base::showbase))
3395227825Stheraven                __me = _VSTD::copy(__sym.begin(), __sym.end(), __me);
3396227825Stheraven            break;
3397227825Stheraven        case money_base::value:
3398227825Stheraven            {
3399227825Stheraven            // remember start of value so we can reverse it
3400227825Stheraven            char_type* __t = __me;
3401227825Stheraven            // find beginning of digits
3402227825Stheraven            if (__neg)
3403227825Stheraven                ++__db;
3404227825Stheraven            // find end of digits
3405227825Stheraven            const char_type* __d;
3406227825Stheraven            for (__d = __db; __d < __de; ++__d)
3407227825Stheraven                if (!__ct.is(ctype_base::digit, *__d))
3408227825Stheraven                    break;
3409227825Stheraven            // print fractional part
3410227825Stheraven            if (__fd > 0)
3411227825Stheraven            {
3412227825Stheraven                int __f;
3413227825Stheraven                for (__f = __fd; __d > __db && __f > 0; --__f)
3414227825Stheraven                    *__me++ = *--__d;
3415227825Stheraven                char_type __z = __f > 0 ? __ct.widen('0') : char_type();
3416227825Stheraven                for (; __f > 0; --__f)
3417227825Stheraven                    *__me++ = __z;
3418227825Stheraven                *__me++ = __dp;
3419227825Stheraven            }
3420227825Stheraven            // print units part
3421227825Stheraven            if (__d == __db)
3422227825Stheraven            {
3423227825Stheraven                *__me++ = __ct.widen('0');
3424227825Stheraven            }
3425227825Stheraven            else
3426227825Stheraven            {
3427227825Stheraven                unsigned __ng = 0;
3428227825Stheraven                unsigned __ig = 0;
3429227825Stheraven                unsigned __gl = __grp.empty() ? numeric_limits<unsigned>::max()
3430227825Stheraven                                              : static_cast<unsigned>(__grp[__ig]);
3431227825Stheraven                while (__d != __db)
3432227825Stheraven                {
3433227825Stheraven                    if (__ng == __gl)
3434227825Stheraven                    {
3435227825Stheraven                        *__me++ = __ts;
3436227825Stheraven                        __ng = 0;
3437227825Stheraven                        if (++__ig < __grp.size())
3438227825Stheraven                            __gl = __grp[__ig] == numeric_limits<char>::max() ?
3439227825Stheraven                                        numeric_limits<unsigned>::max() :
3440227825Stheraven                                        static_cast<unsigned>(__grp[__ig]);
3441227825Stheraven                    }
3442227825Stheraven                    *__me++ = *--__d;
3443227825Stheraven                    ++__ng;
3444227825Stheraven                }
3445227825Stheraven            }
3446227825Stheraven            // reverse it
3447227825Stheraven            reverse(__t, __me);
3448227825Stheraven            }
3449227825Stheraven            break;
3450227825Stheraven        }
3451227825Stheraven    }
3452227825Stheraven    // print rest of sign, if any
3453227825Stheraven    if (__sn.size() > 1)
3454227825Stheraven        __me = _VSTD::copy(__sn.begin()+1, __sn.end(), __me);
3455227825Stheraven    // set alignment
3456227825Stheraven    if ((__flags & ios_base::adjustfield) == ios_base::left)
3457227825Stheraven        __mi = __me;
3458227825Stheraven    else if ((__flags & ios_base::adjustfield) != ios_base::internal)
3459227825Stheraven        __mi = __mb;
3460227825Stheraven}
3461227825Stheraven
3462262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS __money_put<char>)
3463262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS __money_put<wchar_t>)
3464227825Stheraven
3465227825Stheraventemplate <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
3466262801Sdimclass _LIBCPP_TYPE_VIS_ONLY money_put
3467227825Stheraven    : public locale::facet,
3468227825Stheraven      private __money_put<_CharT>
3469227825Stheraven{
3470227825Stheravenpublic:
3471227825Stheraven    typedef _CharT                  char_type;
3472227825Stheraven    typedef _OutputIterator         iter_type;
3473227825Stheraven    typedef basic_string<char_type> string_type;
3474227825Stheraven
3475227825Stheraven    _LIBCPP_ALWAYS_INLINE
3476227825Stheraven    explicit money_put(size_t __refs = 0)
3477227825Stheraven        : locale::facet(__refs) {}
3478227825Stheraven
3479227825Stheraven    _LIBCPP_ALWAYS_INLINE
3480227825Stheraven    iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3481227825Stheraven                  long double __units) const
3482227825Stheraven    {
3483227825Stheraven        return do_put(__s, __intl, __iob, __fl, __units);
3484227825Stheraven    }
3485227825Stheraven
3486227825Stheraven    _LIBCPP_ALWAYS_INLINE
3487227825Stheraven    iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3488227825Stheraven                  const string_type& __digits) const
3489227825Stheraven    {
3490227825Stheraven        return do_put(__s, __intl, __iob, __fl, __digits);
3491227825Stheraven    }
3492227825Stheraven
3493227825Stheraven    static locale::id id;
3494227825Stheraven
3495227825Stheravenprotected:
3496227825Stheraven    _LIBCPP_ALWAYS_INLINE
3497227825Stheraven    ~money_put() {}
3498227825Stheraven
3499227825Stheraven    virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3500227825Stheraven                             char_type __fl, long double __units) const;
3501227825Stheraven    virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3502227825Stheraven                             char_type __fl, const string_type& __digits) const;
3503227825Stheraven};
3504227825Stheraven
3505227825Stheraventemplate <class _CharT, class _OutputIterator>
3506227825Stheravenlocale::id
3507227825Stheravenmoney_put<_CharT, _OutputIterator>::id;
3508227825Stheraven
3509227825Stheraventemplate <class _CharT, class _OutputIterator>
3510227825Stheraven_OutputIterator
3511227825Stheravenmoney_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3512227825Stheraven                                           ios_base& __iob, char_type __fl,
3513227825Stheraven                                           long double __units) const
3514227825Stheraven{
3515227825Stheraven    // convert to char
3516227825Stheraven    const size_t __bs = 100;
3517227825Stheraven    char __buf[__bs];
3518227825Stheraven    char* __bb = __buf;
3519227825Stheraven    char_type __digits[__bs];
3520227825Stheraven    char_type* __db = __digits;
3521232950Stheraven    size_t __n = static_cast<size_t>(snprintf(__bb, __bs, "%.0Lf", __units));
3522227825Stheraven    unique_ptr<char, void(*)(void*)> __hn(0, free);
3523227825Stheraven    unique_ptr<char_type, void(*)(void*)> __hd(0, free);
3524227825Stheraven    // secure memory for digit storage
3525227825Stheraven    if (__n > __bs-1)
3526227825Stheraven    {
3527227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
3528232950Stheraven        __n = static_cast<size_t>(asprintf_l(&__bb, _LIBCPP_GET_C_LOCALE, "%.0Lf", __units));
3529227825Stheraven#else
3530227825Stheraven        __n = __asprintf_l(&__bb, __cloc(), "%.0Lf", __units);
3531227825Stheraven#endif
3532227825Stheraven        if (__bb == 0)
3533227825Stheraven            __throw_bad_alloc();
3534227825Stheraven        __hn.reset(__bb);
3535227825Stheraven        __hd.reset((char_type*)malloc(__n * sizeof(char_type)));
3536232950Stheraven        if (__hd == nullptr)
3537227825Stheraven            __throw_bad_alloc();
3538227825Stheraven        __db = __hd.get();
3539227825Stheraven    }
3540227825Stheraven    // gather info
3541227825Stheraven    locale __loc = __iob.getloc();
3542227825Stheraven    const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3543227825Stheraven    __ct.widen(__bb, __bb + __n, __db);
3544227825Stheraven    bool __neg = __n > 0 && __bb[0] == '-';
3545227825Stheraven    money_base::pattern __pat;
3546227825Stheraven    char_type __dp;
3547227825Stheraven    char_type __ts;
3548227825Stheraven    string __grp;
3549227825Stheraven    string_type __sym;
3550227825Stheraven    string_type __sn;
3551227825Stheraven    int __fd;
3552227825Stheraven    this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3553227825Stheraven    // secure memory for formatting
3554227825Stheraven    char_type __mbuf[__bs];
3555227825Stheraven    char_type* __mb = __mbuf;
3556227825Stheraven    unique_ptr<char_type, void(*)(void*)> __hw(0, free);
3557227825Stheraven    size_t __exn = static_cast<int>(__n) > __fd ?
3558232950Stheraven                   (__n - static_cast<size_t>(__fd)) * 2 + __sn.size() +
3559232950Stheraven                    __sym.size() + static_cast<size_t>(__fd) + 1
3560232950Stheraven                 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
3561227825Stheraven    if (__exn > __bs)
3562227825Stheraven    {
3563227825Stheraven        __hw.reset((char_type*)malloc(__exn * sizeof(char_type)));
3564227825Stheraven        __mb = __hw.get();
3565227825Stheraven        if (__mb == 0)
3566227825Stheraven            __throw_bad_alloc();
3567227825Stheraven    }
3568227825Stheraven    // format
3569227825Stheraven    char_type* __mi;
3570227825Stheraven    char_type* __me;
3571227825Stheraven    this->__format(__mb, __mi, __me, __iob.flags(),
3572227825Stheraven                   __db, __db + __n, __ct,
3573227825Stheraven                   __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3574227825Stheraven    return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3575227825Stheraven}
3576227825Stheraven
3577227825Stheraventemplate <class _CharT, class _OutputIterator>
3578227825Stheraven_OutputIterator
3579227825Stheravenmoney_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3580227825Stheraven                                           ios_base& __iob, char_type __fl,
3581227825Stheraven                                           const string_type& __digits) const
3582227825Stheraven{
3583227825Stheraven    // gather info
3584227825Stheraven    locale __loc = __iob.getloc();
3585227825Stheraven    const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3586227825Stheraven    bool __neg = __digits.size() > 0 && __digits[0] == __ct.widen('-');
3587227825Stheraven    money_base::pattern __pat;
3588227825Stheraven    char_type __dp;
3589227825Stheraven    char_type __ts;
3590227825Stheraven    string __grp;
3591227825Stheraven    string_type __sym;
3592227825Stheraven    string_type __sn;
3593227825Stheraven    int __fd;
3594227825Stheraven    this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3595227825Stheraven    // secure memory for formatting
3596227825Stheraven    char_type __mbuf[100];
3597227825Stheraven    char_type* __mb = __mbuf;
3598227825Stheraven    unique_ptr<char_type, void(*)(void*)> __h(0, free);
3599232950Stheraven    size_t __exn = static_cast<int>(__digits.size()) > __fd ?
3600232950Stheraven                   (__digits.size() - static_cast<size_t>(__fd)) * 2 +
3601232950Stheraven                    __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 1
3602232950Stheraven                 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
3603227825Stheraven    if (__exn > 100)
3604227825Stheraven    {
3605227825Stheraven        __h.reset((char_type*)malloc(__exn * sizeof(char_type)));
3606227825Stheraven        __mb = __h.get();
3607227825Stheraven        if (__mb == 0)
3608227825Stheraven            __throw_bad_alloc();
3609227825Stheraven    }
3610227825Stheraven    // format
3611227825Stheraven    char_type* __mi;
3612227825Stheraven    char_type* __me;
3613227825Stheraven    this->__format(__mb, __mi, __me, __iob.flags(),
3614227825Stheraven                   __digits.data(), __digits.data() + __digits.size(), __ct,
3615227825Stheraven                   __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3616227825Stheraven    return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3617227825Stheraven}
3618227825Stheraven
3619262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS money_put<char>)
3620262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS money_put<wchar_t>)
3621227825Stheraven
3622227825Stheraven// messages
3623227825Stheraven
3624249998Sdimclass _LIBCPP_TYPE_VIS messages_base
3625227825Stheraven{
3626227825Stheravenpublic:
3627227825Stheraven    typedef ptrdiff_t catalog;
3628227825Stheraven
3629227825Stheraven    _LIBCPP_ALWAYS_INLINE messages_base() {}
3630227825Stheraven};
3631227825Stheraven
3632227825Stheraventemplate <class _CharT>
3633262801Sdimclass _LIBCPP_TYPE_VIS_ONLY messages
3634227825Stheraven    : public locale::facet,
3635227825Stheraven      public messages_base
3636227825Stheraven{
3637227825Stheravenpublic:
3638227825Stheraven    typedef _CharT               char_type;
3639227825Stheraven    typedef basic_string<_CharT> string_type;
3640227825Stheraven
3641227825Stheraven    _LIBCPP_ALWAYS_INLINE
3642227825Stheraven    explicit messages(size_t __refs = 0)
3643227825Stheraven        : locale::facet(__refs) {}
3644227825Stheraven
3645227825Stheraven    _LIBCPP_ALWAYS_INLINE
3646227825Stheraven    catalog open(const basic_string<char>& __nm, const locale& __loc) const
3647227825Stheraven    {
3648227825Stheraven        return do_open(__nm, __loc);
3649227825Stheraven    }
3650227825Stheraven
3651227825Stheraven    _LIBCPP_ALWAYS_INLINE
3652227825Stheraven    string_type get(catalog __c, int __set, int __msgid,
3653227825Stheraven                    const string_type& __dflt) const
3654227825Stheraven    {
3655227825Stheraven        return do_get(__c, __set, __msgid, __dflt);
3656227825Stheraven    }
3657227825Stheraven
3658227825Stheraven    _LIBCPP_ALWAYS_INLINE
3659227825Stheraven    void close(catalog __c) const
3660227825Stheraven    {
3661227825Stheraven        do_close(__c);
3662227825Stheraven    }
3663227825Stheraven
3664227825Stheraven    static locale::id id;
3665227825Stheraven
3666227825Stheravenprotected:
3667227825Stheraven    _LIBCPP_ALWAYS_INLINE
3668227825Stheraven    ~messages() {}
3669227825Stheraven
3670227825Stheraven    virtual catalog do_open(const basic_string<char>&, const locale&) const;
3671227825Stheraven    virtual string_type do_get(catalog, int __set, int __msgid,
3672227825Stheraven                               const string_type& __dflt) const;
3673227825Stheraven    virtual void do_close(catalog) const;
3674227825Stheraven};
3675227825Stheraven
3676227825Stheraventemplate <class _CharT>
3677227825Stheravenlocale::id
3678227825Stheravenmessages<_CharT>::id;
3679227825Stheraven
3680227825Stheraventemplate <class _CharT>
3681227825Stheraventypename messages<_CharT>::catalog
3682227825Stheravenmessages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const
3683227825Stheraven{
3684278724Sdim#if defined(_WIN32) || defined(__ANDROID__) || defined(_NEWLIB_VERSION)
3685227825Stheraven    return -1;
3686278724Sdim#else // _WIN32 || __ANDROID__
3687227825Stheraven    catalog __cat = (catalog)catopen(__nm.c_str(), NL_CAT_LOCALE);
3688227825Stheraven    if (__cat != -1)
3689227825Stheraven        __cat = static_cast<catalog>((static_cast<size_t>(__cat) >> 1));
3690227825Stheraven    return __cat;
3691278724Sdim#endif // _WIN32 || __ANDROID__
3692227825Stheraven}
3693227825Stheraven
3694227825Stheraventemplate <class _CharT>
3695227825Stheraventypename messages<_CharT>::string_type
3696227825Stheravenmessages<_CharT>::do_get(catalog __c, int __set, int __msgid,
3697227825Stheraven                         const string_type& __dflt) const
3698227825Stheraven{
3699278724Sdim#if defined(_WIN32) || defined(__ANDROID__) || defined(_NEWLIB_VERSION)
3700227825Stheraven    return __dflt;
3701227825Stheraven#else // _WIN32
3702227825Stheraven    string __ndflt;
3703227825Stheraven    __narrow_to_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__ndflt),
3704227825Stheraven                                                       __dflt.c_str(),
3705227825Stheraven                                                       __dflt.c_str() + __dflt.size());
3706227825Stheraven    if (__c != -1)
3707227825Stheraven        __c <<= 1;
3708227825Stheraven    nl_catd __cat = (nl_catd)__c;
3709227825Stheraven    char* __n = catgets(__cat, __set, __msgid, __ndflt.c_str());
3710227825Stheraven    string_type __w;
3711227825Stheraven    __widen_from_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__w),
3712227825Stheraven                                                        __n, __n + strlen(__n));
3713227825Stheraven    return __w;
3714227825Stheraven#endif // _WIN32
3715227825Stheraven}
3716227825Stheraven
3717227825Stheraventemplate <class _CharT>
3718227825Stheravenvoid
3719227825Stheravenmessages<_CharT>::do_close(catalog __c) const
3720227825Stheraven{
3721278724Sdim#if !defined(_WIN32) && !defined(__ANDROID__) && !defined(_NEWLIB_VERSION)
3722227825Stheraven    if (__c != -1)
3723227825Stheraven        __c <<= 1;
3724227825Stheraven    nl_catd __cat = (nl_catd)__c;
3725227825Stheraven    catclose(__cat);
3726227825Stheraven#endif // !_WIN32
3727227825Stheraven}
3728227825Stheraven
3729262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS messages<char>)
3730262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS messages<wchar_t>)
3731227825Stheraven
3732227825Stheraventemplate <class _CharT>
3733262801Sdimclass _LIBCPP_TYPE_VIS_ONLY messages_byname
3734227825Stheraven    : public messages<_CharT>
3735227825Stheraven{
3736227825Stheravenpublic:
3737227825Stheraven    typedef messages_base::catalog catalog;
3738227825Stheraven    typedef basic_string<_CharT> string_type;
3739227825Stheraven
3740227825Stheraven    _LIBCPP_ALWAYS_INLINE
3741227825Stheraven    explicit messages_byname(const char*, size_t __refs = 0)
3742227825Stheraven        : messages<_CharT>(__refs) {}
3743227825Stheraven
3744227825Stheraven    _LIBCPP_ALWAYS_INLINE
3745227825Stheraven    explicit messages_byname(const string&, size_t __refs = 0)
3746227825Stheraven        : messages<_CharT>(__refs) {}
3747227825Stheraven
3748227825Stheravenprotected:
3749227825Stheraven    _LIBCPP_ALWAYS_INLINE
3750227825Stheraven    ~messages_byname() {}
3751227825Stheraven};
3752227825Stheraven
3753262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS messages_byname<char>)
3754262801Sdim_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS messages_byname<wchar_t>)
3755227825Stheraven
3756227825Stheraventemplate<class _Codecvt, class _Elem = wchar_t,
3757227825Stheraven         class _Wide_alloc = allocator<_Elem>,
3758227825Stheraven         class _Byte_alloc = allocator<char> >
3759262801Sdimclass _LIBCPP_TYPE_VIS_ONLY wstring_convert
3760227825Stheraven{
3761227825Stheravenpublic:
3762227825Stheraven    typedef basic_string<char, char_traits<char>, _Byte_alloc>   byte_string;
3763227825Stheraven    typedef basic_string<_Elem, char_traits<_Elem>, _Wide_alloc> wide_string;
3764227825Stheraven    typedef typename _Codecvt::state_type                        state_type;
3765227825Stheraven    typedef typename wide_string::traits_type::int_type          int_type;
3766227825Stheraven
3767227825Stheravenprivate:
3768227825Stheraven    byte_string __byte_err_string_;
3769227825Stheraven    wide_string __wide_err_string_;
3770227825Stheraven    _Codecvt* __cvtptr_;
3771227825Stheraven    state_type __cvtstate_;
3772227825Stheraven    size_t __cvtcount_;
3773227825Stheraven
3774227825Stheraven    wstring_convert(const wstring_convert& __wc);
3775227825Stheraven    wstring_convert& operator=(const wstring_convert& __wc);
3776227825Stheravenpublic:
3777262801Sdim    _LIBCPP_EXPLICIT_AFTER_CXX11 wstring_convert(_Codecvt* __pcvt = new _Codecvt);
3778227825Stheraven    wstring_convert(_Codecvt* __pcvt, state_type __state);
3779262801Sdim    _LIBCPP_EXPLICIT_AFTER_CXX11 wstring_convert(const byte_string& __byte_err,
3780227825Stheraven                    const wide_string& __wide_err = wide_string());
3781227825Stheraven#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3782227825Stheraven    wstring_convert(wstring_convert&& __wc);
3783227825Stheraven#endif
3784227825Stheraven    ~wstring_convert();
3785227825Stheraven
3786227825Stheraven    _LIBCPP_ALWAYS_INLINE
3787227825Stheraven    wide_string from_bytes(char __byte)
3788227825Stheraven        {return from_bytes(&__byte, &__byte+1);}
3789227825Stheraven    _LIBCPP_ALWAYS_INLINE
3790227825Stheraven    wide_string from_bytes(const char* __ptr)
3791227825Stheraven        {return from_bytes(__ptr, __ptr + char_traits<char>::length(__ptr));}
3792227825Stheraven    _LIBCPP_ALWAYS_INLINE
3793227825Stheraven    wide_string from_bytes(const byte_string& __str)
3794227825Stheraven        {return from_bytes(__str.data(), __str.data() + __str.size());}
3795227825Stheraven    wide_string from_bytes(const char* __first, const char* __last);
3796227825Stheraven
3797227825Stheraven    _LIBCPP_ALWAYS_INLINE
3798227825Stheraven    byte_string to_bytes(_Elem __wchar)
3799227825Stheraven        {return to_bytes(&__wchar, &__wchar+1);}
3800227825Stheraven    _LIBCPP_ALWAYS_INLINE
3801227825Stheraven    byte_string to_bytes(const _Elem* __wptr)
3802227825Stheraven        {return to_bytes(__wptr, __wptr + char_traits<_Elem>::length(__wptr));}
3803227825Stheraven    _LIBCPP_ALWAYS_INLINE
3804227825Stheraven    byte_string to_bytes(const wide_string& __wstr)
3805227825Stheraven        {return to_bytes(__wstr.data(), __wstr.data() + __wstr.size());}
3806227825Stheraven    byte_string to_bytes(const _Elem* __first, const _Elem* __last);
3807227825Stheraven
3808227825Stheraven    _LIBCPP_ALWAYS_INLINE
3809262801Sdim    size_t converted() const _NOEXCEPT {return __cvtcount_;}
3810227825Stheraven    _LIBCPP_ALWAYS_INLINE
3811227825Stheraven    state_type state() const {return __cvtstate_;}
3812227825Stheraven};
3813227825Stheraven
3814227825Stheraventemplate<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3815227825Stheraveninline _LIBCPP_ALWAYS_INLINE
3816227825Stheravenwstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3817227825Stheraven    wstring_convert(_Codecvt* __pcvt)
3818227825Stheraven        : __cvtptr_(__pcvt), __cvtstate_(), __cvtcount_(0)
3819227825Stheraven{
3820227825Stheraven}
3821227825Stheraven
3822227825Stheraventemplate<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3823227825Stheraveninline _LIBCPP_ALWAYS_INLINE
3824227825Stheravenwstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3825227825Stheraven    wstring_convert(_Codecvt* __pcvt, state_type __state)
3826227825Stheraven        : __cvtptr_(__pcvt), __cvtstate_(__state), __cvtcount_(0)
3827227825Stheraven{
3828227825Stheraven}
3829227825Stheraven
3830227825Stheraventemplate<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3831227825Stheravenwstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3832227825Stheraven    wstring_convert(const byte_string& __byte_err, const wide_string& __wide_err)
3833227825Stheraven        : __byte_err_string_(__byte_err), __wide_err_string_(__wide_err),
3834227825Stheraven          __cvtstate_(), __cvtcount_(0)
3835227825Stheraven{
3836227825Stheraven    __cvtptr_ = new _Codecvt;
3837227825Stheraven}
3838227825Stheraven
3839227825Stheraven#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3840227825Stheraven
3841227825Stheraventemplate<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3842227825Stheraveninline _LIBCPP_ALWAYS_INLINE
3843227825Stheravenwstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3844227825Stheraven    wstring_convert(wstring_convert&& __wc)
3845227825Stheraven        : __byte_err_string_(_VSTD::move(__wc.__byte_err_string_)),
3846227825Stheraven          __wide_err_string_(_VSTD::move(__wc.__wide_err_string_)),
3847227825Stheraven          __cvtptr_(__wc.__cvtptr_),
3848227825Stheraven          __cvtstate_(__wc.__cvtstate_), __cvtcount_(__wc.__cvtstate_)
3849227825Stheraven{
3850227825Stheraven    __wc.__cvtptr_ = nullptr;
3851227825Stheraven}
3852227825Stheraven
3853227825Stheraven#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
3854227825Stheraven
3855227825Stheraventemplate<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3856227825Stheravenwstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::~wstring_convert()
3857227825Stheraven{
3858227825Stheraven    delete __cvtptr_;
3859227825Stheraven}
3860227825Stheraven
3861227825Stheraventemplate<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3862227825Stheraventypename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::wide_string
3863227825Stheravenwstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3864227825Stheraven    from_bytes(const char* __frm, const char* __frm_end)
3865227825Stheraven{
3866227825Stheraven    __cvtcount_ = 0;
3867227825Stheraven    if (__cvtptr_ != nullptr)
3868227825Stheraven    {
3869227825Stheraven        wide_string __ws(2*(__frm_end - __frm), _Elem());
3870241903Sdim        if (__frm != __frm_end)
3871241903Sdim            __ws.resize(__ws.capacity());
3872227825Stheraven        codecvt_base::result __r = codecvt_base::ok;
3873227825Stheraven        state_type __st = __cvtstate_;
3874227825Stheraven        if (__frm != __frm_end)
3875227825Stheraven        {
3876227825Stheraven            _Elem* __to = &__ws[0];
3877227825Stheraven            _Elem* __to_end = __to + __ws.size();
3878227825Stheraven            const char* __frm_nxt;
3879227825Stheraven            do
3880227825Stheraven            {
3881227825Stheraven                _Elem* __to_nxt;
3882227825Stheraven                __r = __cvtptr_->in(__st, __frm, __frm_end, __frm_nxt,
3883227825Stheraven                                          __to, __to_end, __to_nxt);
3884227825Stheraven                __cvtcount_ += __frm_nxt - __frm;
3885227825Stheraven                if (__frm_nxt == __frm)
3886227825Stheraven                {
3887227825Stheraven                    __r = codecvt_base::error;
3888227825Stheraven                }
3889227825Stheraven                else if (__r == codecvt_base::noconv)
3890227825Stheraven                {
3891227825Stheraven                    __ws.resize(__to - &__ws[0]);
3892227825Stheraven                    // This only gets executed if _Elem is char
3893227825Stheraven                    __ws.append((const _Elem*)__frm, (const _Elem*)__frm_end);
3894227825Stheraven                    __frm = __frm_nxt;
3895227825Stheraven                    __r = codecvt_base::ok;
3896227825Stheraven                }
3897227825Stheraven                else if (__r == codecvt_base::ok)
3898227825Stheraven                {
3899227825Stheraven                    __ws.resize(__to_nxt - &__ws[0]);
3900227825Stheraven                    __frm = __frm_nxt;
3901227825Stheraven                }
3902227825Stheraven                else if (__r == codecvt_base::partial)
3903227825Stheraven                {
3904227825Stheraven                    ptrdiff_t __s = __to_nxt - &__ws[0];
3905227825Stheraven                    __ws.resize(2 * __s);
3906227825Stheraven                    __to = &__ws[0] + __s;
3907227825Stheraven                    __to_end = &__ws[0] + __ws.size();
3908227825Stheraven                    __frm = __frm_nxt;
3909227825Stheraven                }
3910227825Stheraven            } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3911227825Stheraven        }
3912227825Stheraven        if (__r == codecvt_base::ok)
3913227825Stheraven            return __ws;
3914227825Stheraven    }
3915227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
3916227825Stheraven    if (__wide_err_string_.empty())
3917227825Stheraven        throw range_error("wstring_convert: from_bytes error");
3918227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
3919227825Stheraven    return __wide_err_string_;
3920227825Stheraven}
3921227825Stheraven
3922227825Stheraventemplate<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3923227825Stheraventypename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::byte_string
3924227825Stheravenwstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3925227825Stheraven    to_bytes(const _Elem* __frm, const _Elem* __frm_end)
3926227825Stheraven{
3927227825Stheraven    __cvtcount_ = 0;
3928227825Stheraven    if (__cvtptr_ != nullptr)
3929227825Stheraven    {
3930227825Stheraven        byte_string __bs(2*(__frm_end - __frm), char());
3931241903Sdim        if (__frm != __frm_end)
3932241903Sdim            __bs.resize(__bs.capacity());
3933227825Stheraven        codecvt_base::result __r = codecvt_base::ok;
3934227825Stheraven        state_type __st = __cvtstate_;
3935227825Stheraven        if (__frm != __frm_end)
3936227825Stheraven        {
3937227825Stheraven            char* __to = &__bs[0];
3938227825Stheraven            char* __to_end = __to + __bs.size();
3939227825Stheraven            const _Elem* __frm_nxt;
3940227825Stheraven            do
3941227825Stheraven            {
3942227825Stheraven                char* __to_nxt;
3943227825Stheraven                __r = __cvtptr_->out(__st, __frm, __frm_end, __frm_nxt,
3944227825Stheraven                                           __to, __to_end, __to_nxt);
3945227825Stheraven                __cvtcount_ += __frm_nxt - __frm;
3946227825Stheraven                if (__frm_nxt == __frm)
3947227825Stheraven                {
3948227825Stheraven                    __r = codecvt_base::error;
3949227825Stheraven                }
3950227825Stheraven                else if (__r == codecvt_base::noconv)
3951227825Stheraven                {
3952227825Stheraven                    __bs.resize(__to - &__bs[0]);
3953227825Stheraven                    // This only gets executed if _Elem is char
3954227825Stheraven                    __bs.append((const char*)__frm, (const char*)__frm_end);
3955227825Stheraven                    __frm = __frm_nxt;
3956227825Stheraven                    __r = codecvt_base::ok;
3957227825Stheraven                }
3958227825Stheraven                else if (__r == codecvt_base::ok)
3959227825Stheraven                {
3960227825Stheraven                    __bs.resize(__to_nxt - &__bs[0]);
3961227825Stheraven                    __frm = __frm_nxt;
3962227825Stheraven                }
3963227825Stheraven                else if (__r == codecvt_base::partial)
3964227825Stheraven                {
3965227825Stheraven                    ptrdiff_t __s = __to_nxt - &__bs[0];
3966227825Stheraven                    __bs.resize(2 * __s);
3967227825Stheraven                    __to = &__bs[0] + __s;
3968227825Stheraven                    __to_end = &__bs[0] + __bs.size();
3969227825Stheraven                    __frm = __frm_nxt;
3970227825Stheraven                }
3971227825Stheraven            } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3972227825Stheraven        }
3973227825Stheraven        if (__r == codecvt_base::ok)
3974227825Stheraven        {
3975227825Stheraven            size_t __s = __bs.size();
3976227825Stheraven            __bs.resize(__bs.capacity());
3977227825Stheraven            char* __to = &__bs[0] + __s;
3978227825Stheraven            char* __to_end = __to + __bs.size();
3979227825Stheraven            do
3980227825Stheraven            {
3981227825Stheraven                char* __to_nxt;
3982227825Stheraven                __r = __cvtptr_->unshift(__st, __to, __to_end, __to_nxt);
3983227825Stheraven                if (__r == codecvt_base::noconv)
3984227825Stheraven                {
3985227825Stheraven                    __bs.resize(__to - &__bs[0]);
3986227825Stheraven                    __r = codecvt_base::ok;
3987227825Stheraven                }
3988227825Stheraven                else if (__r == codecvt_base::ok)
3989227825Stheraven                {
3990227825Stheraven                    __bs.resize(__to_nxt - &__bs[0]);
3991227825Stheraven                }
3992227825Stheraven                else if (__r == codecvt_base::partial)
3993227825Stheraven                {
3994232950Stheraven                    ptrdiff_t __sp = __to_nxt - &__bs[0];
3995232950Stheraven                    __bs.resize(2 * __sp);
3996232950Stheraven                    __to = &__bs[0] + __sp;
3997227825Stheraven                    __to_end = &__bs[0] + __bs.size();
3998227825Stheraven                }
3999227825Stheraven            } while (__r == codecvt_base::partial);
4000227825Stheraven            if (__r == codecvt_base::ok)
4001227825Stheraven                return __bs;
4002227825Stheraven        }
4003227825Stheraven    }
4004227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
4005227825Stheraven    if (__byte_err_string_.empty())
4006227825Stheraven        throw range_error("wstring_convert: to_bytes error");
4007227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
4008227825Stheraven    return __byte_err_string_;
4009227825Stheraven}
4010227825Stheraven
4011227825Stheraventemplate <class _Codecvt, class _Elem = wchar_t, class _Tr = char_traits<_Elem> >
4012262801Sdimclass _LIBCPP_TYPE_VIS_ONLY wbuffer_convert
4013227825Stheraven    : public basic_streambuf<_Elem, _Tr>
4014227825Stheraven{
4015227825Stheravenpublic:
4016227825Stheraven    // types:
4017227825Stheraven    typedef _Elem                          char_type;
4018227825Stheraven    typedef _Tr                            traits_type;
4019227825Stheraven    typedef typename traits_type::int_type int_type;
4020227825Stheraven    typedef typename traits_type::pos_type pos_type;
4021227825Stheraven    typedef typename traits_type::off_type off_type;
4022227825Stheraven    typedef typename _Codecvt::state_type  state_type;
4023227825Stheraven
4024227825Stheravenprivate:
4025227825Stheraven    char*       __extbuf_;
4026227825Stheraven    const char* __extbufnext_;
4027227825Stheraven    const char* __extbufend_;
4028227825Stheraven    char __extbuf_min_[8];
4029227825Stheraven    size_t __ebs_;
4030227825Stheraven    char_type* __intbuf_;
4031227825Stheraven    size_t __ibs_;
4032227825Stheraven    streambuf* __bufptr_;
4033227825Stheraven    _Codecvt* __cv_;
4034227825Stheraven    state_type __st_;
4035227825Stheraven    ios_base::openmode __cm_;
4036227825Stheraven    bool __owns_eb_;
4037227825Stheraven    bool __owns_ib_;
4038227825Stheraven    bool __always_noconv_;
4039227825Stheraven
4040227825Stheraven    wbuffer_convert(const wbuffer_convert&);
4041227825Stheraven    wbuffer_convert& operator=(const wbuffer_convert&);
4042227825Stheravenpublic:
4043262801Sdim    _LIBCPP_EXPLICIT_AFTER_CXX11 wbuffer_convert(streambuf* __bytebuf = 0, 
4044262801Sdim            _Codecvt* __pcvt = new _Codecvt, state_type __state = state_type());
4045227825Stheraven    ~wbuffer_convert();
4046227825Stheraven
4047227825Stheraven    _LIBCPP_INLINE_VISIBILITY
4048227825Stheraven    streambuf* rdbuf() const {return __bufptr_;}
4049227825Stheraven    _LIBCPP_INLINE_VISIBILITY
4050227825Stheraven    streambuf* rdbuf(streambuf* __bytebuf)
4051227825Stheraven    {
4052227825Stheraven        streambuf* __r = __bufptr_;
4053227825Stheraven        __bufptr_ = __bytebuf;
4054227825Stheraven        return __r;
4055227825Stheraven    }
4056227825Stheraven
4057227825Stheraven    _LIBCPP_INLINE_VISIBILITY
4058227825Stheraven    state_type state() const {return __st_;}
4059227825Stheraven
4060227825Stheravenprotected:
4061227825Stheraven    virtual int_type underflow();
4062227825Stheraven    virtual int_type pbackfail(int_type __c = traits_type::eof());
4063227825Stheraven    virtual int_type overflow (int_type __c = traits_type::eof());
4064227825Stheraven    virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s,
4065227825Stheraven                                                            streamsize __n);
4066227825Stheraven    virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
4067227825Stheraven                             ios_base::openmode __wch = ios_base::in | ios_base::out);
4068227825Stheraven    virtual pos_type seekpos(pos_type __sp,
4069227825Stheraven                             ios_base::openmode __wch = ios_base::in | ios_base::out);
4070227825Stheraven    virtual int sync();
4071227825Stheraven
4072227825Stheravenprivate:
4073227825Stheraven    bool __read_mode();
4074227825Stheraven    void __write_mode();
4075227825Stheraven    wbuffer_convert* __close();
4076227825Stheraven};
4077227825Stheraven
4078227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4079227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::
4080227825Stheraven    wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt, state_type __state)
4081227825Stheraven    : __extbuf_(0),
4082227825Stheraven      __extbufnext_(0),
4083227825Stheraven      __extbufend_(0),
4084227825Stheraven      __ebs_(0),
4085227825Stheraven      __intbuf_(0),
4086227825Stheraven      __ibs_(0),
4087227825Stheraven      __bufptr_(__bytebuf),
4088227825Stheraven      __cv_(__pcvt),
4089227825Stheraven      __st_(__state),
4090227825Stheraven      __cm_(0),
4091227825Stheraven      __owns_eb_(false),
4092227825Stheraven      __owns_ib_(false),
4093227825Stheraven      __always_noconv_(__cv_ ? __cv_->always_noconv() : false)
4094227825Stheraven{
4095227825Stheraven    setbuf(0, 4096);
4096227825Stheraven}
4097227825Stheraven
4098227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4099227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::~wbuffer_convert()
4100227825Stheraven{
4101227825Stheraven    __close();
4102227825Stheraven    delete __cv_;
4103227825Stheraven    if (__owns_eb_)
4104227825Stheraven        delete [] __extbuf_;
4105227825Stheraven    if (__owns_ib_)
4106227825Stheraven        delete [] __intbuf_;
4107227825Stheraven}
4108227825Stheraven
4109227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4110227825Stheraventypename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4111227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::underflow()
4112227825Stheraven{
4113227825Stheraven    if (__cv_ == 0 || __bufptr_ == 0)
4114227825Stheraven        return traits_type::eof();
4115227825Stheraven    bool __initial = __read_mode();
4116227825Stheraven    char_type __1buf;
4117227825Stheraven    if (this->gptr() == 0)
4118227825Stheraven        this->setg(&__1buf, &__1buf+1, &__1buf+1);
4119227825Stheraven    const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
4120227825Stheraven    int_type __c = traits_type::eof();
4121227825Stheraven    if (this->gptr() == this->egptr())
4122227825Stheraven    {
4123227825Stheraven        memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
4124227825Stheraven        if (__always_noconv_)
4125227825Stheraven        {
4126227825Stheraven            streamsize __nmemb = static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz);
4127227825Stheraven            __nmemb = __bufptr_->sgetn((char*)this->eback() + __unget_sz, __nmemb);
4128227825Stheraven            if (__nmemb != 0)
4129227825Stheraven            {
4130227825Stheraven                this->setg(this->eback(),
4131227825Stheraven                           this->eback() + __unget_sz,
4132227825Stheraven                           this->eback() + __unget_sz + __nmemb);
4133227825Stheraven                __c = *this->gptr();
4134227825Stheraven            }
4135227825Stheraven        }
4136227825Stheraven        else
4137227825Stheraven        {
4138227825Stheraven            memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
4139227825Stheraven            __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
4140227825Stheraven            __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
4141227825Stheraven            streamsize __nmemb = _VSTD::min(static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz),
4142227825Stheraven                                 static_cast<streamsize>(__extbufend_ - __extbufnext_));
4143227825Stheraven            codecvt_base::result __r;
4144227825Stheraven            state_type __svs = __st_;
4145227825Stheraven            streamsize __nr = __bufptr_->sgetn(const_cast<char*>(__extbufnext_), __nmemb);
4146227825Stheraven            if (__nr != 0)
4147227825Stheraven            {
4148227825Stheraven                __extbufend_ = __extbufnext_ + __nr;
4149227825Stheraven                char_type*  __inext;
4150227825Stheraven                __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
4151227825Stheraven                                       this->eback() + __unget_sz,
4152227825Stheraven                                       this->egptr(), __inext);
4153227825Stheraven                if (__r == codecvt_base::noconv)
4154227825Stheraven                {
4155227825Stheraven                    this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)__extbufend_);
4156227825Stheraven                    __c = *this->gptr();
4157227825Stheraven                }
4158227825Stheraven                else if (__inext != this->eback() + __unget_sz)
4159227825Stheraven                {
4160227825Stheraven                    this->setg(this->eback(), this->eback() + __unget_sz, __inext);
4161227825Stheraven                    __c = *this->gptr();
4162227825Stheraven                }
4163227825Stheraven            }
4164227825Stheraven        }
4165227825Stheraven    }
4166227825Stheraven    else
4167227825Stheraven        __c = *this->gptr();
4168227825Stheraven    if (this->eback() == &__1buf)
4169227825Stheraven        this->setg(0, 0, 0);
4170227825Stheraven    return __c;
4171227825Stheraven}
4172227825Stheraven
4173227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4174227825Stheraventypename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4175227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::pbackfail(int_type __c)
4176227825Stheraven{
4177227825Stheraven    if (__cv_ != 0 && __bufptr_ != 0 && this->eback() < this->gptr())
4178227825Stheraven    {
4179227825Stheraven        if (traits_type::eq_int_type(__c, traits_type::eof()))
4180227825Stheraven        {
4181227825Stheraven            this->gbump(-1);
4182227825Stheraven            return traits_type::not_eof(__c);
4183227825Stheraven        }
4184227825Stheraven        if (traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
4185227825Stheraven        {
4186227825Stheraven            this->gbump(-1);
4187227825Stheraven            *this->gptr() = traits_type::to_char_type(__c);
4188227825Stheraven            return __c;
4189227825Stheraven        }
4190227825Stheraven    }
4191227825Stheraven    return traits_type::eof();
4192227825Stheraven}
4193227825Stheraven
4194227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4195227825Stheraventypename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4196227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::overflow(int_type __c)
4197227825Stheraven{
4198227825Stheraven    if (__cv_ == 0 || __bufptr_ == 0)
4199227825Stheraven        return traits_type::eof();
4200227825Stheraven    __write_mode();
4201227825Stheraven    char_type __1buf;
4202227825Stheraven    char_type* __pb_save = this->pbase();
4203227825Stheraven    char_type* __epb_save = this->epptr();
4204227825Stheraven    if (!traits_type::eq_int_type(__c, traits_type::eof()))
4205227825Stheraven    {
4206227825Stheraven        if (this->pptr() == 0)
4207227825Stheraven            this->setp(&__1buf, &__1buf+1);
4208227825Stheraven        *this->pptr() = traits_type::to_char_type(__c);
4209227825Stheraven        this->pbump(1);
4210227825Stheraven    }
4211227825Stheraven    if (this->pptr() != this->pbase())
4212227825Stheraven    {
4213227825Stheraven        if (__always_noconv_)
4214227825Stheraven        {
4215227825Stheraven            streamsize __nmemb = static_cast<streamsize>(this->pptr() - this->pbase());
4216227825Stheraven            if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4217227825Stheraven                return traits_type::eof();
4218227825Stheraven        }
4219227825Stheraven        else
4220227825Stheraven        {
4221227825Stheraven            char* __extbe = __extbuf_;
4222227825Stheraven            codecvt_base::result __r;
4223227825Stheraven            do
4224227825Stheraven            {
4225227825Stheraven                const char_type* __e;
4226227825Stheraven                __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
4227227825Stheraven                                        __extbuf_, __extbuf_ + __ebs_, __extbe);
4228227825Stheraven                if (__e == this->pbase())
4229227825Stheraven                    return traits_type::eof();
4230227825Stheraven                if (__r == codecvt_base::noconv)
4231227825Stheraven                {
4232227825Stheraven                    streamsize __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
4233227825Stheraven                    if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4234227825Stheraven                        return traits_type::eof();
4235227825Stheraven                }
4236227825Stheraven                else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
4237227825Stheraven                {
4238227825Stheraven                    streamsize __nmemb = static_cast<size_t>(__extbe - __extbuf_);
4239227825Stheraven                    if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4240227825Stheraven                        return traits_type::eof();
4241227825Stheraven                    if (__r == codecvt_base::partial)
4242227825Stheraven                    {
4243227825Stheraven                        this->setp((char_type*)__e, this->pptr());
4244227825Stheraven                        this->pbump(this->epptr() - this->pbase());
4245227825Stheraven                    }
4246227825Stheraven                }
4247227825Stheraven                else
4248227825Stheraven                    return traits_type::eof();
4249227825Stheraven            } while (__r == codecvt_base::partial);
4250227825Stheraven        }
4251227825Stheraven        this->setp(__pb_save, __epb_save);
4252227825Stheraven    }
4253227825Stheraven    return traits_type::not_eof(__c);
4254227825Stheraven}
4255227825Stheraven
4256227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4257227825Stheravenbasic_streambuf<_Elem, _Tr>*
4258227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::setbuf(char_type* __s, streamsize __n)
4259227825Stheraven{
4260227825Stheraven    this->setg(0, 0, 0);
4261227825Stheraven    this->setp(0, 0);
4262227825Stheraven    if (__owns_eb_)
4263227825Stheraven        delete [] __extbuf_;
4264227825Stheraven    if (__owns_ib_)
4265227825Stheraven        delete [] __intbuf_;
4266227825Stheraven    __ebs_ = __n;
4267227825Stheraven    if (__ebs_ > sizeof(__extbuf_min_))
4268227825Stheraven    {
4269227825Stheraven        if (__always_noconv_ && __s)
4270227825Stheraven        {
4271227825Stheraven            __extbuf_ = (char*)__s;
4272227825Stheraven            __owns_eb_ = false;
4273227825Stheraven        }
4274227825Stheraven        else
4275227825Stheraven        {
4276227825Stheraven            __extbuf_ = new char[__ebs_];
4277227825Stheraven            __owns_eb_ = true;
4278227825Stheraven        }
4279227825Stheraven    }
4280227825Stheraven    else
4281227825Stheraven    {
4282227825Stheraven        __extbuf_ = __extbuf_min_;
4283227825Stheraven        __ebs_ = sizeof(__extbuf_min_);
4284227825Stheraven        __owns_eb_ = false;
4285227825Stheraven    }
4286227825Stheraven    if (!__always_noconv_)
4287227825Stheraven    {
4288227825Stheraven        __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
4289227825Stheraven        if (__s && __ibs_ >= sizeof(__extbuf_min_))
4290227825Stheraven        {
4291227825Stheraven            __intbuf_ = __s;
4292227825Stheraven            __owns_ib_ = false;
4293227825Stheraven        }
4294227825Stheraven        else
4295227825Stheraven        {
4296227825Stheraven            __intbuf_ = new char_type[__ibs_];
4297227825Stheraven            __owns_ib_ = true;
4298227825Stheraven        }
4299227825Stheraven    }
4300227825Stheraven    else
4301227825Stheraven    {
4302227825Stheraven        __ibs_ = 0;
4303227825Stheraven        __intbuf_ = 0;
4304227825Stheraven        __owns_ib_ = false;
4305227825Stheraven    }
4306227825Stheraven    return this;
4307227825Stheraven}
4308227825Stheraven
4309227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4310227825Stheraventypename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4311227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::seekoff(off_type __off, ios_base::seekdir __way,
4312227825Stheraven                                        ios_base::openmode __om)
4313227825Stheraven{
4314227825Stheraven    int __width = __cv_->encoding();
4315227825Stheraven    if (__cv_ == 0 || __bufptr_ == 0 || (__width <= 0 && __off != 0) || sync())
4316227825Stheraven        return pos_type(off_type(-1));
4317227825Stheraven    // __width > 0 || __off == 0
4318227825Stheraven    switch (__way)
4319227825Stheraven    {
4320227825Stheraven    case ios_base::beg:
4321227825Stheraven        break;
4322227825Stheraven    case ios_base::cur:
4323227825Stheraven        break;
4324227825Stheraven    case ios_base::end:
4325227825Stheraven        break;
4326227825Stheraven    default:
4327227825Stheraven        return pos_type(off_type(-1));
4328227825Stheraven    }
4329227825Stheraven    pos_type __r = __bufptr_->pubseekoff(__width * __off, __way, __om);
4330227825Stheraven    __r.state(__st_);
4331227825Stheraven    return __r;
4332227825Stheraven}
4333227825Stheraven
4334227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4335227825Stheraventypename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4336227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::seekpos(pos_type __sp, ios_base::openmode __wch)
4337227825Stheraven{
4338227825Stheraven    if (__cv_ == 0 || __bufptr_ == 0 || sync())
4339227825Stheraven        return pos_type(off_type(-1));
4340227825Stheraven    if (__bufptr_->pubseekpos(__sp, __wch) == pos_type(off_type(-1)))
4341227825Stheraven        return pos_type(off_type(-1));
4342227825Stheraven    return __sp;
4343227825Stheraven}
4344227825Stheraven
4345227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4346227825Stheravenint
4347227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::sync()
4348227825Stheraven{
4349227825Stheraven    if (__cv_ == 0 || __bufptr_ == 0)
4350227825Stheraven        return 0;
4351227825Stheraven    if (__cm_ & ios_base::out)
4352227825Stheraven    {
4353227825Stheraven        if (this->pptr() != this->pbase())
4354227825Stheraven            if (overflow() == traits_type::eof())
4355227825Stheraven                return -1;
4356227825Stheraven        codecvt_base::result __r;
4357227825Stheraven        do
4358227825Stheraven        {
4359227825Stheraven            char* __extbe;
4360227825Stheraven            __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
4361227825Stheraven            streamsize __nmemb = static_cast<streamsize>(__extbe - __extbuf_);
4362227825Stheraven            if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4363227825Stheraven                return -1;
4364227825Stheraven        } while (__r == codecvt_base::partial);
4365227825Stheraven        if (__r == codecvt_base::error)
4366227825Stheraven            return -1;
4367227825Stheraven        if (__bufptr_->pubsync())
4368227825Stheraven            return -1;
4369227825Stheraven    }
4370227825Stheraven    else if (__cm_ & ios_base::in)
4371227825Stheraven    {
4372227825Stheraven        off_type __c;
4373227825Stheraven        if (__always_noconv_)
4374227825Stheraven            __c = this->egptr() - this->gptr();
4375227825Stheraven        else
4376227825Stheraven        {
4377227825Stheraven            int __width = __cv_->encoding();
4378227825Stheraven            __c = __extbufend_ - __extbufnext_;
4379227825Stheraven            if (__width > 0)
4380227825Stheraven                __c += __width * (this->egptr() - this->gptr());
4381227825Stheraven            else
4382227825Stheraven            {
4383227825Stheraven                if (this->gptr() != this->egptr())
4384227825Stheraven                {
4385227825Stheraven                    reverse(this->gptr(), this->egptr());
4386227825Stheraven                    codecvt_base::result __r;
4387227825Stheraven                    const char_type* __e = this->gptr();
4388227825Stheraven                    char* __extbe;
4389227825Stheraven                    do
4390227825Stheraven                    {
4391227825Stheraven                        __r = __cv_->out(__st_, __e, this->egptr(), __e,
4392227825Stheraven                                         __extbuf_, __extbuf_ + __ebs_, __extbe);
4393227825Stheraven                        switch (__r)
4394227825Stheraven                        {
4395227825Stheraven                        case codecvt_base::noconv:
4396227825Stheraven                            __c += this->egptr() - this->gptr();
4397227825Stheraven                            break;
4398227825Stheraven                        case codecvt_base::ok:
4399227825Stheraven                        case codecvt_base::partial:
4400227825Stheraven                            __c += __extbe - __extbuf_;
4401227825Stheraven                            break;
4402227825Stheraven                        default:
4403227825Stheraven                            return -1;
4404227825Stheraven                        }
4405227825Stheraven                    } while (__r == codecvt_base::partial);
4406227825Stheraven                }
4407227825Stheraven            }
4408227825Stheraven        }
4409227825Stheraven        if (__bufptr_->pubseekoff(-__c, ios_base::cur, __cm_) == pos_type(off_type(-1)))
4410227825Stheraven            return -1;
4411227825Stheraven        this->setg(0, 0, 0);
4412227825Stheraven        __cm_ = 0;
4413227825Stheraven    }
4414227825Stheraven    return 0;
4415227825Stheraven}
4416227825Stheraven
4417227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4418227825Stheravenbool
4419227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::__read_mode()
4420227825Stheraven{
4421227825Stheraven    if (!(__cm_ & ios_base::in))
4422227825Stheraven    {
4423227825Stheraven        this->setp(0, 0);
4424227825Stheraven        if (__always_noconv_)
4425227825Stheraven            this->setg((char_type*)__extbuf_,
4426227825Stheraven                       (char_type*)__extbuf_ + __ebs_,
4427227825Stheraven                       (char_type*)__extbuf_ + __ebs_);
4428227825Stheraven        else
4429227825Stheraven            this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
4430227825Stheraven        __cm_ = ios_base::in;
4431227825Stheraven        return true;
4432227825Stheraven    }
4433227825Stheraven    return false;
4434227825Stheraven}
4435227825Stheraven
4436227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4437227825Stheravenvoid
4438227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::__write_mode()
4439227825Stheraven{
4440227825Stheraven    if (!(__cm_ & ios_base::out))
4441227825Stheraven    {
4442227825Stheraven        this->setg(0, 0, 0);
4443227825Stheraven        if (__ebs_ > sizeof(__extbuf_min_))
4444227825Stheraven        {
4445227825Stheraven            if (__always_noconv_)
4446227825Stheraven                this->setp((char_type*)__extbuf_,
4447227825Stheraven                           (char_type*)__extbuf_ + (__ebs_ - 1));
4448227825Stheraven            else
4449227825Stheraven                this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
4450227825Stheraven        }
4451227825Stheraven        else
4452227825Stheraven            this->setp(0, 0);
4453227825Stheraven        __cm_ = ios_base::out;
4454227825Stheraven    }
4455227825Stheraven}
4456227825Stheraven
4457227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4458227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>*
4459227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::__close()
4460227825Stheraven{
4461227825Stheraven    wbuffer_convert* __rt = 0;
4462227825Stheraven    if (__cv_ != 0 && __bufptr_ != 0)
4463227825Stheraven    {
4464227825Stheraven        __rt = this;
4465227825Stheraven        if ((__cm_ & ios_base::out) && sync())
4466227825Stheraven            __rt = 0;
4467227825Stheraven    }
4468227825Stheraven    return __rt;
4469227825Stheraven}
4470227825Stheraven
4471227825Stheraven_LIBCPP_END_NAMESPACE_STD
4472227825Stheraven
4473227825Stheraven#endif  // _LIBCPP_LOCALE
4474