blob: 2a1085d72686b6e8a5186d5d71f7563d2c54dd78 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- locale ------------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_LOCALE
12#define _LIBCPP_LOCALE
13
14/*
15 locale synopsis
16
17namespace std
18{
19
20class locale
21{
22public:
23 // types:
24 class facet;
25 class id;
26
27 typedef int category;
28 static const category // values assigned here are for exposition only
29 none = 0x000,
30 collate = 0x010,
31 ctype = 0x020,
32 monetary = 0x040,
33 numeric = 0x080,
34 time = 0x100,
35 messages = 0x200,
36 all = collate | ctype | monetary | numeric | time | messages;
37
38 // construct/copy/destroy:
Howard Hinnant7c9e5732011-05-31 15:34:58 +000039 locale() noexcept;
40 locale(const locale& other) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000041 explicit locale(const char* std_name);
42 explicit locale(const string& std_name);
43 locale(const locale& other, const char* std_name, category);
44 locale(const locale& other, const string& std_name, category);
45 template <class Facet> locale(const locale& other, Facet* f);
46 locale(const locale& other, const locale& one, category);
47
Howard Hinnant7c9e5732011-05-31 15:34:58 +000048 ~locale(); // not virtual
Howard Hinnantc51e1022010-05-11 19:42:16 +000049
Howard Hinnant7c9e5732011-05-31 15:34:58 +000050 const locale& operator=(const locale& other) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000051
52 template <class Facet> locale combine(const locale& other) const;
53
54 // locale operations:
55 basic_string<char> name() const;
56 bool operator==(const locale& other) const;
57 bool operator!=(const locale& other) const;
58 template <class charT, class Traits, class Allocator>
59 bool operator()(const basic_string<charT,Traits,Allocator>& s1,
60 const basic_string<charT,Traits,Allocator>& s2) const;
61
62 // global locale objects:
63 static locale global(const locale&);
64 static const locale& classic();
65};
66
67template <class Facet> const Facet& use_facet(const locale&);
Howard Hinnant7c9e5732011-05-31 15:34:58 +000068template <class Facet> bool has_facet(const locale&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000069
70// 22.3.3, convenience interfaces:
71template <class charT> bool isspace (charT c, const locale& loc);
72template <class charT> bool isprint (charT c, const locale& loc);
73template <class charT> bool iscntrl (charT c, const locale& loc);
74template <class charT> bool isupper (charT c, const locale& loc);
75template <class charT> bool islower (charT c, const locale& loc);
76template <class charT> bool isalpha (charT c, const locale& loc);
77template <class charT> bool isdigit (charT c, const locale& loc);
78template <class charT> bool ispunct (charT c, const locale& loc);
79template <class charT> bool isxdigit(charT c, const locale& loc);
80template <class charT> bool isalnum (charT c, const locale& loc);
81template <class charT> bool isgraph (charT c, const locale& loc);
82template <class charT> charT toupper(charT c, const locale& loc);
83template <class charT> charT tolower(charT c, const locale& loc);
Howard Hinnant9dd7e892010-05-31 20:58:54 +000084
85template<class Codecvt, class Elem = wchar_t,
86 class Wide_alloc = allocator<Elem>,
87 class Byte_alloc = allocator<char>>
88class wstring_convert
89{
90public:
91 typedef basic_string<char, char_traits<char>, Byte_alloc> byte_string;
92 typedef basic_string<Elem, char_traits<Elem>, Wide_alloc> wide_string;
93 typedef typename Codecvt::state_type state_type;
94 typedef typename wide_string::traits_type::int_type int_type;
95
Marshall Clowccaa0fb2013-08-27 20:18:59 +000096 explicit wstring_convert(Codecvt* pcvt = new Codecvt); // explicit in C++14
Howard Hinnant9dd7e892010-05-31 20:58:54 +000097 wstring_convert(Codecvt* pcvt, state_type state);
Marshall Clowccaa0fb2013-08-27 20:18:59 +000098 explicit wstring_convert(const byte_string& byte_err, // explicit in C++14
Howard Hinnant9dd7e892010-05-31 20:58:54 +000099 const wide_string& wide_err = wide_string());
Marshall Clowccaa0fb2013-08-27 20:18:59 +0000100 wstring_convert(const wstring_convert&) = delete; // C++14
101 wstring_convert & operator=(const wstring_convert &) = delete; // C++14
Howard Hinnant9dd7e892010-05-31 20:58:54 +0000102 ~wstring_convert();
103
104 wide_string from_bytes(char byte);
105 wide_string from_bytes(const char* ptr);
106 wide_string from_bytes(const byte_string& str);
107 wide_string from_bytes(const char* first, const char* last);
108
109 byte_string to_bytes(Elem wchar);
110 byte_string to_bytes(const Elem* wptr);
111 byte_string to_bytes(const wide_string& wstr);
112 byte_string to_bytes(const Elem* first, const Elem* last);
113
Marshall Clowccaa0fb2013-08-27 20:18:59 +0000114 size_t converted() const; // noexcept in C++14
Howard Hinnant9dd7e892010-05-31 20:58:54 +0000115 state_type state() const;
116};
117
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118template <class Codecvt, class Elem = wchar_t, class Tr = char_traits<Elem>>
Howard Hinnant9dd7e892010-05-31 20:58:54 +0000119class wbuffer_convert
120 : public basic_streambuf<Elem, Tr>
121{
122public:
123 typedef typename Tr::state_type state_type;
124
Marshall Clowccaa0fb2013-08-27 20:18:59 +0000125 explicit wbuffer_convert(streambuf* bytebuf = 0, Codecvt* pcvt = new Codecvt,
126 state_type state = state_type()); // explicit in C++14
127 wbuffer_convert(const wbuffer_convert&) = delete; // C++14
128 wbuffer_convert & operator=(const wbuffer_convert &) = delete; // C++14
129 ~wbuffer_convert(); // C++14
130
Howard Hinnant9dd7e892010-05-31 20:58:54 +0000131 streambuf* rdbuf() const;
132 streambuf* rdbuf(streambuf* bytebuf);
133
134 state_type state() const;
135};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000136
137// 22.4.1 and 22.4.1.3, ctype:
138class ctype_base;
139template <class charT> class ctype;
140template <> class ctype<char>; // specialization
141template <class charT> class ctype_byname;
142template <> class ctype_byname<char>; // specialization
143
144class codecvt_base;
145template <class internT, class externT, class stateT> class codecvt;
146template <class internT, class externT, class stateT> class codecvt_byname;
147
148// 22.4.2 and 22.4.3, numeric:
149template <class charT, class InputIterator> class num_get;
150template <class charT, class OutputIterator> class num_put;
151template <class charT> class numpunct;
152template <class charT> class numpunct_byname;
153
154// 22.4.4, col lation:
155template <class charT> class collate;
156template <class charT> class collate_byname;
157
158// 22.4.5, date and time:
159class time_base;
160template <class charT, class InputIterator> class time_get;
161template <class charT, class InputIterator> class time_get_byname;
162template <class charT, class OutputIterator> class time_put;
163template <class charT, class OutputIterator> class time_put_byname;
164
165// 22.4.6, money:
166class money_base;
167template <class charT, class InputIterator> class money_get;
168template <class charT, class OutputIterator> class money_put;
169template <class charT, bool Intl> class moneypunct;
170template <class charT, bool Intl> class moneypunct_byname;
171
172// 22.4.7, message retrieval:
173class messages_base;
174template <class charT> class messages;
175template <class charT> class messages_byname;
176
177} // std
178
179*/
180
181#include <__config>
182#include <__locale>
183#include <algorithm>
184#include <memory>
185#include <ios>
186#include <streambuf>
187#include <iterator>
188#include <limits>
Marshall Clowdde4bfe2013-03-18 17:45:34 +0000189#ifndef __APPLE__
Howard Hinnant155c2af2010-05-24 17:49:41 +0000190#include <cstdarg>
191#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000192#include <cstdlib>
193#include <ctime>
Howard Hinnant8ad70912013-09-17 01:34:47 +0000194#if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
Howard Hinnantae0f80b2011-09-29 20:33:10 +0000195#include <support/win32/locale_win32.h>
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000196#else // _LIBCPP_MSVCRT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000197#include <nl_types.h>
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000198#endif // !_LIBCPP_MSVCRT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000199
Marshall Clowdde4bfe2013-03-18 17:45:34 +0000200#ifdef __APPLE__
Howard Hinnant48fd5d52012-11-14 21:17:15 +0000201#include <Availability.h>
202#endif
203
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000204#include <__undef_min_max>
205
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000206#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000208#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000209
210_LIBCPP_BEGIN_NAMESPACE_STD
211
Marshall Clow82378c02013-03-18 19:34:07 +0000212#if defined(__APPLE__) || defined(__FreeBSD__)
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000213# define _LIBCPP_GET_C_LOCALE 0
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000214#elif defined(__NetBSD__)
215# define _LIBCPP_GET_C_LOCALE LC_C_LOCALE
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000216#else
217# define _LIBCPP_GET_C_LOCALE __cloc()
Howard Hinnantf312e3e2011-09-28 23:39:33 +0000218 // Get the C locale object
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000219 _LIBCPP_FUNC_VIS locale_t __cloc();
Howard Hinnantf312e3e2011-09-28 23:39:33 +0000220#define __cloc_defined
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000221#endif
222
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000223typedef _VSTD::remove_pointer<locale_t>::type __locale_struct;
224typedef _VSTD::unique_ptr<__locale_struct, decltype(&freelocale)> __locale_unique_ptr;
David Chisnall4d958ed2012-02-29 13:00:07 +0000225#ifndef _LIBCPP_LOCALE__L_EXTENSIONS
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000226typedef _VSTD::unique_ptr<__locale_struct, decltype(&uselocale)> __locale_raii;
David Chisnall4d958ed2012-02-29 13:00:07 +0000227#endif
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000228
Howard Hinnant155c2af2010-05-24 17:49:41 +0000229// OSX has nice foo_l() functions that let you turn off use of the global
230// locale. Linux, not so much. The following functions avoid the locale when
231// that's possible and otherwise do the wrong thing. FIXME.
Howard Hinnantea382952013-08-14 18:00:20 +0000232#if defined(__linux__) || defined(EMSCRIPTEN) || defined(_AIX)
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000233
234#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
235decltype(MB_CUR_MAX_L(_VSTD::declval<locale_t>()))
Howard Hinnant756c69b2010-09-22 16:48:34 +0000236inline _LIBCPP_INLINE_VISIBILITY
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000237__mb_cur_max_l(locale_t __l)
Howard Hinnant155c2af2010-05-24 17:49:41 +0000238{
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000239 return MB_CUR_MAX_L(__l);
240}
241#else // _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000242inline _LIBCPP_ALWAYS_INLINE
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000243decltype(MB_CUR_MAX) __mb_cur_max_l(locale_t __l)
244{
245 __locale_raii __current(uselocale(__l), uselocale);
246 return MB_CUR_MAX;
247}
248#endif // _LIBCPP_LOCALE__L_EXTENSIONS
249
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000250inline _LIBCPP_ALWAYS_INLINE
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000251wint_t __btowc_l(int __c, locale_t __l)
252{
Howard Hinnantf312e3e2011-09-28 23:39:33 +0000253#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000254 return btowc_l(__c, __l);
255#else
256 __locale_raii __current(uselocale(__l), uselocale);
257 return btowc(__c);
258#endif
Alexis Huntd4a24912011-07-13 06:40:50 +0000259}
Howard Hinnant2de5c472011-07-13 15:48:16 +0000260
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000261inline _LIBCPP_ALWAYS_INLINE
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000262int __wctob_l(wint_t __c, locale_t __l)
Alexis Huntd4a24912011-07-13 06:40:50 +0000263{
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000264#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
265 return wctob_l(__c, __l);
266#else
267 __locale_raii __current(uselocale(__l), uselocale);
268 return wctob(__c);
269#endif
Alexis Huntd4a24912011-07-13 06:40:50 +0000270}
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000271
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000272inline _LIBCPP_ALWAYS_INLINE
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000273size_t __wcsnrtombs_l(char *__dest, const wchar_t **__src, size_t __nwc,
274 size_t __len, mbstate_t *__ps, locale_t __l)
275{
276#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
277 return wcsnrtombs_l(__dest, __src, __nwc, __len, __ps, __l);
278#else
279 __locale_raii __current(uselocale(__l), uselocale);
280 return wcsnrtombs(__dest, __src, __nwc, __len, __ps);
281#endif
282}
283
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000284inline _LIBCPP_ALWAYS_INLINE
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000285size_t __wcrtomb_l(char *__s, wchar_t __wc, mbstate_t *__ps, locale_t __l)
286{
287#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
288 return wcrtomb_l(__s, __wc, __ps, __l);
289#else
290 __locale_raii __current(uselocale(__l), uselocale);
291 return wcrtomb(__s, __wc, __ps);
292#endif
293}
294
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000295inline _LIBCPP_ALWAYS_INLINE
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000296size_t __mbsnrtowcs_l(wchar_t * __dest, const char **__src, size_t __nms,
297 size_t __len, mbstate_t *__ps, locale_t __l)
298{
299#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
David Chisnall1d581062011-09-21 08:39:44 +0000300 return mbsnrtowcs_l(__dest, __src, __nms, __len, __ps, __l);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000301#else
302 __locale_raii __current(uselocale(__l), uselocale);
303 return mbsnrtowcs(__dest, __src, __nms, __len, __ps);
304#endif
305}
306
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000307inline _LIBCPP_ALWAYS_INLINE
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000308size_t __mbrtowc_l(wchar_t *__pwc, const char *__s, size_t __n,
309 mbstate_t *__ps, locale_t __l)
310{
311#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
312 return mbrtowc_l(__pwc, __s, __n, __ps, __l);
313#else
314 __locale_raii __current(uselocale(__l), uselocale);
315 return mbrtowc(__pwc, __s, __n, __ps);
316#endif
317}
318
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000319inline _LIBCPP_ALWAYS_INLINE
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000320int __mbtowc_l(wchar_t *__pwc, const char *__pmb, size_t __max, locale_t __l)
321{
322#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
David Chisnall1d581062011-09-21 08:39:44 +0000323 return mbtowc_l(__pwc, __pmb, __max, __l);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000324#else
325 __locale_raii __current(uselocale(__l), uselocale);
326 return mbtowc(__pwc, __pmb, __max);
327#endif
328}
329
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000330inline _LIBCPP_ALWAYS_INLINE
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000331size_t __mbrlen_l(const char *__s, size_t __n, mbstate_t *__ps, locale_t __l)
332{
333#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
334 return mbrlen_l(__s, __n, __ps, __l);
335#else
336 __locale_raii __current(uselocale(__l), uselocale);
337 return mbrlen(__s, __n, __ps);
338#endif
339}
340
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000341inline _LIBCPP_ALWAYS_INLINE
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000342lconv *__localeconv_l(locale_t __l)
343{
344#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
345 return localeconv_l(__l);
346#else
347 __locale_raii __current(uselocale(__l), uselocale);
348 return localeconv();
349#endif
350}
351
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000352inline _LIBCPP_ALWAYS_INLINE
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000353size_t __mbsrtowcs_l(wchar_t *__dest, const char **__src, size_t __len,
354 mbstate_t *__ps, locale_t __l)
355{
356#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
357 return mbsrtowcs_l(__dest, __src, __len, __ps, __l);
358#else
359 __locale_raii __current(uselocale(__l), uselocale);
360 return mbsrtowcs(__dest, __src, __len, __ps);
361#endif
362}
363
Chandler Carruth952bdc82012-12-31 06:09:54 +0000364inline
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000365int __snprintf_l(char *__s, size_t __n, locale_t __l, const char *__format, ...) {
366 va_list __va;
367 va_start(__va, __format);
368#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
369 int __res = vsnprintf_l(__s, __n, __l, __format, __va);
370#else
371 __locale_raii __current(uselocale(__l), uselocale);
372 int __res = vsnprintf(__s, __n, __format, __va);
373#endif
374 va_end(__va);
375 return __res;
376}
377
Chandler Carruth952bdc82012-12-31 06:09:54 +0000378inline
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000379int __asprintf_l(char **__s, locale_t __l, const char *__format, ...) {
380 va_list __va;
381 va_start(__va, __format);
382#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
383 int __res = vasprintf_l(__s, __l, __format, __va);
384#else
385 __locale_raii __current(uselocale(__l), uselocale);
386 int __res = vasprintf(__s, __format, __va);
387#endif
388 va_end(__va);
389 return __res;
390}
391
Chandler Carruth952bdc82012-12-31 06:09:54 +0000392inline
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000393int __sscanf_l(const char *__s, locale_t __l, const char *__format, ...) {
394 va_list __va;
395 va_start(__va, __format);
396#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
397 int __res = vsscanf_l(__s, __l, __format, __va);
398#else
399 __locale_raii __current(uselocale(__l), uselocale);
400 int __res = vsscanf(__s, __format, __va);
401#endif
402 va_end(__va);
403 return __res;
404}
405
Howard Hinnantf312e3e2011-09-28 23:39:33 +0000406#endif // __linux__
Howard Hinnant155c2af2010-05-24 17:49:41 +0000407
Howard Hinnantc51e1022010-05-11 19:42:16 +0000408// __scan_keyword
409// Scans [__b, __e) until a match is found in the basic_strings range
410// [__kb, __ke) or until it can be shown that there is no match in [__kb, __ke).
411// __b will be incremented (visibly), consuming CharT until a match is found
412// or proved to not exist. A keyword may be "", in which will match anything.
413// If one keyword is a prefix of another, and the next CharT in the input
414// might match another keyword, the algorithm will attempt to find the longest
415// matching keyword. If the longer matching keyword ends up not matching, then
416// no keyword match is found. If no keyword match is found, __ke is returned
417// and failbit is set in __err.
418// Else an iterator pointing to the matching keyword is found. If more than
419// one keyword matches, an iterator to the first matching keyword is returned.
420// If on exit __b == __e, eofbit is set in __err. If __case_senstive is false,
421// __ct is used to force to lower case before comparing characters.
422// Examples:
423// Keywords: "a", "abb"
424// If the input is "a", the first keyword matches and eofbit is set.
425// If the input is "abc", no match is found and "ab" are consumed.
426template <class _InputIterator, class _ForwardIterator, class _Ctype>
427_LIBCPP_HIDDEN
428_ForwardIterator
429__scan_keyword(_InputIterator& __b, _InputIterator __e,
430 _ForwardIterator __kb, _ForwardIterator __ke,
431 const _Ctype& __ct, ios_base::iostate& __err,
432 bool __case_sensitive = true)
433{
434 typedef typename iterator_traits<_InputIterator>::value_type _CharT;
Howard Hinnant28b24882011-12-01 20:21:04 +0000435 size_t __nkw = static_cast<size_t>(_VSTD::distance(__kb, __ke));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000436 const unsigned char __doesnt_match = '\0';
437 const unsigned char __might_match = '\1';
438 const unsigned char __does_match = '\2';
439 unsigned char __statbuf[100];
440 unsigned char* __status = __statbuf;
441 unique_ptr<unsigned char, void(*)(void*)> __stat_hold(0, free);
442 if (__nkw > sizeof(__statbuf))
443 {
444 __status = (unsigned char*)malloc(__nkw);
445 if (__status == 0)
446 __throw_bad_alloc();
447 __stat_hold.reset(__status);
448 }
449 size_t __n_might_match = __nkw; // At this point, any keyword might match
450 size_t __n_does_match = 0; // but none of them definitely do
451 // Initialize all statuses to __might_match, except for "" keywords are __does_match
452 unsigned char* __st = __status;
453 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, ++__st)
454 {
455 if (!__ky->empty())
456 *__st = __might_match;
457 else
458 {
459 *__st = __does_match;
460 --__n_might_match;
461 ++__n_does_match;
462 }
463 }
464 // While there might be a match, test keywords against the next CharT
465 for (size_t __indx = 0; __b != __e && __n_might_match > 0; ++__indx)
466 {
467 // Peek at the next CharT but don't consume it
468 _CharT __c = *__b;
469 if (!__case_sensitive)
470 __c = __ct.toupper(__c);
471 bool __consume = false;
472 // For each keyword which might match, see if the __indx character is __c
473 // If a match if found, consume __c
474 // If a match is found, and that is the last character in the keyword,
475 // then that keyword matches.
476 // If the keyword doesn't match this character, then change the keyword
477 // to doesn't match
478 __st = __status;
479 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, ++__st)
480 {
481 if (*__st == __might_match)
482 {
483 _CharT __kc = (*__ky)[__indx];
484 if (!__case_sensitive)
485 __kc = __ct.toupper(__kc);
486 if (__c == __kc)
487 {
488 __consume = true;
489 if (__ky->size() == __indx+1)
490 {
491 *__st = __does_match;
492 --__n_might_match;
493 ++__n_does_match;
494 }
495 }
496 else
497 {
498 *__st = __doesnt_match;
499 --__n_might_match;
500 }
501 }
502 }
503 // consume if we matched a character
504 if (__consume)
505 {
506 ++__b;
507 // If we consumed a character and there might be a matched keyword that
508 // was marked matched on a previous iteration, then such keywords
509 // which are now marked as not matching.
510 if (__n_might_match + __n_does_match > 1)
511 {
512 __st = __status;
513 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, ++__st)
514 {
515 if (*__st == __does_match && __ky->size() != __indx+1)
516 {
517 *__st = __doesnt_match;
518 --__n_does_match;
519 }
520 }
521 }
522 }
523 }
524 // We've exited the loop because we hit eof and/or we have no more "might matches".
525 if (__b == __e)
526 __err |= ios_base::eofbit;
527 // Return the first matching result
528 for (__st = __status; __kb != __ke; ++__kb, ++__st)
529 if (*__st == __does_match)
530 break;
531 if (__kb == __ke)
532 __err |= ios_base::failbit;
533 return __kb;
534}
535
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000536struct _LIBCPP_TYPE_VIS __num_get_base
Howard Hinnantc51e1022010-05-11 19:42:16 +0000537{
538 static const int __num_get_buf_sz = 40;
539
540 static int __get_base(ios_base&);
541 static const char __src[33];
542};
543
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000544_LIBCPP_FUNC_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000545void __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end,
546 ios_base::iostate& __err);
547
Howard Hinnantc51e1022010-05-11 19:42:16 +0000548template <class _CharT>
549struct __num_get
550 : protected __num_get_base
551{
552 static string __stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep);
553 static string __stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
554 _CharT& __thousands_sep);
555 static int __stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
556 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
557 unsigned* __g, unsigned*& __g_end, _CharT* __atoms);
558 static int __stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp,
559 char* __a, char*& __a_end,
560 _CharT __decimal_point, _CharT __thousands_sep,
561 const string& __grouping, unsigned* __g,
562 unsigned*& __g_end, unsigned& __dc, _CharT* __atoms);
563};
564
565template <class _CharT>
566string
567__num_get<_CharT>::__stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep)
568{
569 locale __loc = __iob.getloc();
570 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 26, __atoms);
571 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
572 __thousands_sep = __np.thousands_sep();
573 return __np.grouping();
574}
575
576template <class _CharT>
577string
578__num_get<_CharT>::__stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
579 _CharT& __thousands_sep)
580{
581 locale __loc = __iob.getloc();
582 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 32, __atoms);
583 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
584 __decimal_point = __np.decimal_point();
585 __thousands_sep = __np.thousands_sep();
586 return __np.grouping();
587}
588
589template <class _CharT>
590int
591__num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
592 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
593 unsigned* __g, unsigned*& __g_end, _CharT* __atoms)
594{
Howard Hinnantf57b3fc2011-03-09 01:03:19 +0000595 if (__a_end == __a && (__ct == __atoms[24] || __ct == __atoms[25]))
596 {
597 *__a_end++ = __ct == __atoms[24] ? '+' : '-';
598 __dc = 0;
599 return 0;
600 }
Howard Hinnant28b24882011-12-01 20:21:04 +0000601 if (__grouping.size() != 0 && __ct == __thousands_sep)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000602 {
603 if (__g_end-__g < __num_get_buf_sz)
604 {
605 *__g_end++ = __dc;
606 __dc = 0;
607 }
608 return 0;
609 }
610 ptrdiff_t __f = find(__atoms, __atoms + 26, __ct) - __atoms;
Howard Hinnantf57b3fc2011-03-09 01:03:19 +0000611 if (__f >= 24)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612 return -1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000613 switch (__base)
614 {
615 case 8:
616 case 10:
617 if (__f >= __base)
Howard Hinnantf57b3fc2011-03-09 01:03:19 +0000618 return -1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000619 break;
Howard Hinnantf57b3fc2011-03-09 01:03:19 +0000620 case 16:
621 if (__f < 22)
622 break;
623 if (__a_end != __a && __a_end - __a <= 2 && __a_end[-1] == '0')
624 {
625 __dc = 0;
626 *__a_end++ = __src[__f];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000627 return 0;
Howard Hinnantf57b3fc2011-03-09 01:03:19 +0000628 }
629 return -1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000630 }
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000631 *__a_end++ = __src[__f];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000632 ++__dc;
633 return 0;
634}
635
636template <class _CharT>
637int
638__num_get<_CharT>::__stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp, char* __a, char*& __a_end,
639 _CharT __decimal_point, _CharT __thousands_sep, const string& __grouping,
640 unsigned* __g, unsigned*& __g_end, unsigned& __dc, _CharT* __atoms)
641{
642 if (__ct == __decimal_point)
643 {
644 if (!__in_units)
645 return -1;
646 __in_units = false;
647 *__a_end++ = '.';
648 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
649 *__g_end++ = __dc;
650 return 0;
651 }
652 if (__ct == __thousands_sep && __grouping.size() != 0)
653 {
654 if (!__in_units)
655 return -1;
656 if (__g_end-__g < __num_get_buf_sz)
657 {
658 *__g_end++ = __dc;
659 __dc = 0;
660 }
661 return 0;
662 }
663 ptrdiff_t __f = find(__atoms, __atoms + 32, __ct) - __atoms;
664 if (__f >= 32)
665 return -1;
666 char __x = __src[__f];
Howard Hinnant5132e192012-02-15 19:19:37 +0000667 if (__x == '-' || __x == '+')
668 {
Howard Hinnant21413152013-03-08 19:06:24 +0000669 if (__a_end == __a || (__a_end[-1] & 0x5F) == (__exp & 0x7F))
Howard Hinnant5132e192012-02-15 19:19:37 +0000670 {
671 *__a_end++ = __x;
672 return 0;
673 }
674 return -1;
675 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000676 if (__x == 'x' || __x == 'X')
677 __exp = 'P';
Howard Hinnant21413152013-03-08 19:06:24 +0000678 else if ((__x & 0x5F) == __exp)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000679 {
Howard Hinnant21413152013-03-08 19:06:24 +0000680 __exp |= 0x80;
681 if (__in_units)
682 {
683 __in_units = false;
684 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
685 *__g_end++ = __dc;
686 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000687 }
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000688 *__a_end++ = __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000689 if (__f >= 22)
690 return 0;
691 ++__dc;
692 return 0;
693}
694
Howard Hinnant8ea98242013-08-23 17:37:05 +0000695_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_TYPE_VIS __num_get<char>)
696_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_TYPE_VIS __num_get<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000697
698template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000699class _LIBCPP_TYPE_VIS_ONLY num_get
Howard Hinnantc51e1022010-05-11 19:42:16 +0000700 : public locale::facet,
701 private __num_get<_CharT>
702{
703public:
704 typedef _CharT char_type;
705 typedef _InputIterator iter_type;
706
707 _LIBCPP_ALWAYS_INLINE
708 explicit num_get(size_t __refs = 0)
709 : locale::facet(__refs) {}
710
711 _LIBCPP_ALWAYS_INLINE
712 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
713 ios_base::iostate& __err, bool& __v) const
714 {
715 return do_get(__b, __e, __iob, __err, __v);
716 }
717
718 _LIBCPP_ALWAYS_INLINE
719 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
720 ios_base::iostate& __err, long& __v) const
721 {
722 return do_get(__b, __e, __iob, __err, __v);
723 }
724
725 _LIBCPP_ALWAYS_INLINE
726 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
727 ios_base::iostate& __err, long long& __v) const
728 {
729 return do_get(__b, __e, __iob, __err, __v);
730 }
731
732 _LIBCPP_ALWAYS_INLINE
733 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
734 ios_base::iostate& __err, unsigned short& __v) const
735 {
736 return do_get(__b, __e, __iob, __err, __v);
737 }
738
739 _LIBCPP_ALWAYS_INLINE
740 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
741 ios_base::iostate& __err, unsigned int& __v) const
742 {
743 return do_get(__b, __e, __iob, __err, __v);
744 }
745
746 _LIBCPP_ALWAYS_INLINE
747 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
748 ios_base::iostate& __err, unsigned long& __v) const
749 {
750 return do_get(__b, __e, __iob, __err, __v);
751 }
752
753 _LIBCPP_ALWAYS_INLINE
754 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
755 ios_base::iostate& __err, unsigned long long& __v) const
756 {
757 return do_get(__b, __e, __iob, __err, __v);
758 }
759
760 _LIBCPP_ALWAYS_INLINE
761 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
762 ios_base::iostate& __err, float& __v) const
763 {
764 return do_get(__b, __e, __iob, __err, __v);
765 }
766
767 _LIBCPP_ALWAYS_INLINE
768 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
769 ios_base::iostate& __err, double& __v) const
770 {
771 return do_get(__b, __e, __iob, __err, __v);
772 }
773
774 _LIBCPP_ALWAYS_INLINE
775 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
776 ios_base::iostate& __err, long double& __v) const
777 {
778 return do_get(__b, __e, __iob, __err, __v);
779 }
780
781 _LIBCPP_ALWAYS_INLINE
782 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
783 ios_base::iostate& __err, void*& __v) const
784 {
785 return do_get(__b, __e, __iob, __err, __v);
786 }
787
788 static locale::id id;
789
790protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000791 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +0000792 ~num_get() {}
793
794 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
795 ios_base::iostate& __err, bool& __v) const;
796 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
797 ios_base::iostate& __err, long& __v) const;
798 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
799 ios_base::iostate& __err, long long& __v) const;
800 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
801 ios_base::iostate& __err, unsigned short& __v) const;
802 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
803 ios_base::iostate& __err, unsigned int& __v) const;
804 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
805 ios_base::iostate& __err, unsigned long& __v) const;
806 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
807 ios_base::iostate& __err, unsigned long long& __v) const;
808 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
809 ios_base::iostate& __err, float& __v) const;
810 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
811 ios_base::iostate& __err, double& __v) const;
812 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
813 ios_base::iostate& __err, long double& __v) const;
814 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
815 ios_base::iostate& __err, void*& __v) const;
Marshall Clowae385382013-11-05 14:28:52 +0000816
817 template <class _Fp>
818 iter_type __do_get_floating_point
819 (iter_type __b, iter_type __e, ios_base& __iob,
820 ios_base::iostate& __err, _Fp& __v) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000821};
822
823template <class _CharT, class _InputIterator>
824locale::id
825num_get<_CharT, _InputIterator>::id;
826
827template <class _Tp>
828_Tp
829__num_get_signed_integral(const char* __a, const char* __a_end,
830 ios_base::iostate& __err, int __base)
831{
832 if (__a != __a_end)
833 {
Howard Hinnantca8923c2013-01-22 17:26:08 +0000834 typename remove_reference<decltype(errno)>::type __save_errno = errno;
Howard Hinnant05c71342011-02-25 19:52:41 +0000835 errno = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000836 char *__p2;
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000837 long long __ll = strtoll_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
Howard Hinnantca8923c2013-01-22 17:26:08 +0000838 typename remove_reference<decltype(errno)>::type __current_errno = errno;
Howard Hinnant05c71342011-02-25 19:52:41 +0000839 if (__current_errno == 0)
840 errno = __save_errno;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000841 if (__p2 != __a_end)
842 {
843 __err = ios_base::failbit;
844 return 0;
845 }
Howard Hinnant05c71342011-02-25 19:52:41 +0000846 else if (__current_errno == ERANGE ||
847 __ll < numeric_limits<_Tp>::min() ||
848 numeric_limits<_Tp>::max() < __ll)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000849 {
850 __err = ios_base::failbit;
Howard Hinnant05c71342011-02-25 19:52:41 +0000851 if (__ll > 0)
852 return numeric_limits<_Tp>::max();
853 else
854 return numeric_limits<_Tp>::min();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000855 }
856 return static_cast<_Tp>(__ll);
857 }
858 __err = ios_base::failbit;
859 return 0;
860}
861
862template <class _Tp>
863_Tp
864__num_get_unsigned_integral(const char* __a, const char* __a_end,
865 ios_base::iostate& __err, int __base)
866{
867 if (__a != __a_end)
868 {
Howard Hinnant05c71342011-02-25 19:52:41 +0000869 if (*__a == '-')
870 {
871 __err = ios_base::failbit;
872 return 0;
873 }
Howard Hinnantca8923c2013-01-22 17:26:08 +0000874 typename remove_reference<decltype(errno)>::type __save_errno = errno;
Howard Hinnant05c71342011-02-25 19:52:41 +0000875 errno = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000876 char *__p2;
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000877 unsigned long long __ll = strtoull_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
Howard Hinnantca8923c2013-01-22 17:26:08 +0000878 typename remove_reference<decltype(errno)>::type __current_errno = errno;
Howard Hinnant05c71342011-02-25 19:52:41 +0000879 if (__current_errno == 0)
880 errno = __save_errno;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000881 if (__p2 != __a_end)
882 {
883 __err = ios_base::failbit;
884 return 0;
885 }
Howard Hinnant05c71342011-02-25 19:52:41 +0000886 else if (__current_errno == ERANGE ||
887 numeric_limits<_Tp>::max() < __ll)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000888 {
889 __err = ios_base::failbit;
890 return numeric_limits<_Tp>::max();
891 }
892 return static_cast<_Tp>(__ll);
893 }
894 __err = ios_base::failbit;
895 return 0;
896}
897
898template <class _Tp>
899_Tp
900__num_get_float(const char* __a, const char* __a_end, ios_base::iostate& __err)
901{
902 if (__a != __a_end)
903 {
Howard Hinnantc9567812013-04-13 18:19:25 +0000904 typename remove_reference<decltype(errno)>::type __save_errno = errno;
905 errno = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000906 char *__p2;
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000907 long double __ld = strtold_l(__a, &__p2, _LIBCPP_GET_C_LOCALE);
Howard Hinnantc9567812013-04-13 18:19:25 +0000908 typename remove_reference<decltype(errno)>::type __current_errno = errno;
909 if (__current_errno == 0)
910 errno = __save_errno;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000911 if (__p2 != __a_end)
912 {
913 __err = ios_base::failbit;
914 return 0;
915 }
Howard Hinnantc9567812013-04-13 18:19:25 +0000916 else if (__current_errno == ERANGE)
917 __err = ios_base::failbit;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000918 return static_cast<_Tp>(__ld);
919 }
920 __err = ios_base::failbit;
921 return 0;
922}
923
924template <class _CharT, class _InputIterator>
925_InputIterator
926num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
927 ios_base& __iob,
928 ios_base::iostate& __err,
929 bool& __v) const
930{
931 if ((__iob.flags() & ios_base::boolalpha) == 0)
932 {
933 long __lv = -1;
934 __b = do_get(__b, __e, __iob, __err, __lv);
935 switch (__lv)
936 {
937 case 0:
938 __v = false;
939 break;
940 case 1:
941 __v = true;
942 break;
943 default:
944 __v = true;
945 __err = ios_base::failbit;
946 break;
947 }
948 return __b;
949 }
950 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__iob.getloc());
951 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__iob.getloc());
952 typedef typename numpunct<_CharT>::string_type string_type;
953 const string_type __names[2] = {__np.truename(), __np.falsename()};
954 const string_type* __i = __scan_keyword(__b, __e, __names, __names+2,
955 __ct, __err);
956 __v = __i == __names;
957 return __b;
958}
959
960template <class _CharT, class _InputIterator>
961_InputIterator
962num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
963 ios_base& __iob,
964 ios_base::iostate& __err,
965 long& __v) const
966{
967 // Stage 1
968 int __base = this->__get_base(__iob);
969 // Stage 2
970 char_type __atoms[26];
971 char_type __thousands_sep;
972 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000973 string __buf;
974 __buf.resize(__buf.capacity());
975 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976 char* __a_end = __a;
977 unsigned __g[__num_get_base::__num_get_buf_sz];
978 unsigned* __g_end = __g;
979 unsigned __dc = 0;
980 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000981 {
982 if (__a_end - __a == __buf.size())
983 {
984 size_t __tmp = __buf.size();
985 __buf.resize(2*__buf.size());
986 __buf.resize(__buf.capacity());
987 __a = &__buf[0];
988 __a_end = __a + __tmp;
989 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000990 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991 __thousands_sep, __grouping, __g, __g_end,
992 __atoms))
993 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000994 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000995 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
996 *__g_end++ = __dc;
997 // Stage 3
998 __v = __num_get_signed_integral<long>(__a, __a_end, __err, __base);
999 // Digit grouping checked
1000 __check_grouping(__grouping, __g, __g_end, __err);
1001 // EOF checked
1002 if (__b == __e)
1003 __err |= ios_base::eofbit;
1004 return __b;
1005}
1006
1007template <class _CharT, class _InputIterator>
1008_InputIterator
1009num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1010 ios_base& __iob,
1011 ios_base::iostate& __err,
1012 long long& __v) const
1013{
1014 // Stage 1
1015 int __base = this->__get_base(__iob);
1016 // Stage 2
1017 char_type __atoms[26];
1018 char_type __thousands_sep;
1019 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001020 string __buf;
1021 __buf.resize(__buf.capacity());
1022 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023 char* __a_end = __a;
1024 unsigned __g[__num_get_base::__num_get_buf_sz];
1025 unsigned* __g_end = __g;
1026 unsigned __dc = 0;
1027 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001028 {
1029 if (__a_end - __a == __buf.size())
1030 {
1031 size_t __tmp = __buf.size();
1032 __buf.resize(2*__buf.size());
1033 __buf.resize(__buf.capacity());
1034 __a = &__buf[0];
1035 __a_end = __a + __tmp;
1036 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001037 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1038 __thousands_sep, __grouping, __g, __g_end,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039 __atoms))
1040 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001041 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001042 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1043 *__g_end++ = __dc;
1044 // Stage 3
1045 __v = __num_get_signed_integral<long long>(__a, __a_end, __err, __base);
1046 // Digit grouping checked
1047 __check_grouping(__grouping, __g, __g_end, __err);
1048 // EOF checked
1049 if (__b == __e)
1050 __err |= ios_base::eofbit;
1051 return __b;
1052}
1053
1054template <class _CharT, class _InputIterator>
1055_InputIterator
1056num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1057 ios_base& __iob,
1058 ios_base::iostate& __err,
1059 unsigned short& __v) const
1060{
1061 // Stage 1
1062 int __base = this->__get_base(__iob);
1063 // Stage 2
1064 char_type __atoms[26];
1065 char_type __thousands_sep;
1066 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001067 string __buf;
1068 __buf.resize(__buf.capacity());
1069 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001070 char* __a_end = __a;
1071 unsigned __g[__num_get_base::__num_get_buf_sz];
1072 unsigned* __g_end = __g;
1073 unsigned __dc = 0;
1074 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001075 {
1076 if (__a_end - __a == __buf.size())
1077 {
1078 size_t __tmp = __buf.size();
1079 __buf.resize(2*__buf.size());
1080 __buf.resize(__buf.capacity());
1081 __a = &__buf[0];
1082 __a_end = __a + __tmp;
1083 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001084 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085 __thousands_sep, __grouping, __g, __g_end,
1086 __atoms))
1087 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001088 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1090 *__g_end++ = __dc;
1091 // Stage 3
1092 __v = __num_get_unsigned_integral<unsigned short>(__a, __a_end, __err, __base);
1093 // Digit grouping checked
1094 __check_grouping(__grouping, __g, __g_end, __err);
1095 // EOF checked
1096 if (__b == __e)
1097 __err |= ios_base::eofbit;
1098 return __b;
1099}
1100
1101template <class _CharT, class _InputIterator>
1102_InputIterator
1103num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1104 ios_base& __iob,
1105 ios_base::iostate& __err,
1106 unsigned int& __v) const
1107{
1108 // Stage 1
1109 int __base = this->__get_base(__iob);
1110 // Stage 2
1111 char_type __atoms[26];
1112 char_type __thousands_sep;
1113 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001114 string __buf;
1115 __buf.resize(__buf.capacity());
1116 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117 char* __a_end = __a;
1118 unsigned __g[__num_get_base::__num_get_buf_sz];
1119 unsigned* __g_end = __g;
1120 unsigned __dc = 0;
1121 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001122 {
1123 if (__a_end - __a == __buf.size())
1124 {
1125 size_t __tmp = __buf.size();
1126 __buf.resize(2*__buf.size());
1127 __buf.resize(__buf.capacity());
1128 __a = &__buf[0];
1129 __a_end = __a + __tmp;
1130 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001131 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132 __thousands_sep, __grouping, __g, __g_end,
1133 __atoms))
1134 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001135 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1137 *__g_end++ = __dc;
1138 // Stage 3
1139 __v = __num_get_unsigned_integral<unsigned int>(__a, __a_end, __err, __base);
1140 // Digit grouping checked
1141 __check_grouping(__grouping, __g, __g_end, __err);
1142 // EOF checked
1143 if (__b == __e)
1144 __err |= ios_base::eofbit;
1145 return __b;
1146}
1147
1148template <class _CharT, class _InputIterator>
1149_InputIterator
1150num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1151 ios_base& __iob,
1152 ios_base::iostate& __err,
1153 unsigned long& __v) const
1154{
1155 // Stage 1
1156 int __base = this->__get_base(__iob);
1157 // Stage 2
1158 char_type __atoms[26];
1159 char_type __thousands_sep;
1160 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001161 string __buf;
1162 __buf.resize(__buf.capacity());
1163 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164 char* __a_end = __a;
1165 unsigned __g[__num_get_base::__num_get_buf_sz];
1166 unsigned* __g_end = __g;
1167 unsigned __dc = 0;
1168 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001169 {
1170 if (__a_end - __a == __buf.size())
1171 {
1172 size_t __tmp = __buf.size();
1173 __buf.resize(2*__buf.size());
1174 __buf.resize(__buf.capacity());
1175 __a = &__buf[0];
1176 __a_end = __a + __tmp;
1177 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001178 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179 __thousands_sep, __grouping, __g, __g_end,
1180 __atoms))
1181 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001182 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001183 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1184 *__g_end++ = __dc;
1185 // Stage 3
1186 __v = __num_get_unsigned_integral<unsigned long>(__a, __a_end, __err, __base);
1187 // Digit grouping checked
1188 __check_grouping(__grouping, __g, __g_end, __err);
1189 // EOF checked
1190 if (__b == __e)
1191 __err |= ios_base::eofbit;
1192 return __b;
1193}
1194
1195template <class _CharT, class _InputIterator>
1196_InputIterator
1197num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1198 ios_base& __iob,
1199 ios_base::iostate& __err,
1200 unsigned long long& __v) const
1201{
1202 // Stage 1
1203 int __base = this->__get_base(__iob);
1204 // Stage 2
1205 char_type __atoms[26];
1206 char_type __thousands_sep;
1207 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001208 string __buf;
1209 __buf.resize(__buf.capacity());
1210 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211 char* __a_end = __a;
1212 unsigned __g[__num_get_base::__num_get_buf_sz];
1213 unsigned* __g_end = __g;
1214 unsigned __dc = 0;
1215 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001216 {
1217 if (__a_end - __a == __buf.size())
1218 {
1219 size_t __tmp = __buf.size();
1220 __buf.resize(2*__buf.size());
1221 __buf.resize(__buf.capacity());
1222 __a = &__buf[0];
1223 __a_end = __a + __tmp;
1224 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001225 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226 __thousands_sep, __grouping, __g, __g_end,
1227 __atoms))
1228 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001229 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001230 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1231 *__g_end++ = __dc;
1232 // Stage 3
1233 __v = __num_get_unsigned_integral<unsigned long long>(__a, __a_end, __err, __base);
1234 // Digit grouping checked
1235 __check_grouping(__grouping, __g, __g_end, __err);
1236 // EOF checked
1237 if (__b == __e)
1238 __err |= ios_base::eofbit;
1239 return __b;
1240}
1241
Marshall Clowae385382013-11-05 14:28:52 +00001242// floating point
1243
Howard Hinnantc51e1022010-05-11 19:42:16 +00001244template <class _CharT, class _InputIterator>
Marshall Clowae385382013-11-05 14:28:52 +00001245template <class _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246_InputIterator
Marshall Clowae385382013-11-05 14:28:52 +00001247num_get<_CharT, _InputIterator>::__do_get_floating_point(iter_type __b, iter_type __e,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248 ios_base& __iob,
1249 ios_base::iostate& __err,
Marshall Clowae385382013-11-05 14:28:52 +00001250 _Fp& __v) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251{
1252 // Stage 1, nothing to do
1253 // Stage 2
1254 char_type __atoms[32];
1255 char_type __decimal_point;
1256 char_type __thousands_sep;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001257 string __grouping = this->__stage2_float_prep(__iob, __atoms,
1258 __decimal_point,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001259 __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001260 string __buf;
1261 __buf.resize(__buf.capacity());
1262 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001263 char* __a_end = __a;
1264 unsigned __g[__num_get_base::__num_get_buf_sz];
1265 unsigned* __g_end = __g;
1266 unsigned __dc = 0;
1267 bool __in_units = true;
1268 char __exp = 'E';
1269 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001270 {
1271 if (__a_end - __a == __buf.size())
1272 {
1273 size_t __tmp = __buf.size();
1274 __buf.resize(2*__buf.size());
1275 __buf.resize(__buf.capacity());
1276 __a = &__buf[0];
1277 __a_end = __a + __tmp;
1278 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001279 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1280 __decimal_point, __thousands_sep,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001281 __grouping, __g, __g_end,
1282 __dc, __atoms))
1283 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001284 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285 if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1286 *__g_end++ = __dc;
1287 // Stage 3
Marshall Clowae385382013-11-05 14:28:52 +00001288 __v = __num_get_float<_Fp>(__a, __a_end, __err);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001289 // Digit grouping checked
1290 __check_grouping(__grouping, __g, __g_end, __err);
1291 // EOF checked
1292 if (__b == __e)
1293 __err |= ios_base::eofbit;
1294 return __b;
1295}
1296
1297template <class _CharT, class _InputIterator>
1298_InputIterator
1299num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1300 ios_base& __iob,
1301 ios_base::iostate& __err,
Marshall Clowae385382013-11-05 14:28:52 +00001302 float& __v) const
1303{
1304 return this->__do_get_floating_point(__b, __e, __iob, __err, __v );
1305}
1306
1307template <class _CharT, class _InputIterator>
1308_InputIterator
1309num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1310 ios_base& __iob,
1311 ios_base::iostate& __err,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001312 double& __v) const
1313{
Marshall Clowae385382013-11-05 14:28:52 +00001314 return this->__do_get_floating_point(__b, __e, __iob, __err, __v );
Howard Hinnantc51e1022010-05-11 19:42:16 +00001315}
1316
1317template <class _CharT, class _InputIterator>
1318_InputIterator
1319num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1320 ios_base& __iob,
1321 ios_base::iostate& __err,
1322 long double& __v) const
1323{
Marshall Clowae385382013-11-05 14:28:52 +00001324 return this->__do_get_floating_point(__b, __e, __iob, __err, __v );
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325}
1326
1327template <class _CharT, class _InputIterator>
1328_InputIterator
1329num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1330 ios_base& __iob,
1331 ios_base::iostate& __err,
1332 void*& __v) const
1333{
1334 // Stage 1
1335 int __base = 16;
1336 // Stage 2
1337 char_type __atoms[26];
Howard Hinnant28b24882011-12-01 20:21:04 +00001338 char_type __thousands_sep = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001339 string __grouping;
1340 use_facet<ctype<_CharT> >(__iob.getloc()).widen(__num_get_base::__src,
1341 __num_get_base::__src + 26, __atoms);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001342 string __buf;
1343 __buf.resize(__buf.capacity());
1344 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001345 char* __a_end = __a;
1346 unsigned __g[__num_get_base::__num_get_buf_sz];
1347 unsigned* __g_end = __g;
1348 unsigned __dc = 0;
1349 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001350 {
1351 if (__a_end - __a == __buf.size())
1352 {
1353 size_t __tmp = __buf.size();
1354 __buf.resize(2*__buf.size());
1355 __buf.resize(__buf.capacity());
1356 __a = &__buf[0];
1357 __a_end = __a + __tmp;
1358 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001359 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1360 __thousands_sep, __grouping,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001361 __g, __g_end, __atoms))
1362 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001363 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001364 // Stage 3
1365 __a[sizeof(__a)-1] = 0;
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001366#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001367 if (sscanf_l(__a, _LIBCPP_GET_C_LOCALE, "%p", &__v) != 1)
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001368#else
1369 if (__sscanf_l(__a, __cloc(), "%p", &__v) != 1)
1370#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001371 __err = ios_base::failbit;
1372 // EOF checked
1373 if (__b == __e)
1374 __err |= ios_base::eofbit;
1375 return __b;
1376}
1377
Howard Hinnant8ea98242013-08-23 17:37:05 +00001378_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS num_get<char>)
1379_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS num_get<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001380
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001381struct _LIBCPP_TYPE_VIS __num_put_base
Howard Hinnantc51e1022010-05-11 19:42:16 +00001382{
1383protected:
1384 static void __format_int(char* __fmt, const char* __len, bool __signd,
1385 ios_base::fmtflags __flags);
1386 static bool __format_float(char* __fmt, const char* __len,
1387 ios_base::fmtflags __flags);
1388 static char* __identify_padding(char* __nb, char* __ne,
1389 const ios_base& __iob);
1390};
1391
1392template <class _CharT>
1393struct __num_put
1394 : protected __num_put_base
1395{
1396 static void __widen_and_group_int(char* __nb, char* __np, char* __ne,
1397 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1398 const locale& __loc);
1399 static void __widen_and_group_float(char* __nb, char* __np, char* __ne,
1400 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1401 const locale& __loc);
1402};
1403
1404template <class _CharT>
1405void
1406__num_put<_CharT>::__widen_and_group_int(char* __nb, char* __np, char* __ne,
1407 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1408 const locale& __loc)
1409{
1410 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1411 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1412 string __grouping = __npt.grouping();
1413 if (__grouping.empty())
1414 {
1415 __ct.widen(__nb, __ne, __ob);
1416 __oe = __ob + (__ne - __nb);
1417 }
1418 else
1419 {
1420 __oe = __ob;
1421 char* __nf = __nb;
1422 if (*__nf == '-' || *__nf == '+')
1423 *__oe++ = __ct.widen(*__nf++);
1424 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1425 __nf[1] == 'X'))
1426 {
1427 *__oe++ = __ct.widen(*__nf++);
1428 *__oe++ = __ct.widen(*__nf++);
1429 }
1430 reverse(__nf, __ne);
1431 _CharT __thousands_sep = __npt.thousands_sep();
1432 unsigned __dc = 0;
1433 unsigned __dg = 0;
1434 for (char* __p = __nf; __p < __ne; ++__p)
1435 {
1436 if (static_cast<unsigned>(__grouping[__dg]) > 0 &&
1437 __dc == static_cast<unsigned>(__grouping[__dg]))
1438 {
1439 *__oe++ = __thousands_sep;
1440 __dc = 0;
1441 if (__dg < __grouping.size()-1)
1442 ++__dg;
1443 }
1444 *__oe++ = __ct.widen(*__p);
1445 ++__dc;
1446 }
1447 reverse(__ob + (__nf - __nb), __oe);
1448 }
1449 if (__np == __ne)
1450 __op = __oe;
1451 else
1452 __op = __ob + (__np - __nb);
1453}
1454
1455template <class _CharT>
1456void
1457__num_put<_CharT>::__widen_and_group_float(char* __nb, char* __np, char* __ne,
1458 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1459 const locale& __loc)
1460{
1461 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1462 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1463 string __grouping = __npt.grouping();
1464 __oe = __ob;
1465 char* __nf = __nb;
1466 if (*__nf == '-' || *__nf == '+')
1467 *__oe++ = __ct.widen(*__nf++);
1468 char* __ns;
1469 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1470 __nf[1] == 'X'))
1471 {
1472 *__oe++ = __ct.widen(*__nf++);
1473 *__oe++ = __ct.widen(*__nf++);
1474 for (__ns = __nf; __ns < __ne; ++__ns)
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001475 if (!isxdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001476 break;
1477 }
1478 else
1479 {
1480 for (__ns = __nf; __ns < __ne; ++__ns)
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001481 if (!isdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001482 break;
1483 }
1484 if (__grouping.empty())
1485 {
1486 __ct.widen(__nf, __ns, __oe);
1487 __oe += __ns - __nf;
1488 }
1489 else
1490 {
1491 reverse(__nf, __ns);
1492 _CharT __thousands_sep = __npt.thousands_sep();
1493 unsigned __dc = 0;
1494 unsigned __dg = 0;
1495 for (char* __p = __nf; __p < __ns; ++__p)
1496 {
1497 if (__grouping[__dg] > 0 && __dc == static_cast<unsigned>(__grouping[__dg]))
1498 {
1499 *__oe++ = __thousands_sep;
1500 __dc = 0;
1501 if (__dg < __grouping.size()-1)
1502 ++__dg;
1503 }
1504 *__oe++ = __ct.widen(*__p);
1505 ++__dc;
1506 }
1507 reverse(__ob + (__nf - __nb), __oe);
1508 }
1509 for (__nf = __ns; __nf < __ne; ++__nf)
1510 {
1511 if (*__nf == '.')
1512 {
1513 *__oe++ = __npt.decimal_point();
1514 ++__nf;
1515 break;
1516 }
1517 else
1518 *__oe++ = __ct.widen(*__nf);
1519 }
1520 __ct.widen(__nf, __ne, __oe);
1521 __oe += __ne - __nf;
1522 if (__np == __ne)
1523 __op = __oe;
1524 else
1525 __op = __ob + (__np - __nb);
1526}
1527
Howard Hinnant8ea98242013-08-23 17:37:05 +00001528_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_TYPE_VIS __num_put<char>)
1529_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_TYPE_VIS __num_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001530
1531template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001532class _LIBCPP_TYPE_VIS_ONLY num_put
Howard Hinnantc51e1022010-05-11 19:42:16 +00001533 : public locale::facet,
1534 private __num_put<_CharT>
1535{
1536public:
1537 typedef _CharT char_type;
1538 typedef _OutputIterator iter_type;
1539
1540 _LIBCPP_ALWAYS_INLINE
1541 explicit num_put(size_t __refs = 0)
1542 : locale::facet(__refs) {}
1543
1544 _LIBCPP_ALWAYS_INLINE
1545 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1546 bool __v) const
1547 {
1548 return do_put(__s, __iob, __fl, __v);
1549 }
1550
1551 _LIBCPP_ALWAYS_INLINE
1552 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1553 long __v) const
1554 {
1555 return do_put(__s, __iob, __fl, __v);
1556 }
1557
1558 _LIBCPP_ALWAYS_INLINE
1559 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1560 long long __v) const
1561 {
1562 return do_put(__s, __iob, __fl, __v);
1563 }
1564
1565 _LIBCPP_ALWAYS_INLINE
1566 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1567 unsigned long __v) const
1568 {
1569 return do_put(__s, __iob, __fl, __v);
1570 }
1571
1572 _LIBCPP_ALWAYS_INLINE
1573 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1574 unsigned long long __v) const
1575 {
1576 return do_put(__s, __iob, __fl, __v);
1577 }
1578
1579 _LIBCPP_ALWAYS_INLINE
1580 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1581 double __v) const
1582 {
1583 return do_put(__s, __iob, __fl, __v);
1584 }
1585
1586 _LIBCPP_ALWAYS_INLINE
1587 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1588 long double __v) const
1589 {
1590 return do_put(__s, __iob, __fl, __v);
1591 }
1592
1593 _LIBCPP_ALWAYS_INLINE
1594 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1595 const void* __v) const
1596 {
1597 return do_put(__s, __iob, __fl, __v);
1598 }
1599
1600 static locale::id id;
1601
1602protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00001603 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604 ~num_put() {}
1605
1606 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1607 bool __v) const;
1608 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1609 long __v) const;
1610 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1611 long long __v) const;
1612 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1613 unsigned long) const;
1614 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1615 unsigned long long) const;
1616 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1617 double __v) const;
1618 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1619 long double __v) const;
1620 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1621 const void* __v) const;
1622};
1623
1624template <class _CharT, class _OutputIterator>
1625locale::id
1626num_put<_CharT, _OutputIterator>::id;
1627
1628template <class _CharT, class _OutputIterator>
1629_LIBCPP_HIDDEN
1630_OutputIterator
1631__pad_and_output(_OutputIterator __s,
1632 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1633 ios_base& __iob, _CharT __fl)
1634{
1635 streamsize __sz = __oe - __ob;
1636 streamsize __ns = __iob.width();
1637 if (__ns > __sz)
1638 __ns -= __sz;
1639 else
1640 __ns = 0;
1641 for (;__ob < __op; ++__ob, ++__s)
1642 *__s = *__ob;
1643 for (; __ns; --__ns, ++__s)
1644 *__s = __fl;
1645 for (; __ob < __oe; ++__ob, ++__s)
1646 *__s = *__ob;
1647 __iob.width(0);
1648 return __s;
1649}
1650
Howard Hinnant48fd5d52012-11-14 21:17:15 +00001651#if !defined(__APPLE__) || \
1652 (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED > __MAC_10_8) || \
1653 (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_6_0)
1654
Howard Hinnant97955172012-09-19 19:14:15 +00001655template <class _CharT, class _Traits>
1656_LIBCPP_HIDDEN
1657ostreambuf_iterator<_CharT, _Traits>
1658__pad_and_output(ostreambuf_iterator<_CharT, _Traits> __s,
1659 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1660 ios_base& __iob, _CharT __fl)
1661{
1662 if (__s.__sbuf_ == nullptr)
1663 return __s;
1664 streamsize __sz = __oe - __ob;
1665 streamsize __ns = __iob.width();
1666 if (__ns > __sz)
1667 __ns -= __sz;
1668 else
1669 __ns = 0;
1670 streamsize __np = __op - __ob;
1671 if (__np > 0)
1672 {
1673 if (__s.__sbuf_->sputn(__ob, __np) != __np)
1674 {
1675 __s.__sbuf_ = nullptr;
1676 return __s;
1677 }
1678 }
1679 if (__ns > 0)
1680 {
1681 basic_string<_CharT, _Traits> __sp(__ns, __fl);
1682 if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns)
1683 {
1684 __s.__sbuf_ = nullptr;
1685 return __s;
1686 }
1687 }
1688 __np = __oe - __op;
1689 if (__np > 0)
1690 {
1691 if (__s.__sbuf_->sputn(__op, __np) != __np)
1692 {
1693 __s.__sbuf_ = nullptr;
1694 return __s;
1695 }
1696 }
1697 __iob.width(0);
1698 return __s;
1699}
1700
Howard Hinnant48fd5d52012-11-14 21:17:15 +00001701#endif
1702
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703template <class _CharT, class _OutputIterator>
1704_OutputIterator
1705num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1706 char_type __fl, bool __v) const
1707{
1708 if ((__iob.flags() & ios_base::boolalpha) == 0)
1709 return do_put(__s, __iob, __fl, (unsigned long)__v);
1710 const numpunct<char_type>& __np = use_facet<numpunct<char_type> >(__iob.getloc());
1711 typedef typename numpunct<char_type>::string_type string_type;
Howard Hinnant8ea98242013-08-23 17:37:05 +00001712#if _LIBCPP_DEBUG_LEVEL >= 2
1713 string_type __tmp(__v ? __np.truename() : __np.falsename());
1714 string_type __nm = _VSTD::move(__tmp);
1715#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001716 string_type __nm = __v ? __np.truename() : __np.falsename();
Howard Hinnant8ea98242013-08-23 17:37:05 +00001717#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718 for (typename string_type::iterator __i = __nm.begin(); __i != __nm.end(); ++__i, ++__s)
1719 *__s = *__i;
1720 return __s;
1721}
1722
1723template <class _CharT, class _OutputIterator>
1724_OutputIterator
1725num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1726 char_type __fl, long __v) const
1727{
1728 // Stage 1 - Get number in narrow char
1729 char __fmt[6] = {'%', 0};
1730 const char* __len = "l";
1731 this->__format_int(__fmt+1, __len, true, __iob.flags());
1732 const unsigned __nbuf = (numeric_limits<long>::digits / 3)
1733 + ((numeric_limits<long>::digits % 3) != 0)
1734 + 1;
1735 char __nar[__nbuf];
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001736#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001737 int __nc = snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001738#else
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001739 int __nc = __snprintf_l(__nar, sizeof(__nar), __cloc(), __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001740#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741 char* __ne = __nar + __nc;
1742 char* __np = this->__identify_padding(__nar, __ne, __iob);
1743 // Stage 2 - Widen __nar while adding thousands separators
1744 char_type __o[2*(__nbuf-1) - 1];
1745 char_type* __op; // pad here
1746 char_type* __oe; // end of output
1747 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1748 // [__o, __oe) contains thousands_sep'd wide number
1749 // Stage 3 & 4
1750 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1751}
1752
1753template <class _CharT, class _OutputIterator>
1754_OutputIterator
1755num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1756 char_type __fl, long long __v) const
1757{
1758 // Stage 1 - Get number in narrow char
1759 char __fmt[8] = {'%', 0};
1760 const char* __len = "ll";
1761 this->__format_int(__fmt+1, __len, true, __iob.flags());
1762 const unsigned __nbuf = (numeric_limits<long long>::digits / 3)
1763 + ((numeric_limits<long long>::digits % 3) != 0)
1764 + 1;
1765 char __nar[__nbuf];
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001766#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001767 int __nc = snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001768#else
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001769 int __nc = __snprintf_l(__nar, sizeof(__nar), __cloc(), __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001770#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001771 char* __ne = __nar + __nc;
1772 char* __np = this->__identify_padding(__nar, __ne, __iob);
1773 // Stage 2 - Widen __nar while adding thousands separators
1774 char_type __o[2*(__nbuf-1) - 1];
1775 char_type* __op; // pad here
1776 char_type* __oe; // end of output
1777 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1778 // [__o, __oe) contains thousands_sep'd wide number
1779 // Stage 3 & 4
1780 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1781}
1782
1783template <class _CharT, class _OutputIterator>
1784_OutputIterator
1785num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1786 char_type __fl, unsigned long __v) const
1787{
1788 // Stage 1 - Get number in narrow char
1789 char __fmt[6] = {'%', 0};
1790 const char* __len = "l";
1791 this->__format_int(__fmt+1, __len, false, __iob.flags());
1792 const unsigned __nbuf = (numeric_limits<unsigned long>::digits / 3)
1793 + ((numeric_limits<unsigned long>::digits % 3) != 0)
1794 + 1;
1795 char __nar[__nbuf];
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001796#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001797 int __nc = snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001798#else
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001799 int __nc = __snprintf_l(__nar, sizeof(__nar), __cloc(), __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001800#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801 char* __ne = __nar + __nc;
1802 char* __np = this->__identify_padding(__nar, __ne, __iob);
1803 // Stage 2 - Widen __nar while adding thousands separators
1804 char_type __o[2*(__nbuf-1) - 1];
1805 char_type* __op; // pad here
1806 char_type* __oe; // end of output
1807 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1808 // [__o, __oe) contains thousands_sep'd wide number
1809 // Stage 3 & 4
1810 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1811}
1812
1813template <class _CharT, class _OutputIterator>
1814_OutputIterator
1815num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1816 char_type __fl, unsigned long long __v) const
1817{
1818 // Stage 1 - Get number in narrow char
1819 char __fmt[8] = {'%', 0};
1820 const char* __len = "ll";
1821 this->__format_int(__fmt+1, __len, false, __iob.flags());
1822 const unsigned __nbuf = (numeric_limits<unsigned long long>::digits / 3)
1823 + ((numeric_limits<unsigned long long>::digits % 3) != 0)
1824 + 1;
1825 char __nar[__nbuf];
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001826#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001827 int __nc = snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001828#else
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001829 int __nc = __snprintf_l(__nar, sizeof(__nar), __cloc(), __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001830#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001831 char* __ne = __nar + __nc;
1832 char* __np = this->__identify_padding(__nar, __ne, __iob);
1833 // Stage 2 - Widen __nar while adding thousands separators
1834 char_type __o[2*(__nbuf-1) - 1];
1835 char_type* __op; // pad here
1836 char_type* __oe; // end of output
1837 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1838 // [__o, __oe) contains thousands_sep'd wide number
1839 // Stage 3 & 4
1840 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1841}
1842
1843template <class _CharT, class _OutputIterator>
1844_OutputIterator
1845num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1846 char_type __fl, double __v) const
1847{
1848 // Stage 1 - Get number in narrow char
1849 char __fmt[8] = {'%', 0};
1850 const char* __len = "";
1851 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1852 const unsigned __nbuf = 30;
1853 char __nar[__nbuf];
1854 char* __nb = __nar;
1855 int __nc;
1856 if (__specify_precision)
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001857#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001858 __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
Howard Hinnant155c2af2010-05-24 17:49:41 +00001859 (int)__iob.precision(), __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001860#else
1861 __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt,
1862 (int)__iob.precision(), __v);
1863#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001864 else
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001865#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001866 __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001867#else
1868 __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt, __v);
1869#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001870 unique_ptr<char, void(*)(void*)> __nbh(0, free);
1871 if (__nc > static_cast<int>(__nbuf-1))
1872 {
1873 if (__specify_precision)
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001874#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001875 __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001876#else
1877 __nc = __asprintf_l(&__nb, __cloc(), __fmt,
David Chisnall1d581062011-09-21 08:39:44 +00001878 (int)__iob.precision(), __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001879#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001880 else
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001881#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001882 __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001883#else
David Chisnall1d581062011-09-21 08:39:44 +00001884 __nc = __asprintf_l(&__nb, __cloc(), __fmt, (int)__iob.precision(), __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001885#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001886 if (__nb == 0)
1887 __throw_bad_alloc();
1888 __nbh.reset(__nb);
1889 }
1890 char* __ne = __nb + __nc;
1891 char* __np = this->__identify_padding(__nb, __ne, __iob);
1892 // Stage 2 - Widen __nar while adding thousands separators
1893 char_type __o[2*(__nbuf-1) - 1];
1894 char_type* __ob = __o;
1895 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1896 if (__nb != __nar)
1897 {
Howard Hinnant28b24882011-12-01 20:21:04 +00001898 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001899 if (__ob == 0)
1900 __throw_bad_alloc();
1901 __obh.reset(__ob);
1902 }
1903 char_type* __op; // pad here
1904 char_type* __oe; // end of output
1905 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1906 // [__o, __oe) contains thousands_sep'd wide number
1907 // Stage 3 & 4
1908 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1909 return __s;
1910}
1911
1912template <class _CharT, class _OutputIterator>
1913_OutputIterator
1914num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1915 char_type __fl, long double __v) const
1916{
1917 // Stage 1 - Get number in narrow char
1918 char __fmt[8] = {'%', 0};
1919 const char* __len = "L";
1920 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1921 const unsigned __nbuf = 30;
1922 char __nar[__nbuf];
1923 char* __nb = __nar;
1924 int __nc;
1925 if (__specify_precision)
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001926#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001927 __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
Howard Hinnant155c2af2010-05-24 17:49:41 +00001928 (int)__iob.precision(), __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001929#else
1930 __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt,
1931 (int)__iob.precision(), __v);
1932#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001933 else
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001934#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001935 __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001936#else
1937 __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt, __v);
1938#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001939 unique_ptr<char, void(*)(void*)> __nbh(0, free);
1940 if (__nc > static_cast<int>(__nbuf-1))
1941 {
1942 if (__specify_precision)
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001943#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001944 __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001945#else
1946 __nc = __asprintf_l(&__nb, __cloc(), __fmt,
David Chisnall1d581062011-09-21 08:39:44 +00001947 (int)__iob.precision(), __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001948#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001949 else
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001950#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001951 __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001952#else
David Chisnall1d581062011-09-21 08:39:44 +00001953 __nc = __asprintf_l(&__nb, __cloc(), __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001954#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955 if (__nb == 0)
1956 __throw_bad_alloc();
1957 __nbh.reset(__nb);
1958 }
1959 char* __ne = __nb + __nc;
1960 char* __np = this->__identify_padding(__nb, __ne, __iob);
1961 // Stage 2 - Widen __nar while adding thousands separators
1962 char_type __o[2*(__nbuf-1) - 1];
1963 char_type* __ob = __o;
1964 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1965 if (__nb != __nar)
1966 {
Howard Hinnant28b24882011-12-01 20:21:04 +00001967 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001968 if (__ob == 0)
1969 __throw_bad_alloc();
1970 __obh.reset(__ob);
1971 }
1972 char_type* __op; // pad here
1973 char_type* __oe; // end of output
1974 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1975 // [__o, __oe) contains thousands_sep'd wide number
1976 // Stage 3 & 4
1977 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1978 return __s;
1979}
1980
1981template <class _CharT, class _OutputIterator>
1982_OutputIterator
1983num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1984 char_type __fl, const void* __v) const
1985{
1986 // Stage 1 - Get pointer in narrow char
1987 char __fmt[6] = "%p";
1988 const unsigned __nbuf = 20;
1989 char __nar[__nbuf];
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001990#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001991 int __nc = snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001992#else
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001993 int __nc = __snprintf_l(__nar, sizeof(__nar), __cloc(), __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001994#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001995 char* __ne = __nar + __nc;
1996 char* __np = this->__identify_padding(__nar, __ne, __iob);
1997 // Stage 2 - Widen __nar
1998 char_type __o[2*(__nbuf-1) - 1];
1999 char_type* __op; // pad here
2000 char_type* __oe; // end of output
2001 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2002 __ct.widen(__nar, __ne, __o);
2003 __oe = __o + (__ne - __nar);
2004 if (__np == __ne)
2005 __op = __oe;
2006 else
2007 __op = __o + (__np - __nar);
2008 // [__o, __oe) contains wide number
2009 // Stage 3 & 4
2010 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
2011}
2012
Howard Hinnant8ea98242013-08-23 17:37:05 +00002013_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS num_put<char>)
2014_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS num_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015
2016template <class _CharT, class _InputIterator>
2017_LIBCPP_HIDDEN
2018int
2019__get_up_to_n_digits(_InputIterator& __b, _InputIterator __e,
2020 ios_base::iostate& __err, const ctype<_CharT>& __ct, int __n)
2021{
2022 // Precondition: __n >= 1
2023 if (__b == __e)
2024 {
2025 __err |= ios_base::eofbit | ios_base::failbit;
2026 return 0;
2027 }
2028 // get first digit
2029 _CharT __c = *__b;
2030 if (!__ct.is(ctype_base::digit, __c))
2031 {
2032 __err |= ios_base::failbit;
2033 return 0;
2034 }
2035 int __r = __ct.narrow(__c, 0) - '0';
2036 for (++__b, --__n; __b != __e && __n > 0; ++__b, --__n)
2037 {
2038 // get next digit
2039 __c = *__b;
2040 if (!__ct.is(ctype_base::digit, __c))
2041 return __r;
2042 __r = __r * 10 + __ct.narrow(__c, 0) - '0';
2043 }
2044 if (__b == __e)
2045 __err |= ios_base::eofbit;
2046 return __r;
2047}
2048
Howard Hinnant8331b762013-03-06 23:30:19 +00002049class _LIBCPP_TYPE_VIS time_base
Howard Hinnantc51e1022010-05-11 19:42:16 +00002050{
2051public:
2052 enum dateorder {no_order, dmy, mdy, ymd, ydm};
2053};
2054
2055template <class _CharT>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002056class _LIBCPP_TYPE_VIS __time_get_c_storage
Howard Hinnantc51e1022010-05-11 19:42:16 +00002057{
2058protected:
2059 typedef basic_string<_CharT> string_type;
2060
2061 virtual const string_type* __weeks() const;
2062 virtual const string_type* __months() const;
2063 virtual const string_type* __am_pm() const;
2064 virtual const string_type& __c() const;
2065 virtual const string_type& __r() const;
2066 virtual const string_type& __x() const;
2067 virtual const string_type& __X() const;
2068};
2069
2070template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002071class _LIBCPP_TYPE_VIS_ONLY time_get
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072 : public locale::facet,
2073 public time_base,
2074 private __time_get_c_storage<_CharT>
2075{
2076public:
2077 typedef _CharT char_type;
2078 typedef _InputIterator iter_type;
2079 typedef time_base::dateorder dateorder;
2080 typedef basic_string<char_type> string_type;
2081
2082 _LIBCPP_ALWAYS_INLINE
2083 explicit time_get(size_t __refs = 0)
2084 : locale::facet(__refs) {}
2085
2086 _LIBCPP_ALWAYS_INLINE
2087 dateorder date_order() const
2088 {
2089 return this->do_date_order();
2090 }
2091
2092 _LIBCPP_ALWAYS_INLINE
2093 iter_type get_time(iter_type __b, iter_type __e, ios_base& __iob,
2094 ios_base::iostate& __err, tm* __tm) const
2095 {
2096 return do_get_time(__b, __e, __iob, __err, __tm);
2097 }
2098
2099 _LIBCPP_ALWAYS_INLINE
2100 iter_type get_date(iter_type __b, iter_type __e, ios_base& __iob,
2101 ios_base::iostate& __err, tm* __tm) const
2102 {
2103 return do_get_date(__b, __e, __iob, __err, __tm);
2104 }
2105
2106 _LIBCPP_ALWAYS_INLINE
2107 iter_type get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
2108 ios_base::iostate& __err, tm* __tm) const
2109 {
2110 return do_get_weekday(__b, __e, __iob, __err, __tm);
2111 }
2112
2113 _LIBCPP_ALWAYS_INLINE
2114 iter_type get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
2115 ios_base::iostate& __err, tm* __tm) const
2116 {
2117 return do_get_monthname(__b, __e, __iob, __err, __tm);
2118 }
2119
2120 _LIBCPP_ALWAYS_INLINE
2121 iter_type get_year(iter_type __b, iter_type __e, ios_base& __iob,
2122 ios_base::iostate& __err, tm* __tm) const
2123 {
2124 return do_get_year(__b, __e, __iob, __err, __tm);
2125 }
2126
2127 _LIBCPP_ALWAYS_INLINE
2128 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
2129 ios_base::iostate& __err, tm *__tm,
2130 char __fmt, char __mod = 0) const
2131 {
2132 return do_get(__b, __e, __iob, __err, __tm, __fmt, __mod);
2133 }
2134
2135 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
2136 ios_base::iostate& __err, tm* __tm,
2137 const char_type* __fmtb, const char_type* __fmte) const;
2138
2139 static locale::id id;
2140
2141protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002142 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002143 ~time_get() {}
2144
2145 virtual dateorder do_date_order() const;
2146 virtual iter_type do_get_time(iter_type __b, iter_type __e, ios_base& __iob,
2147 ios_base::iostate& __err, tm* __tm) const;
2148 virtual iter_type do_get_date(iter_type __b, iter_type __e, ios_base& __iob,
2149 ios_base::iostate& __err, tm* __tm) const;
2150 virtual iter_type do_get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
2151 ios_base::iostate& __err, tm* __tm) const;
2152 virtual iter_type do_get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
2153 ios_base::iostate& __err, tm* __tm) const;
2154 virtual iter_type do_get_year(iter_type __b, iter_type __e, ios_base& __iob,
2155 ios_base::iostate& __err, tm* __tm) const;
2156 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
2157 ios_base::iostate& __err, tm* __tm,
2158 char __fmt, char __mod) const;
2159private:
2160 void __get_white_space(iter_type& __b, iter_type __e,
2161 ios_base::iostate& __err, const ctype<char_type>& __ct) const;
2162 void __get_percent(iter_type& __b, iter_type __e, ios_base::iostate& __err,
2163 const ctype<char_type>& __ct) const;
2164
2165 void __get_weekdayname(int& __m,
2166 iter_type& __b, iter_type __e,
2167 ios_base::iostate& __err,
2168 const ctype<char_type>& __ct) const;
2169 void __get_monthname(int& __m,
2170 iter_type& __b, iter_type __e,
2171 ios_base::iostate& __err,
2172 const ctype<char_type>& __ct) const;
2173 void __get_day(int& __d,
2174 iter_type& __b, iter_type __e,
2175 ios_base::iostate& __err,
2176 const ctype<char_type>& __ct) const;
2177 void __get_month(int& __m,
2178 iter_type& __b, iter_type __e,
2179 ios_base::iostate& __err,
2180 const ctype<char_type>& __ct) const;
2181 void __get_year(int& __y,
2182 iter_type& __b, iter_type __e,
2183 ios_base::iostate& __err,
2184 const ctype<char_type>& __ct) const;
2185 void __get_year4(int& __y,
2186 iter_type& __b, iter_type __e,
2187 ios_base::iostate& __err,
2188 const ctype<char_type>& __ct) const;
2189 void __get_hour(int& __d,
2190 iter_type& __b, iter_type __e,
2191 ios_base::iostate& __err,
2192 const ctype<char_type>& __ct) const;
2193 void __get_12_hour(int& __h,
2194 iter_type& __b, iter_type __e,
2195 ios_base::iostate& __err,
2196 const ctype<char_type>& __ct) const;
2197 void __get_am_pm(int& __h,
2198 iter_type& __b, iter_type __e,
2199 ios_base::iostate& __err,
2200 const ctype<char_type>& __ct) const;
2201 void __get_minute(int& __m,
2202 iter_type& __b, iter_type __e,
2203 ios_base::iostate& __err,
2204 const ctype<char_type>& __ct) const;
2205 void __get_second(int& __s,
2206 iter_type& __b, iter_type __e,
2207 ios_base::iostate& __err,
2208 const ctype<char_type>& __ct) const;
2209 void __get_weekday(int& __w,
2210 iter_type& __b, iter_type __e,
2211 ios_base::iostate& __err,
2212 const ctype<char_type>& __ct) const;
2213 void __get_day_year_num(int& __w,
2214 iter_type& __b, iter_type __e,
2215 ios_base::iostate& __err,
2216 const ctype<char_type>& __ct) const;
2217};
2218
2219template <class _CharT, class _InputIterator>
2220locale::id
2221time_get<_CharT, _InputIterator>::id;
2222
2223// time_get primatives
2224
2225template <class _CharT, class _InputIterator>
2226void
2227time_get<_CharT, _InputIterator>::__get_weekdayname(int& __w,
2228 iter_type& __b, iter_type __e,
2229 ios_base::iostate& __err,
2230 const ctype<char_type>& __ct) const
2231{
2232 // Note: ignoring case comes from the POSIX strptime spec
2233 const string_type* __wk = this->__weeks();
Howard Hinnant28b24882011-12-01 20:21:04 +00002234 ptrdiff_t __i = __scan_keyword(__b, __e, __wk, __wk+14, __ct, __err, false) - __wk;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002235 if (__i < 14)
2236 __w = __i % 7;
2237}
2238
2239template <class _CharT, class _InputIterator>
2240void
2241time_get<_CharT, _InputIterator>::__get_monthname(int& __m,
2242 iter_type& __b, iter_type __e,
2243 ios_base::iostate& __err,
2244 const ctype<char_type>& __ct) const
2245{
2246 // Note: ignoring case comes from the POSIX strptime spec
2247 const string_type* __month = this->__months();
Howard Hinnant28b24882011-12-01 20:21:04 +00002248 ptrdiff_t __i = __scan_keyword(__b, __e, __month, __month+24, __ct, __err, false) - __month;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002249 if (__i < 24)
2250 __m = __i % 12;
2251}
2252
2253template <class _CharT, class _InputIterator>
2254void
2255time_get<_CharT, _InputIterator>::__get_day(int& __d,
2256 iter_type& __b, iter_type __e,
2257 ios_base::iostate& __err,
2258 const ctype<char_type>& __ct) const
2259{
2260 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2261 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 31)
2262 __d = __t;
2263 else
2264 __err |= ios_base::failbit;
2265}
2266
2267template <class _CharT, class _InputIterator>
2268void
2269time_get<_CharT, _InputIterator>::__get_month(int& __m,
2270 iter_type& __b, iter_type __e,
2271 ios_base::iostate& __err,
2272 const ctype<char_type>& __ct) const
2273{
2274 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2) - 1;
2275 if (!(__err & ios_base::failbit) && __t <= 11)
2276 __m = __t;
2277 else
2278 __err |= ios_base::failbit;
2279}
2280
2281template <class _CharT, class _InputIterator>
2282void
2283time_get<_CharT, _InputIterator>::__get_year(int& __y,
2284 iter_type& __b, iter_type __e,
2285 ios_base::iostate& __err,
2286 const ctype<char_type>& __ct) const
2287{
2288 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
2289 if (!(__err & ios_base::failbit))
2290 {
2291 if (__t < 69)
2292 __t += 2000;
2293 else if (69 <= __t && __t <= 99)
2294 __t += 1900;
2295 __y = __t - 1900;
2296 }
2297}
2298
2299template <class _CharT, class _InputIterator>
2300void
2301time_get<_CharT, _InputIterator>::__get_year4(int& __y,
2302 iter_type& __b, iter_type __e,
2303 ios_base::iostate& __err,
2304 const ctype<char_type>& __ct) const
2305{
2306 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
2307 if (!(__err & ios_base::failbit))
2308 __y = __t - 1900;
2309}
2310
2311template <class _CharT, class _InputIterator>
2312void
2313time_get<_CharT, _InputIterator>::__get_hour(int& __h,
2314 iter_type& __b, iter_type __e,
2315 ios_base::iostate& __err,
2316 const ctype<char_type>& __ct) const
2317{
2318 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2319 if (!(__err & ios_base::failbit) && __t <= 23)
2320 __h = __t;
2321 else
2322 __err |= ios_base::failbit;
2323}
2324
2325template <class _CharT, class _InputIterator>
2326void
2327time_get<_CharT, _InputIterator>::__get_12_hour(int& __h,
2328 iter_type& __b, iter_type __e,
2329 ios_base::iostate& __err,
2330 const ctype<char_type>& __ct) const
2331{
2332 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2333 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 12)
2334 __h = __t;
2335 else
2336 __err |= ios_base::failbit;
2337}
2338
2339template <class _CharT, class _InputIterator>
2340void
2341time_get<_CharT, _InputIterator>::__get_minute(int& __m,
2342 iter_type& __b, iter_type __e,
2343 ios_base::iostate& __err,
2344 const ctype<char_type>& __ct) const
2345{
2346 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2347 if (!(__err & ios_base::failbit) && __t <= 59)
2348 __m = __t;
2349 else
2350 __err |= ios_base::failbit;
2351}
2352
2353template <class _CharT, class _InputIterator>
2354void
2355time_get<_CharT, _InputIterator>::__get_second(int& __s,
2356 iter_type& __b, iter_type __e,
2357 ios_base::iostate& __err,
2358 const ctype<char_type>& __ct) const
2359{
2360 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2361 if (!(__err & ios_base::failbit) && __t <= 60)
2362 __s = __t;
2363 else
2364 __err |= ios_base::failbit;
2365}
2366
2367template <class _CharT, class _InputIterator>
2368void
2369time_get<_CharT, _InputIterator>::__get_weekday(int& __w,
2370 iter_type& __b, iter_type __e,
2371 ios_base::iostate& __err,
2372 const ctype<char_type>& __ct) const
2373{
2374 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 1);
2375 if (!(__err & ios_base::failbit) && __t <= 6)
2376 __w = __t;
2377 else
2378 __err |= ios_base::failbit;
2379}
2380
2381template <class _CharT, class _InputIterator>
2382void
2383time_get<_CharT, _InputIterator>::__get_day_year_num(int& __d,
2384 iter_type& __b, iter_type __e,
2385 ios_base::iostate& __err,
2386 const ctype<char_type>& __ct) const
2387{
2388 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 3);
2389 if (!(__err & ios_base::failbit) && __t <= 365)
2390 __d = __t;
2391 else
2392 __err |= ios_base::failbit;
2393}
2394
2395template <class _CharT, class _InputIterator>
2396void
2397time_get<_CharT, _InputIterator>::__get_white_space(iter_type& __b, iter_type __e,
2398 ios_base::iostate& __err,
2399 const ctype<char_type>& __ct) const
2400{
2401 for (; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2402 ;
2403 if (__b == __e)
2404 __err |= ios_base::eofbit;
2405}
2406
2407template <class _CharT, class _InputIterator>
2408void
2409time_get<_CharT, _InputIterator>::__get_am_pm(int& __h,
2410 iter_type& __b, iter_type __e,
2411 ios_base::iostate& __err,
2412 const ctype<char_type>& __ct) const
2413{
2414 const string_type* __ap = this->__am_pm();
2415 if (__ap[0].size() + __ap[1].size() == 0)
2416 {
2417 __err |= ios_base::failbit;
2418 return;
2419 }
Howard Hinnant28b24882011-12-01 20:21:04 +00002420 ptrdiff_t __i = __scan_keyword(__b, __e, __ap, __ap+2, __ct, __err, false) - __ap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002421 if (__i == 0 && __h == 12)
2422 __h = 0;
2423 else if (__i == 1 && __h < 12)
2424 __h += 12;
2425}
2426
2427template <class _CharT, class _InputIterator>
2428void
2429time_get<_CharT, _InputIterator>::__get_percent(iter_type& __b, iter_type __e,
2430 ios_base::iostate& __err,
2431 const ctype<char_type>& __ct) const
2432{
2433 if (__b == __e)
2434 {
2435 __err |= ios_base::eofbit | ios_base::failbit;
2436 return;
2437 }
2438 if (__ct.narrow(*__b, 0) != '%')
2439 __err |= ios_base::failbit;
2440 else if(++__b == __e)
2441 __err |= ios_base::eofbit;
2442}
2443
2444// time_get end primatives
2445
2446template <class _CharT, class _InputIterator>
2447_InputIterator
2448time_get<_CharT, _InputIterator>::get(iter_type __b, iter_type __e,
2449 ios_base& __iob,
2450 ios_base::iostate& __err, tm* __tm,
2451 const char_type* __fmtb, const char_type* __fmte) const
2452{
2453 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2454 __err = ios_base::goodbit;
2455 while (__fmtb != __fmte && __err == ios_base::goodbit)
2456 {
2457 if (__b == __e)
2458 {
2459 __err = ios_base::failbit;
2460 break;
2461 }
2462 if (__ct.narrow(*__fmtb, 0) == '%')
2463 {
2464 if (++__fmtb == __fmte)
2465 {
2466 __err = ios_base::failbit;
2467 break;
2468 }
2469 char __cmd = __ct.narrow(*__fmtb, 0);
2470 char __opt = '\0';
2471 if (__cmd == 'E' || __cmd == '0')
2472 {
2473 if (++__fmtb == __fmte)
2474 {
2475 __err = ios_base::failbit;
2476 break;
2477 }
2478 __opt = __cmd;
2479 __cmd = __ct.narrow(*__fmtb, 0);
2480 }
2481 __b = do_get(__b, __e, __iob, __err, __tm, __cmd, __opt);
2482 ++__fmtb;
2483 }
2484 else if (__ct.is(ctype_base::space, *__fmtb))
2485 {
2486 for (++__fmtb; __fmtb != __fmte && __ct.is(ctype_base::space, *__fmtb); ++__fmtb)
2487 ;
2488 for ( ; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2489 ;
2490 }
2491 else if (__ct.toupper(*__b) == __ct.toupper(*__fmtb))
2492 {
2493 ++__b;
2494 ++__fmtb;
2495 }
2496 else
2497 __err = ios_base::failbit;
2498 }
2499 if (__b == __e)
2500 __err |= ios_base::eofbit;
2501 return __b;
2502}
2503
2504template <class _CharT, class _InputIterator>
2505typename time_get<_CharT, _InputIterator>::dateorder
2506time_get<_CharT, _InputIterator>::do_date_order() const
2507{
2508 return mdy;
2509}
2510
2511template <class _CharT, class _InputIterator>
2512_InputIterator
2513time_get<_CharT, _InputIterator>::do_get_time(iter_type __b, iter_type __e,
2514 ios_base& __iob,
2515 ios_base::iostate& __err,
2516 tm* __tm) const
2517{
2518 const char_type __fmt[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2519 return get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt)/sizeof(__fmt[0]));
2520}
2521
2522template <class _CharT, class _InputIterator>
2523_InputIterator
2524time_get<_CharT, _InputIterator>::do_get_date(iter_type __b, iter_type __e,
2525 ios_base& __iob,
2526 ios_base::iostate& __err,
2527 tm* __tm) const
2528{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002529 const string_type& __fmt = this->__x();
2530 return get(__b, __e, __iob, __err, __tm, __fmt.data(), __fmt.data() + __fmt.size());
2531}
2532
2533template <class _CharT, class _InputIterator>
2534_InputIterator
2535time_get<_CharT, _InputIterator>::do_get_weekday(iter_type __b, iter_type __e,
2536 ios_base& __iob,
2537 ios_base::iostate& __err,
2538 tm* __tm) const
2539{
2540 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2541 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2542 return __b;
2543}
2544
2545template <class _CharT, class _InputIterator>
2546_InputIterator
2547time_get<_CharT, _InputIterator>::do_get_monthname(iter_type __b, iter_type __e,
2548 ios_base& __iob,
2549 ios_base::iostate& __err,
2550 tm* __tm) const
2551{
2552 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2553 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2554 return __b;
2555}
2556
2557template <class _CharT, class _InputIterator>
2558_InputIterator
2559time_get<_CharT, _InputIterator>::do_get_year(iter_type __b, iter_type __e,
2560 ios_base& __iob,
2561 ios_base::iostate& __err,
2562 tm* __tm) const
2563{
2564 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2565 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2566 return __b;
2567}
2568
2569template <class _CharT, class _InputIterator>
2570_InputIterator
2571time_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
2572 ios_base& __iob,
2573 ios_base::iostate& __err, tm* __tm,
2574 char __fmt, char) const
2575{
2576 __err = ios_base::goodbit;
2577 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2578 switch (__fmt)
2579 {
2580 case 'a':
2581 case 'A':
2582 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2583 break;
2584 case 'b':
2585 case 'B':
2586 case 'h':
2587 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2588 break;
2589 case 'c':
2590 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002591 const string_type& __fm = this->__c();
2592 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002593 }
2594 break;
2595 case 'd':
2596 case 'e':
2597 __get_day(__tm->tm_mday, __b, __e, __err, __ct);
2598 break;
2599 case 'D':
2600 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002601 const char_type __fm[] = {'%', 'm', '/', '%', 'd', '/', '%', 'y'};
2602 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002603 }
2604 break;
Howard Hinnantf60ff4e2011-04-10 17:54:14 +00002605 case 'F':
2606 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002607 const char_type __fm[] = {'%', 'Y', '-', '%', 'm', '-', '%', 'd'};
2608 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantf60ff4e2011-04-10 17:54:14 +00002609 }
2610 break;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002611 case 'H':
2612 __get_hour(__tm->tm_hour, __b, __e, __err, __ct);
2613 break;
2614 case 'I':
2615 __get_12_hour(__tm->tm_hour, __b, __e, __err, __ct);
2616 break;
2617 case 'j':
2618 __get_day_year_num(__tm->tm_yday, __b, __e, __err, __ct);
2619 break;
2620 case 'm':
2621 __get_month(__tm->tm_mon, __b, __e, __err, __ct);
2622 break;
2623 case 'M':
2624 __get_minute(__tm->tm_min, __b, __e, __err, __ct);
2625 break;
2626 case 'n':
2627 case 't':
2628 __get_white_space(__b, __e, __err, __ct);
2629 break;
2630 case 'p':
2631 __get_am_pm(__tm->tm_hour, __b, __e, __err, __ct);
2632 break;
2633 case 'r':
2634 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002635 const char_type __fm[] = {'%', 'I', ':', '%', 'M', ':', '%', 'S', ' ', '%', 'p'};
2636 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002637 }
2638 break;
2639 case 'R':
2640 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002641 const char_type __fm[] = {'%', 'H', ':', '%', 'M'};
2642 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002643 }
2644 break;
2645 case 'S':
2646 __get_second(__tm->tm_sec, __b, __e, __err, __ct);
2647 break;
2648 case 'T':
2649 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002650 const char_type __fm[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2651 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002652 }
2653 break;
2654 case 'w':
2655 __get_weekday(__tm->tm_wday, __b, __e, __err, __ct);
2656 break;
2657 case 'x':
2658 return do_get_date(__b, __e, __iob, __err, __tm);
2659 case 'X':
2660 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002661 const string_type& __fm = this->__X();
2662 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002663 }
2664 break;
2665 case 'y':
2666 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2667 break;
2668 case 'Y':
2669 __get_year4(__tm->tm_year, __b, __e, __err, __ct);
2670 break;
2671 case '%':
2672 __get_percent(__b, __e, __err, __ct);
2673 break;
2674 default:
2675 __err |= ios_base::failbit;
2676 }
2677 return __b;
2678}
2679
Howard Hinnant8ea98242013-08-23 17:37:05 +00002680_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_get<char>)
2681_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_get<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002683class _LIBCPP_TYPE_VIS __time_get
Howard Hinnantc51e1022010-05-11 19:42:16 +00002684{
2685protected:
2686 locale_t __loc_;
2687
2688 __time_get(const char* __nm);
2689 __time_get(const string& __nm);
2690 ~__time_get();
2691};
2692
2693template <class _CharT>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002694class _LIBCPP_TYPE_VIS __time_get_storage
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695 : public __time_get
2696{
2697protected:
2698 typedef basic_string<_CharT> string_type;
2699
2700 string_type __weeks_[14];
2701 string_type __months_[24];
2702 string_type __am_pm_[2];
2703 string_type __c_;
2704 string_type __r_;
2705 string_type __x_;
2706 string_type __X_;
2707
2708 explicit __time_get_storage(const char* __nm);
2709 explicit __time_get_storage(const string& __nm);
2710
2711 _LIBCPP_ALWAYS_INLINE ~__time_get_storage() {}
2712
2713 time_base::dateorder __do_date_order() const;
2714
2715private:
2716 void init(const ctype<_CharT>&);
2717 string_type __analyze(char __fmt, const ctype<_CharT>&);
2718};
2719
2720template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002721class _LIBCPP_TYPE_VIS_ONLY time_get_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00002722 : public time_get<_CharT, _InputIterator>,
2723 private __time_get_storage<_CharT>
2724{
2725public:
2726 typedef time_base::dateorder dateorder;
2727 typedef _InputIterator iter_type;
2728 typedef _CharT char_type;
2729 typedef basic_string<char_type> string_type;
2730
Howard Hinnant756c69b2010-09-22 16:48:34 +00002731 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002732 explicit time_get_byname(const char* __nm, size_t __refs = 0)
2733 : time_get<_CharT, _InputIterator>(__refs),
2734 __time_get_storage<_CharT>(__nm) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002735 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002736 explicit time_get_byname(const string& __nm, size_t __refs = 0)
2737 : time_get<_CharT, _InputIterator>(__refs),
2738 __time_get_storage<_CharT>(__nm) {}
2739
2740protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002742 ~time_get_byname() {}
2743
Howard Hinnant756c69b2010-09-22 16:48:34 +00002744 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002745 virtual dateorder do_date_order() const {return this->__do_date_order();}
2746private:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002747 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002748 virtual const string_type* __weeks() const {return this->__weeks_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002749 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750 virtual const string_type* __months() const {return this->__months_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002751 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002752 virtual const string_type* __am_pm() const {return this->__am_pm_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002753 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754 virtual const string_type& __c() const {return this->__c_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002756 virtual const string_type& __r() const {return this->__r_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002758 virtual const string_type& __x() const {return this->__x_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002759 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002760 virtual const string_type& __X() const {return this->__X_;}
2761};
2762
Howard Hinnant8ea98242013-08-23 17:37:05 +00002763_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_get_byname<char>)
2764_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_get_byname<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002765
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002766class _LIBCPP_TYPE_VIS __time_put
Howard Hinnantc51e1022010-05-11 19:42:16 +00002767{
2768 locale_t __loc_;
2769protected:
Howard Hinnant1ab52da2011-09-28 21:05:01 +00002770 _LIBCPP_ALWAYS_INLINE __time_put() : __loc_(_LIBCPP_GET_C_LOCALE) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002771 __time_put(const char* __nm);
2772 __time_put(const string& __nm);
2773 ~__time_put();
2774 void __do_put(char* __nb, char*& __ne, const tm* __tm,
2775 char __fmt, char __mod) const;
2776 void __do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm,
2777 char __fmt, char __mod) const;
2778};
2779
2780template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002781class _LIBCPP_TYPE_VIS_ONLY time_put
Howard Hinnantc51e1022010-05-11 19:42:16 +00002782 : public locale::facet,
2783 private __time_put
2784{
2785public:
2786 typedef _CharT char_type;
2787 typedef _OutputIterator iter_type;
2788
2789 _LIBCPP_ALWAYS_INLINE
2790 explicit time_put(size_t __refs = 0)
2791 : locale::facet(__refs) {}
2792
2793 iter_type put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm,
2794 const char_type* __pb, const char_type* __pe) const;
2795
2796 _LIBCPP_ALWAYS_INLINE
2797 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
2798 const tm* __tm, char __fmt, char __mod = 0) const
2799 {
2800 return do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2801 }
2802
2803 static locale::id id;
2804
2805protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002806 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002807 ~time_put() {}
2808 virtual iter_type do_put(iter_type __s, ios_base&, char_type, const tm* __tm,
2809 char __fmt, char __mod) const;
2810
Howard Hinnant756c69b2010-09-22 16:48:34 +00002811 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002812 explicit time_put(const char* __nm, size_t __refs)
2813 : locale::facet(__refs),
2814 __time_put(__nm) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002815 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816 explicit time_put(const string& __nm, size_t __refs)
2817 : locale::facet(__refs),
2818 __time_put(__nm) {}
2819};
2820
2821template <class _CharT, class _OutputIterator>
2822locale::id
2823time_put<_CharT, _OutputIterator>::id;
2824
2825template <class _CharT, class _OutputIterator>
2826_OutputIterator
2827time_put<_CharT, _OutputIterator>::put(iter_type __s, ios_base& __iob,
2828 char_type __fl, const tm* __tm,
2829 const char_type* __pb,
2830 const char_type* __pe) const
2831{
2832 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2833 for (; __pb != __pe; ++__pb)
2834 {
2835 if (__ct.narrow(*__pb, 0) == '%')
2836 {
2837 if (++__pb == __pe)
2838 {
2839 *__s++ = __pb[-1];
2840 break;
2841 }
2842 char __mod = 0;
2843 char __fmt = __ct.narrow(*__pb, 0);
2844 if (__fmt == 'E' || __fmt == 'O')
2845 {
2846 if (++__pb == __pe)
2847 {
2848 *__s++ = __pb[-2];
2849 *__s++ = __pb[-1];
2850 break;
2851 }
2852 __mod = __fmt;
2853 __fmt = __ct.narrow(*__pb, 0);
2854 }
2855 __s = do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2856 }
2857 else
2858 *__s++ = *__pb;
2859 }
2860 return __s;
2861}
2862
2863template <class _CharT, class _OutputIterator>
2864_OutputIterator
Howard Hinnant28b24882011-12-01 20:21:04 +00002865time_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002866 char_type, const tm* __tm,
2867 char __fmt, char __mod) const
2868{
2869 char_type __nar[100];
2870 char_type* __nb = __nar;
2871 char_type* __ne = __nb + 100;
2872 __do_put(__nb, __ne, __tm, __fmt, __mod);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002873 return _VSTD::copy(__nb, __ne, __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002874}
2875
Howard Hinnant8ea98242013-08-23 17:37:05 +00002876_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_put<char>)
2877_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002878
2879template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002880class _LIBCPP_TYPE_VIS_ONLY time_put_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00002881 : public time_put<_CharT, _OutputIterator>
2882{
2883public:
2884 _LIBCPP_ALWAYS_INLINE
2885 explicit time_put_byname(const char* __nm, size_t __refs = 0)
2886 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2887
2888 _LIBCPP_ALWAYS_INLINE
2889 explicit time_put_byname(const string& __nm, size_t __refs = 0)
2890 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2891
2892protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002893 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002894 ~time_put_byname() {}
2895};
2896
Howard Hinnant8ea98242013-08-23 17:37:05 +00002897_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_put_byname<char>)
2898_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_put_byname<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002899
2900// money_base
2901
Howard Hinnant8331b762013-03-06 23:30:19 +00002902class _LIBCPP_TYPE_VIS money_base
Howard Hinnantc51e1022010-05-11 19:42:16 +00002903{
2904public:
2905 enum part {none, space, symbol, sign, value};
2906 struct pattern {char field[4];};
2907
2908 _LIBCPP_ALWAYS_INLINE money_base() {}
2909};
2910
2911// moneypunct
2912
2913template <class _CharT, bool _International = false>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002914class _LIBCPP_TYPE_VIS_ONLY moneypunct
Howard Hinnantc51e1022010-05-11 19:42:16 +00002915 : public locale::facet,
2916 public money_base
2917{
2918public:
2919 typedef _CharT char_type;
2920 typedef basic_string<char_type> string_type;
2921
Howard Hinnant756c69b2010-09-22 16:48:34 +00002922 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002923 explicit moneypunct(size_t __refs = 0)
2924 : locale::facet(__refs) {}
2925
2926 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
2927 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
2928 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
2929 _LIBCPP_ALWAYS_INLINE string_type curr_symbol() const {return do_curr_symbol();}
2930 _LIBCPP_ALWAYS_INLINE string_type positive_sign() const {return do_positive_sign();}
2931 _LIBCPP_ALWAYS_INLINE string_type negative_sign() const {return do_negative_sign();}
2932 _LIBCPP_ALWAYS_INLINE int frac_digits() const {return do_frac_digits();}
2933 _LIBCPP_ALWAYS_INLINE pattern pos_format() const {return do_pos_format();}
2934 _LIBCPP_ALWAYS_INLINE pattern neg_format() const {return do_neg_format();}
2935
2936 static locale::id id;
2937 static const bool intl = _International;
2938
2939protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002940 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002941 ~moneypunct() {}
2942
2943 virtual char_type do_decimal_point() const {return numeric_limits<char_type>::max();}
2944 virtual char_type do_thousands_sep() const {return numeric_limits<char_type>::max();}
2945 virtual string do_grouping() const {return string();}
2946 virtual string_type do_curr_symbol() const {return string_type();}
2947 virtual string_type do_positive_sign() const {return string_type();}
2948 virtual string_type do_negative_sign() const {return string_type(1, '-');}
2949 virtual int do_frac_digits() const {return 0;}
2950 virtual pattern do_pos_format() const
Howard Hinnantb10e6cf2012-11-06 21:48:33 +00002951 {pattern __p = {{symbol, sign, none, value}}; return __p;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002952 virtual pattern do_neg_format() const
Howard Hinnantb10e6cf2012-11-06 21:48:33 +00002953 {pattern __p = {{symbol, sign, none, value}}; return __p;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002954};
2955
2956template <class _CharT, bool _International>
2957locale::id
2958moneypunct<_CharT, _International>::id;
2959
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002960template <class _CharT, bool _International>
2961const bool
2962moneypunct<_CharT, _International>::intl;
2963
Howard Hinnant8ea98242013-08-23 17:37:05 +00002964_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct<char, false>)
2965_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct<char, true>)
2966_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct<wchar_t, false>)
2967_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct<wchar_t, true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002968
2969// moneypunct_byname
2970
2971template <class _CharT, bool _International = false>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002972class _LIBCPP_TYPE_VIS_ONLY moneypunct_byname
Howard Hinnant756c69b2010-09-22 16:48:34 +00002973 : public moneypunct<_CharT, _International>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002974{
2975public:
2976 typedef money_base::pattern pattern;
2977 typedef _CharT char_type;
2978 typedef basic_string<char_type> string_type;
2979
2980 _LIBCPP_ALWAYS_INLINE
2981 explicit moneypunct_byname(const char* __nm, size_t __refs = 0)
2982 : moneypunct<_CharT, _International>(__refs) {init(__nm);}
2983
2984 _LIBCPP_ALWAYS_INLINE
2985 explicit moneypunct_byname(const string& __nm, size_t __refs = 0)
2986 : moneypunct<_CharT, _International>(__refs) {init(__nm.c_str());}
2987
2988protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002989 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002990 ~moneypunct_byname() {}
2991
2992 virtual char_type do_decimal_point() const {return __decimal_point_;}
2993 virtual char_type do_thousands_sep() const {return __thousands_sep_;}
2994 virtual string do_grouping() const {return __grouping_;}
2995 virtual string_type do_curr_symbol() const {return __curr_symbol_;}
2996 virtual string_type do_positive_sign() const {return __positive_sign_;}
2997 virtual string_type do_negative_sign() const {return __negative_sign_;}
2998 virtual int do_frac_digits() const {return __frac_digits_;}
2999 virtual pattern do_pos_format() const {return __pos_format_;}
3000 virtual pattern do_neg_format() const {return __neg_format_;}
3001
3002private:
3003 char_type __decimal_point_;
3004 char_type __thousands_sep_;
3005 string __grouping_;
3006 string_type __curr_symbol_;
3007 string_type __positive_sign_;
3008 string_type __negative_sign_;
3009 int __frac_digits_;
3010 pattern __pos_format_;
3011 pattern __neg_format_;
3012
3013 void init(const char*);
3014};
3015
3016template<> void moneypunct_byname<char, false>::init(const char*);
3017template<> void moneypunct_byname<char, true>::init(const char*);
3018template<> void moneypunct_byname<wchar_t, false>::init(const char*);
3019template<> void moneypunct_byname<wchar_t, true>::init(const char*);
3020
Howard Hinnant8ea98242013-08-23 17:37:05 +00003021_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct_byname<char, false>)
3022_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct_byname<char, true>)
3023_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct_byname<wchar_t, false>)
3024_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct_byname<wchar_t, true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003025
3026// money_get
3027
3028template <class _CharT>
3029class __money_get
3030{
3031protected:
3032 typedef _CharT char_type;
3033 typedef basic_string<char_type> string_type;
3034
3035 _LIBCPP_ALWAYS_INLINE __money_get() {}
3036
3037 static void __gather_info(bool __intl, const locale& __loc,
3038 money_base::pattern& __pat, char_type& __dp,
3039 char_type& __ts, string& __grp,
3040 string_type& __sym, string_type& __psn,
3041 string_type& __nsn, int& __fd);
3042};
3043
3044template <class _CharT>
3045void
3046__money_get<_CharT>::__gather_info(bool __intl, const locale& __loc,
3047 money_base::pattern& __pat, char_type& __dp,
3048 char_type& __ts, string& __grp,
3049 string_type& __sym, string_type& __psn,
3050 string_type& __nsn, int& __fd)
3051{
3052 if (__intl)
3053 {
3054 const moneypunct<char_type, true>& __mp =
3055 use_facet<moneypunct<char_type, true> >(__loc);
3056 __pat = __mp.neg_format();
3057 __nsn = __mp.negative_sign();
3058 __psn = __mp.positive_sign();
3059 __dp = __mp.decimal_point();
3060 __ts = __mp.thousands_sep();
3061 __grp = __mp.grouping();
3062 __sym = __mp.curr_symbol();
3063 __fd = __mp.frac_digits();
3064 }
3065 else
3066 {
3067 const moneypunct<char_type, false>& __mp =
3068 use_facet<moneypunct<char_type, false> >(__loc);
3069 __pat = __mp.neg_format();
3070 __nsn = __mp.negative_sign();
3071 __psn = __mp.positive_sign();
3072 __dp = __mp.decimal_point();
3073 __ts = __mp.thousands_sep();
3074 __grp = __mp.grouping();
3075 __sym = __mp.curr_symbol();
3076 __fd = __mp.frac_digits();
3077 }
3078}
3079
Howard Hinnant8ea98242013-08-23 17:37:05 +00003080_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS __money_get<char>)
3081_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS __money_get<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003082
3083template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003084class _LIBCPP_TYPE_VIS_ONLY money_get
Howard Hinnantc51e1022010-05-11 19:42:16 +00003085 : public locale::facet,
3086 private __money_get<_CharT>
3087{
3088public:
3089 typedef _CharT char_type;
3090 typedef _InputIterator iter_type;
3091 typedef basic_string<char_type> string_type;
3092
3093 _LIBCPP_ALWAYS_INLINE
3094 explicit money_get(size_t __refs = 0)
3095 : locale::facet(__refs) {}
3096
3097 _LIBCPP_ALWAYS_INLINE
3098 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
3099 ios_base::iostate& __err, long double& __v) const
3100 {
3101 return do_get(__b, __e, __intl, __iob, __err, __v);
3102 }
3103
3104 _LIBCPP_ALWAYS_INLINE
3105 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
3106 ios_base::iostate& __err, string_type& __v) const
3107 {
3108 return do_get(__b, __e, __intl, __iob, __err, __v);
3109 }
3110
3111 static locale::id id;
3112
3113protected:
3114
Howard Hinnant756c69b2010-09-22 16:48:34 +00003115 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00003116 ~money_get() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003117
Howard Hinnantc51e1022010-05-11 19:42:16 +00003118 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
3119 ios_base& __iob, ios_base::iostate& __err,
3120 long double& __v) const;
3121 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
3122 ios_base& __iob, ios_base::iostate& __err,
3123 string_type& __v) const;
3124
3125private:
3126 static bool __do_get(iter_type& __b, iter_type __e,
3127 bool __intl, const locale& __loc,
3128 ios_base::fmtflags __flags, ios_base::iostate& __err,
3129 bool& __neg, const ctype<char_type>& __ct,
3130 unique_ptr<char_type, void(*)(void*)>& __wb,
3131 char_type*& __wn, char_type* __we);
3132};
3133
3134template <class _CharT, class _InputIterator>
3135locale::id
3136money_get<_CharT, _InputIterator>::id;
3137
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003138_LIBCPP_FUNC_VIS void __do_nothing(void*);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003139
3140template <class _Tp>
3141_LIBCPP_HIDDEN
3142void
3143__double_or_nothing(unique_ptr<_Tp, void(*)(void*)>& __b, _Tp*& __n, _Tp*& __e)
3144{
3145 bool __owns = __b.get_deleter() != __do_nothing;
Howard Hinnant28b24882011-12-01 20:21:04 +00003146 size_t __cur_cap = static_cast<size_t>(__e-__b.get()) * sizeof(_Tp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003147 size_t __new_cap = __cur_cap < numeric_limits<size_t>::max() / 2 ?
3148 2 * __cur_cap : numeric_limits<size_t>::max();
Howard Hinnant28b24882011-12-01 20:21:04 +00003149 size_t __n_off = static_cast<size_t>(__n - __b.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003150 _Tp* __t = (_Tp*)realloc(__owns ? __b.get() : 0, __new_cap);
3151 if (__t == 0)
3152 __throw_bad_alloc();
3153 if (__owns)
3154 __b.release();
3155 __b = unique_ptr<_Tp, void(*)(void*)>(__t, free);
3156 __new_cap /= sizeof(_Tp);
3157 __n = __b.get() + __n_off;
3158 __e = __b.get() + __new_cap;
3159}
3160
3161// true == success
3162template <class _CharT, class _InputIterator>
3163bool
3164money_get<_CharT, _InputIterator>::__do_get(iter_type& __b, iter_type __e,
3165 bool __intl, const locale& __loc,
3166 ios_base::fmtflags __flags,
3167 ios_base::iostate& __err,
3168 bool& __neg,
3169 const ctype<char_type>& __ct,
3170 unique_ptr<char_type, void(*)(void*)>& __wb,
3171 char_type*& __wn, char_type* __we)
3172{
3173 const unsigned __bz = 100;
3174 unsigned __gbuf[__bz];
3175 unique_ptr<unsigned, void(*)(void*)> __gb(__gbuf, __do_nothing);
3176 unsigned* __gn = __gb.get();
3177 unsigned* __ge = __gn + __bz;
3178 money_base::pattern __pat;
3179 char_type __dp;
3180 char_type __ts;
3181 string __grp;
3182 string_type __sym;
3183 string_type __psn;
3184 string_type __nsn;
Jeffrey Yasskin6b045fb2012-03-10 18:31:43 +00003185 // Capture the spaces read into money_base::{space,none} so they
3186 // can be compared to initial spaces in __sym.
3187 string_type __spaces;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003188 int __fd;
3189 __money_get<_CharT>::__gather_info(__intl, __loc, __pat, __dp, __ts, __grp,
3190 __sym, __psn, __nsn, __fd);
3191 const string_type* __trailing_sign = 0;
3192 __wn = __wb.get();
3193 for (unsigned __p = 0; __p < 4 && __b != __e; ++__p)
3194 {
3195 switch (__pat.field[__p])
3196 {
3197 case money_base::space:
3198 if (__p != 3)
3199 {
3200 if (__ct.is(ctype_base::space, *__b))
Jeffrey Yasskin6b045fb2012-03-10 18:31:43 +00003201 __spaces.push_back(*__b++);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003202 else
3203 {
3204 __err |= ios_base::failbit;
3205 return false;
3206 }
3207 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003208 // drop through
Howard Hinnantc51e1022010-05-11 19:42:16 +00003209 case money_base::none:
3210 if (__p != 3)
3211 {
3212 while (__b != __e && __ct.is(ctype_base::space, *__b))
Jeffrey Yasskin6b045fb2012-03-10 18:31:43 +00003213 __spaces.push_back(*__b++);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003214 }
3215 break;
3216 case money_base::sign:
3217 if (__psn.size() + __nsn.size() > 0)
3218 {
3219 if (__psn.size() == 0 || __nsn.size() == 0)
3220 { // sign is optional
3221 if (__psn.size() > 0)
3222 { // __nsn.size() == 0
3223 if (*__b == __psn[0])
3224 {
3225 ++__b;
3226 if (__psn.size() > 1)
3227 __trailing_sign = &__psn;
3228 }
3229 else
3230 __neg = true;
3231 }
3232 else if (*__b == __nsn[0]) // __nsn.size() > 0 && __psn.size() == 0
3233 {
3234 ++__b;
3235 __neg = true;
3236 if (__nsn.size() > 1)
3237 __trailing_sign = &__nsn;
3238 }
3239 }
3240 else // sign is required
3241 {
3242 if (*__b == __psn[0])
3243 {
3244 ++__b;
3245 if (__psn.size() > 1)
3246 __trailing_sign = &__psn;
3247 }
3248 else if (*__b == __nsn[0])
3249 {
3250 ++__b;
3251 __neg = true;
3252 if (__nsn.size() > 1)
3253 __trailing_sign = &__nsn;
3254 }
3255 else
3256 {
3257 __err |= ios_base::failbit;
3258 return false;
3259 }
3260 }
3261 }
3262 break;
3263 case money_base::symbol:
3264 {
3265 bool __more_needed = __trailing_sign ||
3266 (__p < 2) ||
3267 (__p == 2 && __pat.field[3] != static_cast<char>(money_base::none));
Marshall Clowdfcbb432013-10-13 01:02:45 +00003268 bool __sb = (__flags & ios_base::showbase) != 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003269 if (__sb || __more_needed)
3270 {
Jeffrey Yasskin6b045fb2012-03-10 18:31:43 +00003271 typename string_type::const_iterator __sym_space_end = __sym.begin();
3272 if (__p > 0 && (__pat.field[__p - 1] == money_base::none ||
3273 __pat.field[__p - 1] == money_base::space)) {
3274 // Match spaces we've already read against spaces at
3275 // the beginning of __sym.
3276 while (__sym_space_end != __sym.end() &&
3277 __ct.is(ctype_base::space, *__sym_space_end))
3278 ++__sym_space_end;
3279 const size_t __num_spaces = __sym_space_end - __sym.begin();
3280 if (__num_spaces > __spaces.size() ||
3281 !equal(__spaces.end() - __num_spaces, __spaces.end(),
3282 __sym.begin())) {
3283 // No match. Put __sym_space_end back at the
3284 // beginning of __sym, which will prevent a
3285 // match in the next loop.
3286 __sym_space_end = __sym.begin();
3287 }
3288 }
3289 typename string_type::const_iterator __sym_curr_char = __sym_space_end;
3290 while (__sym_curr_char != __sym.end() && __b != __e &&
3291 *__b == *__sym_curr_char) {
3292 ++__b;
3293 ++__sym_curr_char;
3294 }
3295 if (__sb && __sym_curr_char != __sym.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003296 {
3297 __err |= ios_base::failbit;
3298 return false;
3299 }
3300 }
3301 }
3302 break;
3303 case money_base::value:
3304 {
3305 unsigned __ng = 0;
3306 for (; __b != __e; ++__b)
3307 {
3308 char_type __c = *__b;
3309 if (__ct.is(ctype_base::digit, __c))
3310 {
3311 if (__wn == __we)
3312 __double_or_nothing(__wb, __wn, __we);
3313 *__wn++ = __c;
3314 ++__ng;
3315 }
3316 else if (__grp.size() > 0 && __ng > 0 && __c == __ts)
3317 {
3318 if (__gn == __ge)
3319 __double_or_nothing(__gb, __gn, __ge);
3320 *__gn++ = __ng;
3321 __ng = 0;
3322 }
3323 else
3324 break;
3325 }
3326 if (__gb.get() != __gn && __ng > 0)
3327 {
3328 if (__gn == __ge)
3329 __double_or_nothing(__gb, __gn, __ge);
3330 *__gn++ = __ng;
3331 }
3332 if (__fd > 0)
3333 {
3334 if (__b == __e || *__b != __dp)
3335 {
3336 __err |= ios_base::failbit;
3337 return false;
3338 }
3339 for (++__b; __fd > 0; --__fd, ++__b)
3340 {
3341 if (__b == __e || !__ct.is(ctype_base::digit, *__b))
3342 {
3343 __err |= ios_base::failbit;
3344 return false;
3345 }
3346 if (__wn == __we)
3347 __double_or_nothing(__wb, __wn, __we);
3348 *__wn++ = *__b;
3349 }
3350 }
3351 if (__wn == __wb.get())
3352 {
3353 __err |= ios_base::failbit;
3354 return false;
3355 }
3356 }
3357 break;
3358 }
3359 }
3360 if (__trailing_sign)
3361 {
3362 for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, ++__b)
3363 {
3364 if (__b == __e || *__b != (*__trailing_sign)[__i])
3365 {
3366 __err |= ios_base::failbit;
3367 return false;
3368 }
3369 }
3370 }
3371 if (__gb.get() != __gn)
3372 {
3373 ios_base::iostate __et = ios_base::goodbit;
3374 __check_grouping(__grp, __gb.get(), __gn, __et);
3375 if (__et)
3376 {
3377 __err |= ios_base::failbit;
3378 return false;
3379 }
3380 }
3381 return true;
3382}
3383
3384template <class _CharT, class _InputIterator>
3385_InputIterator
3386money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3387 bool __intl, ios_base& __iob,
3388 ios_base::iostate& __err,
3389 long double& __v) const
3390{
Howard Hinnant28b24882011-12-01 20:21:04 +00003391 const int __bz = 100;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003392 char_type __wbuf[__bz];
3393 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3394 char_type* __wn;
3395 char_type* __we = __wbuf + __bz;
3396 locale __loc = __iob.getloc();
3397 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3398 bool __neg = false;
3399 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3400 __wb, __wn, __we))
3401 {
3402 const char __src[] = "0123456789";
3403 char_type __atoms[sizeof(__src)-1];
3404 __ct.widen(__src, __src + (sizeof(__src)-1), __atoms);
3405 char __nbuf[__bz];
3406 char* __nc = __nbuf;
3407 unique_ptr<char, void(*)(void*)> __h(0, free);
3408 if (__wn - __wb.get() > __bz-2)
3409 {
Howard Hinnant28b24882011-12-01 20:21:04 +00003410 __h.reset((char*)malloc(static_cast<size_t>(__wn - __wb.get() + 2)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003411 if (__h.get() == 0)
3412 __throw_bad_alloc();
3413 __nc = __h.get();
3414 }
3415 if (__neg)
3416 *__nc++ = '-';
3417 for (const char_type* __w = __wb.get(); __w < __wn; ++__w, ++__nc)
Marshall Clow1e68fd42013-03-22 02:14:40 +00003418 *__nc = __src[find(__atoms, _VSTD::end(__atoms), *__w) - __atoms];
Howard Hinnantc51e1022010-05-11 19:42:16 +00003419 *__nc = char();
3420 if (sscanf(__nbuf, "%Lf", &__v) != 1)
3421 __throw_runtime_error("money_get error");
3422 }
3423 if (__b == __e)
3424 __err |= ios_base::eofbit;
3425 return __b;
3426}
3427
3428template <class _CharT, class _InputIterator>
3429_InputIterator
3430money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3431 bool __intl, ios_base& __iob,
3432 ios_base::iostate& __err,
3433 string_type& __v) const
3434{
Howard Hinnant28b24882011-12-01 20:21:04 +00003435 const int __bz = 100;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003436 char_type __wbuf[__bz];
3437 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3438 char_type* __wn;
3439 char_type* __we = __wbuf + __bz;
3440 locale __loc = __iob.getloc();
3441 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3442 bool __neg = false;
3443 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3444 __wb, __wn, __we))
3445 {
3446 __v.clear();
3447 if (__neg)
3448 __v.push_back(__ct.widen('-'));
3449 char_type __z = __ct.widen('0');
3450 char_type* __w;
3451 for (__w = __wb.get(); __w < __wn-1; ++__w)
3452 if (*__w != __z)
3453 break;
3454 __v.append(__w, __wn);
3455 }
3456 if (__b == __e)
3457 __err |= ios_base::eofbit;
3458 return __b;
3459}
3460
Howard Hinnant8ea98242013-08-23 17:37:05 +00003461_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS money_get<char>)
3462_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS money_get<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003463
3464// money_put
3465
3466template <class _CharT>
3467class __money_put
3468{
3469protected:
3470 typedef _CharT char_type;
3471 typedef basic_string<char_type> string_type;
3472
3473 _LIBCPP_ALWAYS_INLINE __money_put() {}
3474
3475 static void __gather_info(bool __intl, bool __neg, const locale& __loc,
3476 money_base::pattern& __pat, char_type& __dp,
3477 char_type& __ts, string& __grp,
3478 string_type& __sym, string_type& __sn,
3479 int& __fd);
3480 static void __format(char_type* __mb, char_type*& __mi, char_type*& __me,
3481 ios_base::fmtflags __flags,
3482 const char_type* __db, const char_type* __de,
3483 const ctype<char_type>& __ct, bool __neg,
3484 const money_base::pattern& __pat, char_type __dp,
3485 char_type __ts, const string& __grp,
3486 const string_type& __sym, const string_type& __sn,
3487 int __fd);
3488};
3489
3490template <class _CharT>
3491void
3492__money_put<_CharT>::__gather_info(bool __intl, bool __neg, const locale& __loc,
3493 money_base::pattern& __pat, char_type& __dp,
3494 char_type& __ts, string& __grp,
3495 string_type& __sym, string_type& __sn,
3496 int& __fd)
3497{
3498 if (__intl)
3499 {
3500 const moneypunct<char_type, true>& __mp =
3501 use_facet<moneypunct<char_type, true> >(__loc);
3502 if (__neg)
3503 {
3504 __pat = __mp.neg_format();
3505 __sn = __mp.negative_sign();
3506 }
3507 else
3508 {
3509 __pat = __mp.pos_format();
3510 __sn = __mp.positive_sign();
3511 }
3512 __dp = __mp.decimal_point();
3513 __ts = __mp.thousands_sep();
3514 __grp = __mp.grouping();
3515 __sym = __mp.curr_symbol();
3516 __fd = __mp.frac_digits();
3517 }
3518 else
3519 {
3520 const moneypunct<char_type, false>& __mp =
3521 use_facet<moneypunct<char_type, false> >(__loc);
3522 if (__neg)
3523 {
3524 __pat = __mp.neg_format();
3525 __sn = __mp.negative_sign();
3526 }
3527 else
3528 {
3529 __pat = __mp.pos_format();
3530 __sn = __mp.positive_sign();
3531 }
3532 __dp = __mp.decimal_point();
3533 __ts = __mp.thousands_sep();
3534 __grp = __mp.grouping();
3535 __sym = __mp.curr_symbol();
3536 __fd = __mp.frac_digits();
3537 }
3538}
3539
3540template <class _CharT>
3541void
3542__money_put<_CharT>::__format(char_type* __mb, char_type*& __mi, char_type*& __me,
3543 ios_base::fmtflags __flags,
3544 const char_type* __db, const char_type* __de,
3545 const ctype<char_type>& __ct, bool __neg,
3546 const money_base::pattern& __pat, char_type __dp,
3547 char_type __ts, const string& __grp,
3548 const string_type& __sym, const string_type& __sn,
3549 int __fd)
3550{
3551 __me = __mb;
3552 for (unsigned __p = 0; __p < 4; ++__p)
3553 {
3554 switch (__pat.field[__p])
3555 {
3556 case money_base::none:
3557 __mi = __me;
3558 break;
3559 case money_base::space:
3560 __mi = __me;
3561 *__me++ = __ct.widen(' ');
3562 break;
3563 case money_base::sign:
3564 if (!__sn.empty())
3565 *__me++ = __sn[0];
3566 break;
3567 case money_base::symbol:
3568 if (!__sym.empty() && (__flags & ios_base::showbase))
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003569 __me = _VSTD::copy(__sym.begin(), __sym.end(), __me);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003570 break;
3571 case money_base::value:
3572 {
3573 // remember start of value so we can reverse it
3574 char_type* __t = __me;
3575 // find beginning of digits
3576 if (__neg)
3577 ++__db;
3578 // find end of digits
3579 const char_type* __d;
3580 for (__d = __db; __d < __de; ++__d)
3581 if (!__ct.is(ctype_base::digit, *__d))
3582 break;
3583 // print fractional part
3584 if (__fd > 0)
3585 {
3586 int __f;
3587 for (__f = __fd; __d > __db && __f > 0; --__f)
3588 *__me++ = *--__d;
3589 char_type __z = __f > 0 ? __ct.widen('0') : char_type();
3590 for (; __f > 0; --__f)
3591 *__me++ = __z;
3592 *__me++ = __dp;
3593 }
3594 // print units part
3595 if (__d == __db)
3596 {
3597 *__me++ = __ct.widen('0');
3598 }
3599 else
3600 {
3601 unsigned __ng = 0;
3602 unsigned __ig = 0;
3603 unsigned __gl = __grp.empty() ? numeric_limits<unsigned>::max()
3604 : static_cast<unsigned>(__grp[__ig]);
3605 while (__d != __db)
3606 {
3607 if (__ng == __gl)
3608 {
3609 *__me++ = __ts;
3610 __ng = 0;
3611 if (++__ig < __grp.size())
3612 __gl = __grp[__ig] == numeric_limits<char>::max() ?
3613 numeric_limits<unsigned>::max() :
3614 static_cast<unsigned>(__grp[__ig]);
3615 }
3616 *__me++ = *--__d;
3617 ++__ng;
3618 }
3619 }
3620 // reverse it
3621 reverse(__t, __me);
3622 }
3623 break;
3624 }
3625 }
3626 // print rest of sign, if any
3627 if (__sn.size() > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003628 __me = _VSTD::copy(__sn.begin()+1, __sn.end(), __me);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003629 // set alignment
3630 if ((__flags & ios_base::adjustfield) == ios_base::left)
3631 __mi = __me;
3632 else if ((__flags & ios_base::adjustfield) != ios_base::internal)
3633 __mi = __mb;
3634}
3635
Howard Hinnant8ea98242013-08-23 17:37:05 +00003636_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS __money_put<char>)
3637_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS __money_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003638
3639template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003640class _LIBCPP_TYPE_VIS_ONLY money_put
Howard Hinnantc51e1022010-05-11 19:42:16 +00003641 : public locale::facet,
3642 private __money_put<_CharT>
3643{
3644public:
3645 typedef _CharT char_type;
3646 typedef _OutputIterator iter_type;
3647 typedef basic_string<char_type> string_type;
3648
3649 _LIBCPP_ALWAYS_INLINE
3650 explicit money_put(size_t __refs = 0)
3651 : locale::facet(__refs) {}
3652
3653 _LIBCPP_ALWAYS_INLINE
3654 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3655 long double __units) const
3656 {
3657 return do_put(__s, __intl, __iob, __fl, __units);
3658 }
3659
3660 _LIBCPP_ALWAYS_INLINE
3661 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3662 const string_type& __digits) const
3663 {
3664 return do_put(__s, __intl, __iob, __fl, __digits);
3665 }
3666
3667 static locale::id id;
3668
3669protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003670 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00003671 ~money_put() {}
3672
3673 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3674 char_type __fl, long double __units) const;
3675 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3676 char_type __fl, const string_type& __digits) const;
3677};
3678
3679template <class _CharT, class _OutputIterator>
3680locale::id
3681money_put<_CharT, _OutputIterator>::id;
3682
3683template <class _CharT, class _OutputIterator>
3684_OutputIterator
3685money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3686 ios_base& __iob, char_type __fl,
3687 long double __units) const
3688{
3689 // convert to char
3690 const size_t __bs = 100;
3691 char __buf[__bs];
3692 char* __bb = __buf;
3693 char_type __digits[__bs];
3694 char_type* __db = __digits;
Howard Hinnant28b24882011-12-01 20:21:04 +00003695 size_t __n = static_cast<size_t>(snprintf(__bb, __bs, "%.0Lf", __units));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003696 unique_ptr<char, void(*)(void*)> __hn(0, free);
3697 unique_ptr<char_type, void(*)(void*)> __hd(0, free);
3698 // secure memory for digit storage
3699 if (__n > __bs-1)
3700 {
Howard Hinnantf312e3e2011-09-28 23:39:33 +00003701#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant28b24882011-12-01 20:21:04 +00003702 __n = static_cast<size_t>(asprintf_l(&__bb, _LIBCPP_GET_C_LOCALE, "%.0Lf", __units));
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00003703#else
3704 __n = __asprintf_l(&__bb, __cloc(), "%.0Lf", __units);
3705#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003706 if (__bb == 0)
3707 __throw_bad_alloc();
3708 __hn.reset(__bb);
3709 __hd.reset((char_type*)malloc(__n * sizeof(char_type)));
Howard Hinnant03de6f92012-03-07 20:37:43 +00003710 if (__hd == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003711 __throw_bad_alloc();
3712 __db = __hd.get();
3713 }
3714 // gather info
3715 locale __loc = __iob.getloc();
3716 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3717 __ct.widen(__bb, __bb + __n, __db);
3718 bool __neg = __n > 0 && __bb[0] == '-';
3719 money_base::pattern __pat;
3720 char_type __dp;
3721 char_type __ts;
3722 string __grp;
3723 string_type __sym;
3724 string_type __sn;
3725 int __fd;
3726 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3727 // secure memory for formatting
3728 char_type __mbuf[__bs];
3729 char_type* __mb = __mbuf;
3730 unique_ptr<char_type, void(*)(void*)> __hw(0, free);
3731 size_t __exn = static_cast<int>(__n) > __fd ?
Howard Hinnant28b24882011-12-01 20:21:04 +00003732 (__n - static_cast<size_t>(__fd)) * 2 + __sn.size() +
3733 __sym.size() + static_cast<size_t>(__fd) + 1
3734 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003735 if (__exn > __bs)
3736 {
3737 __hw.reset((char_type*)malloc(__exn * sizeof(char_type)));
3738 __mb = __hw.get();
3739 if (__mb == 0)
3740 __throw_bad_alloc();
3741 }
3742 // format
3743 char_type* __mi;
3744 char_type* __me;
3745 this->__format(__mb, __mi, __me, __iob.flags(),
3746 __db, __db + __n, __ct,
3747 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3748 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3749}
3750
3751template <class _CharT, class _OutputIterator>
3752_OutputIterator
3753money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3754 ios_base& __iob, char_type __fl,
3755 const string_type& __digits) const
3756{
3757 // gather info
3758 locale __loc = __iob.getloc();
3759 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3760 bool __neg = __digits.size() > 0 && __digits[0] == __ct.widen('-');
3761 money_base::pattern __pat;
3762 char_type __dp;
3763 char_type __ts;
3764 string __grp;
3765 string_type __sym;
3766 string_type __sn;
3767 int __fd;
3768 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3769 // secure memory for formatting
3770 char_type __mbuf[100];
3771 char_type* __mb = __mbuf;
3772 unique_ptr<char_type, void(*)(void*)> __h(0, free);
Howard Hinnant28b24882011-12-01 20:21:04 +00003773 size_t __exn = static_cast<int>(__digits.size()) > __fd ?
3774 (__digits.size() - static_cast<size_t>(__fd)) * 2 +
3775 __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 1
3776 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003777 if (__exn > 100)
3778 {
3779 __h.reset((char_type*)malloc(__exn * sizeof(char_type)));
3780 __mb = __h.get();
3781 if (__mb == 0)
3782 __throw_bad_alloc();
3783 }
3784 // format
3785 char_type* __mi;
3786 char_type* __me;
3787 this->__format(__mb, __mi, __me, __iob.flags(),
3788 __digits.data(), __digits.data() + __digits.size(), __ct,
3789 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3790 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3791}
3792
Howard Hinnant8ea98242013-08-23 17:37:05 +00003793_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS money_put<char>)
3794_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS money_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003795
3796// messages
3797
Howard Hinnant8331b762013-03-06 23:30:19 +00003798class _LIBCPP_TYPE_VIS messages_base
Howard Hinnantc51e1022010-05-11 19:42:16 +00003799{
3800public:
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003801 typedef ptrdiff_t catalog;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003802
3803 _LIBCPP_ALWAYS_INLINE messages_base() {}
3804};
3805
3806template <class _CharT>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003807class _LIBCPP_TYPE_VIS_ONLY messages
Howard Hinnantc51e1022010-05-11 19:42:16 +00003808 : public locale::facet,
3809 public messages_base
3810{
3811public:
3812 typedef _CharT char_type;
3813 typedef basic_string<_CharT> string_type;
3814
3815 _LIBCPP_ALWAYS_INLINE
3816 explicit messages(size_t __refs = 0)
3817 : locale::facet(__refs) {}
3818
3819 _LIBCPP_ALWAYS_INLINE
3820 catalog open(const basic_string<char>& __nm, const locale& __loc) const
3821 {
3822 return do_open(__nm, __loc);
3823 }
3824
3825 _LIBCPP_ALWAYS_INLINE
3826 string_type get(catalog __c, int __set, int __msgid,
3827 const string_type& __dflt) const
3828 {
3829 return do_get(__c, __set, __msgid, __dflt);
3830 }
3831
3832 _LIBCPP_ALWAYS_INLINE
3833 void close(catalog __c) const
3834 {
3835 do_close(__c);
3836 }
3837
3838 static locale::id id;
3839
3840protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003841 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00003842 ~messages() {}
3843
3844 virtual catalog do_open(const basic_string<char>&, const locale&) const;
3845 virtual string_type do_get(catalog, int __set, int __msgid,
3846 const string_type& __dflt) const;
3847 virtual void do_close(catalog) const;
3848};
3849
3850template <class _CharT>
3851locale::id
3852messages<_CharT>::id;
3853
3854template <class _CharT>
3855typename messages<_CharT>::catalog
3856messages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const
3857{
Marshall Clow1f257322013-03-18 17:04:29 +00003858#ifdef _WIN32
Howard Hinnanteb505f62011-09-23 16:11:27 +00003859 return -1;
3860#else // _WIN32
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +00003861 catalog __cat = (catalog)catopen(__nm.c_str(), NL_CAT_LOCALE);
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003862 if (__cat != -1)
3863 __cat = static_cast<catalog>((static_cast<size_t>(__cat) >> 1));
3864 return __cat;
Howard Hinnanteb505f62011-09-23 16:11:27 +00003865#endif // _WIN32
Howard Hinnantc51e1022010-05-11 19:42:16 +00003866}
3867
3868template <class _CharT>
3869typename messages<_CharT>::string_type
3870messages<_CharT>::do_get(catalog __c, int __set, int __msgid,
3871 const string_type& __dflt) const
3872{
Marshall Clow1f257322013-03-18 17:04:29 +00003873#ifdef _WIN32
Howard Hinnanteb505f62011-09-23 16:11:27 +00003874 return __dflt;
3875#else // _WIN32
Howard Hinnantc51e1022010-05-11 19:42:16 +00003876 string __ndflt;
3877 __narrow_to_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__ndflt),
3878 __dflt.c_str(),
3879 __dflt.c_str() + __dflt.size());
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003880 if (__c != -1)
3881 __c <<= 1;
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +00003882 nl_catd __cat = (nl_catd)__c;
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003883 char* __n = catgets(__cat, __set, __msgid, __ndflt.c_str());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003884 string_type __w;
3885 __widen_from_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__w),
3886 __n, __n + strlen(__n));
3887 return __w;
Howard Hinnanteb505f62011-09-23 16:11:27 +00003888#endif // _WIN32
Howard Hinnantc51e1022010-05-11 19:42:16 +00003889}
3890
3891template <class _CharT>
3892void
3893messages<_CharT>::do_close(catalog __c) const
3894{
Marshall Clow1f257322013-03-18 17:04:29 +00003895#if !defined(_WIN32)
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003896 if (__c != -1)
3897 __c <<= 1;
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +00003898 nl_catd __cat = (nl_catd)__c;
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003899 catclose(__cat);
Howard Hinnanteb505f62011-09-23 16:11:27 +00003900#endif // !_WIN32
Howard Hinnantc51e1022010-05-11 19:42:16 +00003901}
3902
Howard Hinnant8ea98242013-08-23 17:37:05 +00003903_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS messages<char>)
3904_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS messages<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003905
3906template <class _CharT>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003907class _LIBCPP_TYPE_VIS_ONLY messages_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00003908 : public messages<_CharT>
3909{
3910public:
3911 typedef messages_base::catalog catalog;
3912 typedef basic_string<_CharT> string_type;
3913
3914 _LIBCPP_ALWAYS_INLINE
3915 explicit messages_byname(const char*, size_t __refs = 0)
3916 : messages<_CharT>(__refs) {}
3917
3918 _LIBCPP_ALWAYS_INLINE
3919 explicit messages_byname(const string&, size_t __refs = 0)
3920 : messages<_CharT>(__refs) {}
3921
3922protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003923 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00003924 ~messages_byname() {}
3925};
3926
Howard Hinnant8ea98242013-08-23 17:37:05 +00003927_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS messages_byname<char>)
3928_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS messages_byname<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003929
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003930template<class _Codecvt, class _Elem = wchar_t,
3931 class _Wide_alloc = allocator<_Elem>,
3932 class _Byte_alloc = allocator<char> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003933class _LIBCPP_TYPE_VIS_ONLY wstring_convert
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003934{
3935public:
3936 typedef basic_string<char, char_traits<char>, _Byte_alloc> byte_string;
3937 typedef basic_string<_Elem, char_traits<_Elem>, _Wide_alloc> wide_string;
3938 typedef typename _Codecvt::state_type state_type;
3939 typedef typename wide_string::traits_type::int_type int_type;
3940
3941private:
3942 byte_string __byte_err_string_;
3943 wide_string __wide_err_string_;
3944 _Codecvt* __cvtptr_;
3945 state_type __cvtstate_;
3946 size_t __cvtcount_;
3947
3948 wstring_convert(const wstring_convert& __wc);
3949 wstring_convert& operator=(const wstring_convert& __wc);
3950public:
Marshall Clowccaa0fb2013-08-27 20:18:59 +00003951 _LIBCPP_EXPLICIT_AFTER_CXX11 wstring_convert(_Codecvt* __pcvt = new _Codecvt);
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003952 wstring_convert(_Codecvt* __pcvt, state_type __state);
Marshall Clowccaa0fb2013-08-27 20:18:59 +00003953 _LIBCPP_EXPLICIT_AFTER_CXX11 wstring_convert(const byte_string& __byte_err,
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003954 const wide_string& __wide_err = wide_string());
Howard Hinnant74279a52010-09-04 23:28:19 +00003955#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003956 wstring_convert(wstring_convert&& __wc);
3957#endif
3958 ~wstring_convert();
3959
Howard Hinnant756c69b2010-09-22 16:48:34 +00003960 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003961 wide_string from_bytes(char __byte)
3962 {return from_bytes(&__byte, &__byte+1);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003963 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003964 wide_string from_bytes(const char* __ptr)
3965 {return from_bytes(__ptr, __ptr + char_traits<char>::length(__ptr));}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003966 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003967 wide_string from_bytes(const byte_string& __str)
3968 {return from_bytes(__str.data(), __str.data() + __str.size());}
3969 wide_string from_bytes(const char* __first, const char* __last);
3970
Howard Hinnant756c69b2010-09-22 16:48:34 +00003971 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003972 byte_string to_bytes(_Elem __wchar)
3973 {return to_bytes(&__wchar, &__wchar+1);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003974 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003975 byte_string to_bytes(const _Elem* __wptr)
3976 {return to_bytes(__wptr, __wptr + char_traits<_Elem>::length(__wptr));}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003977 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003978 byte_string to_bytes(const wide_string& __wstr)
3979 {return to_bytes(__wstr.data(), __wstr.data() + __wstr.size());}
3980 byte_string to_bytes(const _Elem* __first, const _Elem* __last);
3981
Howard Hinnant756c69b2010-09-22 16:48:34 +00003982 _LIBCPP_ALWAYS_INLINE
Marshall Clowccaa0fb2013-08-27 20:18:59 +00003983 size_t converted() const _NOEXCEPT {return __cvtcount_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003984 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003985 state_type state() const {return __cvtstate_;}
3986};
3987
3988template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003989inline _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003990wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3991 wstring_convert(_Codecvt* __pcvt)
3992 : __cvtptr_(__pcvt), __cvtstate_(), __cvtcount_(0)
3993{
3994}
3995
3996template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003997inline _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003998wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3999 wstring_convert(_Codecvt* __pcvt, state_type __state)
4000 : __cvtptr_(__pcvt), __cvtstate_(__state), __cvtcount_(0)
4001{
4002}
4003
4004template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
4005wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4006 wstring_convert(const byte_string& __byte_err, const wide_string& __wide_err)
4007 : __byte_err_string_(__byte_err), __wide_err_string_(__wide_err),
4008 __cvtstate_(), __cvtcount_(0)
4009{
4010 __cvtptr_ = new _Codecvt;
4011}
4012
Howard Hinnant74279a52010-09-04 23:28:19 +00004013#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004014
4015template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004016inline _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004017wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4018 wstring_convert(wstring_convert&& __wc)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004019 : __byte_err_string_(_VSTD::move(__wc.__byte_err_string_)),
4020 __wide_err_string_(_VSTD::move(__wc.__wide_err_string_)),
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004021 __cvtptr_(__wc.__cvtptr_),
4022 __cvtstate_(__wc.__cvtstate_), __cvtcount_(__wc.__cvtstate_)
4023{
4024 __wc.__cvtptr_ = nullptr;
4025}
4026
Howard Hinnant5dc89112010-09-04 23:46:48 +00004027#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004028
4029template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
4030wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::~wstring_convert()
4031{
4032 delete __cvtptr_;
4033}
4034
4035template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
4036typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::wide_string
4037wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4038 from_bytes(const char* __frm, const char* __frm_end)
4039{
4040 __cvtcount_ = 0;
4041 if (__cvtptr_ != nullptr)
4042 {
4043 wide_string __ws(2*(__frm_end - __frm), _Elem());
Howard Hinnant16d54f22012-07-12 18:07:41 +00004044 if (__frm != __frm_end)
4045 __ws.resize(__ws.capacity());
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004046 codecvt_base::result __r = codecvt_base::ok;
4047 state_type __st = __cvtstate_;
4048 if (__frm != __frm_end)
4049 {
4050 _Elem* __to = &__ws[0];
4051 _Elem* __to_end = __to + __ws.size();
4052 const char* __frm_nxt;
4053 do
4054 {
4055 _Elem* __to_nxt;
4056 __r = __cvtptr_->in(__st, __frm, __frm_end, __frm_nxt,
4057 __to, __to_end, __to_nxt);
4058 __cvtcount_ += __frm_nxt - __frm;
4059 if (__frm_nxt == __frm)
4060 {
4061 __r = codecvt_base::error;
4062 }
4063 else if (__r == codecvt_base::noconv)
4064 {
4065 __ws.resize(__to - &__ws[0]);
4066 // This only gets executed if _Elem is char
4067 __ws.append((const _Elem*)__frm, (const _Elem*)__frm_end);
4068 __frm = __frm_nxt;
4069 __r = codecvt_base::ok;
4070 }
4071 else if (__r == codecvt_base::ok)
4072 {
4073 __ws.resize(__to_nxt - &__ws[0]);
4074 __frm = __frm_nxt;
4075 }
4076 else if (__r == codecvt_base::partial)
4077 {
4078 ptrdiff_t __s = __to_nxt - &__ws[0];
4079 __ws.resize(2 * __s);
4080 __to = &__ws[0] + __s;
4081 __to_end = &__ws[0] + __ws.size();
4082 __frm = __frm_nxt;
4083 }
4084 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
4085 }
4086 if (__r == codecvt_base::ok)
4087 return __ws;
4088 }
Howard Hinnant72f73582010-08-11 17:04:31 +00004089#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004090 if (__wide_err_string_.empty())
4091 throw range_error("wstring_convert: from_bytes error");
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004092#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004093 return __wide_err_string_;
4094}
4095
4096template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
4097typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::byte_string
4098wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4099 to_bytes(const _Elem* __frm, const _Elem* __frm_end)
4100{
4101 __cvtcount_ = 0;
4102 if (__cvtptr_ != nullptr)
4103 {
4104 byte_string __bs(2*(__frm_end - __frm), char());
Howard Hinnant16d54f22012-07-12 18:07:41 +00004105 if (__frm != __frm_end)
4106 __bs.resize(__bs.capacity());
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004107 codecvt_base::result __r = codecvt_base::ok;
4108 state_type __st = __cvtstate_;
4109 if (__frm != __frm_end)
4110 {
4111 char* __to = &__bs[0];
4112 char* __to_end = __to + __bs.size();
4113 const _Elem* __frm_nxt;
4114 do
4115 {
4116 char* __to_nxt;
4117 __r = __cvtptr_->out(__st, __frm, __frm_end, __frm_nxt,
4118 __to, __to_end, __to_nxt);
4119 __cvtcount_ += __frm_nxt - __frm;
4120 if (__frm_nxt == __frm)
4121 {
4122 __r = codecvt_base::error;
4123 }
4124 else if (__r == codecvt_base::noconv)
4125 {
4126 __bs.resize(__to - &__bs[0]);
4127 // This only gets executed if _Elem is char
4128 __bs.append((const char*)__frm, (const char*)__frm_end);
4129 __frm = __frm_nxt;
4130 __r = codecvt_base::ok;
4131 }
4132 else if (__r == codecvt_base::ok)
4133 {
4134 __bs.resize(__to_nxt - &__bs[0]);
4135 __frm = __frm_nxt;
4136 }
4137 else if (__r == codecvt_base::partial)
4138 {
4139 ptrdiff_t __s = __to_nxt - &__bs[0];
4140 __bs.resize(2 * __s);
4141 __to = &__bs[0] + __s;
4142 __to_end = &__bs[0] + __bs.size();
4143 __frm = __frm_nxt;
4144 }
4145 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
4146 }
4147 if (__r == codecvt_base::ok)
4148 {
4149 size_t __s = __bs.size();
4150 __bs.resize(__bs.capacity());
4151 char* __to = &__bs[0] + __s;
4152 char* __to_end = __to + __bs.size();
4153 do
4154 {
4155 char* __to_nxt;
4156 __r = __cvtptr_->unshift(__st, __to, __to_end, __to_nxt);
4157 if (__r == codecvt_base::noconv)
4158 {
4159 __bs.resize(__to - &__bs[0]);
4160 __r = codecvt_base::ok;
4161 }
4162 else if (__r == codecvt_base::ok)
4163 {
4164 __bs.resize(__to_nxt - &__bs[0]);
4165 }
4166 else if (__r == codecvt_base::partial)
4167 {
Howard Hinnant28b24882011-12-01 20:21:04 +00004168 ptrdiff_t __sp = __to_nxt - &__bs[0];
4169 __bs.resize(2 * __sp);
4170 __to = &__bs[0] + __sp;
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004171 __to_end = &__bs[0] + __bs.size();
4172 }
4173 } while (__r == codecvt_base::partial);
4174 if (__r == codecvt_base::ok)
4175 return __bs;
4176 }
4177 }
Howard Hinnant72f73582010-08-11 17:04:31 +00004178#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004179 if (__byte_err_string_.empty())
4180 throw range_error("wstring_convert: to_bytes error");
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004181#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004182 return __byte_err_string_;
4183}
4184
4185template <class _Codecvt, class _Elem = wchar_t, class _Tr = char_traits<_Elem> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004186class _LIBCPP_TYPE_VIS_ONLY wbuffer_convert
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004187 : public basic_streambuf<_Elem, _Tr>
4188{
4189public:
4190 // types:
4191 typedef _Elem char_type;
4192 typedef _Tr traits_type;
4193 typedef typename traits_type::int_type int_type;
4194 typedef typename traits_type::pos_type pos_type;
4195 typedef typename traits_type::off_type off_type;
4196 typedef typename _Codecvt::state_type state_type;
4197
4198private:
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004199 char* __extbuf_;
4200 const char* __extbufnext_;
4201 const char* __extbufend_;
4202 char __extbuf_min_[8];
4203 size_t __ebs_;
4204 char_type* __intbuf_;
4205 size_t __ibs_;
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004206 streambuf* __bufptr_;
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004207 _Codecvt* __cv_;
4208 state_type __st_;
4209 ios_base::openmode __cm_;
4210 bool __owns_eb_;
4211 bool __owns_ib_;
4212 bool __always_noconv_;
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004213
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004214 wbuffer_convert(const wbuffer_convert&);
4215 wbuffer_convert& operator=(const wbuffer_convert&);
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004216public:
Marshall Clowccaa0fb2013-08-27 20:18:59 +00004217 _LIBCPP_EXPLICIT_AFTER_CXX11 wbuffer_convert(streambuf* __bytebuf = 0,
4218 _Codecvt* __pcvt = new _Codecvt, state_type __state = state_type());
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004219 ~wbuffer_convert();
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004220
Howard Hinnant756c69b2010-09-22 16:48:34 +00004221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004222 streambuf* rdbuf() const {return __bufptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004223 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004224 streambuf* rdbuf(streambuf* __bytebuf)
4225 {
4226 streambuf* __r = __bufptr_;
4227 __bufptr_ = __bytebuf;
4228 return __r;
4229 }
4230
Howard Hinnant756c69b2010-09-22 16:48:34 +00004231 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004232 state_type state() const {return __st_;}
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004233
4234protected:
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004235 virtual int_type underflow();
4236 virtual int_type pbackfail(int_type __c = traits_type::eof());
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004237 virtual int_type overflow (int_type __c = traits_type::eof());
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004238 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s,
4239 streamsize __n);
4240 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
4241 ios_base::openmode __wch = ios_base::in | ios_base::out);
4242 virtual pos_type seekpos(pos_type __sp,
4243 ios_base::openmode __wch = ios_base::in | ios_base::out);
4244 virtual int sync();
4245
4246private:
4247 bool __read_mode();
4248 void __write_mode();
4249 wbuffer_convert* __close();
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004250};
4251
4252template <class _Codecvt, class _Elem, class _Tr>
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004253wbuffer_convert<_Codecvt, _Elem, _Tr>::
4254 wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt, state_type __state)
4255 : __extbuf_(0),
4256 __extbufnext_(0),
4257 __extbufend_(0),
4258 __ebs_(0),
4259 __intbuf_(0),
4260 __ibs_(0),
4261 __bufptr_(__bytebuf),
4262 __cv_(__pcvt),
4263 __st_(__state),
4264 __cm_(0),
4265 __owns_eb_(false),
4266 __owns_ib_(false),
4267 __always_noconv_(__cv_ ? __cv_->always_noconv() : false)
4268{
4269 setbuf(0, 4096);
4270}
4271
4272template <class _Codecvt, class _Elem, class _Tr>
4273wbuffer_convert<_Codecvt, _Elem, _Tr>::~wbuffer_convert()
4274{
4275 __close();
4276 delete __cv_;
4277 if (__owns_eb_)
4278 delete [] __extbuf_;
4279 if (__owns_ib_)
4280 delete [] __intbuf_;
4281}
4282
4283template <class _Codecvt, class _Elem, class _Tr>
4284typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4285wbuffer_convert<_Codecvt, _Elem, _Tr>::underflow()
4286{
4287 if (__cv_ == 0 || __bufptr_ == 0)
4288 return traits_type::eof();
4289 bool __initial = __read_mode();
4290 char_type __1buf;
4291 if (this->gptr() == 0)
4292 this->setg(&__1buf, &__1buf+1, &__1buf+1);
4293 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
4294 int_type __c = traits_type::eof();
4295 if (this->gptr() == this->egptr())
4296 {
4297 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
4298 if (__always_noconv_)
4299 {
4300 streamsize __nmemb = static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz);
4301 __nmemb = __bufptr_->sgetn((char*)this->eback() + __unget_sz, __nmemb);
4302 if (__nmemb != 0)
4303 {
4304 this->setg(this->eback(),
4305 this->eback() + __unget_sz,
4306 this->eback() + __unget_sz + __nmemb);
4307 __c = *this->gptr();
4308 }
4309 }
4310 else
4311 {
4312 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
4313 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
4314 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004315 streamsize __nmemb = _VSTD::min(static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz),
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004316 static_cast<streamsize>(__extbufend_ - __extbufnext_));
4317 codecvt_base::result __r;
4318 state_type __svs = __st_;
4319 streamsize __nr = __bufptr_->sgetn(const_cast<char*>(__extbufnext_), __nmemb);
4320 if (__nr != 0)
4321 {
4322 __extbufend_ = __extbufnext_ + __nr;
4323 char_type* __inext;
4324 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
4325 this->eback() + __unget_sz,
4326 this->egptr(), __inext);
4327 if (__r == codecvt_base::noconv)
4328 {
4329 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)__extbufend_);
4330 __c = *this->gptr();
4331 }
4332 else if (__inext != this->eback() + __unget_sz)
4333 {
4334 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
4335 __c = *this->gptr();
4336 }
4337 }
4338 }
4339 }
4340 else
4341 __c = *this->gptr();
4342 if (this->eback() == &__1buf)
4343 this->setg(0, 0, 0);
4344 return __c;
4345}
4346
4347template <class _Codecvt, class _Elem, class _Tr>
4348typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4349wbuffer_convert<_Codecvt, _Elem, _Tr>::pbackfail(int_type __c)
4350{
4351 if (__cv_ != 0 && __bufptr_ != 0 && this->eback() < this->gptr())
4352 {
4353 if (traits_type::eq_int_type(__c, traits_type::eof()))
4354 {
4355 this->gbump(-1);
4356 return traits_type::not_eof(__c);
4357 }
4358 if (traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
4359 {
4360 this->gbump(-1);
4361 *this->gptr() = traits_type::to_char_type(__c);
4362 return __c;
4363 }
4364 }
4365 return traits_type::eof();
4366}
4367
4368template <class _Codecvt, class _Elem, class _Tr>
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004369typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4370wbuffer_convert<_Codecvt, _Elem, _Tr>::overflow(int_type __c)
4371{
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004372 if (__cv_ == 0 || __bufptr_ == 0)
4373 return traits_type::eof();
4374 __write_mode();
4375 char_type __1buf;
4376 char_type* __pb_save = this->pbase();
4377 char_type* __epb_save = this->epptr();
4378 if (!traits_type::eq_int_type(__c, traits_type::eof()))
4379 {
4380 if (this->pptr() == 0)
4381 this->setp(&__1buf, &__1buf+1);
4382 *this->pptr() = traits_type::to_char_type(__c);
4383 this->pbump(1);
4384 }
4385 if (this->pptr() != this->pbase())
4386 {
4387 if (__always_noconv_)
4388 {
4389 streamsize __nmemb = static_cast<streamsize>(this->pptr() - this->pbase());
4390 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4391 return traits_type::eof();
4392 }
4393 else
4394 {
4395 char* __extbe = __extbuf_;
4396 codecvt_base::result __r;
4397 do
4398 {
4399 const char_type* __e;
4400 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
4401 __extbuf_, __extbuf_ + __ebs_, __extbe);
4402 if (__e == this->pbase())
4403 return traits_type::eof();
4404 if (__r == codecvt_base::noconv)
4405 {
4406 streamsize __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
4407 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4408 return traits_type::eof();
4409 }
4410 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
4411 {
4412 streamsize __nmemb = static_cast<size_t>(__extbe - __extbuf_);
4413 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4414 return traits_type::eof();
4415 if (__r == codecvt_base::partial)
4416 {
4417 this->setp((char_type*)__e, this->pptr());
4418 this->pbump(this->epptr() - this->pbase());
4419 }
4420 }
4421 else
4422 return traits_type::eof();
4423 } while (__r == codecvt_base::partial);
4424 }
4425 this->setp(__pb_save, __epb_save);
4426 }
4427 return traits_type::not_eof(__c);
4428}
4429
4430template <class _Codecvt, class _Elem, class _Tr>
4431basic_streambuf<_Elem, _Tr>*
4432wbuffer_convert<_Codecvt, _Elem, _Tr>::setbuf(char_type* __s, streamsize __n)
4433{
4434 this->setg(0, 0, 0);
4435 this->setp(0, 0);
4436 if (__owns_eb_)
4437 delete [] __extbuf_;
4438 if (__owns_ib_)
4439 delete [] __intbuf_;
4440 __ebs_ = __n;
4441 if (__ebs_ > sizeof(__extbuf_min_))
4442 {
4443 if (__always_noconv_ && __s)
4444 {
4445 __extbuf_ = (char*)__s;
4446 __owns_eb_ = false;
4447 }
4448 else
4449 {
4450 __extbuf_ = new char[__ebs_];
4451 __owns_eb_ = true;
4452 }
4453 }
4454 else
4455 {
4456 __extbuf_ = __extbuf_min_;
4457 __ebs_ = sizeof(__extbuf_min_);
4458 __owns_eb_ = false;
4459 }
4460 if (!__always_noconv_)
4461 {
4462 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
4463 if (__s && __ibs_ >= sizeof(__extbuf_min_))
4464 {
4465 __intbuf_ = __s;
4466 __owns_ib_ = false;
4467 }
4468 else
4469 {
4470 __intbuf_ = new char_type[__ibs_];
4471 __owns_ib_ = true;
4472 }
4473 }
4474 else
4475 {
4476 __ibs_ = 0;
4477 __intbuf_ = 0;
4478 __owns_ib_ = false;
4479 }
4480 return this;
4481}
4482
4483template <class _Codecvt, class _Elem, class _Tr>
4484typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4485wbuffer_convert<_Codecvt, _Elem, _Tr>::seekoff(off_type __off, ios_base::seekdir __way,
4486 ios_base::openmode __om)
4487{
4488 int __width = __cv_->encoding();
4489 if (__cv_ == 0 || __bufptr_ == 0 || (__width <= 0 && __off != 0) || sync())
4490 return pos_type(off_type(-1));
4491 // __width > 0 || __off == 0
4492 switch (__way)
4493 {
4494 case ios_base::beg:
4495 break;
4496 case ios_base::cur:
4497 break;
4498 case ios_base::end:
4499 break;
4500 default:
4501 return pos_type(off_type(-1));
4502 }
4503 pos_type __r = __bufptr_->pubseekoff(__width * __off, __way, __om);
4504 __r.state(__st_);
4505 return __r;
4506}
4507
4508template <class _Codecvt, class _Elem, class _Tr>
4509typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4510wbuffer_convert<_Codecvt, _Elem, _Tr>::seekpos(pos_type __sp, ios_base::openmode __wch)
4511{
4512 if (__cv_ == 0 || __bufptr_ == 0 || sync())
4513 return pos_type(off_type(-1));
4514 if (__bufptr_->pubseekpos(__sp, __wch) == pos_type(off_type(-1)))
4515 return pos_type(off_type(-1));
4516 return __sp;
4517}
4518
4519template <class _Codecvt, class _Elem, class _Tr>
4520int
4521wbuffer_convert<_Codecvt, _Elem, _Tr>::sync()
4522{
4523 if (__cv_ == 0 || __bufptr_ == 0)
4524 return 0;
4525 if (__cm_ & ios_base::out)
4526 {
4527 if (this->pptr() != this->pbase())
4528 if (overflow() == traits_type::eof())
4529 return -1;
4530 codecvt_base::result __r;
4531 do
4532 {
4533 char* __extbe;
4534 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
4535 streamsize __nmemb = static_cast<streamsize>(__extbe - __extbuf_);
4536 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4537 return -1;
4538 } while (__r == codecvt_base::partial);
4539 if (__r == codecvt_base::error)
4540 return -1;
4541 if (__bufptr_->pubsync())
4542 return -1;
4543 }
4544 else if (__cm_ & ios_base::in)
4545 {
4546 off_type __c;
4547 if (__always_noconv_)
4548 __c = this->egptr() - this->gptr();
4549 else
4550 {
4551 int __width = __cv_->encoding();
4552 __c = __extbufend_ - __extbufnext_;
4553 if (__width > 0)
4554 __c += __width * (this->egptr() - this->gptr());
4555 else
4556 {
4557 if (this->gptr() != this->egptr())
4558 {
4559 reverse(this->gptr(), this->egptr());
4560 codecvt_base::result __r;
4561 const char_type* __e = this->gptr();
4562 char* __extbe;
4563 do
4564 {
4565 __r = __cv_->out(__st_, __e, this->egptr(), __e,
4566 __extbuf_, __extbuf_ + __ebs_, __extbe);
4567 switch (__r)
4568 {
4569 case codecvt_base::noconv:
4570 __c += this->egptr() - this->gptr();
4571 break;
4572 case codecvt_base::ok:
4573 case codecvt_base::partial:
4574 __c += __extbe - __extbuf_;
4575 break;
4576 default:
4577 return -1;
4578 }
4579 } while (__r == codecvt_base::partial);
4580 }
4581 }
4582 }
4583 if (__bufptr_->pubseekoff(-__c, ios_base::cur, __cm_) == pos_type(off_type(-1)))
4584 return -1;
4585 this->setg(0, 0, 0);
4586 __cm_ = 0;
4587 }
4588 return 0;
4589}
4590
4591template <class _Codecvt, class _Elem, class _Tr>
4592bool
4593wbuffer_convert<_Codecvt, _Elem, _Tr>::__read_mode()
4594{
4595 if (!(__cm_ & ios_base::in))
4596 {
4597 this->setp(0, 0);
4598 if (__always_noconv_)
4599 this->setg((char_type*)__extbuf_,
4600 (char_type*)__extbuf_ + __ebs_,
4601 (char_type*)__extbuf_ + __ebs_);
4602 else
4603 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
4604 __cm_ = ios_base::in;
4605 return true;
4606 }
4607 return false;
4608}
4609
4610template <class _Codecvt, class _Elem, class _Tr>
4611void
4612wbuffer_convert<_Codecvt, _Elem, _Tr>::__write_mode()
4613{
4614 if (!(__cm_ & ios_base::out))
4615 {
4616 this->setg(0, 0, 0);
4617 if (__ebs_ > sizeof(__extbuf_min_))
4618 {
4619 if (__always_noconv_)
4620 this->setp((char_type*)__extbuf_,
4621 (char_type*)__extbuf_ + (__ebs_ - 1));
4622 else
4623 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
4624 }
4625 else
4626 this->setp(0, 0);
4627 __cm_ = ios_base::out;
4628 }
4629}
4630
4631template <class _Codecvt, class _Elem, class _Tr>
4632wbuffer_convert<_Codecvt, _Elem, _Tr>*
4633wbuffer_convert<_Codecvt, _Elem, _Tr>::__close()
4634{
4635 wbuffer_convert* __rt = 0;
4636 if (__cv_ != 0 && __bufptr_ != 0)
4637 {
4638 __rt = this;
4639 if ((__cm_ & ios_base::out) && sync())
4640 __rt = 0;
4641 }
4642 return __rt;
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004643}
4644
Howard Hinnantc51e1022010-05-11 19:42:16 +00004645_LIBCPP_END_NAMESPACE_STD
4646
4647#endif // _LIBCPP_LOCALE