blob: 2e0d0f925cec7b04170b7b3b5b33325f552ada31 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- locale ------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-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 Hinnantbc8d3f92010-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 Hinnantc9834542011-05-31 15:34:58 +000039 locale() noexcept;
40 locale(const locale& other) noexcept;
Howard Hinnantbc8d3f92010-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 Hinnantc9834542011-05-31 15:34:58 +000048 ~locale(); // not virtual
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000049
Howard Hinnantc9834542011-05-31 15:34:58 +000050 const locale& operator=(const locale& other) noexcept;
Howard Hinnantbc8d3f92010-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 Hinnantc9834542011-05-31 15:34:58 +000068template <class Facet> bool has_facet(const locale&) noexcept;
Howard Hinnantbc8d3f92010-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 Hinnantd23b4642010-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
96 wstring_convert(Codecvt* pcvt = new Codecvt);
97 wstring_convert(Codecvt* pcvt, state_type state);
98 wstring_convert(const byte_string& byte_err,
99 const wide_string& wide_err = wide_string());
100 ~wstring_convert();
101
102 wide_string from_bytes(char byte);
103 wide_string from_bytes(const char* ptr);
104 wide_string from_bytes(const byte_string& str);
105 wide_string from_bytes(const char* first, const char* last);
106
107 byte_string to_bytes(Elem wchar);
108 byte_string to_bytes(const Elem* wptr);
109 byte_string to_bytes(const wide_string& wstr);
110 byte_string to_bytes(const Elem* first, const Elem* last);
111
112 size_t converted() const;
113 state_type state() const;
114};
115
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000116template <class Codecvt, class Elem = wchar_t, class Tr = char_traits<Elem>>
Howard Hinnantd23b4642010-05-31 20:58:54 +0000117class wbuffer_convert
118 : public basic_streambuf<Elem, Tr>
119{
120public:
121 typedef typename Tr::state_type state_type;
122
123 wbuffer_convert(streambuf* bytebuf = 0, Codecvt* pcvt = new Codecvt,
124 state_type state = state_type());
125
126 streambuf* rdbuf() const;
127 streambuf* rdbuf(streambuf* bytebuf);
128
129 state_type state() const;
130};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000131
132// 22.4.1 and 22.4.1.3, ctype:
133class ctype_base;
134template <class charT> class ctype;
135template <> class ctype<char>; // specialization
136template <class charT> class ctype_byname;
137template <> class ctype_byname<char>; // specialization
138
139class codecvt_base;
140template <class internT, class externT, class stateT> class codecvt;
141template <class internT, class externT, class stateT> class codecvt_byname;
142
143// 22.4.2 and 22.4.3, numeric:
144template <class charT, class InputIterator> class num_get;
145template <class charT, class OutputIterator> class num_put;
146template <class charT> class numpunct;
147template <class charT> class numpunct_byname;
148
149// 22.4.4, col lation:
150template <class charT> class collate;
151template <class charT> class collate_byname;
152
153// 22.4.5, date and time:
154class time_base;
155template <class charT, class InputIterator> class time_get;
156template <class charT, class InputIterator> class time_get_byname;
157template <class charT, class OutputIterator> class time_put;
158template <class charT, class OutputIterator> class time_put_byname;
159
160// 22.4.6, money:
161class money_base;
162template <class charT, class InputIterator> class money_get;
163template <class charT, class OutputIterator> class money_put;
164template <class charT, bool Intl> class moneypunct;
165template <class charT, bool Intl> class moneypunct_byname;
166
167// 22.4.7, message retrieval:
168class messages_base;
169template <class charT> class messages;
170template <class charT> class messages_byname;
171
172} // std
173
174*/
175
176#include <__config>
177#include <__locale>
178#include <algorithm>
179#include <memory>
180#include <ios>
181#include <streambuf>
182#include <iterator>
183#include <limits>
Marshall Clowdece7fe2013-03-18 17:45:34 +0000184#ifndef __APPLE__
Howard Hinnantadff4892010-05-24 17:49:41 +0000185#include <cstdarg>
186#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000187#include <cstdlib>
188#include <ctime>
Marshall Clowa22d2ad2013-03-18 17:04:29 +0000189#ifdef _WIN32
Howard Hinnant14fa9f92011-09-29 20:33:10 +0000190#include <support/win32/locale_win32.h>
Howard Hinnant6cd05ee2011-09-23 16:11:27 +0000191#else // _WIN32
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000192#include <nl_types.h>
Howard Hinnant92a07002011-09-22 19:10:18 +0000193#endif // !_WIN32
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000194
Marshall Clowdece7fe2013-03-18 17:45:34 +0000195#ifdef __APPLE__
Howard Hinnant537b2fa2012-11-14 21:17:15 +0000196#include <Availability.h>
197#endif
198
Howard Hinnant66c6f972011-11-29 16:45:27 +0000199#include <__undef_min_max>
200
Howard Hinnant08e17472011-10-17 20:05:10 +0000201#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000202#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000203#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000204
205_LIBCPP_BEGIN_NAMESPACE_STD
206
Marshall Clow53e27632013-03-18 19:34:07 +0000207#if defined(__APPLE__) || defined(__FreeBSD__)
Howard Hinnant2ea1ca92011-09-28 21:05:01 +0000208# define _LIBCPP_GET_C_LOCALE 0
209#else
210# define _LIBCPP_GET_C_LOCALE __cloc()
Howard Hinnant866569b2011-09-28 23:39:33 +0000211 // Get the C locale object
212 locale_t __cloc();
213#define __cloc_defined
Howard Hinnant2ea1ca92011-09-28 21:05:01 +0000214#endif
215
Sean Huntf3907e62011-07-15 05:40:33 +0000216typedef _VSTD::remove_pointer<locale_t>::type __locale_struct;
217typedef _VSTD::unique_ptr<__locale_struct, decltype(&freelocale)> __locale_unique_ptr;
David Chisnall8fa14e92012-02-29 13:00:07 +0000218#ifndef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +0000219typedef _VSTD::unique_ptr<__locale_struct, decltype(&uselocale)> __locale_raii;
David Chisnall8fa14e92012-02-29 13:00:07 +0000220#endif
Sean Huntf3907e62011-07-15 05:40:33 +0000221
Howard Hinnantadff4892010-05-24 17:49:41 +0000222// OSX has nice foo_l() functions that let you turn off use of the global
223// locale. Linux, not so much. The following functions avoid the locale when
224// that's possible and otherwise do the wrong thing. FIXME.
Howard Hinnantfc2f0212013-03-29 18:27:28 +0000225#if defined(__linux__) || defined(EMSCRIPTEN)
Sean Huntf3907e62011-07-15 05:40:33 +0000226
227#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
228decltype(MB_CUR_MAX_L(_VSTD::declval<locale_t>()))
Howard Hinnant82894812010-09-22 16:48:34 +0000229inline _LIBCPP_INLINE_VISIBILITY
Sean Huntf3907e62011-07-15 05:40:33 +0000230__mb_cur_max_l(locale_t __l)
Howard Hinnantadff4892010-05-24 17:49:41 +0000231{
Sean Huntf3907e62011-07-15 05:40:33 +0000232 return MB_CUR_MAX_L(__l);
233}
234#else // _LIBCPP_LOCALE__L_EXTENSIONS
235_LIBCPP_ALWAYS_INLINE inline
236decltype(MB_CUR_MAX) __mb_cur_max_l(locale_t __l)
237{
238 __locale_raii __current(uselocale(__l), uselocale);
239 return MB_CUR_MAX;
240}
241#endif // _LIBCPP_LOCALE__L_EXTENSIONS
242
243_LIBCPP_ALWAYS_INLINE inline
244wint_t __btowc_l(int __c, locale_t __l)
245{
Howard Hinnant866569b2011-09-28 23:39:33 +0000246#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +0000247 return btowc_l(__c, __l);
248#else
249 __locale_raii __current(uselocale(__l), uselocale);
250 return btowc(__c);
251#endif
Sean Huntc97da3a2011-07-13 06:40:50 +0000252}
Howard Hinnant8d756322011-07-13 15:48:16 +0000253
Sean Huntf3907e62011-07-15 05:40:33 +0000254_LIBCPP_ALWAYS_INLINE inline
255int __wctob_l(wint_t __c, locale_t __l)
Sean Huntc97da3a2011-07-13 06:40:50 +0000256{
Sean Huntf3907e62011-07-15 05:40:33 +0000257#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
258 return wctob_l(__c, __l);
259#else
260 __locale_raii __current(uselocale(__l), uselocale);
261 return wctob(__c);
262#endif
Sean Huntc97da3a2011-07-13 06:40:50 +0000263}
Sean Huntf3907e62011-07-15 05:40:33 +0000264
265_LIBCPP_ALWAYS_INLINE inline
266size_t __wcsnrtombs_l(char *__dest, const wchar_t **__src, size_t __nwc,
267 size_t __len, mbstate_t *__ps, locale_t __l)
268{
269#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
270 return wcsnrtombs_l(__dest, __src, __nwc, __len, __ps, __l);
271#else
272 __locale_raii __current(uselocale(__l), uselocale);
273 return wcsnrtombs(__dest, __src, __nwc, __len, __ps);
274#endif
275}
276
277_LIBCPP_ALWAYS_INLINE inline
278size_t __wcrtomb_l(char *__s, wchar_t __wc, mbstate_t *__ps, locale_t __l)
279{
280#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
281 return wcrtomb_l(__s, __wc, __ps, __l);
282#else
283 __locale_raii __current(uselocale(__l), uselocale);
284 return wcrtomb(__s, __wc, __ps);
285#endif
286}
287
288_LIBCPP_ALWAYS_INLINE inline
289size_t __mbsnrtowcs_l(wchar_t * __dest, const char **__src, size_t __nms,
290 size_t __len, mbstate_t *__ps, locale_t __l)
291{
292#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
David Chisnallc512df12011-09-21 08:39:44 +0000293 return mbsnrtowcs_l(__dest, __src, __nms, __len, __ps, __l);
Sean Huntf3907e62011-07-15 05:40:33 +0000294#else
295 __locale_raii __current(uselocale(__l), uselocale);
296 return mbsnrtowcs(__dest, __src, __nms, __len, __ps);
297#endif
298}
299
300_LIBCPP_ALWAYS_INLINE inline
301size_t __mbrtowc_l(wchar_t *__pwc, const char *__s, size_t __n,
302 mbstate_t *__ps, locale_t __l)
303{
304#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
305 return mbrtowc_l(__pwc, __s, __n, __ps, __l);
306#else
307 __locale_raii __current(uselocale(__l), uselocale);
308 return mbrtowc(__pwc, __s, __n, __ps);
309#endif
310}
311
312_LIBCPP_ALWAYS_INLINE inline
313int __mbtowc_l(wchar_t *__pwc, const char *__pmb, size_t __max, locale_t __l)
314{
315#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
David Chisnallc512df12011-09-21 08:39:44 +0000316 return mbtowc_l(__pwc, __pmb, __max, __l);
Sean Huntf3907e62011-07-15 05:40:33 +0000317#else
318 __locale_raii __current(uselocale(__l), uselocale);
319 return mbtowc(__pwc, __pmb, __max);
320#endif
321}
322
323_LIBCPP_ALWAYS_INLINE inline
324size_t __mbrlen_l(const char *__s, size_t __n, mbstate_t *__ps, locale_t __l)
325{
326#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
327 return mbrlen_l(__s, __n, __ps, __l);
328#else
329 __locale_raii __current(uselocale(__l), uselocale);
330 return mbrlen(__s, __n, __ps);
331#endif
332}
333
334_LIBCPP_ALWAYS_INLINE inline
335lconv *__localeconv_l(locale_t __l)
336{
337#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
338 return localeconv_l(__l);
339#else
340 __locale_raii __current(uselocale(__l), uselocale);
341 return localeconv();
342#endif
343}
344
345_LIBCPP_ALWAYS_INLINE inline
346size_t __mbsrtowcs_l(wchar_t *__dest, const char **__src, size_t __len,
347 mbstate_t *__ps, locale_t __l)
348{
349#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
350 return mbsrtowcs_l(__dest, __src, __len, __ps, __l);
351#else
352 __locale_raii __current(uselocale(__l), uselocale);
353 return mbsrtowcs(__dest, __src, __len, __ps);
354#endif
355}
356
Chandler Carruthed9f69d2012-12-31 06:09:54 +0000357inline
Sean Huntf3907e62011-07-15 05:40:33 +0000358int __sprintf_l(char *__s, locale_t __l, const char *__format, ...) {
359 va_list __va;
360 va_start(__va, __format);
361#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
362 int __res = vsprintf_l(__s, __l, __format, __va);
363#else
364 __locale_raii __current(uselocale(__l), uselocale);
365 int __res = vsprintf(__s, __format, __va);
366#endif
367 va_end(__va);
368 return __res;
369}
370
Chandler Carruthed9f69d2012-12-31 06:09:54 +0000371inline
Sean Huntf3907e62011-07-15 05:40:33 +0000372int __snprintf_l(char *__s, size_t __n, locale_t __l, const char *__format, ...) {
373 va_list __va;
374 va_start(__va, __format);
375#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
376 int __res = vsnprintf_l(__s, __n, __l, __format, __va);
377#else
378 __locale_raii __current(uselocale(__l), uselocale);
379 int __res = vsnprintf(__s, __n, __format, __va);
380#endif
381 va_end(__va);
382 return __res;
383}
384
Chandler Carruthed9f69d2012-12-31 06:09:54 +0000385inline
Sean Huntf3907e62011-07-15 05:40:33 +0000386int __asprintf_l(char **__s, locale_t __l, const char *__format, ...) {
387 va_list __va;
388 va_start(__va, __format);
389#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
390 int __res = vasprintf_l(__s, __l, __format, __va);
391#else
392 __locale_raii __current(uselocale(__l), uselocale);
393 int __res = vasprintf(__s, __format, __va);
394#endif
395 va_end(__va);
396 return __res;
397}
398
Chandler Carruthed9f69d2012-12-31 06:09:54 +0000399inline
Sean Huntf3907e62011-07-15 05:40:33 +0000400int __sscanf_l(const char *__s, locale_t __l, const char *__format, ...) {
401 va_list __va;
402 va_start(__va, __format);
403#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
404 int __res = vsscanf_l(__s, __l, __format, __va);
405#else
406 __locale_raii __current(uselocale(__l), uselocale);
407 int __res = vsscanf(__s, __format, __va);
408#endif
409 va_end(__va);
410 return __res;
411}
412
Howard Hinnant866569b2011-09-28 23:39:33 +0000413#endif // __linux__
Howard Hinnantadff4892010-05-24 17:49:41 +0000414
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000415// __scan_keyword
416// Scans [__b, __e) until a match is found in the basic_strings range
417// [__kb, __ke) or until it can be shown that there is no match in [__kb, __ke).
418// __b will be incremented (visibly), consuming CharT until a match is found
419// or proved to not exist. A keyword may be "", in which will match anything.
420// If one keyword is a prefix of another, and the next CharT in the input
421// might match another keyword, the algorithm will attempt to find the longest
422// matching keyword. If the longer matching keyword ends up not matching, then
423// no keyword match is found. If no keyword match is found, __ke is returned
424// and failbit is set in __err.
425// Else an iterator pointing to the matching keyword is found. If more than
426// one keyword matches, an iterator to the first matching keyword is returned.
427// If on exit __b == __e, eofbit is set in __err. If __case_senstive is false,
428// __ct is used to force to lower case before comparing characters.
429// Examples:
430// Keywords: "a", "abb"
431// If the input is "a", the first keyword matches and eofbit is set.
432// If the input is "abc", no match is found and "ab" are consumed.
433template <class _InputIterator, class _ForwardIterator, class _Ctype>
434_LIBCPP_HIDDEN
435_ForwardIterator
436__scan_keyword(_InputIterator& __b, _InputIterator __e,
437 _ForwardIterator __kb, _ForwardIterator __ke,
438 const _Ctype& __ct, ios_base::iostate& __err,
439 bool __case_sensitive = true)
440{
441 typedef typename iterator_traits<_InputIterator>::value_type _CharT;
Howard Hinnantec3773c2011-12-01 20:21:04 +0000442 size_t __nkw = static_cast<size_t>(_VSTD::distance(__kb, __ke));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000443 const unsigned char __doesnt_match = '\0';
444 const unsigned char __might_match = '\1';
445 const unsigned char __does_match = '\2';
446 unsigned char __statbuf[100];
447 unsigned char* __status = __statbuf;
448 unique_ptr<unsigned char, void(*)(void*)> __stat_hold(0, free);
449 if (__nkw > sizeof(__statbuf))
450 {
451 __status = (unsigned char*)malloc(__nkw);
452 if (__status == 0)
453 __throw_bad_alloc();
454 __stat_hold.reset(__status);
455 }
456 size_t __n_might_match = __nkw; // At this point, any keyword might match
457 size_t __n_does_match = 0; // but none of them definitely do
458 // Initialize all statuses to __might_match, except for "" keywords are __does_match
459 unsigned char* __st = __status;
460 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, ++__st)
461 {
462 if (!__ky->empty())
463 *__st = __might_match;
464 else
465 {
466 *__st = __does_match;
467 --__n_might_match;
468 ++__n_does_match;
469 }
470 }
471 // While there might be a match, test keywords against the next CharT
472 for (size_t __indx = 0; __b != __e && __n_might_match > 0; ++__indx)
473 {
474 // Peek at the next CharT but don't consume it
475 _CharT __c = *__b;
476 if (!__case_sensitive)
477 __c = __ct.toupper(__c);
478 bool __consume = false;
479 // For each keyword which might match, see if the __indx character is __c
480 // If a match if found, consume __c
481 // If a match is found, and that is the last character in the keyword,
482 // then that keyword matches.
483 // If the keyword doesn't match this character, then change the keyword
484 // to doesn't match
485 __st = __status;
486 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, ++__st)
487 {
488 if (*__st == __might_match)
489 {
490 _CharT __kc = (*__ky)[__indx];
491 if (!__case_sensitive)
492 __kc = __ct.toupper(__kc);
493 if (__c == __kc)
494 {
495 __consume = true;
496 if (__ky->size() == __indx+1)
497 {
498 *__st = __does_match;
499 --__n_might_match;
500 ++__n_does_match;
501 }
502 }
503 else
504 {
505 *__st = __doesnt_match;
506 --__n_might_match;
507 }
508 }
509 }
510 // consume if we matched a character
511 if (__consume)
512 {
513 ++__b;
514 // If we consumed a character and there might be a matched keyword that
515 // was marked matched on a previous iteration, then such keywords
516 // which are now marked as not matching.
517 if (__n_might_match + __n_does_match > 1)
518 {
519 __st = __status;
520 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, ++__st)
521 {
522 if (*__st == __does_match && __ky->size() != __indx+1)
523 {
524 *__st = __doesnt_match;
525 --__n_does_match;
526 }
527 }
528 }
529 }
530 }
531 // We've exited the loop because we hit eof and/or we have no more "might matches".
532 if (__b == __e)
533 __err |= ios_base::eofbit;
534 // Return the first matching result
535 for (__st = __status; __kb != __ke; ++__kb, ++__st)
536 if (*__st == __does_match)
537 break;
538 if (__kb == __ke)
539 __err |= ios_base::failbit;
540 return __kb;
541}
542
543struct __num_get_base
544{
545 static const int __num_get_buf_sz = 40;
546
547 static int __get_base(ios_base&);
548 static const char __src[33];
549};
550
551void __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end,
552 ios_base::iostate& __err);
553
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000554template <class _CharT>
555struct __num_get
556 : protected __num_get_base
557{
558 static string __stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep);
559 static string __stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
560 _CharT& __thousands_sep);
561 static int __stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
562 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
563 unsigned* __g, unsigned*& __g_end, _CharT* __atoms);
564 static int __stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp,
565 char* __a, char*& __a_end,
566 _CharT __decimal_point, _CharT __thousands_sep,
567 const string& __grouping, unsigned* __g,
568 unsigned*& __g_end, unsigned& __dc, _CharT* __atoms);
569};
570
571template <class _CharT>
572string
573__num_get<_CharT>::__stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep)
574{
575 locale __loc = __iob.getloc();
576 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 26, __atoms);
577 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
578 __thousands_sep = __np.thousands_sep();
579 return __np.grouping();
580}
581
582template <class _CharT>
583string
584__num_get<_CharT>::__stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
585 _CharT& __thousands_sep)
586{
587 locale __loc = __iob.getloc();
588 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 32, __atoms);
589 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
590 __decimal_point = __np.decimal_point();
591 __thousands_sep = __np.thousands_sep();
592 return __np.grouping();
593}
594
595template <class _CharT>
596int
597__num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
598 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
599 unsigned* __g, unsigned*& __g_end, _CharT* __atoms)
600{
Howard Hinnant80586722011-03-09 01:03:19 +0000601 if (__a_end == __a && (__ct == __atoms[24] || __ct == __atoms[25]))
602 {
603 *__a_end++ = __ct == __atoms[24] ? '+' : '-';
604 __dc = 0;
605 return 0;
606 }
Howard Hinnantec3773c2011-12-01 20:21:04 +0000607 if (__grouping.size() != 0 && __ct == __thousands_sep)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000608 {
609 if (__g_end-__g < __num_get_buf_sz)
610 {
611 *__g_end++ = __dc;
612 __dc = 0;
613 }
614 return 0;
615 }
616 ptrdiff_t __f = find(__atoms, __atoms + 26, __ct) - __atoms;
Howard Hinnant80586722011-03-09 01:03:19 +0000617 if (__f >= 24)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000618 return -1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000619 switch (__base)
620 {
621 case 8:
622 case 10:
623 if (__f >= __base)
Howard Hinnant80586722011-03-09 01:03:19 +0000624 return -1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000625 break;
Howard Hinnant80586722011-03-09 01:03:19 +0000626 case 16:
627 if (__f < 22)
628 break;
629 if (__a_end != __a && __a_end - __a <= 2 && __a_end[-1] == '0')
630 {
631 __dc = 0;
632 *__a_end++ = __src[__f];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000633 return 0;
Howard Hinnant80586722011-03-09 01:03:19 +0000634 }
635 return -1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000636 }
Howard Hinnant80586722011-03-09 01:03:19 +0000637 if (__a_end-__a < __num_get_buf_sz - 1)
638 *__a_end++ = __src[__f];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000639 ++__dc;
640 return 0;
641}
642
643template <class _CharT>
644int
645__num_get<_CharT>::__stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp, char* __a, char*& __a_end,
646 _CharT __decimal_point, _CharT __thousands_sep, const string& __grouping,
647 unsigned* __g, unsigned*& __g_end, unsigned& __dc, _CharT* __atoms)
648{
Howard Hinnant6319f142013-03-08 19:06:24 +0000649 if (__a_end-__a >= __num_get_buf_sz - 1)
650 return -1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000651 if (__ct == __decimal_point)
652 {
653 if (!__in_units)
654 return -1;
655 __in_units = false;
656 *__a_end++ = '.';
657 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
658 *__g_end++ = __dc;
659 return 0;
660 }
661 if (__ct == __thousands_sep && __grouping.size() != 0)
662 {
663 if (!__in_units)
664 return -1;
665 if (__g_end-__g < __num_get_buf_sz)
666 {
667 *__g_end++ = __dc;
668 __dc = 0;
669 }
670 return 0;
671 }
672 ptrdiff_t __f = find(__atoms, __atoms + 32, __ct) - __atoms;
673 if (__f >= 32)
674 return -1;
675 char __x = __src[__f];
Howard Hinnantb04ad412012-02-15 19:19:37 +0000676 if (__x == '-' || __x == '+')
677 {
Howard Hinnant6319f142013-03-08 19:06:24 +0000678 if (__a_end == __a || (__a_end[-1] & 0x5F) == (__exp & 0x7F))
Howard Hinnantb04ad412012-02-15 19:19:37 +0000679 {
680 *__a_end++ = __x;
681 return 0;
682 }
683 return -1;
684 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000685 if (__x == 'x' || __x == 'X')
686 __exp = 'P';
Howard Hinnant6319f142013-03-08 19:06:24 +0000687 else if ((__x & 0x5F) == __exp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000688 {
Howard Hinnant6319f142013-03-08 19:06:24 +0000689 __exp |= 0x80;
690 if (__in_units)
691 {
692 __in_units = false;
693 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
694 *__g_end++ = __dc;
695 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000696 }
Howard Hinnant6319f142013-03-08 19:06:24 +0000697 if (__a_end-__a < __num_get_buf_sz - ((__exp & 0x80) ? 1 : 11))
698 *__a_end++ = __x;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000699 if (__f >= 22)
700 return 0;
701 ++__dc;
702 return 0;
703}
704
Howard Hinnantff926772012-11-06 21:08:48 +0000705_LIBCPP_EXTERN_TEMPLATE(struct __num_get<char>)
706_LIBCPP_EXTERN_TEMPLATE(struct __num_get<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000707
708template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Howard Hinnant83eade62013-03-06 23:30:19 +0000709class _LIBCPP_TYPE_VIS num_get
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000710 : public locale::facet,
711 private __num_get<_CharT>
712{
713public:
714 typedef _CharT char_type;
715 typedef _InputIterator iter_type;
716
717 _LIBCPP_ALWAYS_INLINE
718 explicit num_get(size_t __refs = 0)
719 : locale::facet(__refs) {}
720
721 _LIBCPP_ALWAYS_INLINE
722 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
723 ios_base::iostate& __err, bool& __v) const
724 {
725 return do_get(__b, __e, __iob, __err, __v);
726 }
727
728 _LIBCPP_ALWAYS_INLINE
729 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
730 ios_base::iostate& __err, long& __v) const
731 {
732 return do_get(__b, __e, __iob, __err, __v);
733 }
734
735 _LIBCPP_ALWAYS_INLINE
736 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
737 ios_base::iostate& __err, long long& __v) const
738 {
739 return do_get(__b, __e, __iob, __err, __v);
740 }
741
742 _LIBCPP_ALWAYS_INLINE
743 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
744 ios_base::iostate& __err, unsigned short& __v) const
745 {
746 return do_get(__b, __e, __iob, __err, __v);
747 }
748
749 _LIBCPP_ALWAYS_INLINE
750 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
751 ios_base::iostate& __err, unsigned int& __v) const
752 {
753 return do_get(__b, __e, __iob, __err, __v);
754 }
755
756 _LIBCPP_ALWAYS_INLINE
757 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
758 ios_base::iostate& __err, unsigned long& __v) const
759 {
760 return do_get(__b, __e, __iob, __err, __v);
761 }
762
763 _LIBCPP_ALWAYS_INLINE
764 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
765 ios_base::iostate& __err, unsigned long long& __v) const
766 {
767 return do_get(__b, __e, __iob, __err, __v);
768 }
769
770 _LIBCPP_ALWAYS_INLINE
771 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
772 ios_base::iostate& __err, float& __v) const
773 {
774 return do_get(__b, __e, __iob, __err, __v);
775 }
776
777 _LIBCPP_ALWAYS_INLINE
778 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
779 ios_base::iostate& __err, double& __v) const
780 {
781 return do_get(__b, __e, __iob, __err, __v);
782 }
783
784 _LIBCPP_ALWAYS_INLINE
785 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
786 ios_base::iostate& __err, long double& __v) const
787 {
788 return do_get(__b, __e, __iob, __err, __v);
789 }
790
791 _LIBCPP_ALWAYS_INLINE
792 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
793 ios_base::iostate& __err, void*& __v) const
794 {
795 return do_get(__b, __e, __iob, __err, __v);
796 }
797
798 static locale::id id;
799
800protected:
Howard Hinnant82894812010-09-22 16:48:34 +0000801 _LIBCPP_ALWAYS_INLINE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000802 ~num_get() {}
803
804 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
805 ios_base::iostate& __err, bool& __v) const;
806 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
807 ios_base::iostate& __err, long& __v) const;
808 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
809 ios_base::iostate& __err, long long& __v) const;
810 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
811 ios_base::iostate& __err, unsigned short& __v) const;
812 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
813 ios_base::iostate& __err, unsigned int& __v) const;
814 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
815 ios_base::iostate& __err, unsigned long& __v) const;
816 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
817 ios_base::iostate& __err, unsigned long long& __v) const;
818 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
819 ios_base::iostate& __err, float& __v) const;
820 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
821 ios_base::iostate& __err, double& __v) const;
822 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
823 ios_base::iostate& __err, long double& __v) const;
824 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
825 ios_base::iostate& __err, void*& __v) const;
826};
827
828template <class _CharT, class _InputIterator>
829locale::id
830num_get<_CharT, _InputIterator>::id;
831
832template <class _Tp>
833_Tp
834__num_get_signed_integral(const char* __a, const char* __a_end,
835 ios_base::iostate& __err, int __base)
836{
837 if (__a != __a_end)
838 {
Howard Hinnant54e2fff2013-01-22 17:26:08 +0000839 typename remove_reference<decltype(errno)>::type __save_errno = errno;
Howard Hinnante7c8da62011-02-25 19:52:41 +0000840 errno = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000841 char *__p2;
Howard Hinnant2ea1ca92011-09-28 21:05:01 +0000842 long long __ll = strtoll_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
Howard Hinnant54e2fff2013-01-22 17:26:08 +0000843 typename remove_reference<decltype(errno)>::type __current_errno = errno;
Howard Hinnante7c8da62011-02-25 19:52:41 +0000844 if (__current_errno == 0)
845 errno = __save_errno;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000846 if (__p2 != __a_end)
847 {
848 __err = ios_base::failbit;
849 return 0;
850 }
Howard Hinnante7c8da62011-02-25 19:52:41 +0000851 else if (__current_errno == ERANGE ||
852 __ll < numeric_limits<_Tp>::min() ||
853 numeric_limits<_Tp>::max() < __ll)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000854 {
855 __err = ios_base::failbit;
Howard Hinnante7c8da62011-02-25 19:52:41 +0000856 if (__ll > 0)
857 return numeric_limits<_Tp>::max();
858 else
859 return numeric_limits<_Tp>::min();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000860 }
861 return static_cast<_Tp>(__ll);
862 }
863 __err = ios_base::failbit;
864 return 0;
865}
866
867template <class _Tp>
868_Tp
869__num_get_unsigned_integral(const char* __a, const char* __a_end,
870 ios_base::iostate& __err, int __base)
871{
872 if (__a != __a_end)
873 {
Howard Hinnante7c8da62011-02-25 19:52:41 +0000874 if (*__a == '-')
875 {
876 __err = ios_base::failbit;
877 return 0;
878 }
Howard Hinnant54e2fff2013-01-22 17:26:08 +0000879 typename remove_reference<decltype(errno)>::type __save_errno = errno;
Howard Hinnante7c8da62011-02-25 19:52:41 +0000880 errno = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000881 char *__p2;
Howard Hinnant2ea1ca92011-09-28 21:05:01 +0000882 unsigned long long __ll = strtoull_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
Howard Hinnant54e2fff2013-01-22 17:26:08 +0000883 typename remove_reference<decltype(errno)>::type __current_errno = errno;
Howard Hinnante7c8da62011-02-25 19:52:41 +0000884 if (__current_errno == 0)
885 errno = __save_errno;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000886 if (__p2 != __a_end)
887 {
888 __err = ios_base::failbit;
889 return 0;
890 }
Howard Hinnante7c8da62011-02-25 19:52:41 +0000891 else if (__current_errno == ERANGE ||
892 numeric_limits<_Tp>::max() < __ll)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000893 {
894 __err = ios_base::failbit;
895 return numeric_limits<_Tp>::max();
896 }
897 return static_cast<_Tp>(__ll);
898 }
899 __err = ios_base::failbit;
900 return 0;
901}
902
903template <class _Tp>
904_Tp
905__num_get_float(const char* __a, const char* __a_end, ios_base::iostate& __err)
906{
907 if (__a != __a_end)
908 {
Howard Hinnant4f671002013-04-13 18:19:25 +0000909 typename remove_reference<decltype(errno)>::type __save_errno = errno;
910 errno = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000911 char *__p2;
Howard Hinnant2ea1ca92011-09-28 21:05:01 +0000912 long double __ld = strtold_l(__a, &__p2, _LIBCPP_GET_C_LOCALE);
Howard Hinnant4f671002013-04-13 18:19:25 +0000913 typename remove_reference<decltype(errno)>::type __current_errno = errno;
914 if (__current_errno == 0)
915 errno = __save_errno;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000916 if (__p2 != __a_end)
917 {
918 __err = ios_base::failbit;
919 return 0;
920 }
Howard Hinnant4f671002013-04-13 18:19:25 +0000921 else if (__current_errno == ERANGE)
922 __err = ios_base::failbit;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000923 return static_cast<_Tp>(__ld);
924 }
925 __err = ios_base::failbit;
926 return 0;
927}
928
929template <class _CharT, class _InputIterator>
930_InputIterator
931num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
932 ios_base& __iob,
933 ios_base::iostate& __err,
934 bool& __v) const
935{
936 if ((__iob.flags() & ios_base::boolalpha) == 0)
937 {
938 long __lv = -1;
939 __b = do_get(__b, __e, __iob, __err, __lv);
940 switch (__lv)
941 {
942 case 0:
943 __v = false;
944 break;
945 case 1:
946 __v = true;
947 break;
948 default:
949 __v = true;
950 __err = ios_base::failbit;
951 break;
952 }
953 return __b;
954 }
955 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__iob.getloc());
956 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__iob.getloc());
957 typedef typename numpunct<_CharT>::string_type string_type;
958 const string_type __names[2] = {__np.truename(), __np.falsename()};
959 const string_type* __i = __scan_keyword(__b, __e, __names, __names+2,
960 __ct, __err);
961 __v = __i == __names;
962 return __b;
963}
964
965template <class _CharT, class _InputIterator>
966_InputIterator
967num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
968 ios_base& __iob,
969 ios_base::iostate& __err,
970 long& __v) const
971{
972 // Stage 1
973 int __base = this->__get_base(__iob);
974 // Stage 2
975 char_type __atoms[26];
976 char_type __thousands_sep;
977 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
978 char __a[__num_get_base::__num_get_buf_sz] = {0};
979 char* __a_end = __a;
980 unsigned __g[__num_get_base::__num_get_buf_sz];
981 unsigned* __g_end = __g;
982 unsigned __dc = 0;
983 for (; __b != __e; ++__b)
Howard Hinnant324bb032010-08-22 00:02:43 +0000984 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000985 __thousands_sep, __grouping, __g, __g_end,
986 __atoms))
987 break;
988 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
989 *__g_end++ = __dc;
990 // Stage 3
991 __v = __num_get_signed_integral<long>(__a, __a_end, __err, __base);
992 // Digit grouping checked
993 __check_grouping(__grouping, __g, __g_end, __err);
994 // EOF checked
995 if (__b == __e)
996 __err |= ios_base::eofbit;
997 return __b;
998}
999
1000template <class _CharT, class _InputIterator>
1001_InputIterator
1002num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1003 ios_base& __iob,
1004 ios_base::iostate& __err,
1005 long long& __v) const
1006{
1007 // Stage 1
1008 int __base = this->__get_base(__iob);
1009 // Stage 2
1010 char_type __atoms[26];
1011 char_type __thousands_sep;
1012 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
1013 char __a[__num_get_base::__num_get_buf_sz] = {0};
1014 char* __a_end = __a;
1015 unsigned __g[__num_get_base::__num_get_buf_sz];
1016 unsigned* __g_end = __g;
1017 unsigned __dc = 0;
1018 for (; __b != __e; ++__b)
Howard Hinnant324bb032010-08-22 00:02:43 +00001019 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1020 __thousands_sep, __grouping, __g, __g_end,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001021 __atoms))
1022 break;
1023 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1024 *__g_end++ = __dc;
1025 // Stage 3
1026 __v = __num_get_signed_integral<long long>(__a, __a_end, __err, __base);
1027 // Digit grouping checked
1028 __check_grouping(__grouping, __g, __g_end, __err);
1029 // EOF checked
1030 if (__b == __e)
1031 __err |= ios_base::eofbit;
1032 return __b;
1033}
1034
1035template <class _CharT, class _InputIterator>
1036_InputIterator
1037num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1038 ios_base& __iob,
1039 ios_base::iostate& __err,
1040 unsigned short& __v) const
1041{
1042 // Stage 1
1043 int __base = this->__get_base(__iob);
1044 // Stage 2
1045 char_type __atoms[26];
1046 char_type __thousands_sep;
1047 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
1048 char __a[__num_get_base::__num_get_buf_sz] = {0};
1049 char* __a_end = __a;
1050 unsigned __g[__num_get_base::__num_get_buf_sz];
1051 unsigned* __g_end = __g;
1052 unsigned __dc = 0;
1053 for (; __b != __e; ++__b)
Howard Hinnant324bb032010-08-22 00:02:43 +00001054 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001055 __thousands_sep, __grouping, __g, __g_end,
1056 __atoms))
1057 break;
1058 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1059 *__g_end++ = __dc;
1060 // Stage 3
1061 __v = __num_get_unsigned_integral<unsigned short>(__a, __a_end, __err, __base);
1062 // Digit grouping checked
1063 __check_grouping(__grouping, __g, __g_end, __err);
1064 // EOF checked
1065 if (__b == __e)
1066 __err |= ios_base::eofbit;
1067 return __b;
1068}
1069
1070template <class _CharT, class _InputIterator>
1071_InputIterator
1072num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1073 ios_base& __iob,
1074 ios_base::iostate& __err,
1075 unsigned int& __v) const
1076{
1077 // Stage 1
1078 int __base = this->__get_base(__iob);
1079 // Stage 2
1080 char_type __atoms[26];
1081 char_type __thousands_sep;
1082 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
1083 char __a[__num_get_base::__num_get_buf_sz] = {0};
1084 char* __a_end = __a;
1085 unsigned __g[__num_get_base::__num_get_buf_sz];
1086 unsigned* __g_end = __g;
1087 unsigned __dc = 0;
1088 for (; __b != __e; ++__b)
Howard Hinnant324bb032010-08-22 00:02:43 +00001089 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001090 __thousands_sep, __grouping, __g, __g_end,
1091 __atoms))
1092 break;
1093 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1094 *__g_end++ = __dc;
1095 // Stage 3
1096 __v = __num_get_unsigned_integral<unsigned int>(__a, __a_end, __err, __base);
1097 // Digit grouping checked
1098 __check_grouping(__grouping, __g, __g_end, __err);
1099 // EOF checked
1100 if (__b == __e)
1101 __err |= ios_base::eofbit;
1102 return __b;
1103}
1104
1105template <class _CharT, class _InputIterator>
1106_InputIterator
1107num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1108 ios_base& __iob,
1109 ios_base::iostate& __err,
1110 unsigned long& __v) const
1111{
1112 // Stage 1
1113 int __base = this->__get_base(__iob);
1114 // Stage 2
1115 char_type __atoms[26];
1116 char_type __thousands_sep;
1117 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
1118 char __a[__num_get_base::__num_get_buf_sz] = {0};
1119 char* __a_end = __a;
1120 unsigned __g[__num_get_base::__num_get_buf_sz];
1121 unsigned* __g_end = __g;
1122 unsigned __dc = 0;
1123 for (; __b != __e; ++__b)
Howard Hinnant324bb032010-08-22 00:02:43 +00001124 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001125 __thousands_sep, __grouping, __g, __g_end,
1126 __atoms))
1127 break;
1128 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1129 *__g_end++ = __dc;
1130 // Stage 3
1131 __v = __num_get_unsigned_integral<unsigned long>(__a, __a_end, __err, __base);
1132 // Digit grouping checked
1133 __check_grouping(__grouping, __g, __g_end, __err);
1134 // EOF checked
1135 if (__b == __e)
1136 __err |= ios_base::eofbit;
1137 return __b;
1138}
1139
1140template <class _CharT, class _InputIterator>
1141_InputIterator
1142num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1143 ios_base& __iob,
1144 ios_base::iostate& __err,
1145 unsigned long long& __v) const
1146{
1147 // Stage 1
1148 int __base = this->__get_base(__iob);
1149 // Stage 2
1150 char_type __atoms[26];
1151 char_type __thousands_sep;
1152 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
1153 char __a[__num_get_base::__num_get_buf_sz] = {0};
1154 char* __a_end = __a;
1155 unsigned __g[__num_get_base::__num_get_buf_sz];
1156 unsigned* __g_end = __g;
1157 unsigned __dc = 0;
1158 for (; __b != __e; ++__b)
Howard Hinnant324bb032010-08-22 00:02:43 +00001159 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001160 __thousands_sep, __grouping, __g, __g_end,
1161 __atoms))
1162 break;
1163 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1164 *__g_end++ = __dc;
1165 // Stage 3
1166 __v = __num_get_unsigned_integral<unsigned long long>(__a, __a_end, __err, __base);
1167 // Digit grouping checked
1168 __check_grouping(__grouping, __g, __g_end, __err);
1169 // EOF checked
1170 if (__b == __e)
1171 __err |= ios_base::eofbit;
1172 return __b;
1173}
1174
1175template <class _CharT, class _InputIterator>
1176_InputIterator
1177num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1178 ios_base& __iob,
1179 ios_base::iostate& __err,
1180 float& __v) const
1181{
1182 // Stage 1, nothing to do
1183 // Stage 2
1184 char_type __atoms[32];
1185 char_type __decimal_point;
1186 char_type __thousands_sep;
Howard Hinnant324bb032010-08-22 00:02:43 +00001187 string __grouping = this->__stage2_float_prep(__iob, __atoms,
1188 __decimal_point,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001189 __thousands_sep);
1190 char __a[__num_get_base::__num_get_buf_sz] = {0};
1191 char* __a_end = __a;
1192 unsigned __g[__num_get_base::__num_get_buf_sz];
1193 unsigned* __g_end = __g;
1194 unsigned __dc = 0;
1195 bool __in_units = true;
1196 char __exp = 'E';
1197 for (; __b != __e; ++__b)
Howard Hinnant324bb032010-08-22 00:02:43 +00001198 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1199 __decimal_point, __thousands_sep,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001200 __grouping, __g, __g_end,
1201 __dc, __atoms))
1202 break;
1203 if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1204 *__g_end++ = __dc;
1205 // Stage 3
1206 __v = __num_get_float<float>(__a, __a_end, __err);
1207 // Digit grouping checked
1208 __check_grouping(__grouping, __g, __g_end, __err);
1209 // EOF checked
1210 if (__b == __e)
1211 __err |= ios_base::eofbit;
1212 return __b;
1213}
1214
1215template <class _CharT, class _InputIterator>
1216_InputIterator
1217num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1218 ios_base& __iob,
1219 ios_base::iostate& __err,
1220 double& __v) const
1221{
1222 // Stage 1, nothing to do
1223 // Stage 2
1224 char_type __atoms[32];
1225 char_type __decimal_point;
1226 char_type __thousands_sep;
Howard Hinnant324bb032010-08-22 00:02:43 +00001227 string __grouping = this->__stage2_float_prep(__iob, __atoms,
1228 __decimal_point,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001229 __thousands_sep);
1230 char __a[__num_get_base::__num_get_buf_sz] = {0};
1231 char* __a_end = __a;
1232 unsigned __g[__num_get_base::__num_get_buf_sz];
1233 unsigned* __g_end = __g;
1234 unsigned __dc = 0;
1235 bool __in_units = true;
1236 char __exp = 'E';
1237 for (; __b != __e; ++__b)
Howard Hinnant324bb032010-08-22 00:02:43 +00001238 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1239 __decimal_point, __thousands_sep,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001240 __grouping, __g, __g_end,
1241 __dc, __atoms))
1242 break;
1243 if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1244 *__g_end++ = __dc;
1245 // Stage 3
1246 __v = __num_get_float<double>(__a, __a_end, __err);
1247 // Digit grouping checked
1248 __check_grouping(__grouping, __g, __g_end, __err);
1249 // EOF checked
1250 if (__b == __e)
1251 __err |= ios_base::eofbit;
1252 return __b;
1253}
1254
1255template <class _CharT, class _InputIterator>
1256_InputIterator
1257num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1258 ios_base& __iob,
1259 ios_base::iostate& __err,
1260 long double& __v) const
1261{
1262 // Stage 1, nothing to do
1263 // Stage 2
1264 char_type __atoms[32];
1265 char_type __decimal_point;
1266 char_type __thousands_sep;
Howard Hinnant324bb032010-08-22 00:02:43 +00001267 string __grouping = this->__stage2_float_prep(__iob, __atoms,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001268 __decimal_point,
1269 __thousands_sep);
1270 char __a[__num_get_base::__num_get_buf_sz] = {0};
1271 char* __a_end = __a;
1272 unsigned __g[__num_get_base::__num_get_buf_sz];
1273 unsigned* __g_end = __g;
1274 unsigned __dc = 0;
1275 bool __in_units = true;
1276 char __exp = 'E';
1277 for (; __b != __e; ++__b)
Howard Hinnant324bb032010-08-22 00:02:43 +00001278 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1279 __decimal_point, __thousands_sep,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001280 __grouping, __g, __g_end,
1281 __dc, __atoms))
1282 break;
1283 if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1284 *__g_end++ = __dc;
1285 // Stage 3
1286 __v = __num_get_float<long double>(__a, __a_end, __err);
1287 // Digit grouping checked
1288 __check_grouping(__grouping, __g, __g_end, __err);
1289 // EOF checked
1290 if (__b == __e)
1291 __err |= ios_base::eofbit;
1292 return __b;
1293}
1294
1295template <class _CharT, class _InputIterator>
1296_InputIterator
1297num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1298 ios_base& __iob,
1299 ios_base::iostate& __err,
1300 void*& __v) const
1301{
1302 // Stage 1
1303 int __base = 16;
1304 // Stage 2
1305 char_type __atoms[26];
Howard Hinnantec3773c2011-12-01 20:21:04 +00001306 char_type __thousands_sep = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001307 string __grouping;
1308 use_facet<ctype<_CharT> >(__iob.getloc()).widen(__num_get_base::__src,
1309 __num_get_base::__src + 26, __atoms);
1310 char __a[__num_get_base::__num_get_buf_sz] = {0};
1311 char* __a_end = __a;
1312 unsigned __g[__num_get_base::__num_get_buf_sz];
1313 unsigned* __g_end = __g;
1314 unsigned __dc = 0;
1315 for (; __b != __e; ++__b)
Howard Hinnant324bb032010-08-22 00:02:43 +00001316 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1317 __thousands_sep, __grouping,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001318 __g, __g_end, __atoms))
1319 break;
1320 // Stage 3
1321 __a[sizeof(__a)-1] = 0;
Howard Hinnant866569b2011-09-28 23:39:33 +00001322#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant2ea1ca92011-09-28 21:05:01 +00001323 if (sscanf_l(__a, _LIBCPP_GET_C_LOCALE, "%p", &__v) != 1)
Sean Huntf3907e62011-07-15 05:40:33 +00001324#else
1325 if (__sscanf_l(__a, __cloc(), "%p", &__v) != 1)
1326#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001327 __err = ios_base::failbit;
1328 // EOF checked
1329 if (__b == __e)
1330 __err |= ios_base::eofbit;
1331 return __b;
1332}
1333
Howard Hinnantff926772012-11-06 21:08:48 +00001334_LIBCPP_EXTERN_TEMPLATE(class num_get<char>)
1335_LIBCPP_EXTERN_TEMPLATE(class num_get<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001336
1337struct __num_put_base
1338{
1339protected:
1340 static void __format_int(char* __fmt, const char* __len, bool __signd,
1341 ios_base::fmtflags __flags);
1342 static bool __format_float(char* __fmt, const char* __len,
1343 ios_base::fmtflags __flags);
1344 static char* __identify_padding(char* __nb, char* __ne,
1345 const ios_base& __iob);
1346};
1347
1348template <class _CharT>
1349struct __num_put
1350 : protected __num_put_base
1351{
1352 static void __widen_and_group_int(char* __nb, char* __np, char* __ne,
1353 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1354 const locale& __loc);
1355 static void __widen_and_group_float(char* __nb, char* __np, char* __ne,
1356 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1357 const locale& __loc);
1358};
1359
1360template <class _CharT>
1361void
1362__num_put<_CharT>::__widen_and_group_int(char* __nb, char* __np, char* __ne,
1363 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1364 const locale& __loc)
1365{
1366 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1367 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1368 string __grouping = __npt.grouping();
1369 if (__grouping.empty())
1370 {
1371 __ct.widen(__nb, __ne, __ob);
1372 __oe = __ob + (__ne - __nb);
1373 }
1374 else
1375 {
1376 __oe = __ob;
1377 char* __nf = __nb;
1378 if (*__nf == '-' || *__nf == '+')
1379 *__oe++ = __ct.widen(*__nf++);
1380 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1381 __nf[1] == 'X'))
1382 {
1383 *__oe++ = __ct.widen(*__nf++);
1384 *__oe++ = __ct.widen(*__nf++);
1385 }
1386 reverse(__nf, __ne);
1387 _CharT __thousands_sep = __npt.thousands_sep();
1388 unsigned __dc = 0;
1389 unsigned __dg = 0;
1390 for (char* __p = __nf; __p < __ne; ++__p)
1391 {
1392 if (static_cast<unsigned>(__grouping[__dg]) > 0 &&
1393 __dc == static_cast<unsigned>(__grouping[__dg]))
1394 {
1395 *__oe++ = __thousands_sep;
1396 __dc = 0;
1397 if (__dg < __grouping.size()-1)
1398 ++__dg;
1399 }
1400 *__oe++ = __ct.widen(*__p);
1401 ++__dc;
1402 }
1403 reverse(__ob + (__nf - __nb), __oe);
1404 }
1405 if (__np == __ne)
1406 __op = __oe;
1407 else
1408 __op = __ob + (__np - __nb);
1409}
1410
1411template <class _CharT>
1412void
1413__num_put<_CharT>::__widen_and_group_float(char* __nb, char* __np, char* __ne,
1414 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1415 const locale& __loc)
1416{
1417 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1418 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1419 string __grouping = __npt.grouping();
1420 __oe = __ob;
1421 char* __nf = __nb;
1422 if (*__nf == '-' || *__nf == '+')
1423 *__oe++ = __ct.widen(*__nf++);
1424 char* __ns;
1425 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1426 __nf[1] == 'X'))
1427 {
1428 *__oe++ = __ct.widen(*__nf++);
1429 *__oe++ = __ct.widen(*__nf++);
1430 for (__ns = __nf; __ns < __ne; ++__ns)
Howard Hinnant2ea1ca92011-09-28 21:05:01 +00001431 if (!isxdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001432 break;
1433 }
1434 else
1435 {
1436 for (__ns = __nf; __ns < __ne; ++__ns)
Howard Hinnant2ea1ca92011-09-28 21:05:01 +00001437 if (!isdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001438 break;
1439 }
1440 if (__grouping.empty())
1441 {
1442 __ct.widen(__nf, __ns, __oe);
1443 __oe += __ns - __nf;
1444 }
1445 else
1446 {
1447 reverse(__nf, __ns);
1448 _CharT __thousands_sep = __npt.thousands_sep();
1449 unsigned __dc = 0;
1450 unsigned __dg = 0;
1451 for (char* __p = __nf; __p < __ns; ++__p)
1452 {
1453 if (__grouping[__dg] > 0 && __dc == static_cast<unsigned>(__grouping[__dg]))
1454 {
1455 *__oe++ = __thousands_sep;
1456 __dc = 0;
1457 if (__dg < __grouping.size()-1)
1458 ++__dg;
1459 }
1460 *__oe++ = __ct.widen(*__p);
1461 ++__dc;
1462 }
1463 reverse(__ob + (__nf - __nb), __oe);
1464 }
1465 for (__nf = __ns; __nf < __ne; ++__nf)
1466 {
1467 if (*__nf == '.')
1468 {
1469 *__oe++ = __npt.decimal_point();
1470 ++__nf;
1471 break;
1472 }
1473 else
1474 *__oe++ = __ct.widen(*__nf);
1475 }
1476 __ct.widen(__nf, __ne, __oe);
1477 __oe += __ne - __nf;
1478 if (__np == __ne)
1479 __op = __oe;
1480 else
1481 __op = __ob + (__np - __nb);
1482}
1483
Howard Hinnantff926772012-11-06 21:08:48 +00001484_LIBCPP_EXTERN_TEMPLATE(struct __num_put<char>)
1485_LIBCPP_EXTERN_TEMPLATE(struct __num_put<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001486
1487template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Howard Hinnant83eade62013-03-06 23:30:19 +00001488class _LIBCPP_TYPE_VIS num_put
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001489 : public locale::facet,
1490 private __num_put<_CharT>
1491{
1492public:
1493 typedef _CharT char_type;
1494 typedef _OutputIterator iter_type;
1495
1496 _LIBCPP_ALWAYS_INLINE
1497 explicit num_put(size_t __refs = 0)
1498 : locale::facet(__refs) {}
1499
1500 _LIBCPP_ALWAYS_INLINE
1501 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1502 bool __v) const
1503 {
1504 return do_put(__s, __iob, __fl, __v);
1505 }
1506
1507 _LIBCPP_ALWAYS_INLINE
1508 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1509 long __v) const
1510 {
1511 return do_put(__s, __iob, __fl, __v);
1512 }
1513
1514 _LIBCPP_ALWAYS_INLINE
1515 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1516 long long __v) const
1517 {
1518 return do_put(__s, __iob, __fl, __v);
1519 }
1520
1521 _LIBCPP_ALWAYS_INLINE
1522 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1523 unsigned long __v) const
1524 {
1525 return do_put(__s, __iob, __fl, __v);
1526 }
1527
1528 _LIBCPP_ALWAYS_INLINE
1529 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1530 unsigned long long __v) const
1531 {
1532 return do_put(__s, __iob, __fl, __v);
1533 }
1534
1535 _LIBCPP_ALWAYS_INLINE
1536 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1537 double __v) const
1538 {
1539 return do_put(__s, __iob, __fl, __v);
1540 }
1541
1542 _LIBCPP_ALWAYS_INLINE
1543 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1544 long double __v) const
1545 {
1546 return do_put(__s, __iob, __fl, __v);
1547 }
1548
1549 _LIBCPP_ALWAYS_INLINE
1550 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1551 const void* __v) const
1552 {
1553 return do_put(__s, __iob, __fl, __v);
1554 }
1555
1556 static locale::id id;
1557
1558protected:
Howard Hinnant82894812010-09-22 16:48:34 +00001559 _LIBCPP_ALWAYS_INLINE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001560 ~num_put() {}
1561
1562 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1563 bool __v) const;
1564 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1565 long __v) const;
1566 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1567 long long __v) const;
1568 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1569 unsigned long) const;
1570 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1571 unsigned long long) const;
1572 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1573 double __v) const;
1574 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1575 long double __v) const;
1576 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1577 const void* __v) const;
1578};
1579
1580template <class _CharT, class _OutputIterator>
1581locale::id
1582num_put<_CharT, _OutputIterator>::id;
1583
1584template <class _CharT, class _OutputIterator>
1585_LIBCPP_HIDDEN
1586_OutputIterator
1587__pad_and_output(_OutputIterator __s,
1588 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1589 ios_base& __iob, _CharT __fl)
1590{
1591 streamsize __sz = __oe - __ob;
1592 streamsize __ns = __iob.width();
1593 if (__ns > __sz)
1594 __ns -= __sz;
1595 else
1596 __ns = 0;
1597 for (;__ob < __op; ++__ob, ++__s)
1598 *__s = *__ob;
1599 for (; __ns; --__ns, ++__s)
1600 *__s = __fl;
1601 for (; __ob < __oe; ++__ob, ++__s)
1602 *__s = *__ob;
1603 __iob.width(0);
1604 return __s;
1605}
1606
Howard Hinnant537b2fa2012-11-14 21:17:15 +00001607#if !defined(__APPLE__) || \
1608 (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED > __MAC_10_8) || \
1609 (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_6_0)
1610
Howard Hinnanta585de62012-09-19 19:14:15 +00001611template <class _CharT, class _Traits>
1612_LIBCPP_HIDDEN
1613ostreambuf_iterator<_CharT, _Traits>
1614__pad_and_output(ostreambuf_iterator<_CharT, _Traits> __s,
1615 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1616 ios_base& __iob, _CharT __fl)
1617{
1618 if (__s.__sbuf_ == nullptr)
1619 return __s;
1620 streamsize __sz = __oe - __ob;
1621 streamsize __ns = __iob.width();
1622 if (__ns > __sz)
1623 __ns -= __sz;
1624 else
1625 __ns = 0;
1626 streamsize __np = __op - __ob;
1627 if (__np > 0)
1628 {
1629 if (__s.__sbuf_->sputn(__ob, __np) != __np)
1630 {
1631 __s.__sbuf_ = nullptr;
1632 return __s;
1633 }
1634 }
1635 if (__ns > 0)
1636 {
1637 basic_string<_CharT, _Traits> __sp(__ns, __fl);
1638 if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns)
1639 {
1640 __s.__sbuf_ = nullptr;
1641 return __s;
1642 }
1643 }
1644 __np = __oe - __op;
1645 if (__np > 0)
1646 {
1647 if (__s.__sbuf_->sputn(__op, __np) != __np)
1648 {
1649 __s.__sbuf_ = nullptr;
1650 return __s;
1651 }
1652 }
1653 __iob.width(0);
1654 return __s;
1655}
1656
Howard Hinnant537b2fa2012-11-14 21:17:15 +00001657#endif
1658
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001659template <class _CharT, class _OutputIterator>
1660_OutputIterator
1661num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1662 char_type __fl, bool __v) const
1663{
1664 if ((__iob.flags() & ios_base::boolalpha) == 0)
1665 return do_put(__s, __iob, __fl, (unsigned long)__v);
1666 const numpunct<char_type>& __np = use_facet<numpunct<char_type> >(__iob.getloc());
1667 typedef typename numpunct<char_type>::string_type string_type;
1668 string_type __nm = __v ? __np.truename() : __np.falsename();
1669 for (typename string_type::iterator __i = __nm.begin(); __i != __nm.end(); ++__i, ++__s)
1670 *__s = *__i;
1671 return __s;
1672}
1673
1674template <class _CharT, class _OutputIterator>
1675_OutputIterator
1676num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1677 char_type __fl, long __v) const
1678{
1679 // Stage 1 - Get number in narrow char
1680 char __fmt[6] = {'%', 0};
1681 const char* __len = "l";
1682 this->__format_int(__fmt+1, __len, true, __iob.flags());
1683 const unsigned __nbuf = (numeric_limits<long>::digits / 3)
1684 + ((numeric_limits<long>::digits % 3) != 0)
1685 + 1;
1686 char __nar[__nbuf];
Howard Hinnant866569b2011-09-28 23:39:33 +00001687#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant2ea1ca92011-09-28 21:05:01 +00001688 int __nc = sprintf_l(__nar, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Sean Huntf3907e62011-07-15 05:40:33 +00001689#else
1690 int __nc = __sprintf_l(__nar, __cloc(), __fmt, __v);
1691#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001692 char* __ne = __nar + __nc;
1693 char* __np = this->__identify_padding(__nar, __ne, __iob);
1694 // Stage 2 - Widen __nar while adding thousands separators
1695 char_type __o[2*(__nbuf-1) - 1];
1696 char_type* __op; // pad here
1697 char_type* __oe; // end of output
1698 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1699 // [__o, __oe) contains thousands_sep'd wide number
1700 // Stage 3 & 4
1701 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1702}
1703
1704template <class _CharT, class _OutputIterator>
1705_OutputIterator
1706num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1707 char_type __fl, long long __v) const
1708{
1709 // Stage 1 - Get number in narrow char
1710 char __fmt[8] = {'%', 0};
1711 const char* __len = "ll";
1712 this->__format_int(__fmt+1, __len, true, __iob.flags());
1713 const unsigned __nbuf = (numeric_limits<long long>::digits / 3)
1714 + ((numeric_limits<long long>::digits % 3) != 0)
1715 + 1;
1716 char __nar[__nbuf];
Howard Hinnant866569b2011-09-28 23:39:33 +00001717#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant2ea1ca92011-09-28 21:05:01 +00001718 int __nc = sprintf_l(__nar, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Sean Huntf3907e62011-07-15 05:40:33 +00001719#else
1720 int __nc = __sprintf_l(__nar, __cloc(), __fmt, __v);
1721#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001722 char* __ne = __nar + __nc;
1723 char* __np = this->__identify_padding(__nar, __ne, __iob);
1724 // Stage 2 - Widen __nar while adding thousands separators
1725 char_type __o[2*(__nbuf-1) - 1];
1726 char_type* __op; // pad here
1727 char_type* __oe; // end of output
1728 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1729 // [__o, __oe) contains thousands_sep'd wide number
1730 // Stage 3 & 4
1731 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1732}
1733
1734template <class _CharT, class _OutputIterator>
1735_OutputIterator
1736num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1737 char_type __fl, unsigned long __v) const
1738{
1739 // Stage 1 - Get number in narrow char
1740 char __fmt[6] = {'%', 0};
1741 const char* __len = "l";
1742 this->__format_int(__fmt+1, __len, false, __iob.flags());
1743 const unsigned __nbuf = (numeric_limits<unsigned long>::digits / 3)
1744 + ((numeric_limits<unsigned long>::digits % 3) != 0)
1745 + 1;
1746 char __nar[__nbuf];
Howard Hinnant866569b2011-09-28 23:39:33 +00001747#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant2ea1ca92011-09-28 21:05:01 +00001748 int __nc = sprintf_l(__nar, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Sean Huntf3907e62011-07-15 05:40:33 +00001749#else
1750 int __nc = __sprintf_l(__nar, __cloc(), __fmt, __v);
1751#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001752 char* __ne = __nar + __nc;
1753 char* __np = this->__identify_padding(__nar, __ne, __iob);
1754 // Stage 2 - Widen __nar while adding thousands separators
1755 char_type __o[2*(__nbuf-1) - 1];
1756 char_type* __op; // pad here
1757 char_type* __oe; // end of output
1758 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1759 // [__o, __oe) contains thousands_sep'd wide number
1760 // Stage 3 & 4
1761 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1762}
1763
1764template <class _CharT, class _OutputIterator>
1765_OutputIterator
1766num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1767 char_type __fl, unsigned long long __v) const
1768{
1769 // Stage 1 - Get number in narrow char
1770 char __fmt[8] = {'%', 0};
1771 const char* __len = "ll";
1772 this->__format_int(__fmt+1, __len, false, __iob.flags());
1773 const unsigned __nbuf = (numeric_limits<unsigned long long>::digits / 3)
1774 + ((numeric_limits<unsigned long long>::digits % 3) != 0)
1775 + 1;
1776 char __nar[__nbuf];
Howard Hinnant866569b2011-09-28 23:39:33 +00001777#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant2ea1ca92011-09-28 21:05:01 +00001778 int __nc = sprintf_l(__nar, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Sean Huntf3907e62011-07-15 05:40:33 +00001779#else
1780 int __nc = __sprintf_l(__nar, __cloc(), __fmt, __v);
1781#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001782 char* __ne = __nar + __nc;
1783 char* __np = this->__identify_padding(__nar, __ne, __iob);
1784 // Stage 2 - Widen __nar while adding thousands separators
1785 char_type __o[2*(__nbuf-1) - 1];
1786 char_type* __op; // pad here
1787 char_type* __oe; // end of output
1788 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1789 // [__o, __oe) contains thousands_sep'd wide number
1790 // Stage 3 & 4
1791 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1792}
1793
1794template <class _CharT, class _OutputIterator>
1795_OutputIterator
1796num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1797 char_type __fl, double __v) const
1798{
1799 // Stage 1 - Get number in narrow char
1800 char __fmt[8] = {'%', 0};
1801 const char* __len = "";
1802 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1803 const unsigned __nbuf = 30;
1804 char __nar[__nbuf];
1805 char* __nb = __nar;
1806 int __nc;
1807 if (__specify_precision)
Howard Hinnant866569b2011-09-28 23:39:33 +00001808#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant2ea1ca92011-09-28 21:05:01 +00001809 __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
Howard Hinnantadff4892010-05-24 17:49:41 +00001810 (int)__iob.precision(), __v);
Sean Huntf3907e62011-07-15 05:40:33 +00001811#else
1812 __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt,
1813 (int)__iob.precision(), __v);
1814#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001815 else
Howard Hinnant866569b2011-09-28 23:39:33 +00001816#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant2ea1ca92011-09-28 21:05:01 +00001817 __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Sean Huntf3907e62011-07-15 05:40:33 +00001818#else
1819 __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt, __v);
1820#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001821 unique_ptr<char, void(*)(void*)> __nbh(0, free);
1822 if (__nc > static_cast<int>(__nbuf-1))
1823 {
1824 if (__specify_precision)
Howard Hinnant866569b2011-09-28 23:39:33 +00001825#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant2ea1ca92011-09-28 21:05:01 +00001826 __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
Sean Huntf3907e62011-07-15 05:40:33 +00001827#else
1828 __nc = __asprintf_l(&__nb, __cloc(), __fmt,
David Chisnallc512df12011-09-21 08:39:44 +00001829 (int)__iob.precision(), __v);
Sean Huntf3907e62011-07-15 05:40:33 +00001830#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001831 else
Howard Hinnant866569b2011-09-28 23:39:33 +00001832#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant2ea1ca92011-09-28 21:05:01 +00001833 __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Sean Huntf3907e62011-07-15 05:40:33 +00001834#else
David Chisnallc512df12011-09-21 08:39:44 +00001835 __nc = __asprintf_l(&__nb, __cloc(), __fmt, (int)__iob.precision(), __v);
Sean Huntf3907e62011-07-15 05:40:33 +00001836#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001837 if (__nb == 0)
1838 __throw_bad_alloc();
1839 __nbh.reset(__nb);
1840 }
1841 char* __ne = __nb + __nc;
1842 char* __np = this->__identify_padding(__nb, __ne, __iob);
1843 // Stage 2 - Widen __nar while adding thousands separators
1844 char_type __o[2*(__nbuf-1) - 1];
1845 char_type* __ob = __o;
1846 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1847 if (__nb != __nar)
1848 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00001849 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001850 if (__ob == 0)
1851 __throw_bad_alloc();
1852 __obh.reset(__ob);
1853 }
1854 char_type* __op; // pad here
1855 char_type* __oe; // end of output
1856 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1857 // [__o, __oe) contains thousands_sep'd wide number
1858 // Stage 3 & 4
1859 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1860 return __s;
1861}
1862
1863template <class _CharT, class _OutputIterator>
1864_OutputIterator
1865num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1866 char_type __fl, long double __v) const
1867{
1868 // Stage 1 - Get number in narrow char
1869 char __fmt[8] = {'%', 0};
1870 const char* __len = "L";
1871 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1872 const unsigned __nbuf = 30;
1873 char __nar[__nbuf];
1874 char* __nb = __nar;
1875 int __nc;
1876 if (__specify_precision)
Howard Hinnant866569b2011-09-28 23:39:33 +00001877#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant2ea1ca92011-09-28 21:05:01 +00001878 __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
Howard Hinnantadff4892010-05-24 17:49:41 +00001879 (int)__iob.precision(), __v);
Sean Huntf3907e62011-07-15 05:40:33 +00001880#else
1881 __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt,
1882 (int)__iob.precision(), __v);
1883#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001884 else
Howard Hinnant866569b2011-09-28 23:39:33 +00001885#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant2ea1ca92011-09-28 21:05:01 +00001886 __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Sean Huntf3907e62011-07-15 05:40:33 +00001887#else
1888 __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt, __v);
1889#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001890 unique_ptr<char, void(*)(void*)> __nbh(0, free);
1891 if (__nc > static_cast<int>(__nbuf-1))
1892 {
1893 if (__specify_precision)
Howard Hinnant866569b2011-09-28 23:39:33 +00001894#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant2ea1ca92011-09-28 21:05:01 +00001895 __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
Sean Huntf3907e62011-07-15 05:40:33 +00001896#else
1897 __nc = __asprintf_l(&__nb, __cloc(), __fmt,
David Chisnallc512df12011-09-21 08:39:44 +00001898 (int)__iob.precision(), __v);
Sean Huntf3907e62011-07-15 05:40:33 +00001899#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001900 else
Howard Hinnant866569b2011-09-28 23:39:33 +00001901#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant2ea1ca92011-09-28 21:05:01 +00001902 __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Sean Huntf3907e62011-07-15 05:40:33 +00001903#else
David Chisnallc512df12011-09-21 08:39:44 +00001904 __nc = __asprintf_l(&__nb, __cloc(), __fmt, __v);
Sean Huntf3907e62011-07-15 05:40:33 +00001905#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001906 if (__nb == 0)
1907 __throw_bad_alloc();
1908 __nbh.reset(__nb);
1909 }
1910 char* __ne = __nb + __nc;
1911 char* __np = this->__identify_padding(__nb, __ne, __iob);
1912 // Stage 2 - Widen __nar while adding thousands separators
1913 char_type __o[2*(__nbuf-1) - 1];
1914 char_type* __ob = __o;
1915 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1916 if (__nb != __nar)
1917 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00001918 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001919 if (__ob == 0)
1920 __throw_bad_alloc();
1921 __obh.reset(__ob);
1922 }
1923 char_type* __op; // pad here
1924 char_type* __oe; // end of output
1925 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1926 // [__o, __oe) contains thousands_sep'd wide number
1927 // Stage 3 & 4
1928 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1929 return __s;
1930}
1931
1932template <class _CharT, class _OutputIterator>
1933_OutputIterator
1934num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1935 char_type __fl, const void* __v) const
1936{
1937 // Stage 1 - Get pointer in narrow char
1938 char __fmt[6] = "%p";
1939 const unsigned __nbuf = 20;
1940 char __nar[__nbuf];
Howard Hinnant866569b2011-09-28 23:39:33 +00001941#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant2ea1ca92011-09-28 21:05:01 +00001942 int __nc = sprintf_l(__nar, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Sean Huntf3907e62011-07-15 05:40:33 +00001943#else
1944 int __nc = __sprintf_l(__nar, __cloc(), __fmt, __v);
1945#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001946 char* __ne = __nar + __nc;
1947 char* __np = this->__identify_padding(__nar, __ne, __iob);
1948 // Stage 2 - Widen __nar
1949 char_type __o[2*(__nbuf-1) - 1];
1950 char_type* __op; // pad here
1951 char_type* __oe; // end of output
1952 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
1953 __ct.widen(__nar, __ne, __o);
1954 __oe = __o + (__ne - __nar);
1955 if (__np == __ne)
1956 __op = __oe;
1957 else
1958 __op = __o + (__np - __nar);
1959 // [__o, __oe) contains wide number
1960 // Stage 3 & 4
1961 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1962}
1963
Howard Hinnantff926772012-11-06 21:08:48 +00001964_LIBCPP_EXTERN_TEMPLATE(class num_put<char>)
1965_LIBCPP_EXTERN_TEMPLATE(class num_put<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001966
1967template <class _CharT, class _InputIterator>
1968_LIBCPP_HIDDEN
1969int
1970__get_up_to_n_digits(_InputIterator& __b, _InputIterator __e,
1971 ios_base::iostate& __err, const ctype<_CharT>& __ct, int __n)
1972{
1973 // Precondition: __n >= 1
1974 if (__b == __e)
1975 {
1976 __err |= ios_base::eofbit | ios_base::failbit;
1977 return 0;
1978 }
1979 // get first digit
1980 _CharT __c = *__b;
1981 if (!__ct.is(ctype_base::digit, __c))
1982 {
1983 __err |= ios_base::failbit;
1984 return 0;
1985 }
1986 int __r = __ct.narrow(__c, 0) - '0';
1987 for (++__b, --__n; __b != __e && __n > 0; ++__b, --__n)
1988 {
1989 // get next digit
1990 __c = *__b;
1991 if (!__ct.is(ctype_base::digit, __c))
1992 return __r;
1993 __r = __r * 10 + __ct.narrow(__c, 0) - '0';
1994 }
1995 if (__b == __e)
1996 __err |= ios_base::eofbit;
1997 return __r;
1998}
1999
Howard Hinnant83eade62013-03-06 23:30:19 +00002000class _LIBCPP_TYPE_VIS time_base
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002001{
2002public:
2003 enum dateorder {no_order, dmy, mdy, ymd, ydm};
2004};
2005
2006template <class _CharT>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00002007class __time_get_c_storage // purposefully not decorated
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002008{
2009protected:
2010 typedef basic_string<_CharT> string_type;
2011
2012 virtual const string_type* __weeks() const;
2013 virtual const string_type* __months() const;
2014 virtual const string_type* __am_pm() const;
2015 virtual const string_type& __c() const;
2016 virtual const string_type& __r() const;
2017 virtual const string_type& __x() const;
2018 virtual const string_type& __X() const;
2019};
2020
2021template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Howard Hinnant83eade62013-03-06 23:30:19 +00002022class _LIBCPP_TYPE_VIS time_get
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002023 : public locale::facet,
2024 public time_base,
2025 private __time_get_c_storage<_CharT>
2026{
2027public:
2028 typedef _CharT char_type;
2029 typedef _InputIterator iter_type;
2030 typedef time_base::dateorder dateorder;
2031 typedef basic_string<char_type> string_type;
2032
2033 _LIBCPP_ALWAYS_INLINE
2034 explicit time_get(size_t __refs = 0)
2035 : locale::facet(__refs) {}
2036
2037 _LIBCPP_ALWAYS_INLINE
2038 dateorder date_order() const
2039 {
2040 return this->do_date_order();
2041 }
2042
2043 _LIBCPP_ALWAYS_INLINE
2044 iter_type get_time(iter_type __b, iter_type __e, ios_base& __iob,
2045 ios_base::iostate& __err, tm* __tm) const
2046 {
2047 return do_get_time(__b, __e, __iob, __err, __tm);
2048 }
2049
2050 _LIBCPP_ALWAYS_INLINE
2051 iter_type get_date(iter_type __b, iter_type __e, ios_base& __iob,
2052 ios_base::iostate& __err, tm* __tm) const
2053 {
2054 return do_get_date(__b, __e, __iob, __err, __tm);
2055 }
2056
2057 _LIBCPP_ALWAYS_INLINE
2058 iter_type get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
2059 ios_base::iostate& __err, tm* __tm) const
2060 {
2061 return do_get_weekday(__b, __e, __iob, __err, __tm);
2062 }
2063
2064 _LIBCPP_ALWAYS_INLINE
2065 iter_type get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
2066 ios_base::iostate& __err, tm* __tm) const
2067 {
2068 return do_get_monthname(__b, __e, __iob, __err, __tm);
2069 }
2070
2071 _LIBCPP_ALWAYS_INLINE
2072 iter_type get_year(iter_type __b, iter_type __e, ios_base& __iob,
2073 ios_base::iostate& __err, tm* __tm) const
2074 {
2075 return do_get_year(__b, __e, __iob, __err, __tm);
2076 }
2077
2078 _LIBCPP_ALWAYS_INLINE
2079 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
2080 ios_base::iostate& __err, tm *__tm,
2081 char __fmt, char __mod = 0) const
2082 {
2083 return do_get(__b, __e, __iob, __err, __tm, __fmt, __mod);
2084 }
2085
2086 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
2087 ios_base::iostate& __err, tm* __tm,
2088 const char_type* __fmtb, const char_type* __fmte) const;
2089
2090 static locale::id id;
2091
2092protected:
Howard Hinnant82894812010-09-22 16:48:34 +00002093 _LIBCPP_ALWAYS_INLINE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002094 ~time_get() {}
2095
2096 virtual dateorder do_date_order() const;
2097 virtual iter_type do_get_time(iter_type __b, iter_type __e, ios_base& __iob,
2098 ios_base::iostate& __err, tm* __tm) const;
2099 virtual iter_type do_get_date(iter_type __b, iter_type __e, ios_base& __iob,
2100 ios_base::iostate& __err, tm* __tm) const;
2101 virtual iter_type do_get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
2102 ios_base::iostate& __err, tm* __tm) const;
2103 virtual iter_type do_get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
2104 ios_base::iostate& __err, tm* __tm) const;
2105 virtual iter_type do_get_year(iter_type __b, iter_type __e, ios_base& __iob,
2106 ios_base::iostate& __err, tm* __tm) const;
2107 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
2108 ios_base::iostate& __err, tm* __tm,
2109 char __fmt, char __mod) const;
2110private:
2111 void __get_white_space(iter_type& __b, iter_type __e,
2112 ios_base::iostate& __err, const ctype<char_type>& __ct) const;
2113 void __get_percent(iter_type& __b, iter_type __e, ios_base::iostate& __err,
2114 const ctype<char_type>& __ct) const;
2115
2116 void __get_weekdayname(int& __m,
2117 iter_type& __b, iter_type __e,
2118 ios_base::iostate& __err,
2119 const ctype<char_type>& __ct) const;
2120 void __get_monthname(int& __m,
2121 iter_type& __b, iter_type __e,
2122 ios_base::iostate& __err,
2123 const ctype<char_type>& __ct) const;
2124 void __get_day(int& __d,
2125 iter_type& __b, iter_type __e,
2126 ios_base::iostate& __err,
2127 const ctype<char_type>& __ct) const;
2128 void __get_month(int& __m,
2129 iter_type& __b, iter_type __e,
2130 ios_base::iostate& __err,
2131 const ctype<char_type>& __ct) const;
2132 void __get_year(int& __y,
2133 iter_type& __b, iter_type __e,
2134 ios_base::iostate& __err,
2135 const ctype<char_type>& __ct) const;
2136 void __get_year4(int& __y,
2137 iter_type& __b, iter_type __e,
2138 ios_base::iostate& __err,
2139 const ctype<char_type>& __ct) const;
2140 void __get_hour(int& __d,
2141 iter_type& __b, iter_type __e,
2142 ios_base::iostate& __err,
2143 const ctype<char_type>& __ct) const;
2144 void __get_12_hour(int& __h,
2145 iter_type& __b, iter_type __e,
2146 ios_base::iostate& __err,
2147 const ctype<char_type>& __ct) const;
2148 void __get_am_pm(int& __h,
2149 iter_type& __b, iter_type __e,
2150 ios_base::iostate& __err,
2151 const ctype<char_type>& __ct) const;
2152 void __get_minute(int& __m,
2153 iter_type& __b, iter_type __e,
2154 ios_base::iostate& __err,
2155 const ctype<char_type>& __ct) const;
2156 void __get_second(int& __s,
2157 iter_type& __b, iter_type __e,
2158 ios_base::iostate& __err,
2159 const ctype<char_type>& __ct) const;
2160 void __get_weekday(int& __w,
2161 iter_type& __b, iter_type __e,
2162 ios_base::iostate& __err,
2163 const ctype<char_type>& __ct) const;
2164 void __get_day_year_num(int& __w,
2165 iter_type& __b, iter_type __e,
2166 ios_base::iostate& __err,
2167 const ctype<char_type>& __ct) const;
2168};
2169
2170template <class _CharT, class _InputIterator>
2171locale::id
2172time_get<_CharT, _InputIterator>::id;
2173
2174// time_get primatives
2175
2176template <class _CharT, class _InputIterator>
2177void
2178time_get<_CharT, _InputIterator>::__get_weekdayname(int& __w,
2179 iter_type& __b, iter_type __e,
2180 ios_base::iostate& __err,
2181 const ctype<char_type>& __ct) const
2182{
2183 // Note: ignoring case comes from the POSIX strptime spec
2184 const string_type* __wk = this->__weeks();
Howard Hinnantec3773c2011-12-01 20:21:04 +00002185 ptrdiff_t __i = __scan_keyword(__b, __e, __wk, __wk+14, __ct, __err, false) - __wk;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002186 if (__i < 14)
2187 __w = __i % 7;
2188}
2189
2190template <class _CharT, class _InputIterator>
2191void
2192time_get<_CharT, _InputIterator>::__get_monthname(int& __m,
2193 iter_type& __b, iter_type __e,
2194 ios_base::iostate& __err,
2195 const ctype<char_type>& __ct) const
2196{
2197 // Note: ignoring case comes from the POSIX strptime spec
2198 const string_type* __month = this->__months();
Howard Hinnantec3773c2011-12-01 20:21:04 +00002199 ptrdiff_t __i = __scan_keyword(__b, __e, __month, __month+24, __ct, __err, false) - __month;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002200 if (__i < 24)
2201 __m = __i % 12;
2202}
2203
2204template <class _CharT, class _InputIterator>
2205void
2206time_get<_CharT, _InputIterator>::__get_day(int& __d,
2207 iter_type& __b, iter_type __e,
2208 ios_base::iostate& __err,
2209 const ctype<char_type>& __ct) const
2210{
2211 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2212 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 31)
2213 __d = __t;
2214 else
2215 __err |= ios_base::failbit;
2216}
2217
2218template <class _CharT, class _InputIterator>
2219void
2220time_get<_CharT, _InputIterator>::__get_month(int& __m,
2221 iter_type& __b, iter_type __e,
2222 ios_base::iostate& __err,
2223 const ctype<char_type>& __ct) const
2224{
2225 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2) - 1;
2226 if (!(__err & ios_base::failbit) && __t <= 11)
2227 __m = __t;
2228 else
2229 __err |= ios_base::failbit;
2230}
2231
2232template <class _CharT, class _InputIterator>
2233void
2234time_get<_CharT, _InputIterator>::__get_year(int& __y,
2235 iter_type& __b, iter_type __e,
2236 ios_base::iostate& __err,
2237 const ctype<char_type>& __ct) const
2238{
2239 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
2240 if (!(__err & ios_base::failbit))
2241 {
2242 if (__t < 69)
2243 __t += 2000;
2244 else if (69 <= __t && __t <= 99)
2245 __t += 1900;
2246 __y = __t - 1900;
2247 }
2248}
2249
2250template <class _CharT, class _InputIterator>
2251void
2252time_get<_CharT, _InputIterator>::__get_year4(int& __y,
2253 iter_type& __b, iter_type __e,
2254 ios_base::iostate& __err,
2255 const ctype<char_type>& __ct) const
2256{
2257 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
2258 if (!(__err & ios_base::failbit))
2259 __y = __t - 1900;
2260}
2261
2262template <class _CharT, class _InputIterator>
2263void
2264time_get<_CharT, _InputIterator>::__get_hour(int& __h,
2265 iter_type& __b, iter_type __e,
2266 ios_base::iostate& __err,
2267 const ctype<char_type>& __ct) const
2268{
2269 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2270 if (!(__err & ios_base::failbit) && __t <= 23)
2271 __h = __t;
2272 else
2273 __err |= ios_base::failbit;
2274}
2275
2276template <class _CharT, class _InputIterator>
2277void
2278time_get<_CharT, _InputIterator>::__get_12_hour(int& __h,
2279 iter_type& __b, iter_type __e,
2280 ios_base::iostate& __err,
2281 const ctype<char_type>& __ct) const
2282{
2283 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2284 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 12)
2285 __h = __t;
2286 else
2287 __err |= ios_base::failbit;
2288}
2289
2290template <class _CharT, class _InputIterator>
2291void
2292time_get<_CharT, _InputIterator>::__get_minute(int& __m,
2293 iter_type& __b, iter_type __e,
2294 ios_base::iostate& __err,
2295 const ctype<char_type>& __ct) const
2296{
2297 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2298 if (!(__err & ios_base::failbit) && __t <= 59)
2299 __m = __t;
2300 else
2301 __err |= ios_base::failbit;
2302}
2303
2304template <class _CharT, class _InputIterator>
2305void
2306time_get<_CharT, _InputIterator>::__get_second(int& __s,
2307 iter_type& __b, iter_type __e,
2308 ios_base::iostate& __err,
2309 const ctype<char_type>& __ct) const
2310{
2311 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2312 if (!(__err & ios_base::failbit) && __t <= 60)
2313 __s = __t;
2314 else
2315 __err |= ios_base::failbit;
2316}
2317
2318template <class _CharT, class _InputIterator>
2319void
2320time_get<_CharT, _InputIterator>::__get_weekday(int& __w,
2321 iter_type& __b, iter_type __e,
2322 ios_base::iostate& __err,
2323 const ctype<char_type>& __ct) const
2324{
2325 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 1);
2326 if (!(__err & ios_base::failbit) && __t <= 6)
2327 __w = __t;
2328 else
2329 __err |= ios_base::failbit;
2330}
2331
2332template <class _CharT, class _InputIterator>
2333void
2334time_get<_CharT, _InputIterator>::__get_day_year_num(int& __d,
2335 iter_type& __b, iter_type __e,
2336 ios_base::iostate& __err,
2337 const ctype<char_type>& __ct) const
2338{
2339 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 3);
2340 if (!(__err & ios_base::failbit) && __t <= 365)
2341 __d = __t;
2342 else
2343 __err |= ios_base::failbit;
2344}
2345
2346template <class _CharT, class _InputIterator>
2347void
2348time_get<_CharT, _InputIterator>::__get_white_space(iter_type& __b, iter_type __e,
2349 ios_base::iostate& __err,
2350 const ctype<char_type>& __ct) const
2351{
2352 for (; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2353 ;
2354 if (__b == __e)
2355 __err |= ios_base::eofbit;
2356}
2357
2358template <class _CharT, class _InputIterator>
2359void
2360time_get<_CharT, _InputIterator>::__get_am_pm(int& __h,
2361 iter_type& __b, iter_type __e,
2362 ios_base::iostate& __err,
2363 const ctype<char_type>& __ct) const
2364{
2365 const string_type* __ap = this->__am_pm();
2366 if (__ap[0].size() + __ap[1].size() == 0)
2367 {
2368 __err |= ios_base::failbit;
2369 return;
2370 }
Howard Hinnantec3773c2011-12-01 20:21:04 +00002371 ptrdiff_t __i = __scan_keyword(__b, __e, __ap, __ap+2, __ct, __err, false) - __ap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002372 if (__i == 0 && __h == 12)
2373 __h = 0;
2374 else if (__i == 1 && __h < 12)
2375 __h += 12;
2376}
2377
2378template <class _CharT, class _InputIterator>
2379void
2380time_get<_CharT, _InputIterator>::__get_percent(iter_type& __b, iter_type __e,
2381 ios_base::iostate& __err,
2382 const ctype<char_type>& __ct) const
2383{
2384 if (__b == __e)
2385 {
2386 __err |= ios_base::eofbit | ios_base::failbit;
2387 return;
2388 }
2389 if (__ct.narrow(*__b, 0) != '%')
2390 __err |= ios_base::failbit;
2391 else if(++__b == __e)
2392 __err |= ios_base::eofbit;
2393}
2394
2395// time_get end primatives
2396
2397template <class _CharT, class _InputIterator>
2398_InputIterator
2399time_get<_CharT, _InputIterator>::get(iter_type __b, iter_type __e,
2400 ios_base& __iob,
2401 ios_base::iostate& __err, tm* __tm,
2402 const char_type* __fmtb, const char_type* __fmte) const
2403{
2404 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2405 __err = ios_base::goodbit;
2406 while (__fmtb != __fmte && __err == ios_base::goodbit)
2407 {
2408 if (__b == __e)
2409 {
2410 __err = ios_base::failbit;
2411 break;
2412 }
2413 if (__ct.narrow(*__fmtb, 0) == '%')
2414 {
2415 if (++__fmtb == __fmte)
2416 {
2417 __err = ios_base::failbit;
2418 break;
2419 }
2420 char __cmd = __ct.narrow(*__fmtb, 0);
2421 char __opt = '\0';
2422 if (__cmd == 'E' || __cmd == '0')
2423 {
2424 if (++__fmtb == __fmte)
2425 {
2426 __err = ios_base::failbit;
2427 break;
2428 }
2429 __opt = __cmd;
2430 __cmd = __ct.narrow(*__fmtb, 0);
2431 }
2432 __b = do_get(__b, __e, __iob, __err, __tm, __cmd, __opt);
2433 ++__fmtb;
2434 }
2435 else if (__ct.is(ctype_base::space, *__fmtb))
2436 {
2437 for (++__fmtb; __fmtb != __fmte && __ct.is(ctype_base::space, *__fmtb); ++__fmtb)
2438 ;
2439 for ( ; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2440 ;
2441 }
2442 else if (__ct.toupper(*__b) == __ct.toupper(*__fmtb))
2443 {
2444 ++__b;
2445 ++__fmtb;
2446 }
2447 else
2448 __err = ios_base::failbit;
2449 }
2450 if (__b == __e)
2451 __err |= ios_base::eofbit;
2452 return __b;
2453}
2454
2455template <class _CharT, class _InputIterator>
2456typename time_get<_CharT, _InputIterator>::dateorder
2457time_get<_CharT, _InputIterator>::do_date_order() const
2458{
2459 return mdy;
2460}
2461
2462template <class _CharT, class _InputIterator>
2463_InputIterator
2464time_get<_CharT, _InputIterator>::do_get_time(iter_type __b, iter_type __e,
2465 ios_base& __iob,
2466 ios_base::iostate& __err,
2467 tm* __tm) const
2468{
2469 const char_type __fmt[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2470 return get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt)/sizeof(__fmt[0]));
2471}
2472
2473template <class _CharT, class _InputIterator>
2474_InputIterator
2475time_get<_CharT, _InputIterator>::do_get_date(iter_type __b, iter_type __e,
2476 ios_base& __iob,
2477 ios_base::iostate& __err,
2478 tm* __tm) const
2479{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002480 const string_type& __fmt = this->__x();
2481 return get(__b, __e, __iob, __err, __tm, __fmt.data(), __fmt.data() + __fmt.size());
2482}
2483
2484template <class _CharT, class _InputIterator>
2485_InputIterator
2486time_get<_CharT, _InputIterator>::do_get_weekday(iter_type __b, iter_type __e,
2487 ios_base& __iob,
2488 ios_base::iostate& __err,
2489 tm* __tm) const
2490{
2491 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2492 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2493 return __b;
2494}
2495
2496template <class _CharT, class _InputIterator>
2497_InputIterator
2498time_get<_CharT, _InputIterator>::do_get_monthname(iter_type __b, iter_type __e,
2499 ios_base& __iob,
2500 ios_base::iostate& __err,
2501 tm* __tm) const
2502{
2503 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2504 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2505 return __b;
2506}
2507
2508template <class _CharT, class _InputIterator>
2509_InputIterator
2510time_get<_CharT, _InputIterator>::do_get_year(iter_type __b, iter_type __e,
2511 ios_base& __iob,
2512 ios_base::iostate& __err,
2513 tm* __tm) const
2514{
2515 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2516 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2517 return __b;
2518}
2519
2520template <class _CharT, class _InputIterator>
2521_InputIterator
2522time_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
2523 ios_base& __iob,
2524 ios_base::iostate& __err, tm* __tm,
2525 char __fmt, char) const
2526{
2527 __err = ios_base::goodbit;
2528 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2529 switch (__fmt)
2530 {
2531 case 'a':
2532 case 'A':
2533 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2534 break;
2535 case 'b':
2536 case 'B':
2537 case 'h':
2538 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2539 break;
2540 case 'c':
2541 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00002542 const string_type& __fm = this->__c();
2543 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002544 }
2545 break;
2546 case 'd':
2547 case 'e':
2548 __get_day(__tm->tm_mday, __b, __e, __err, __ct);
2549 break;
2550 case 'D':
2551 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00002552 const char_type __fm[] = {'%', 'm', '/', '%', 'd', '/', '%', 'y'};
2553 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002554 }
2555 break;
Howard Hinnant506b3642011-04-10 17:54:14 +00002556 case 'F':
2557 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00002558 const char_type __fm[] = {'%', 'Y', '-', '%', 'm', '-', '%', 'd'};
2559 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnant506b3642011-04-10 17:54:14 +00002560 }
2561 break;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002562 case 'H':
2563 __get_hour(__tm->tm_hour, __b, __e, __err, __ct);
2564 break;
2565 case 'I':
2566 __get_12_hour(__tm->tm_hour, __b, __e, __err, __ct);
2567 break;
2568 case 'j':
2569 __get_day_year_num(__tm->tm_yday, __b, __e, __err, __ct);
2570 break;
2571 case 'm':
2572 __get_month(__tm->tm_mon, __b, __e, __err, __ct);
2573 break;
2574 case 'M':
2575 __get_minute(__tm->tm_min, __b, __e, __err, __ct);
2576 break;
2577 case 'n':
2578 case 't':
2579 __get_white_space(__b, __e, __err, __ct);
2580 break;
2581 case 'p':
2582 __get_am_pm(__tm->tm_hour, __b, __e, __err, __ct);
2583 break;
2584 case 'r':
2585 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00002586 const char_type __fm[] = {'%', 'I', ':', '%', 'M', ':', '%', 'S', ' ', '%', 'p'};
2587 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002588 }
2589 break;
2590 case 'R':
2591 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00002592 const char_type __fm[] = {'%', 'H', ':', '%', 'M'};
2593 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002594 }
2595 break;
2596 case 'S':
2597 __get_second(__tm->tm_sec, __b, __e, __err, __ct);
2598 break;
2599 case 'T':
2600 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00002601 const char_type __fm[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2602 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002603 }
2604 break;
2605 case 'w':
2606 __get_weekday(__tm->tm_wday, __b, __e, __err, __ct);
2607 break;
2608 case 'x':
2609 return do_get_date(__b, __e, __iob, __err, __tm);
2610 case 'X':
2611 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00002612 const string_type& __fm = this->__X();
2613 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002614 }
2615 break;
2616 case 'y':
2617 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2618 break;
2619 case 'Y':
2620 __get_year4(__tm->tm_year, __b, __e, __err, __ct);
2621 break;
2622 case '%':
2623 __get_percent(__b, __e, __err, __ct);
2624 break;
2625 default:
2626 __err |= ios_base::failbit;
2627 }
2628 return __b;
2629}
2630
Howard Hinnantff926772012-11-06 21:08:48 +00002631_LIBCPP_EXTERN_TEMPLATE(class time_get<char>)
2632_LIBCPP_EXTERN_TEMPLATE(class time_get<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002633
2634class __time_get
2635{
2636protected:
2637 locale_t __loc_;
2638
2639 __time_get(const char* __nm);
2640 __time_get(const string& __nm);
2641 ~__time_get();
2642};
2643
2644template <class _CharT>
2645class __time_get_storage
2646 : public __time_get
2647{
2648protected:
2649 typedef basic_string<_CharT> string_type;
2650
2651 string_type __weeks_[14];
2652 string_type __months_[24];
2653 string_type __am_pm_[2];
2654 string_type __c_;
2655 string_type __r_;
2656 string_type __x_;
2657 string_type __X_;
2658
2659 explicit __time_get_storage(const char* __nm);
2660 explicit __time_get_storage(const string& __nm);
2661
2662 _LIBCPP_ALWAYS_INLINE ~__time_get_storage() {}
2663
2664 time_base::dateorder __do_date_order() const;
2665
2666private:
2667 void init(const ctype<_CharT>&);
2668 string_type __analyze(char __fmt, const ctype<_CharT>&);
2669};
2670
2671template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Howard Hinnant83eade62013-03-06 23:30:19 +00002672class _LIBCPP_TYPE_VIS time_get_byname
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002673 : public time_get<_CharT, _InputIterator>,
2674 private __time_get_storage<_CharT>
2675{
2676public:
2677 typedef time_base::dateorder dateorder;
2678 typedef _InputIterator iter_type;
2679 typedef _CharT char_type;
2680 typedef basic_string<char_type> string_type;
2681
Howard Hinnant82894812010-09-22 16:48:34 +00002682 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002683 explicit time_get_byname(const char* __nm, size_t __refs = 0)
2684 : time_get<_CharT, _InputIterator>(__refs),
2685 __time_get_storage<_CharT>(__nm) {}
Howard Hinnant82894812010-09-22 16:48:34 +00002686 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002687 explicit time_get_byname(const string& __nm, size_t __refs = 0)
2688 : time_get<_CharT, _InputIterator>(__refs),
2689 __time_get_storage<_CharT>(__nm) {}
2690
2691protected:
Howard Hinnant82894812010-09-22 16:48:34 +00002692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002693 ~time_get_byname() {}
2694
Howard Hinnant82894812010-09-22 16:48:34 +00002695 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002696 virtual dateorder do_date_order() const {return this->__do_date_order();}
2697private:
Howard Hinnant82894812010-09-22 16:48:34 +00002698 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002699 virtual const string_type* __weeks() const {return this->__weeks_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002700 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002701 virtual const string_type* __months() const {return this->__months_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002702 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002703 virtual const string_type* __am_pm() const {return this->__am_pm_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002704 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002705 virtual const string_type& __c() const {return this->__c_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002706 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002707 virtual const string_type& __r() const {return this->__r_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002708 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002709 virtual const string_type& __x() const {return this->__x_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002710 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002711 virtual const string_type& __X() const {return this->__X_;}
2712};
2713
Howard Hinnantff926772012-11-06 21:08:48 +00002714_LIBCPP_EXTERN_TEMPLATE(class time_get_byname<char>)
2715_LIBCPP_EXTERN_TEMPLATE(class time_get_byname<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002716
2717class __time_put
2718{
2719 locale_t __loc_;
2720protected:
Howard Hinnant2ea1ca92011-09-28 21:05:01 +00002721 _LIBCPP_ALWAYS_INLINE __time_put() : __loc_(_LIBCPP_GET_C_LOCALE) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002722 __time_put(const char* __nm);
2723 __time_put(const string& __nm);
2724 ~__time_put();
2725 void __do_put(char* __nb, char*& __ne, const tm* __tm,
2726 char __fmt, char __mod) const;
2727 void __do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm,
2728 char __fmt, char __mod) const;
2729};
2730
2731template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Howard Hinnant83eade62013-03-06 23:30:19 +00002732class _LIBCPP_TYPE_VIS time_put
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002733 : public locale::facet,
2734 private __time_put
2735{
2736public:
2737 typedef _CharT char_type;
2738 typedef _OutputIterator iter_type;
2739
2740 _LIBCPP_ALWAYS_INLINE
2741 explicit time_put(size_t __refs = 0)
2742 : locale::facet(__refs) {}
2743
2744 iter_type put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm,
2745 const char_type* __pb, const char_type* __pe) const;
2746
2747 _LIBCPP_ALWAYS_INLINE
2748 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
2749 const tm* __tm, char __fmt, char __mod = 0) const
2750 {
2751 return do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2752 }
2753
2754 static locale::id id;
2755
2756protected:
Howard Hinnant82894812010-09-22 16:48:34 +00002757 _LIBCPP_ALWAYS_INLINE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002758 ~time_put() {}
2759 virtual iter_type do_put(iter_type __s, ios_base&, char_type, const tm* __tm,
2760 char __fmt, char __mod) const;
2761
Howard Hinnant82894812010-09-22 16:48:34 +00002762 _LIBCPP_ALWAYS_INLINE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002763 explicit time_put(const char* __nm, size_t __refs)
2764 : locale::facet(__refs),
2765 __time_put(__nm) {}
Howard Hinnant82894812010-09-22 16:48:34 +00002766 _LIBCPP_ALWAYS_INLINE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002767 explicit time_put(const string& __nm, size_t __refs)
2768 : locale::facet(__refs),
2769 __time_put(__nm) {}
2770};
2771
2772template <class _CharT, class _OutputIterator>
2773locale::id
2774time_put<_CharT, _OutputIterator>::id;
2775
2776template <class _CharT, class _OutputIterator>
2777_OutputIterator
2778time_put<_CharT, _OutputIterator>::put(iter_type __s, ios_base& __iob,
2779 char_type __fl, const tm* __tm,
2780 const char_type* __pb,
2781 const char_type* __pe) const
2782{
2783 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2784 for (; __pb != __pe; ++__pb)
2785 {
2786 if (__ct.narrow(*__pb, 0) == '%')
2787 {
2788 if (++__pb == __pe)
2789 {
2790 *__s++ = __pb[-1];
2791 break;
2792 }
2793 char __mod = 0;
2794 char __fmt = __ct.narrow(*__pb, 0);
2795 if (__fmt == 'E' || __fmt == 'O')
2796 {
2797 if (++__pb == __pe)
2798 {
2799 *__s++ = __pb[-2];
2800 *__s++ = __pb[-1];
2801 break;
2802 }
2803 __mod = __fmt;
2804 __fmt = __ct.narrow(*__pb, 0);
2805 }
2806 __s = do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2807 }
2808 else
2809 *__s++ = *__pb;
2810 }
2811 return __s;
2812}
2813
2814template <class _CharT, class _OutputIterator>
2815_OutputIterator
Howard Hinnantec3773c2011-12-01 20:21:04 +00002816time_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002817 char_type, const tm* __tm,
2818 char __fmt, char __mod) const
2819{
2820 char_type __nar[100];
2821 char_type* __nb = __nar;
2822 char_type* __ne = __nb + 100;
2823 __do_put(__nb, __ne, __tm, __fmt, __mod);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002824 return _VSTD::copy(__nb, __ne, __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002825}
2826
Howard Hinnantff926772012-11-06 21:08:48 +00002827_LIBCPP_EXTERN_TEMPLATE(class time_put<char>)
2828_LIBCPP_EXTERN_TEMPLATE(class time_put<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002829
2830template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Howard Hinnant83eade62013-03-06 23:30:19 +00002831class _LIBCPP_TYPE_VIS time_put_byname
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002832 : public time_put<_CharT, _OutputIterator>
2833{
2834public:
2835 _LIBCPP_ALWAYS_INLINE
2836 explicit time_put_byname(const char* __nm, size_t __refs = 0)
2837 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2838
2839 _LIBCPP_ALWAYS_INLINE
2840 explicit time_put_byname(const string& __nm, size_t __refs = 0)
2841 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2842
2843protected:
Howard Hinnant82894812010-09-22 16:48:34 +00002844 _LIBCPP_ALWAYS_INLINE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002845 ~time_put_byname() {}
2846};
2847
Howard Hinnantff926772012-11-06 21:08:48 +00002848_LIBCPP_EXTERN_TEMPLATE(class time_put_byname<char>)
2849_LIBCPP_EXTERN_TEMPLATE(class time_put_byname<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002850
2851// money_base
2852
Howard Hinnant83eade62013-03-06 23:30:19 +00002853class _LIBCPP_TYPE_VIS money_base
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002854{
2855public:
2856 enum part {none, space, symbol, sign, value};
2857 struct pattern {char field[4];};
2858
2859 _LIBCPP_ALWAYS_INLINE money_base() {}
2860};
2861
2862// moneypunct
2863
2864template <class _CharT, bool _International = false>
Howard Hinnant83eade62013-03-06 23:30:19 +00002865class _LIBCPP_TYPE_VIS moneypunct
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002866 : public locale::facet,
2867 public money_base
2868{
2869public:
2870 typedef _CharT char_type;
2871 typedef basic_string<char_type> string_type;
2872
Howard Hinnant82894812010-09-22 16:48:34 +00002873 _LIBCPP_ALWAYS_INLINE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002874 explicit moneypunct(size_t __refs = 0)
2875 : locale::facet(__refs) {}
2876
2877 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
2878 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
2879 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
2880 _LIBCPP_ALWAYS_INLINE string_type curr_symbol() const {return do_curr_symbol();}
2881 _LIBCPP_ALWAYS_INLINE string_type positive_sign() const {return do_positive_sign();}
2882 _LIBCPP_ALWAYS_INLINE string_type negative_sign() const {return do_negative_sign();}
2883 _LIBCPP_ALWAYS_INLINE int frac_digits() const {return do_frac_digits();}
2884 _LIBCPP_ALWAYS_INLINE pattern pos_format() const {return do_pos_format();}
2885 _LIBCPP_ALWAYS_INLINE pattern neg_format() const {return do_neg_format();}
2886
2887 static locale::id id;
2888 static const bool intl = _International;
2889
2890protected:
Howard Hinnant82894812010-09-22 16:48:34 +00002891 _LIBCPP_ALWAYS_INLINE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002892 ~moneypunct() {}
2893
2894 virtual char_type do_decimal_point() const {return numeric_limits<char_type>::max();}
2895 virtual char_type do_thousands_sep() const {return numeric_limits<char_type>::max();}
2896 virtual string do_grouping() const {return string();}
2897 virtual string_type do_curr_symbol() const {return string_type();}
2898 virtual string_type do_positive_sign() const {return string_type();}
2899 virtual string_type do_negative_sign() const {return string_type(1, '-');}
2900 virtual int do_frac_digits() const {return 0;}
2901 virtual pattern do_pos_format() const
Howard Hinnant9bae2a92012-11-06 21:48:33 +00002902 {pattern __p = {{symbol, sign, none, value}}; return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002903 virtual pattern do_neg_format() const
Howard Hinnant9bae2a92012-11-06 21:48:33 +00002904 {pattern __p = {{symbol, sign, none, value}}; return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002905};
2906
2907template <class _CharT, bool _International>
2908locale::id
2909moneypunct<_CharT, _International>::id;
2910
Howard Hinnant0a69fa12012-12-12 21:14:28 +00002911template <class _CharT, bool _International>
2912const bool
2913moneypunct<_CharT, _International>::intl;
2914
Howard Hinnantff926772012-11-06 21:08:48 +00002915_LIBCPP_EXTERN_TEMPLATE(class moneypunct<char, false>)
2916_LIBCPP_EXTERN_TEMPLATE(class moneypunct<char, true>)
2917_LIBCPP_EXTERN_TEMPLATE(class moneypunct<wchar_t, false>)
2918_LIBCPP_EXTERN_TEMPLATE(class moneypunct<wchar_t, true>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002919
2920// moneypunct_byname
2921
2922template <class _CharT, bool _International = false>
Howard Hinnant83eade62013-03-06 23:30:19 +00002923class _LIBCPP_TYPE_VIS moneypunct_byname
Howard Hinnant82894812010-09-22 16:48:34 +00002924 : public moneypunct<_CharT, _International>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002925{
2926public:
2927 typedef money_base::pattern pattern;
2928 typedef _CharT char_type;
2929 typedef basic_string<char_type> string_type;
2930
2931 _LIBCPP_ALWAYS_INLINE
2932 explicit moneypunct_byname(const char* __nm, size_t __refs = 0)
2933 : moneypunct<_CharT, _International>(__refs) {init(__nm);}
2934
2935 _LIBCPP_ALWAYS_INLINE
2936 explicit moneypunct_byname(const string& __nm, size_t __refs = 0)
2937 : moneypunct<_CharT, _International>(__refs) {init(__nm.c_str());}
2938
2939protected:
Howard Hinnant82894812010-09-22 16:48:34 +00002940 _LIBCPP_ALWAYS_INLINE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002941 ~moneypunct_byname() {}
2942
2943 virtual char_type do_decimal_point() const {return __decimal_point_;}
2944 virtual char_type do_thousands_sep() const {return __thousands_sep_;}
2945 virtual string do_grouping() const {return __grouping_;}
2946 virtual string_type do_curr_symbol() const {return __curr_symbol_;}
2947 virtual string_type do_positive_sign() const {return __positive_sign_;}
2948 virtual string_type do_negative_sign() const {return __negative_sign_;}
2949 virtual int do_frac_digits() const {return __frac_digits_;}
2950 virtual pattern do_pos_format() const {return __pos_format_;}
2951 virtual pattern do_neg_format() const {return __neg_format_;}
2952
2953private:
2954 char_type __decimal_point_;
2955 char_type __thousands_sep_;
2956 string __grouping_;
2957 string_type __curr_symbol_;
2958 string_type __positive_sign_;
2959 string_type __negative_sign_;
2960 int __frac_digits_;
2961 pattern __pos_format_;
2962 pattern __neg_format_;
2963
2964 void init(const char*);
2965};
2966
2967template<> void moneypunct_byname<char, false>::init(const char*);
2968template<> void moneypunct_byname<char, true>::init(const char*);
2969template<> void moneypunct_byname<wchar_t, false>::init(const char*);
2970template<> void moneypunct_byname<wchar_t, true>::init(const char*);
2971
Howard Hinnantff926772012-11-06 21:08:48 +00002972_LIBCPP_EXTERN_TEMPLATE(class moneypunct_byname<char, false>)
2973_LIBCPP_EXTERN_TEMPLATE(class moneypunct_byname<char, true>)
2974_LIBCPP_EXTERN_TEMPLATE(class moneypunct_byname<wchar_t, false>)
2975_LIBCPP_EXTERN_TEMPLATE(class moneypunct_byname<wchar_t, true>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002976
2977// money_get
2978
2979template <class _CharT>
2980class __money_get
2981{
2982protected:
2983 typedef _CharT char_type;
2984 typedef basic_string<char_type> string_type;
2985
2986 _LIBCPP_ALWAYS_INLINE __money_get() {}
2987
2988 static void __gather_info(bool __intl, const locale& __loc,
2989 money_base::pattern& __pat, char_type& __dp,
2990 char_type& __ts, string& __grp,
2991 string_type& __sym, string_type& __psn,
2992 string_type& __nsn, int& __fd);
2993};
2994
2995template <class _CharT>
2996void
2997__money_get<_CharT>::__gather_info(bool __intl, const locale& __loc,
2998 money_base::pattern& __pat, char_type& __dp,
2999 char_type& __ts, string& __grp,
3000 string_type& __sym, string_type& __psn,
3001 string_type& __nsn, int& __fd)
3002{
3003 if (__intl)
3004 {
3005 const moneypunct<char_type, true>& __mp =
3006 use_facet<moneypunct<char_type, true> >(__loc);
3007 __pat = __mp.neg_format();
3008 __nsn = __mp.negative_sign();
3009 __psn = __mp.positive_sign();
3010 __dp = __mp.decimal_point();
3011 __ts = __mp.thousands_sep();
3012 __grp = __mp.grouping();
3013 __sym = __mp.curr_symbol();
3014 __fd = __mp.frac_digits();
3015 }
3016 else
3017 {
3018 const moneypunct<char_type, false>& __mp =
3019 use_facet<moneypunct<char_type, false> >(__loc);
3020 __pat = __mp.neg_format();
3021 __nsn = __mp.negative_sign();
3022 __psn = __mp.positive_sign();
3023 __dp = __mp.decimal_point();
3024 __ts = __mp.thousands_sep();
3025 __grp = __mp.grouping();
3026 __sym = __mp.curr_symbol();
3027 __fd = __mp.frac_digits();
3028 }
3029}
3030
Howard Hinnantff926772012-11-06 21:08:48 +00003031_LIBCPP_EXTERN_TEMPLATE(class __money_get<char>)
3032_LIBCPP_EXTERN_TEMPLATE(class __money_get<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003033
3034template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Howard Hinnant83eade62013-03-06 23:30:19 +00003035class _LIBCPP_TYPE_VIS money_get
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003036 : public locale::facet,
3037 private __money_get<_CharT>
3038{
3039public:
3040 typedef _CharT char_type;
3041 typedef _InputIterator iter_type;
3042 typedef basic_string<char_type> string_type;
3043
3044 _LIBCPP_ALWAYS_INLINE
3045 explicit money_get(size_t __refs = 0)
3046 : locale::facet(__refs) {}
3047
3048 _LIBCPP_ALWAYS_INLINE
3049 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
3050 ios_base::iostate& __err, long double& __v) const
3051 {
3052 return do_get(__b, __e, __intl, __iob, __err, __v);
3053 }
3054
3055 _LIBCPP_ALWAYS_INLINE
3056 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
3057 ios_base::iostate& __err, string_type& __v) const
3058 {
3059 return do_get(__b, __e, __intl, __iob, __err, __v);
3060 }
3061
3062 static locale::id id;
3063
3064protected:
3065
Howard Hinnant82894812010-09-22 16:48:34 +00003066 _LIBCPP_ALWAYS_INLINE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003067 ~money_get() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00003068
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003069 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
3070 ios_base& __iob, ios_base::iostate& __err,
3071 long double& __v) const;
3072 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
3073 ios_base& __iob, ios_base::iostate& __err,
3074 string_type& __v) const;
3075
3076private:
3077 static bool __do_get(iter_type& __b, iter_type __e,
3078 bool __intl, const locale& __loc,
3079 ios_base::fmtflags __flags, ios_base::iostate& __err,
3080 bool& __neg, const ctype<char_type>& __ct,
3081 unique_ptr<char_type, void(*)(void*)>& __wb,
3082 char_type*& __wn, char_type* __we);
3083};
3084
3085template <class _CharT, class _InputIterator>
3086locale::id
3087money_get<_CharT, _InputIterator>::id;
3088
3089void __do_nothing(void*);
3090
3091template <class _Tp>
3092_LIBCPP_HIDDEN
3093void
3094__double_or_nothing(unique_ptr<_Tp, void(*)(void*)>& __b, _Tp*& __n, _Tp*& __e)
3095{
3096 bool __owns = __b.get_deleter() != __do_nothing;
Howard Hinnantec3773c2011-12-01 20:21:04 +00003097 size_t __cur_cap = static_cast<size_t>(__e-__b.get()) * sizeof(_Tp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003098 size_t __new_cap = __cur_cap < numeric_limits<size_t>::max() / 2 ?
3099 2 * __cur_cap : numeric_limits<size_t>::max();
Howard Hinnantec3773c2011-12-01 20:21:04 +00003100 size_t __n_off = static_cast<size_t>(__n - __b.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003101 _Tp* __t = (_Tp*)realloc(__owns ? __b.get() : 0, __new_cap);
3102 if (__t == 0)
3103 __throw_bad_alloc();
3104 if (__owns)
3105 __b.release();
3106 __b = unique_ptr<_Tp, void(*)(void*)>(__t, free);
3107 __new_cap /= sizeof(_Tp);
3108 __n = __b.get() + __n_off;
3109 __e = __b.get() + __new_cap;
3110}
3111
3112// true == success
3113template <class _CharT, class _InputIterator>
3114bool
3115money_get<_CharT, _InputIterator>::__do_get(iter_type& __b, iter_type __e,
3116 bool __intl, const locale& __loc,
3117 ios_base::fmtflags __flags,
3118 ios_base::iostate& __err,
3119 bool& __neg,
3120 const ctype<char_type>& __ct,
3121 unique_ptr<char_type, void(*)(void*)>& __wb,
3122 char_type*& __wn, char_type* __we)
3123{
3124 const unsigned __bz = 100;
3125 unsigned __gbuf[__bz];
3126 unique_ptr<unsigned, void(*)(void*)> __gb(__gbuf, __do_nothing);
3127 unsigned* __gn = __gb.get();
3128 unsigned* __ge = __gn + __bz;
3129 money_base::pattern __pat;
3130 char_type __dp;
3131 char_type __ts;
3132 string __grp;
3133 string_type __sym;
3134 string_type __psn;
3135 string_type __nsn;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00003136 // Capture the spaces read into money_base::{space,none} so they
3137 // can be compared to initial spaces in __sym.
3138 string_type __spaces;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003139 int __fd;
3140 __money_get<_CharT>::__gather_info(__intl, __loc, __pat, __dp, __ts, __grp,
3141 __sym, __psn, __nsn, __fd);
3142 const string_type* __trailing_sign = 0;
3143 __wn = __wb.get();
3144 for (unsigned __p = 0; __p < 4 && __b != __e; ++__p)
3145 {
3146 switch (__pat.field[__p])
3147 {
3148 case money_base::space:
3149 if (__p != 3)
3150 {
3151 if (__ct.is(ctype_base::space, *__b))
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00003152 __spaces.push_back(*__b++);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003153 else
3154 {
3155 __err |= ios_base::failbit;
3156 return false;
3157 }
3158 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003159 // drop through
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003160 case money_base::none:
3161 if (__p != 3)
3162 {
3163 while (__b != __e && __ct.is(ctype_base::space, *__b))
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00003164 __spaces.push_back(*__b++);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003165 }
3166 break;
3167 case money_base::sign:
3168 if (__psn.size() + __nsn.size() > 0)
3169 {
3170 if (__psn.size() == 0 || __nsn.size() == 0)
3171 { // sign is optional
3172 if (__psn.size() > 0)
3173 { // __nsn.size() == 0
3174 if (*__b == __psn[0])
3175 {
3176 ++__b;
3177 if (__psn.size() > 1)
3178 __trailing_sign = &__psn;
3179 }
3180 else
3181 __neg = true;
3182 }
3183 else if (*__b == __nsn[0]) // __nsn.size() > 0 && __psn.size() == 0
3184 {
3185 ++__b;
3186 __neg = true;
3187 if (__nsn.size() > 1)
3188 __trailing_sign = &__nsn;
3189 }
3190 }
3191 else // sign is required
3192 {
3193 if (*__b == __psn[0])
3194 {
3195 ++__b;
3196 if (__psn.size() > 1)
3197 __trailing_sign = &__psn;
3198 }
3199 else if (*__b == __nsn[0])
3200 {
3201 ++__b;
3202 __neg = true;
3203 if (__nsn.size() > 1)
3204 __trailing_sign = &__nsn;
3205 }
3206 else
3207 {
3208 __err |= ios_base::failbit;
3209 return false;
3210 }
3211 }
3212 }
3213 break;
3214 case money_base::symbol:
3215 {
3216 bool __more_needed = __trailing_sign ||
3217 (__p < 2) ||
3218 (__p == 2 && __pat.field[3] != static_cast<char>(money_base::none));
3219 bool __sb = __flags & ios_base::showbase;
3220 if (__sb || __more_needed)
3221 {
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00003222 typename string_type::const_iterator __sym_space_end = __sym.begin();
3223 if (__p > 0 && (__pat.field[__p - 1] == money_base::none ||
3224 __pat.field[__p - 1] == money_base::space)) {
3225 // Match spaces we've already read against spaces at
3226 // the beginning of __sym.
3227 while (__sym_space_end != __sym.end() &&
3228 __ct.is(ctype_base::space, *__sym_space_end))
3229 ++__sym_space_end;
3230 const size_t __num_spaces = __sym_space_end - __sym.begin();
3231 if (__num_spaces > __spaces.size() ||
3232 !equal(__spaces.end() - __num_spaces, __spaces.end(),
3233 __sym.begin())) {
3234 // No match. Put __sym_space_end back at the
3235 // beginning of __sym, which will prevent a
3236 // match in the next loop.
3237 __sym_space_end = __sym.begin();
3238 }
3239 }
3240 typename string_type::const_iterator __sym_curr_char = __sym_space_end;
3241 while (__sym_curr_char != __sym.end() && __b != __e &&
3242 *__b == *__sym_curr_char) {
3243 ++__b;
3244 ++__sym_curr_char;
3245 }
3246 if (__sb && __sym_curr_char != __sym.end())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003247 {
3248 __err |= ios_base::failbit;
3249 return false;
3250 }
3251 }
3252 }
3253 break;
3254 case money_base::value:
3255 {
3256 unsigned __ng = 0;
3257 for (; __b != __e; ++__b)
3258 {
3259 char_type __c = *__b;
3260 if (__ct.is(ctype_base::digit, __c))
3261 {
3262 if (__wn == __we)
3263 __double_or_nothing(__wb, __wn, __we);
3264 *__wn++ = __c;
3265 ++__ng;
3266 }
3267 else if (__grp.size() > 0 && __ng > 0 && __c == __ts)
3268 {
3269 if (__gn == __ge)
3270 __double_or_nothing(__gb, __gn, __ge);
3271 *__gn++ = __ng;
3272 __ng = 0;
3273 }
3274 else
3275 break;
3276 }
3277 if (__gb.get() != __gn && __ng > 0)
3278 {
3279 if (__gn == __ge)
3280 __double_or_nothing(__gb, __gn, __ge);
3281 *__gn++ = __ng;
3282 }
3283 if (__fd > 0)
3284 {
3285 if (__b == __e || *__b != __dp)
3286 {
3287 __err |= ios_base::failbit;
3288 return false;
3289 }
3290 for (++__b; __fd > 0; --__fd, ++__b)
3291 {
3292 if (__b == __e || !__ct.is(ctype_base::digit, *__b))
3293 {
3294 __err |= ios_base::failbit;
3295 return false;
3296 }
3297 if (__wn == __we)
3298 __double_or_nothing(__wb, __wn, __we);
3299 *__wn++ = *__b;
3300 }
3301 }
3302 if (__wn == __wb.get())
3303 {
3304 __err |= ios_base::failbit;
3305 return false;
3306 }
3307 }
3308 break;
3309 }
3310 }
3311 if (__trailing_sign)
3312 {
3313 for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, ++__b)
3314 {
3315 if (__b == __e || *__b != (*__trailing_sign)[__i])
3316 {
3317 __err |= ios_base::failbit;
3318 return false;
3319 }
3320 }
3321 }
3322 if (__gb.get() != __gn)
3323 {
3324 ios_base::iostate __et = ios_base::goodbit;
3325 __check_grouping(__grp, __gb.get(), __gn, __et);
3326 if (__et)
3327 {
3328 __err |= ios_base::failbit;
3329 return false;
3330 }
3331 }
3332 return true;
3333}
3334
3335template <class _CharT, class _InputIterator>
3336_InputIterator
3337money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3338 bool __intl, ios_base& __iob,
3339 ios_base::iostate& __err,
3340 long double& __v) const
3341{
Howard Hinnantec3773c2011-12-01 20:21:04 +00003342 const int __bz = 100;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003343 char_type __wbuf[__bz];
3344 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3345 char_type* __wn;
3346 char_type* __we = __wbuf + __bz;
3347 locale __loc = __iob.getloc();
3348 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3349 bool __neg = false;
3350 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3351 __wb, __wn, __we))
3352 {
3353 const char __src[] = "0123456789";
3354 char_type __atoms[sizeof(__src)-1];
3355 __ct.widen(__src, __src + (sizeof(__src)-1), __atoms);
3356 char __nbuf[__bz];
3357 char* __nc = __nbuf;
3358 unique_ptr<char, void(*)(void*)> __h(0, free);
3359 if (__wn - __wb.get() > __bz-2)
3360 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00003361 __h.reset((char*)malloc(static_cast<size_t>(__wn - __wb.get() + 2)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003362 if (__h.get() == 0)
3363 __throw_bad_alloc();
3364 __nc = __h.get();
3365 }
3366 if (__neg)
3367 *__nc++ = '-';
3368 for (const char_type* __w = __wb.get(); __w < __wn; ++__w, ++__nc)
Marshall Clow9b145da2013-03-22 02:14:40 +00003369 *__nc = __src[find(__atoms, _VSTD::end(__atoms), *__w) - __atoms];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003370 *__nc = char();
3371 if (sscanf(__nbuf, "%Lf", &__v) != 1)
3372 __throw_runtime_error("money_get error");
3373 }
3374 if (__b == __e)
3375 __err |= ios_base::eofbit;
3376 return __b;
3377}
3378
3379template <class _CharT, class _InputIterator>
3380_InputIterator
3381money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3382 bool __intl, ios_base& __iob,
3383 ios_base::iostate& __err,
3384 string_type& __v) const
3385{
Howard Hinnantec3773c2011-12-01 20:21:04 +00003386 const int __bz = 100;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003387 char_type __wbuf[__bz];
3388 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3389 char_type* __wn;
3390 char_type* __we = __wbuf + __bz;
3391 locale __loc = __iob.getloc();
3392 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3393 bool __neg = false;
3394 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3395 __wb, __wn, __we))
3396 {
3397 __v.clear();
3398 if (__neg)
3399 __v.push_back(__ct.widen('-'));
3400 char_type __z = __ct.widen('0');
3401 char_type* __w;
3402 for (__w = __wb.get(); __w < __wn-1; ++__w)
3403 if (*__w != __z)
3404 break;
3405 __v.append(__w, __wn);
3406 }
3407 if (__b == __e)
3408 __err |= ios_base::eofbit;
3409 return __b;
3410}
3411
Howard Hinnantff926772012-11-06 21:08:48 +00003412_LIBCPP_EXTERN_TEMPLATE(class money_get<char>)
3413_LIBCPP_EXTERN_TEMPLATE(class money_get<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003414
3415// money_put
3416
3417template <class _CharT>
3418class __money_put
3419{
3420protected:
3421 typedef _CharT char_type;
3422 typedef basic_string<char_type> string_type;
3423
3424 _LIBCPP_ALWAYS_INLINE __money_put() {}
3425
3426 static void __gather_info(bool __intl, bool __neg, const locale& __loc,
3427 money_base::pattern& __pat, char_type& __dp,
3428 char_type& __ts, string& __grp,
3429 string_type& __sym, string_type& __sn,
3430 int& __fd);
3431 static void __format(char_type* __mb, char_type*& __mi, char_type*& __me,
3432 ios_base::fmtflags __flags,
3433 const char_type* __db, const char_type* __de,
3434 const ctype<char_type>& __ct, bool __neg,
3435 const money_base::pattern& __pat, char_type __dp,
3436 char_type __ts, const string& __grp,
3437 const string_type& __sym, const string_type& __sn,
3438 int __fd);
3439};
3440
3441template <class _CharT>
3442void
3443__money_put<_CharT>::__gather_info(bool __intl, bool __neg, const locale& __loc,
3444 money_base::pattern& __pat, char_type& __dp,
3445 char_type& __ts, string& __grp,
3446 string_type& __sym, string_type& __sn,
3447 int& __fd)
3448{
3449 if (__intl)
3450 {
3451 const moneypunct<char_type, true>& __mp =
3452 use_facet<moneypunct<char_type, true> >(__loc);
3453 if (__neg)
3454 {
3455 __pat = __mp.neg_format();
3456 __sn = __mp.negative_sign();
3457 }
3458 else
3459 {
3460 __pat = __mp.pos_format();
3461 __sn = __mp.positive_sign();
3462 }
3463 __dp = __mp.decimal_point();
3464 __ts = __mp.thousands_sep();
3465 __grp = __mp.grouping();
3466 __sym = __mp.curr_symbol();
3467 __fd = __mp.frac_digits();
3468 }
3469 else
3470 {
3471 const moneypunct<char_type, false>& __mp =
3472 use_facet<moneypunct<char_type, false> >(__loc);
3473 if (__neg)
3474 {
3475 __pat = __mp.neg_format();
3476 __sn = __mp.negative_sign();
3477 }
3478 else
3479 {
3480 __pat = __mp.pos_format();
3481 __sn = __mp.positive_sign();
3482 }
3483 __dp = __mp.decimal_point();
3484 __ts = __mp.thousands_sep();
3485 __grp = __mp.grouping();
3486 __sym = __mp.curr_symbol();
3487 __fd = __mp.frac_digits();
3488 }
3489}
3490
3491template <class _CharT>
3492void
3493__money_put<_CharT>::__format(char_type* __mb, char_type*& __mi, char_type*& __me,
3494 ios_base::fmtflags __flags,
3495 const char_type* __db, const char_type* __de,
3496 const ctype<char_type>& __ct, bool __neg,
3497 const money_base::pattern& __pat, char_type __dp,
3498 char_type __ts, const string& __grp,
3499 const string_type& __sym, const string_type& __sn,
3500 int __fd)
3501{
3502 __me = __mb;
3503 for (unsigned __p = 0; __p < 4; ++__p)
3504 {
3505 switch (__pat.field[__p])
3506 {
3507 case money_base::none:
3508 __mi = __me;
3509 break;
3510 case money_base::space:
3511 __mi = __me;
3512 *__me++ = __ct.widen(' ');
3513 break;
3514 case money_base::sign:
3515 if (!__sn.empty())
3516 *__me++ = __sn[0];
3517 break;
3518 case money_base::symbol:
3519 if (!__sym.empty() && (__flags & ios_base::showbase))
Howard Hinnant0949eed2011-06-30 21:18:19 +00003520 __me = _VSTD::copy(__sym.begin(), __sym.end(), __me);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003521 break;
3522 case money_base::value:
3523 {
3524 // remember start of value so we can reverse it
3525 char_type* __t = __me;
3526 // find beginning of digits
3527 if (__neg)
3528 ++__db;
3529 // find end of digits
3530 const char_type* __d;
3531 for (__d = __db; __d < __de; ++__d)
3532 if (!__ct.is(ctype_base::digit, *__d))
3533 break;
3534 // print fractional part
3535 if (__fd > 0)
3536 {
3537 int __f;
3538 for (__f = __fd; __d > __db && __f > 0; --__f)
3539 *__me++ = *--__d;
3540 char_type __z = __f > 0 ? __ct.widen('0') : char_type();
3541 for (; __f > 0; --__f)
3542 *__me++ = __z;
3543 *__me++ = __dp;
3544 }
3545 // print units part
3546 if (__d == __db)
3547 {
3548 *__me++ = __ct.widen('0');
3549 }
3550 else
3551 {
3552 unsigned __ng = 0;
3553 unsigned __ig = 0;
3554 unsigned __gl = __grp.empty() ? numeric_limits<unsigned>::max()
3555 : static_cast<unsigned>(__grp[__ig]);
3556 while (__d != __db)
3557 {
3558 if (__ng == __gl)
3559 {
3560 *__me++ = __ts;
3561 __ng = 0;
3562 if (++__ig < __grp.size())
3563 __gl = __grp[__ig] == numeric_limits<char>::max() ?
3564 numeric_limits<unsigned>::max() :
3565 static_cast<unsigned>(__grp[__ig]);
3566 }
3567 *__me++ = *--__d;
3568 ++__ng;
3569 }
3570 }
3571 // reverse it
3572 reverse(__t, __me);
3573 }
3574 break;
3575 }
3576 }
3577 // print rest of sign, if any
3578 if (__sn.size() > 1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003579 __me = _VSTD::copy(__sn.begin()+1, __sn.end(), __me);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003580 // set alignment
3581 if ((__flags & ios_base::adjustfield) == ios_base::left)
3582 __mi = __me;
3583 else if ((__flags & ios_base::adjustfield) != ios_base::internal)
3584 __mi = __mb;
3585}
3586
Howard Hinnantff926772012-11-06 21:08:48 +00003587_LIBCPP_EXTERN_TEMPLATE(class __money_put<char>)
3588_LIBCPP_EXTERN_TEMPLATE(class __money_put<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003589
3590template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Howard Hinnant83eade62013-03-06 23:30:19 +00003591class _LIBCPP_TYPE_VIS money_put
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003592 : public locale::facet,
3593 private __money_put<_CharT>
3594{
3595public:
3596 typedef _CharT char_type;
3597 typedef _OutputIterator iter_type;
3598 typedef basic_string<char_type> string_type;
3599
3600 _LIBCPP_ALWAYS_INLINE
3601 explicit money_put(size_t __refs = 0)
3602 : locale::facet(__refs) {}
3603
3604 _LIBCPP_ALWAYS_INLINE
3605 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3606 long double __units) const
3607 {
3608 return do_put(__s, __intl, __iob, __fl, __units);
3609 }
3610
3611 _LIBCPP_ALWAYS_INLINE
3612 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3613 const string_type& __digits) const
3614 {
3615 return do_put(__s, __intl, __iob, __fl, __digits);
3616 }
3617
3618 static locale::id id;
3619
3620protected:
Howard Hinnant82894812010-09-22 16:48:34 +00003621 _LIBCPP_ALWAYS_INLINE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003622 ~money_put() {}
3623
3624 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3625 char_type __fl, long double __units) const;
3626 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3627 char_type __fl, const string_type& __digits) const;
3628};
3629
3630template <class _CharT, class _OutputIterator>
3631locale::id
3632money_put<_CharT, _OutputIterator>::id;
3633
3634template <class _CharT, class _OutputIterator>
3635_OutputIterator
3636money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3637 ios_base& __iob, char_type __fl,
3638 long double __units) const
3639{
3640 // convert to char
3641 const size_t __bs = 100;
3642 char __buf[__bs];
3643 char* __bb = __buf;
3644 char_type __digits[__bs];
3645 char_type* __db = __digits;
Howard Hinnantec3773c2011-12-01 20:21:04 +00003646 size_t __n = static_cast<size_t>(snprintf(__bb, __bs, "%.0Lf", __units));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003647 unique_ptr<char, void(*)(void*)> __hn(0, free);
3648 unique_ptr<char_type, void(*)(void*)> __hd(0, free);
3649 // secure memory for digit storage
3650 if (__n > __bs-1)
3651 {
Howard Hinnant866569b2011-09-28 23:39:33 +00003652#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnantec3773c2011-12-01 20:21:04 +00003653 __n = static_cast<size_t>(asprintf_l(&__bb, _LIBCPP_GET_C_LOCALE, "%.0Lf", __units));
Sean Huntf3907e62011-07-15 05:40:33 +00003654#else
3655 __n = __asprintf_l(&__bb, __cloc(), "%.0Lf", __units);
3656#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003657 if (__bb == 0)
3658 __throw_bad_alloc();
3659 __hn.reset(__bb);
3660 __hd.reset((char_type*)malloc(__n * sizeof(char_type)));
Howard Hinnant05b57d52012-03-07 20:37:43 +00003661 if (__hd == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003662 __throw_bad_alloc();
3663 __db = __hd.get();
3664 }
3665 // gather info
3666 locale __loc = __iob.getloc();
3667 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3668 __ct.widen(__bb, __bb + __n, __db);
3669 bool __neg = __n > 0 && __bb[0] == '-';
3670 money_base::pattern __pat;
3671 char_type __dp;
3672 char_type __ts;
3673 string __grp;
3674 string_type __sym;
3675 string_type __sn;
3676 int __fd;
3677 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3678 // secure memory for formatting
3679 char_type __mbuf[__bs];
3680 char_type* __mb = __mbuf;
3681 unique_ptr<char_type, void(*)(void*)> __hw(0, free);
3682 size_t __exn = static_cast<int>(__n) > __fd ?
Howard Hinnantec3773c2011-12-01 20:21:04 +00003683 (__n - static_cast<size_t>(__fd)) * 2 + __sn.size() +
3684 __sym.size() + static_cast<size_t>(__fd) + 1
3685 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003686 if (__exn > __bs)
3687 {
3688 __hw.reset((char_type*)malloc(__exn * sizeof(char_type)));
3689 __mb = __hw.get();
3690 if (__mb == 0)
3691 __throw_bad_alloc();
3692 }
3693 // format
3694 char_type* __mi;
3695 char_type* __me;
3696 this->__format(__mb, __mi, __me, __iob.flags(),
3697 __db, __db + __n, __ct,
3698 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3699 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3700}
3701
3702template <class _CharT, class _OutputIterator>
3703_OutputIterator
3704money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3705 ios_base& __iob, char_type __fl,
3706 const string_type& __digits) const
3707{
3708 // gather info
3709 locale __loc = __iob.getloc();
3710 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3711 bool __neg = __digits.size() > 0 && __digits[0] == __ct.widen('-');
3712 money_base::pattern __pat;
3713 char_type __dp;
3714 char_type __ts;
3715 string __grp;
3716 string_type __sym;
3717 string_type __sn;
3718 int __fd;
3719 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3720 // secure memory for formatting
3721 char_type __mbuf[100];
3722 char_type* __mb = __mbuf;
3723 unique_ptr<char_type, void(*)(void*)> __h(0, free);
Howard Hinnantec3773c2011-12-01 20:21:04 +00003724 size_t __exn = static_cast<int>(__digits.size()) > __fd ?
3725 (__digits.size() - static_cast<size_t>(__fd)) * 2 +
3726 __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 1
3727 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003728 if (__exn > 100)
3729 {
3730 __h.reset((char_type*)malloc(__exn * sizeof(char_type)));
3731 __mb = __h.get();
3732 if (__mb == 0)
3733 __throw_bad_alloc();
3734 }
3735 // format
3736 char_type* __mi;
3737 char_type* __me;
3738 this->__format(__mb, __mi, __me, __iob.flags(),
3739 __digits.data(), __digits.data() + __digits.size(), __ct,
3740 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3741 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3742}
3743
Howard Hinnantff926772012-11-06 21:08:48 +00003744_LIBCPP_EXTERN_TEMPLATE(class money_put<char>)
3745_LIBCPP_EXTERN_TEMPLATE(class money_put<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003746
3747// messages
3748
Howard Hinnant83eade62013-03-06 23:30:19 +00003749class _LIBCPP_TYPE_VIS messages_base
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003750{
3751public:
Howard Hinnantb2080c72011-02-25 00:51:08 +00003752 typedef ptrdiff_t catalog;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003753
3754 _LIBCPP_ALWAYS_INLINE messages_base() {}
3755};
3756
3757template <class _CharT>
Howard Hinnant83eade62013-03-06 23:30:19 +00003758class _LIBCPP_TYPE_VIS messages
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003759 : public locale::facet,
3760 public messages_base
3761{
3762public:
3763 typedef _CharT char_type;
3764 typedef basic_string<_CharT> string_type;
3765
3766 _LIBCPP_ALWAYS_INLINE
3767 explicit messages(size_t __refs = 0)
3768 : locale::facet(__refs) {}
3769
3770 _LIBCPP_ALWAYS_INLINE
3771 catalog open(const basic_string<char>& __nm, const locale& __loc) const
3772 {
3773 return do_open(__nm, __loc);
3774 }
3775
3776 _LIBCPP_ALWAYS_INLINE
3777 string_type get(catalog __c, int __set, int __msgid,
3778 const string_type& __dflt) const
3779 {
3780 return do_get(__c, __set, __msgid, __dflt);
3781 }
3782
3783 _LIBCPP_ALWAYS_INLINE
3784 void close(catalog __c) const
3785 {
3786 do_close(__c);
3787 }
3788
3789 static locale::id id;
3790
3791protected:
Howard Hinnant82894812010-09-22 16:48:34 +00003792 _LIBCPP_ALWAYS_INLINE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003793 ~messages() {}
3794
3795 virtual catalog do_open(const basic_string<char>&, const locale&) const;
3796 virtual string_type do_get(catalog, int __set, int __msgid,
3797 const string_type& __dflt) const;
3798 virtual void do_close(catalog) const;
3799};
3800
3801template <class _CharT>
3802locale::id
3803messages<_CharT>::id;
3804
3805template <class _CharT>
3806typename messages<_CharT>::catalog
3807messages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const
3808{
Marshall Clowa22d2ad2013-03-18 17:04:29 +00003809#ifdef _WIN32
Howard Hinnant6cd05ee2011-09-23 16:11:27 +00003810 return -1;
3811#else // _WIN32
Howard Hinnant11624452011-10-11 16:00:46 +00003812 catalog __cat = (catalog)catopen(__nm.c_str(), NL_CAT_LOCALE);
Howard Hinnantb2080c72011-02-25 00:51:08 +00003813 if (__cat != -1)
3814 __cat = static_cast<catalog>((static_cast<size_t>(__cat) >> 1));
3815 return __cat;
Howard Hinnant6cd05ee2011-09-23 16:11:27 +00003816#endif // _WIN32
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003817}
3818
3819template <class _CharT>
3820typename messages<_CharT>::string_type
3821messages<_CharT>::do_get(catalog __c, int __set, int __msgid,
3822 const string_type& __dflt) const
3823{
Marshall Clowa22d2ad2013-03-18 17:04:29 +00003824#ifdef _WIN32
Howard Hinnant6cd05ee2011-09-23 16:11:27 +00003825 return __dflt;
3826#else // _WIN32
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003827 string __ndflt;
3828 __narrow_to_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__ndflt),
3829 __dflt.c_str(),
3830 __dflt.c_str() + __dflt.size());
Howard Hinnantb2080c72011-02-25 00:51:08 +00003831 if (__c != -1)
3832 __c <<= 1;
Howard Hinnant11624452011-10-11 16:00:46 +00003833 nl_catd __cat = (nl_catd)__c;
Howard Hinnantb2080c72011-02-25 00:51:08 +00003834 char* __n = catgets(__cat, __set, __msgid, __ndflt.c_str());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003835 string_type __w;
3836 __widen_from_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__w),
3837 __n, __n + strlen(__n));
3838 return __w;
Howard Hinnant6cd05ee2011-09-23 16:11:27 +00003839#endif // _WIN32
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003840}
3841
3842template <class _CharT>
3843void
3844messages<_CharT>::do_close(catalog __c) const
3845{
Marshall Clowa22d2ad2013-03-18 17:04:29 +00003846#if !defined(_WIN32)
Howard Hinnantb2080c72011-02-25 00:51:08 +00003847 if (__c != -1)
3848 __c <<= 1;
Howard Hinnant11624452011-10-11 16:00:46 +00003849 nl_catd __cat = (nl_catd)__c;
Howard Hinnantb2080c72011-02-25 00:51:08 +00003850 catclose(__cat);
Howard Hinnant6cd05ee2011-09-23 16:11:27 +00003851#endif // !_WIN32
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003852}
3853
Howard Hinnantff926772012-11-06 21:08:48 +00003854_LIBCPP_EXTERN_TEMPLATE(class messages<char>)
3855_LIBCPP_EXTERN_TEMPLATE(class messages<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003856
3857template <class _CharT>
Howard Hinnant83eade62013-03-06 23:30:19 +00003858class _LIBCPP_TYPE_VIS messages_byname
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003859 : public messages<_CharT>
3860{
3861public:
3862 typedef messages_base::catalog catalog;
3863 typedef basic_string<_CharT> string_type;
3864
3865 _LIBCPP_ALWAYS_INLINE
3866 explicit messages_byname(const char*, size_t __refs = 0)
3867 : messages<_CharT>(__refs) {}
3868
3869 _LIBCPP_ALWAYS_INLINE
3870 explicit messages_byname(const string&, size_t __refs = 0)
3871 : messages<_CharT>(__refs) {}
3872
3873protected:
Howard Hinnant82894812010-09-22 16:48:34 +00003874 _LIBCPP_ALWAYS_INLINE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003875 ~messages_byname() {}
3876};
3877
Howard Hinnantff926772012-11-06 21:08:48 +00003878_LIBCPP_EXTERN_TEMPLATE(class messages_byname<char>)
3879_LIBCPP_EXTERN_TEMPLATE(class messages_byname<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003880
Howard Hinnantd23b4642010-05-31 20:58:54 +00003881template<class _Codecvt, class _Elem = wchar_t,
3882 class _Wide_alloc = allocator<_Elem>,
3883 class _Byte_alloc = allocator<char> >
Howard Hinnant83eade62013-03-06 23:30:19 +00003884class _LIBCPP_TYPE_VIS wstring_convert
Howard Hinnantd23b4642010-05-31 20:58:54 +00003885{
3886public:
3887 typedef basic_string<char, char_traits<char>, _Byte_alloc> byte_string;
3888 typedef basic_string<_Elem, char_traits<_Elem>, _Wide_alloc> wide_string;
3889 typedef typename _Codecvt::state_type state_type;
3890 typedef typename wide_string::traits_type::int_type int_type;
3891
3892private:
3893 byte_string __byte_err_string_;
3894 wide_string __wide_err_string_;
3895 _Codecvt* __cvtptr_;
3896 state_type __cvtstate_;
3897 size_t __cvtcount_;
3898
3899 wstring_convert(const wstring_convert& __wc);
3900 wstring_convert& operator=(const wstring_convert& __wc);
3901public:
3902 wstring_convert(_Codecvt* __pcvt = new _Codecvt);
3903 wstring_convert(_Codecvt* __pcvt, state_type __state);
3904 wstring_convert(const byte_string& __byte_err,
3905 const wide_string& __wide_err = wide_string());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003906#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd23b4642010-05-31 20:58:54 +00003907 wstring_convert(wstring_convert&& __wc);
3908#endif
3909 ~wstring_convert();
3910
Howard Hinnant82894812010-09-22 16:48:34 +00003911 _LIBCPP_ALWAYS_INLINE
Howard Hinnantd23b4642010-05-31 20:58:54 +00003912 wide_string from_bytes(char __byte)
3913 {return from_bytes(&__byte, &__byte+1);}
Howard Hinnant82894812010-09-22 16:48:34 +00003914 _LIBCPP_ALWAYS_INLINE
Howard Hinnantd23b4642010-05-31 20:58:54 +00003915 wide_string from_bytes(const char* __ptr)
3916 {return from_bytes(__ptr, __ptr + char_traits<char>::length(__ptr));}
Howard Hinnant82894812010-09-22 16:48:34 +00003917 _LIBCPP_ALWAYS_INLINE
Howard Hinnantd23b4642010-05-31 20:58:54 +00003918 wide_string from_bytes(const byte_string& __str)
3919 {return from_bytes(__str.data(), __str.data() + __str.size());}
3920 wide_string from_bytes(const char* __first, const char* __last);
3921
Howard Hinnant82894812010-09-22 16:48:34 +00003922 _LIBCPP_ALWAYS_INLINE
Howard Hinnantd23b4642010-05-31 20:58:54 +00003923 byte_string to_bytes(_Elem __wchar)
3924 {return to_bytes(&__wchar, &__wchar+1);}
Howard Hinnant82894812010-09-22 16:48:34 +00003925 _LIBCPP_ALWAYS_INLINE
Howard Hinnantd23b4642010-05-31 20:58:54 +00003926 byte_string to_bytes(const _Elem* __wptr)
3927 {return to_bytes(__wptr, __wptr + char_traits<_Elem>::length(__wptr));}
Howard Hinnant82894812010-09-22 16:48:34 +00003928 _LIBCPP_ALWAYS_INLINE
Howard Hinnantd23b4642010-05-31 20:58:54 +00003929 byte_string to_bytes(const wide_string& __wstr)
3930 {return to_bytes(__wstr.data(), __wstr.data() + __wstr.size());}
3931 byte_string to_bytes(const _Elem* __first, const _Elem* __last);
3932
Howard Hinnant82894812010-09-22 16:48:34 +00003933 _LIBCPP_ALWAYS_INLINE
Howard Hinnantd23b4642010-05-31 20:58:54 +00003934 size_t converted() const {return __cvtcount_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003935 _LIBCPP_ALWAYS_INLINE
Howard Hinnantd23b4642010-05-31 20:58:54 +00003936 state_type state() const {return __cvtstate_;}
3937};
3938
3939template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00003940inline _LIBCPP_ALWAYS_INLINE
Howard Hinnantd23b4642010-05-31 20:58:54 +00003941wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3942 wstring_convert(_Codecvt* __pcvt)
3943 : __cvtptr_(__pcvt), __cvtstate_(), __cvtcount_(0)
3944{
3945}
3946
3947template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00003948inline _LIBCPP_ALWAYS_INLINE
Howard Hinnantd23b4642010-05-31 20:58:54 +00003949wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3950 wstring_convert(_Codecvt* __pcvt, state_type __state)
3951 : __cvtptr_(__pcvt), __cvtstate_(__state), __cvtcount_(0)
3952{
3953}
3954
3955template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3956wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3957 wstring_convert(const byte_string& __byte_err, const wide_string& __wide_err)
3958 : __byte_err_string_(__byte_err), __wide_err_string_(__wide_err),
3959 __cvtstate_(), __cvtcount_(0)
3960{
3961 __cvtptr_ = new _Codecvt;
3962}
3963
Howard Hinnant73d21a42010-09-04 23:28:19 +00003964#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd23b4642010-05-31 20:58:54 +00003965
3966template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00003967inline _LIBCPP_ALWAYS_INLINE
Howard Hinnantd23b4642010-05-31 20:58:54 +00003968wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3969 wstring_convert(wstring_convert&& __wc)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003970 : __byte_err_string_(_VSTD::move(__wc.__byte_err_string_)),
3971 __wide_err_string_(_VSTD::move(__wc.__wide_err_string_)),
Howard Hinnantd23b4642010-05-31 20:58:54 +00003972 __cvtptr_(__wc.__cvtptr_),
3973 __cvtstate_(__wc.__cvtstate_), __cvtcount_(__wc.__cvtstate_)
3974{
3975 __wc.__cvtptr_ = nullptr;
3976}
3977
Howard Hinnantbfd55302010-09-04 23:46:48 +00003978#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd23b4642010-05-31 20:58:54 +00003979
3980template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3981wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::~wstring_convert()
3982{
3983 delete __cvtptr_;
3984}
3985
3986template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3987typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::wide_string
3988wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3989 from_bytes(const char* __frm, const char* __frm_end)
3990{
3991 __cvtcount_ = 0;
3992 if (__cvtptr_ != nullptr)
3993 {
3994 wide_string __ws(2*(__frm_end - __frm), _Elem());
Howard Hinnant1ca23672012-07-12 18:07:41 +00003995 if (__frm != __frm_end)
3996 __ws.resize(__ws.capacity());
Howard Hinnantd23b4642010-05-31 20:58:54 +00003997 codecvt_base::result __r = codecvt_base::ok;
3998 state_type __st = __cvtstate_;
3999 if (__frm != __frm_end)
4000 {
4001 _Elem* __to = &__ws[0];
4002 _Elem* __to_end = __to + __ws.size();
4003 const char* __frm_nxt;
4004 do
4005 {
4006 _Elem* __to_nxt;
4007 __r = __cvtptr_->in(__st, __frm, __frm_end, __frm_nxt,
4008 __to, __to_end, __to_nxt);
4009 __cvtcount_ += __frm_nxt - __frm;
4010 if (__frm_nxt == __frm)
4011 {
4012 __r = codecvt_base::error;
4013 }
4014 else if (__r == codecvt_base::noconv)
4015 {
4016 __ws.resize(__to - &__ws[0]);
4017 // This only gets executed if _Elem is char
4018 __ws.append((const _Elem*)__frm, (const _Elem*)__frm_end);
4019 __frm = __frm_nxt;
4020 __r = codecvt_base::ok;
4021 }
4022 else if (__r == codecvt_base::ok)
4023 {
4024 __ws.resize(__to_nxt - &__ws[0]);
4025 __frm = __frm_nxt;
4026 }
4027 else if (__r == codecvt_base::partial)
4028 {
4029 ptrdiff_t __s = __to_nxt - &__ws[0];
4030 __ws.resize(2 * __s);
4031 __to = &__ws[0] + __s;
4032 __to_end = &__ws[0] + __ws.size();
4033 __frm = __frm_nxt;
4034 }
4035 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
4036 }
4037 if (__r == codecvt_base::ok)
4038 return __ws;
4039 }
Howard Hinnantd4444702010-08-11 17:04:31 +00004040#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantd23b4642010-05-31 20:58:54 +00004041 if (__wide_err_string_.empty())
4042 throw range_error("wstring_convert: from_bytes error");
Howard Hinnant324bb032010-08-22 00:02:43 +00004043#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantd23b4642010-05-31 20:58:54 +00004044 return __wide_err_string_;
4045}
4046
4047template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
4048typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::byte_string
4049wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4050 to_bytes(const _Elem* __frm, const _Elem* __frm_end)
4051{
4052 __cvtcount_ = 0;
4053 if (__cvtptr_ != nullptr)
4054 {
4055 byte_string __bs(2*(__frm_end - __frm), char());
Howard Hinnant1ca23672012-07-12 18:07:41 +00004056 if (__frm != __frm_end)
4057 __bs.resize(__bs.capacity());
Howard Hinnantd23b4642010-05-31 20:58:54 +00004058 codecvt_base::result __r = codecvt_base::ok;
4059 state_type __st = __cvtstate_;
4060 if (__frm != __frm_end)
4061 {
4062 char* __to = &__bs[0];
4063 char* __to_end = __to + __bs.size();
4064 const _Elem* __frm_nxt;
4065 do
4066 {
4067 char* __to_nxt;
4068 __r = __cvtptr_->out(__st, __frm, __frm_end, __frm_nxt,
4069 __to, __to_end, __to_nxt);
4070 __cvtcount_ += __frm_nxt - __frm;
4071 if (__frm_nxt == __frm)
4072 {
4073 __r = codecvt_base::error;
4074 }
4075 else if (__r == codecvt_base::noconv)
4076 {
4077 __bs.resize(__to - &__bs[0]);
4078 // This only gets executed if _Elem is char
4079 __bs.append((const char*)__frm, (const char*)__frm_end);
4080 __frm = __frm_nxt;
4081 __r = codecvt_base::ok;
4082 }
4083 else if (__r == codecvt_base::ok)
4084 {
4085 __bs.resize(__to_nxt - &__bs[0]);
4086 __frm = __frm_nxt;
4087 }
4088 else if (__r == codecvt_base::partial)
4089 {
4090 ptrdiff_t __s = __to_nxt - &__bs[0];
4091 __bs.resize(2 * __s);
4092 __to = &__bs[0] + __s;
4093 __to_end = &__bs[0] + __bs.size();
4094 __frm = __frm_nxt;
4095 }
4096 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
4097 }
4098 if (__r == codecvt_base::ok)
4099 {
4100 size_t __s = __bs.size();
4101 __bs.resize(__bs.capacity());
4102 char* __to = &__bs[0] + __s;
4103 char* __to_end = __to + __bs.size();
4104 do
4105 {
4106 char* __to_nxt;
4107 __r = __cvtptr_->unshift(__st, __to, __to_end, __to_nxt);
4108 if (__r == codecvt_base::noconv)
4109 {
4110 __bs.resize(__to - &__bs[0]);
4111 __r = codecvt_base::ok;
4112 }
4113 else if (__r == codecvt_base::ok)
4114 {
4115 __bs.resize(__to_nxt - &__bs[0]);
4116 }
4117 else if (__r == codecvt_base::partial)
4118 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00004119 ptrdiff_t __sp = __to_nxt - &__bs[0];
4120 __bs.resize(2 * __sp);
4121 __to = &__bs[0] + __sp;
Howard Hinnantd23b4642010-05-31 20:58:54 +00004122 __to_end = &__bs[0] + __bs.size();
4123 }
4124 } while (__r == codecvt_base::partial);
4125 if (__r == codecvt_base::ok)
4126 return __bs;
4127 }
4128 }
Howard Hinnantd4444702010-08-11 17:04:31 +00004129#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantd23b4642010-05-31 20:58:54 +00004130 if (__byte_err_string_.empty())
4131 throw range_error("wstring_convert: to_bytes error");
Howard Hinnant324bb032010-08-22 00:02:43 +00004132#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantd23b4642010-05-31 20:58:54 +00004133 return __byte_err_string_;
4134}
4135
4136template <class _Codecvt, class _Elem = wchar_t, class _Tr = char_traits<_Elem> >
Howard Hinnant83eade62013-03-06 23:30:19 +00004137class _LIBCPP_TYPE_VIS wbuffer_convert
Howard Hinnantd23b4642010-05-31 20:58:54 +00004138 : public basic_streambuf<_Elem, _Tr>
4139{
4140public:
4141 // types:
4142 typedef _Elem char_type;
4143 typedef _Tr traits_type;
4144 typedef typename traits_type::int_type int_type;
4145 typedef typename traits_type::pos_type pos_type;
4146 typedef typename traits_type::off_type off_type;
4147 typedef typename _Codecvt::state_type state_type;
4148
4149private:
Howard Hinnant4b53f502010-06-01 20:09:18 +00004150 char* __extbuf_;
4151 const char* __extbufnext_;
4152 const char* __extbufend_;
4153 char __extbuf_min_[8];
4154 size_t __ebs_;
4155 char_type* __intbuf_;
4156 size_t __ibs_;
Howard Hinnantd23b4642010-05-31 20:58:54 +00004157 streambuf* __bufptr_;
Howard Hinnant4b53f502010-06-01 20:09:18 +00004158 _Codecvt* __cv_;
4159 state_type __st_;
4160 ios_base::openmode __cm_;
4161 bool __owns_eb_;
4162 bool __owns_ib_;
4163 bool __always_noconv_;
Howard Hinnantd23b4642010-05-31 20:58:54 +00004164
Howard Hinnant4b53f502010-06-01 20:09:18 +00004165 wbuffer_convert(const wbuffer_convert&);
4166 wbuffer_convert& operator=(const wbuffer_convert&);
Howard Hinnantd23b4642010-05-31 20:58:54 +00004167public:
4168 wbuffer_convert(streambuf* __bytebuf = 0, _Codecvt* __pcvt = new _Codecvt,
Howard Hinnant4b53f502010-06-01 20:09:18 +00004169 state_type __state = state_type());
4170 ~wbuffer_convert();
Howard Hinnantd23b4642010-05-31 20:58:54 +00004171
Howard Hinnant82894812010-09-22 16:48:34 +00004172 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd23b4642010-05-31 20:58:54 +00004173 streambuf* rdbuf() const {return __bufptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004174 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd23b4642010-05-31 20:58:54 +00004175 streambuf* rdbuf(streambuf* __bytebuf)
4176 {
4177 streambuf* __r = __bufptr_;
4178 __bufptr_ = __bytebuf;
4179 return __r;
4180 }
4181
Howard Hinnant82894812010-09-22 16:48:34 +00004182 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4b53f502010-06-01 20:09:18 +00004183 state_type state() const {return __st_;}
Howard Hinnantd23b4642010-05-31 20:58:54 +00004184
4185protected:
Howard Hinnant4b53f502010-06-01 20:09:18 +00004186 virtual int_type underflow();
4187 virtual int_type pbackfail(int_type __c = traits_type::eof());
Howard Hinnantd23b4642010-05-31 20:58:54 +00004188 virtual int_type overflow (int_type __c = traits_type::eof());
Howard Hinnant4b53f502010-06-01 20:09:18 +00004189 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s,
4190 streamsize __n);
4191 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
4192 ios_base::openmode __wch = ios_base::in | ios_base::out);
4193 virtual pos_type seekpos(pos_type __sp,
4194 ios_base::openmode __wch = ios_base::in | ios_base::out);
4195 virtual int sync();
4196
4197private:
4198 bool __read_mode();
4199 void __write_mode();
4200 wbuffer_convert* __close();
Howard Hinnantd23b4642010-05-31 20:58:54 +00004201};
4202
4203template <class _Codecvt, class _Elem, class _Tr>
Howard Hinnant4b53f502010-06-01 20:09:18 +00004204wbuffer_convert<_Codecvt, _Elem, _Tr>::
4205 wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt, state_type __state)
4206 : __extbuf_(0),
4207 __extbufnext_(0),
4208 __extbufend_(0),
4209 __ebs_(0),
4210 __intbuf_(0),
4211 __ibs_(0),
4212 __bufptr_(__bytebuf),
4213 __cv_(__pcvt),
4214 __st_(__state),
4215 __cm_(0),
4216 __owns_eb_(false),
4217 __owns_ib_(false),
4218 __always_noconv_(__cv_ ? __cv_->always_noconv() : false)
4219{
4220 setbuf(0, 4096);
4221}
4222
4223template <class _Codecvt, class _Elem, class _Tr>
4224wbuffer_convert<_Codecvt, _Elem, _Tr>::~wbuffer_convert()
4225{
4226 __close();
4227 delete __cv_;
4228 if (__owns_eb_)
4229 delete [] __extbuf_;
4230 if (__owns_ib_)
4231 delete [] __intbuf_;
4232}
4233
4234template <class _Codecvt, class _Elem, class _Tr>
4235typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4236wbuffer_convert<_Codecvt, _Elem, _Tr>::underflow()
4237{
4238 if (__cv_ == 0 || __bufptr_ == 0)
4239 return traits_type::eof();
4240 bool __initial = __read_mode();
4241 char_type __1buf;
4242 if (this->gptr() == 0)
4243 this->setg(&__1buf, &__1buf+1, &__1buf+1);
4244 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
4245 int_type __c = traits_type::eof();
4246 if (this->gptr() == this->egptr())
4247 {
4248 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
4249 if (__always_noconv_)
4250 {
4251 streamsize __nmemb = static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz);
4252 __nmemb = __bufptr_->sgetn((char*)this->eback() + __unget_sz, __nmemb);
4253 if (__nmemb != 0)
4254 {
4255 this->setg(this->eback(),
4256 this->eback() + __unget_sz,
4257 this->eback() + __unget_sz + __nmemb);
4258 __c = *this->gptr();
4259 }
4260 }
4261 else
4262 {
4263 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
4264 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
4265 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004266 streamsize __nmemb = _VSTD::min(static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz),
Howard Hinnant4b53f502010-06-01 20:09:18 +00004267 static_cast<streamsize>(__extbufend_ - __extbufnext_));
4268 codecvt_base::result __r;
4269 state_type __svs = __st_;
4270 streamsize __nr = __bufptr_->sgetn(const_cast<char*>(__extbufnext_), __nmemb);
4271 if (__nr != 0)
4272 {
4273 __extbufend_ = __extbufnext_ + __nr;
4274 char_type* __inext;
4275 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
4276 this->eback() + __unget_sz,
4277 this->egptr(), __inext);
4278 if (__r == codecvt_base::noconv)
4279 {
4280 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)__extbufend_);
4281 __c = *this->gptr();
4282 }
4283 else if (__inext != this->eback() + __unget_sz)
4284 {
4285 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
4286 __c = *this->gptr();
4287 }
4288 }
4289 }
4290 }
4291 else
4292 __c = *this->gptr();
4293 if (this->eback() == &__1buf)
4294 this->setg(0, 0, 0);
4295 return __c;
4296}
4297
4298template <class _Codecvt, class _Elem, class _Tr>
4299typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4300wbuffer_convert<_Codecvt, _Elem, _Tr>::pbackfail(int_type __c)
4301{
4302 if (__cv_ != 0 && __bufptr_ != 0 && this->eback() < this->gptr())
4303 {
4304 if (traits_type::eq_int_type(__c, traits_type::eof()))
4305 {
4306 this->gbump(-1);
4307 return traits_type::not_eof(__c);
4308 }
4309 if (traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
4310 {
4311 this->gbump(-1);
4312 *this->gptr() = traits_type::to_char_type(__c);
4313 return __c;
4314 }
4315 }
4316 return traits_type::eof();
4317}
4318
4319template <class _Codecvt, class _Elem, class _Tr>
Howard Hinnantd23b4642010-05-31 20:58:54 +00004320typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4321wbuffer_convert<_Codecvt, _Elem, _Tr>::overflow(int_type __c)
4322{
Howard Hinnant4b53f502010-06-01 20:09:18 +00004323 if (__cv_ == 0 || __bufptr_ == 0)
4324 return traits_type::eof();
4325 __write_mode();
4326 char_type __1buf;
4327 char_type* __pb_save = this->pbase();
4328 char_type* __epb_save = this->epptr();
4329 if (!traits_type::eq_int_type(__c, traits_type::eof()))
4330 {
4331 if (this->pptr() == 0)
4332 this->setp(&__1buf, &__1buf+1);
4333 *this->pptr() = traits_type::to_char_type(__c);
4334 this->pbump(1);
4335 }
4336 if (this->pptr() != this->pbase())
4337 {
4338 if (__always_noconv_)
4339 {
4340 streamsize __nmemb = static_cast<streamsize>(this->pptr() - this->pbase());
4341 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4342 return traits_type::eof();
4343 }
4344 else
4345 {
4346 char* __extbe = __extbuf_;
4347 codecvt_base::result __r;
4348 do
4349 {
4350 const char_type* __e;
4351 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
4352 __extbuf_, __extbuf_ + __ebs_, __extbe);
4353 if (__e == this->pbase())
4354 return traits_type::eof();
4355 if (__r == codecvt_base::noconv)
4356 {
4357 streamsize __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
4358 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4359 return traits_type::eof();
4360 }
4361 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
4362 {
4363 streamsize __nmemb = static_cast<size_t>(__extbe - __extbuf_);
4364 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4365 return traits_type::eof();
4366 if (__r == codecvt_base::partial)
4367 {
4368 this->setp((char_type*)__e, this->pptr());
4369 this->pbump(this->epptr() - this->pbase());
4370 }
4371 }
4372 else
4373 return traits_type::eof();
4374 } while (__r == codecvt_base::partial);
4375 }
4376 this->setp(__pb_save, __epb_save);
4377 }
4378 return traits_type::not_eof(__c);
4379}
4380
4381template <class _Codecvt, class _Elem, class _Tr>
4382basic_streambuf<_Elem, _Tr>*
4383wbuffer_convert<_Codecvt, _Elem, _Tr>::setbuf(char_type* __s, streamsize __n)
4384{
4385 this->setg(0, 0, 0);
4386 this->setp(0, 0);
4387 if (__owns_eb_)
4388 delete [] __extbuf_;
4389 if (__owns_ib_)
4390 delete [] __intbuf_;
4391 __ebs_ = __n;
4392 if (__ebs_ > sizeof(__extbuf_min_))
4393 {
4394 if (__always_noconv_ && __s)
4395 {
4396 __extbuf_ = (char*)__s;
4397 __owns_eb_ = false;
4398 }
4399 else
4400 {
4401 __extbuf_ = new char[__ebs_];
4402 __owns_eb_ = true;
4403 }
4404 }
4405 else
4406 {
4407 __extbuf_ = __extbuf_min_;
4408 __ebs_ = sizeof(__extbuf_min_);
4409 __owns_eb_ = false;
4410 }
4411 if (!__always_noconv_)
4412 {
4413 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
4414 if (__s && __ibs_ >= sizeof(__extbuf_min_))
4415 {
4416 __intbuf_ = __s;
4417 __owns_ib_ = false;
4418 }
4419 else
4420 {
4421 __intbuf_ = new char_type[__ibs_];
4422 __owns_ib_ = true;
4423 }
4424 }
4425 else
4426 {
4427 __ibs_ = 0;
4428 __intbuf_ = 0;
4429 __owns_ib_ = false;
4430 }
4431 return this;
4432}
4433
4434template <class _Codecvt, class _Elem, class _Tr>
4435typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4436wbuffer_convert<_Codecvt, _Elem, _Tr>::seekoff(off_type __off, ios_base::seekdir __way,
4437 ios_base::openmode __om)
4438{
4439 int __width = __cv_->encoding();
4440 if (__cv_ == 0 || __bufptr_ == 0 || (__width <= 0 && __off != 0) || sync())
4441 return pos_type(off_type(-1));
4442 // __width > 0 || __off == 0
4443 switch (__way)
4444 {
4445 case ios_base::beg:
4446 break;
4447 case ios_base::cur:
4448 break;
4449 case ios_base::end:
4450 break;
4451 default:
4452 return pos_type(off_type(-1));
4453 }
4454 pos_type __r = __bufptr_->pubseekoff(__width * __off, __way, __om);
4455 __r.state(__st_);
4456 return __r;
4457}
4458
4459template <class _Codecvt, class _Elem, class _Tr>
4460typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4461wbuffer_convert<_Codecvt, _Elem, _Tr>::seekpos(pos_type __sp, ios_base::openmode __wch)
4462{
4463 if (__cv_ == 0 || __bufptr_ == 0 || sync())
4464 return pos_type(off_type(-1));
4465 if (__bufptr_->pubseekpos(__sp, __wch) == pos_type(off_type(-1)))
4466 return pos_type(off_type(-1));
4467 return __sp;
4468}
4469
4470template <class _Codecvt, class _Elem, class _Tr>
4471int
4472wbuffer_convert<_Codecvt, _Elem, _Tr>::sync()
4473{
4474 if (__cv_ == 0 || __bufptr_ == 0)
4475 return 0;
4476 if (__cm_ & ios_base::out)
4477 {
4478 if (this->pptr() != this->pbase())
4479 if (overflow() == traits_type::eof())
4480 return -1;
4481 codecvt_base::result __r;
4482 do
4483 {
4484 char* __extbe;
4485 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
4486 streamsize __nmemb = static_cast<streamsize>(__extbe - __extbuf_);
4487 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4488 return -1;
4489 } while (__r == codecvt_base::partial);
4490 if (__r == codecvt_base::error)
4491 return -1;
4492 if (__bufptr_->pubsync())
4493 return -1;
4494 }
4495 else if (__cm_ & ios_base::in)
4496 {
4497 off_type __c;
4498 if (__always_noconv_)
4499 __c = this->egptr() - this->gptr();
4500 else
4501 {
4502 int __width = __cv_->encoding();
4503 __c = __extbufend_ - __extbufnext_;
4504 if (__width > 0)
4505 __c += __width * (this->egptr() - this->gptr());
4506 else
4507 {
4508 if (this->gptr() != this->egptr())
4509 {
4510 reverse(this->gptr(), this->egptr());
4511 codecvt_base::result __r;
4512 const char_type* __e = this->gptr();
4513 char* __extbe;
4514 do
4515 {
4516 __r = __cv_->out(__st_, __e, this->egptr(), __e,
4517 __extbuf_, __extbuf_ + __ebs_, __extbe);
4518 switch (__r)
4519 {
4520 case codecvt_base::noconv:
4521 __c += this->egptr() - this->gptr();
4522 break;
4523 case codecvt_base::ok:
4524 case codecvt_base::partial:
4525 __c += __extbe - __extbuf_;
4526 break;
4527 default:
4528 return -1;
4529 }
4530 } while (__r == codecvt_base::partial);
4531 }
4532 }
4533 }
4534 if (__bufptr_->pubseekoff(-__c, ios_base::cur, __cm_) == pos_type(off_type(-1)))
4535 return -1;
4536 this->setg(0, 0, 0);
4537 __cm_ = 0;
4538 }
4539 return 0;
4540}
4541
4542template <class _Codecvt, class _Elem, class _Tr>
4543bool
4544wbuffer_convert<_Codecvt, _Elem, _Tr>::__read_mode()
4545{
4546 if (!(__cm_ & ios_base::in))
4547 {
4548 this->setp(0, 0);
4549 if (__always_noconv_)
4550 this->setg((char_type*)__extbuf_,
4551 (char_type*)__extbuf_ + __ebs_,
4552 (char_type*)__extbuf_ + __ebs_);
4553 else
4554 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
4555 __cm_ = ios_base::in;
4556 return true;
4557 }
4558 return false;
4559}
4560
4561template <class _Codecvt, class _Elem, class _Tr>
4562void
4563wbuffer_convert<_Codecvt, _Elem, _Tr>::__write_mode()
4564{
4565 if (!(__cm_ & ios_base::out))
4566 {
4567 this->setg(0, 0, 0);
4568 if (__ebs_ > sizeof(__extbuf_min_))
4569 {
4570 if (__always_noconv_)
4571 this->setp((char_type*)__extbuf_,
4572 (char_type*)__extbuf_ + (__ebs_ - 1));
4573 else
4574 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
4575 }
4576 else
4577 this->setp(0, 0);
4578 __cm_ = ios_base::out;
4579 }
4580}
4581
4582template <class _Codecvt, class _Elem, class _Tr>
4583wbuffer_convert<_Codecvt, _Elem, _Tr>*
4584wbuffer_convert<_Codecvt, _Elem, _Tr>::__close()
4585{
4586 wbuffer_convert* __rt = 0;
4587 if (__cv_ != 0 && __bufptr_ != 0)
4588 {
4589 __rt = this;
4590 if ((__cm_ & ios_base::out) && sync())
4591 __rt = 0;
4592 }
4593 return __rt;
Howard Hinnantd23b4642010-05-31 20:58:54 +00004594}
4595
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004596_LIBCPP_END_NAMESPACE_STD
4597
4598#endif // _LIBCPP_LOCALE