blob: 768bb39b13d9bf3d2fa6a91daab18f850984c13b [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- locale ------------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_LOCALE
12#define _LIBCPP_LOCALE
13
14/*
15 locale synopsis
16
17namespace std
18{
19
20class locale
21{
22public:
23 // types:
24 class facet;
25 class id;
26
27 typedef int category;
28 static const category // values assigned here are for exposition only
29 none = 0x000,
30 collate = 0x010,
31 ctype = 0x020,
32 monetary = 0x040,
33 numeric = 0x080,
34 time = 0x100,
35 messages = 0x200,
36 all = collate | ctype | monetary | numeric | time | messages;
37
38 // construct/copy/destroy:
Howard Hinnant7c9e5732011-05-31 15:34:58 +000039 locale() noexcept;
40 locale(const locale& other) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000041 explicit locale(const char* std_name);
42 explicit locale(const string& std_name);
43 locale(const locale& other, const char* std_name, category);
44 locale(const locale& other, const string& std_name, category);
45 template <class Facet> locale(const locale& other, Facet* f);
46 locale(const locale& other, const locale& one, category);
47
Howard Hinnant7c9e5732011-05-31 15:34:58 +000048 ~locale(); // not virtual
Howard Hinnantc51e1022010-05-11 19:42:16 +000049
Howard Hinnant7c9e5732011-05-31 15:34:58 +000050 const locale& operator=(const locale& other) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000051
52 template <class Facet> locale combine(const locale& other) const;
53
54 // locale operations:
55 basic_string<char> name() const;
56 bool operator==(const locale& other) const;
57 bool operator!=(const locale& other) const;
58 template <class charT, class Traits, class Allocator>
59 bool operator()(const basic_string<charT,Traits,Allocator>& s1,
60 const basic_string<charT,Traits,Allocator>& s2) const;
61
62 // global locale objects:
63 static locale global(const locale&);
64 static const locale& classic();
65};
66
67template <class Facet> const Facet& use_facet(const locale&);
Howard Hinnant7c9e5732011-05-31 15:34:58 +000068template <class Facet> bool has_facet(const locale&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000069
70// 22.3.3, convenience interfaces:
71template <class charT> bool isspace (charT c, const locale& loc);
72template <class charT> bool isprint (charT c, const locale& loc);
73template <class charT> bool iscntrl (charT c, const locale& loc);
74template <class charT> bool isupper (charT c, const locale& loc);
75template <class charT> bool islower (charT c, const locale& loc);
76template <class charT> bool isalpha (charT c, const locale& loc);
77template <class charT> bool isdigit (charT c, const locale& loc);
78template <class charT> bool ispunct (charT c, const locale& loc);
79template <class charT> bool isxdigit(charT c, const locale& loc);
80template <class charT> bool isalnum (charT c, const locale& loc);
81template <class charT> bool isgraph (charT c, const locale& loc);
82template <class charT> charT toupper(charT c, const locale& loc);
83template <class charT> charT tolower(charT c, const locale& loc);
Howard Hinnant9dd7e892010-05-31 20:58:54 +000084
85template<class Codecvt, class Elem = wchar_t,
86 class Wide_alloc = allocator<Elem>,
87 class Byte_alloc = allocator<char>>
88class wstring_convert
89{
90public:
91 typedef basic_string<char, char_traits<char>, Byte_alloc> byte_string;
92 typedef basic_string<Elem, char_traits<Elem>, Wide_alloc> wide_string;
93 typedef typename Codecvt::state_type state_type;
94 typedef typename wide_string::traits_type::int_type int_type;
95
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 Hinnantc51e1022010-05-11 19:42:16 +0000116template <class Codecvt, class Elem = wchar_t, class Tr = char_traits<Elem>>
Howard Hinnant9dd7e892010-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 Hinnantc51e1022010-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 Clowdde4bfe2013-03-18 17:45:34 +0000184#ifndef __APPLE__
Howard Hinnant155c2af2010-05-24 17:49:41 +0000185#include <cstdarg>
186#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187#include <cstdlib>
188#include <ctime>
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000189#ifdef _LIBCPP_MSVCRT
Howard Hinnantae0f80b2011-09-29 20:33:10 +0000190#include <support/win32/locale_win32.h>
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000191#else // _LIBCPP_MSVCRT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000192#include <nl_types.h>
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000193#endif // !_LIBCPP_MSVCRT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194
Marshall Clowdde4bfe2013-03-18 17:45:34 +0000195#ifdef __APPLE__
Howard Hinnant48fd5d52012-11-14 21:17:15 +0000196#include <Availability.h>
197#endif
198
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000199#include <__undef_min_max>
200
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000201#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000202#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000203#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000204
205_LIBCPP_BEGIN_NAMESPACE_STD
206
Marshall Clow82378c02013-03-18 19:34:07 +0000207#if defined(__APPLE__) || defined(__FreeBSD__)
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000208# define _LIBCPP_GET_C_LOCALE 0
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000209#elif defined(__NetBSD__)
210# define _LIBCPP_GET_C_LOCALE LC_C_LOCALE
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000211#else
212# define _LIBCPP_GET_C_LOCALE __cloc()
Howard Hinnantf312e3e2011-09-28 23:39:33 +0000213 // Get the C locale object
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000214 _LIBCPP_FUNC_VIS locale_t __cloc();
Howard Hinnantf312e3e2011-09-28 23:39:33 +0000215#define __cloc_defined
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000216#endif
217
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000218typedef _VSTD::remove_pointer<locale_t>::type __locale_struct;
219typedef _VSTD::unique_ptr<__locale_struct, decltype(&freelocale)> __locale_unique_ptr;
David Chisnall4d958ed2012-02-29 13:00:07 +0000220#ifndef _LIBCPP_LOCALE__L_EXTENSIONS
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000221typedef _VSTD::unique_ptr<__locale_struct, decltype(&uselocale)> __locale_raii;
David Chisnall4d958ed2012-02-29 13:00:07 +0000222#endif
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000223
Howard Hinnant155c2af2010-05-24 17:49:41 +0000224// OSX has nice foo_l() functions that let you turn off use of the global
225// locale. Linux, not so much. The following functions avoid the locale when
226// that's possible and otherwise do the wrong thing. FIXME.
Howard Hinnantea382952013-08-14 18:00:20 +0000227#if defined(__linux__) || defined(EMSCRIPTEN) || defined(_AIX)
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000228
229#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
230decltype(MB_CUR_MAX_L(_VSTD::declval<locale_t>()))
Howard Hinnant756c69b2010-09-22 16:48:34 +0000231inline _LIBCPP_INLINE_VISIBILITY
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000232__mb_cur_max_l(locale_t __l)
Howard Hinnant155c2af2010-05-24 17:49:41 +0000233{
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000234 return MB_CUR_MAX_L(__l);
235}
236#else // _LIBCPP_LOCALE__L_EXTENSIONS
237_LIBCPP_ALWAYS_INLINE inline
238decltype(MB_CUR_MAX) __mb_cur_max_l(locale_t __l)
239{
240 __locale_raii __current(uselocale(__l), uselocale);
241 return MB_CUR_MAX;
242}
243#endif // _LIBCPP_LOCALE__L_EXTENSIONS
244
245_LIBCPP_ALWAYS_INLINE inline
246wint_t __btowc_l(int __c, locale_t __l)
247{
Howard Hinnantf312e3e2011-09-28 23:39:33 +0000248#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000249 return btowc_l(__c, __l);
250#else
251 __locale_raii __current(uselocale(__l), uselocale);
252 return btowc(__c);
253#endif
Alexis Huntd4a24912011-07-13 06:40:50 +0000254}
Howard Hinnant2de5c472011-07-13 15:48:16 +0000255
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000256_LIBCPP_ALWAYS_INLINE inline
257int __wctob_l(wint_t __c, locale_t __l)
Alexis Huntd4a24912011-07-13 06:40:50 +0000258{
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000259#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
260 return wctob_l(__c, __l);
261#else
262 __locale_raii __current(uselocale(__l), uselocale);
263 return wctob(__c);
264#endif
Alexis Huntd4a24912011-07-13 06:40:50 +0000265}
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000266
267_LIBCPP_ALWAYS_INLINE inline
268size_t __wcsnrtombs_l(char *__dest, const wchar_t **__src, size_t __nwc,
269 size_t __len, mbstate_t *__ps, locale_t __l)
270{
271#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
272 return wcsnrtombs_l(__dest, __src, __nwc, __len, __ps, __l);
273#else
274 __locale_raii __current(uselocale(__l), uselocale);
275 return wcsnrtombs(__dest, __src, __nwc, __len, __ps);
276#endif
277}
278
279_LIBCPP_ALWAYS_INLINE inline
280size_t __wcrtomb_l(char *__s, wchar_t __wc, mbstate_t *__ps, locale_t __l)
281{
282#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
283 return wcrtomb_l(__s, __wc, __ps, __l);
284#else
285 __locale_raii __current(uselocale(__l), uselocale);
286 return wcrtomb(__s, __wc, __ps);
287#endif
288}
289
290_LIBCPP_ALWAYS_INLINE inline
291size_t __mbsnrtowcs_l(wchar_t * __dest, const char **__src, size_t __nms,
292 size_t __len, mbstate_t *__ps, locale_t __l)
293{
294#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
David Chisnall1d581062011-09-21 08:39:44 +0000295 return mbsnrtowcs_l(__dest, __src, __nms, __len, __ps, __l);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000296#else
297 __locale_raii __current(uselocale(__l), uselocale);
298 return mbsnrtowcs(__dest, __src, __nms, __len, __ps);
299#endif
300}
301
302_LIBCPP_ALWAYS_INLINE inline
303size_t __mbrtowc_l(wchar_t *__pwc, const char *__s, size_t __n,
304 mbstate_t *__ps, locale_t __l)
305{
306#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
307 return mbrtowc_l(__pwc, __s, __n, __ps, __l);
308#else
309 __locale_raii __current(uselocale(__l), uselocale);
310 return mbrtowc(__pwc, __s, __n, __ps);
311#endif
312}
313
314_LIBCPP_ALWAYS_INLINE inline
315int __mbtowc_l(wchar_t *__pwc, const char *__pmb, size_t __max, locale_t __l)
316{
317#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
David Chisnall1d581062011-09-21 08:39:44 +0000318 return mbtowc_l(__pwc, __pmb, __max, __l);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000319#else
320 __locale_raii __current(uselocale(__l), uselocale);
321 return mbtowc(__pwc, __pmb, __max);
322#endif
323}
324
325_LIBCPP_ALWAYS_INLINE inline
326size_t __mbrlen_l(const char *__s, size_t __n, mbstate_t *__ps, locale_t __l)
327{
328#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
329 return mbrlen_l(__s, __n, __ps, __l);
330#else
331 __locale_raii __current(uselocale(__l), uselocale);
332 return mbrlen(__s, __n, __ps);
333#endif
334}
335
336_LIBCPP_ALWAYS_INLINE inline
337lconv *__localeconv_l(locale_t __l)
338{
339#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
340 return localeconv_l(__l);
341#else
342 __locale_raii __current(uselocale(__l), uselocale);
343 return localeconv();
344#endif
345}
346
347_LIBCPP_ALWAYS_INLINE inline
348size_t __mbsrtowcs_l(wchar_t *__dest, const char **__src, size_t __len,
349 mbstate_t *__ps, locale_t __l)
350{
351#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
352 return mbsrtowcs_l(__dest, __src, __len, __ps, __l);
353#else
354 __locale_raii __current(uselocale(__l), uselocale);
355 return mbsrtowcs(__dest, __src, __len, __ps);
356#endif
357}
358
Chandler Carruth952bdc82012-12-31 06:09:54 +0000359inline
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000360int __snprintf_l(char *__s, size_t __n, locale_t __l, const char *__format, ...) {
361 va_list __va;
362 va_start(__va, __format);
363#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
364 int __res = vsnprintf_l(__s, __n, __l, __format, __va);
365#else
366 __locale_raii __current(uselocale(__l), uselocale);
367 int __res = vsnprintf(__s, __n, __format, __va);
368#endif
369 va_end(__va);
370 return __res;
371}
372
Chandler Carruth952bdc82012-12-31 06:09:54 +0000373inline
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000374int __asprintf_l(char **__s, locale_t __l, const char *__format, ...) {
375 va_list __va;
376 va_start(__va, __format);
377#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
378 int __res = vasprintf_l(__s, __l, __format, __va);
379#else
380 __locale_raii __current(uselocale(__l), uselocale);
381 int __res = vasprintf(__s, __format, __va);
382#endif
383 va_end(__va);
384 return __res;
385}
386
Chandler Carruth952bdc82012-12-31 06:09:54 +0000387inline
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000388int __sscanf_l(const char *__s, locale_t __l, const char *__format, ...) {
389 va_list __va;
390 va_start(__va, __format);
391#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
392 int __res = vsscanf_l(__s, __l, __format, __va);
393#else
394 __locale_raii __current(uselocale(__l), uselocale);
395 int __res = vsscanf(__s, __format, __va);
396#endif
397 va_end(__va);
398 return __res;
399}
400
Howard Hinnantf312e3e2011-09-28 23:39:33 +0000401#endif // __linux__
Howard Hinnant155c2af2010-05-24 17:49:41 +0000402
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403// __scan_keyword
404// Scans [__b, __e) until a match is found in the basic_strings range
405// [__kb, __ke) or until it can be shown that there is no match in [__kb, __ke).
406// __b will be incremented (visibly), consuming CharT until a match is found
407// or proved to not exist. A keyword may be "", in which will match anything.
408// If one keyword is a prefix of another, and the next CharT in the input
409// might match another keyword, the algorithm will attempt to find the longest
410// matching keyword. If the longer matching keyword ends up not matching, then
411// no keyword match is found. If no keyword match is found, __ke is returned
412// and failbit is set in __err.
413// Else an iterator pointing to the matching keyword is found. If more than
414// one keyword matches, an iterator to the first matching keyword is returned.
415// If on exit __b == __e, eofbit is set in __err. If __case_senstive is false,
416// __ct is used to force to lower case before comparing characters.
417// Examples:
418// Keywords: "a", "abb"
419// If the input is "a", the first keyword matches and eofbit is set.
420// If the input is "abc", no match is found and "ab" are consumed.
421template <class _InputIterator, class _ForwardIterator, class _Ctype>
422_LIBCPP_HIDDEN
423_ForwardIterator
424__scan_keyword(_InputIterator& __b, _InputIterator __e,
425 _ForwardIterator __kb, _ForwardIterator __ke,
426 const _Ctype& __ct, ios_base::iostate& __err,
427 bool __case_sensitive = true)
428{
429 typedef typename iterator_traits<_InputIterator>::value_type _CharT;
Howard Hinnant28b24882011-12-01 20:21:04 +0000430 size_t __nkw = static_cast<size_t>(_VSTD::distance(__kb, __ke));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000431 const unsigned char __doesnt_match = '\0';
432 const unsigned char __might_match = '\1';
433 const unsigned char __does_match = '\2';
434 unsigned char __statbuf[100];
435 unsigned char* __status = __statbuf;
436 unique_ptr<unsigned char, void(*)(void*)> __stat_hold(0, free);
437 if (__nkw > sizeof(__statbuf))
438 {
439 __status = (unsigned char*)malloc(__nkw);
440 if (__status == 0)
441 __throw_bad_alloc();
442 __stat_hold.reset(__status);
443 }
444 size_t __n_might_match = __nkw; // At this point, any keyword might match
445 size_t __n_does_match = 0; // but none of them definitely do
446 // Initialize all statuses to __might_match, except for "" keywords are __does_match
447 unsigned char* __st = __status;
448 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, ++__st)
449 {
450 if (!__ky->empty())
451 *__st = __might_match;
452 else
453 {
454 *__st = __does_match;
455 --__n_might_match;
456 ++__n_does_match;
457 }
458 }
459 // While there might be a match, test keywords against the next CharT
460 for (size_t __indx = 0; __b != __e && __n_might_match > 0; ++__indx)
461 {
462 // Peek at the next CharT but don't consume it
463 _CharT __c = *__b;
464 if (!__case_sensitive)
465 __c = __ct.toupper(__c);
466 bool __consume = false;
467 // For each keyword which might match, see if the __indx character is __c
468 // If a match if found, consume __c
469 // If a match is found, and that is the last character in the keyword,
470 // then that keyword matches.
471 // If the keyword doesn't match this character, then change the keyword
472 // to doesn't match
473 __st = __status;
474 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, ++__st)
475 {
476 if (*__st == __might_match)
477 {
478 _CharT __kc = (*__ky)[__indx];
479 if (!__case_sensitive)
480 __kc = __ct.toupper(__kc);
481 if (__c == __kc)
482 {
483 __consume = true;
484 if (__ky->size() == __indx+1)
485 {
486 *__st = __does_match;
487 --__n_might_match;
488 ++__n_does_match;
489 }
490 }
491 else
492 {
493 *__st = __doesnt_match;
494 --__n_might_match;
495 }
496 }
497 }
498 // consume if we matched a character
499 if (__consume)
500 {
501 ++__b;
502 // If we consumed a character and there might be a matched keyword that
503 // was marked matched on a previous iteration, then such keywords
504 // which are now marked as not matching.
505 if (__n_might_match + __n_does_match > 1)
506 {
507 __st = __status;
508 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, ++__st)
509 {
510 if (*__st == __does_match && __ky->size() != __indx+1)
511 {
512 *__st = __doesnt_match;
513 --__n_does_match;
514 }
515 }
516 }
517 }
518 }
519 // We've exited the loop because we hit eof and/or we have no more "might matches".
520 if (__b == __e)
521 __err |= ios_base::eofbit;
522 // Return the first matching result
523 for (__st = __status; __kb != __ke; ++__kb, ++__st)
524 if (*__st == __does_match)
525 break;
526 if (__kb == __ke)
527 __err |= ios_base::failbit;
528 return __kb;
529}
530
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000531struct _LIBCPP_TYPE_VIS __num_get_base
Howard Hinnantc51e1022010-05-11 19:42:16 +0000532{
533 static const int __num_get_buf_sz = 40;
534
535 static int __get_base(ios_base&);
536 static const char __src[33];
537};
538
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000539_LIBCPP_FUNC_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000540void __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end,
541 ios_base::iostate& __err);
542
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543template <class _CharT>
544struct __num_get
545 : protected __num_get_base
546{
547 static string __stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep);
548 static string __stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
549 _CharT& __thousands_sep);
550 static int __stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
551 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
552 unsigned* __g, unsigned*& __g_end, _CharT* __atoms);
553 static int __stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp,
554 char* __a, char*& __a_end,
555 _CharT __decimal_point, _CharT __thousands_sep,
556 const string& __grouping, unsigned* __g,
557 unsigned*& __g_end, unsigned& __dc, _CharT* __atoms);
558};
559
560template <class _CharT>
561string
562__num_get<_CharT>::__stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep)
563{
564 locale __loc = __iob.getloc();
565 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 26, __atoms);
566 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
567 __thousands_sep = __np.thousands_sep();
568 return __np.grouping();
569}
570
571template <class _CharT>
572string
573__num_get<_CharT>::__stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
574 _CharT& __thousands_sep)
575{
576 locale __loc = __iob.getloc();
577 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 32, __atoms);
578 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
579 __decimal_point = __np.decimal_point();
580 __thousands_sep = __np.thousands_sep();
581 return __np.grouping();
582}
583
584template <class _CharT>
585int
586__num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
587 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
588 unsigned* __g, unsigned*& __g_end, _CharT* __atoms)
589{
Howard Hinnantf57b3fc2011-03-09 01:03:19 +0000590 if (__a_end == __a && (__ct == __atoms[24] || __ct == __atoms[25]))
591 {
592 *__a_end++ = __ct == __atoms[24] ? '+' : '-';
593 __dc = 0;
594 return 0;
595 }
Howard Hinnant28b24882011-12-01 20:21:04 +0000596 if (__grouping.size() != 0 && __ct == __thousands_sep)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000597 {
598 if (__g_end-__g < __num_get_buf_sz)
599 {
600 *__g_end++ = __dc;
601 __dc = 0;
602 }
603 return 0;
604 }
605 ptrdiff_t __f = find(__atoms, __atoms + 26, __ct) - __atoms;
Howard Hinnantf57b3fc2011-03-09 01:03:19 +0000606 if (__f >= 24)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000607 return -1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000608 switch (__base)
609 {
610 case 8:
611 case 10:
612 if (__f >= __base)
Howard Hinnantf57b3fc2011-03-09 01:03:19 +0000613 return -1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000614 break;
Howard Hinnantf57b3fc2011-03-09 01:03:19 +0000615 case 16:
616 if (__f < 22)
617 break;
618 if (__a_end != __a && __a_end - __a <= 2 && __a_end[-1] == '0')
619 {
620 __dc = 0;
621 *__a_end++ = __src[__f];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000622 return 0;
Howard Hinnantf57b3fc2011-03-09 01:03:19 +0000623 }
624 return -1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000625 }
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000626 *__a_end++ = __src[__f];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000627 ++__dc;
628 return 0;
629}
630
631template <class _CharT>
632int
633__num_get<_CharT>::__stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp, char* __a, char*& __a_end,
634 _CharT __decimal_point, _CharT __thousands_sep, const string& __grouping,
635 unsigned* __g, unsigned*& __g_end, unsigned& __dc, _CharT* __atoms)
636{
637 if (__ct == __decimal_point)
638 {
639 if (!__in_units)
640 return -1;
641 __in_units = false;
642 *__a_end++ = '.';
643 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
644 *__g_end++ = __dc;
645 return 0;
646 }
647 if (__ct == __thousands_sep && __grouping.size() != 0)
648 {
649 if (!__in_units)
650 return -1;
651 if (__g_end-__g < __num_get_buf_sz)
652 {
653 *__g_end++ = __dc;
654 __dc = 0;
655 }
656 return 0;
657 }
658 ptrdiff_t __f = find(__atoms, __atoms + 32, __ct) - __atoms;
659 if (__f >= 32)
660 return -1;
661 char __x = __src[__f];
Howard Hinnant5132e192012-02-15 19:19:37 +0000662 if (__x == '-' || __x == '+')
663 {
Howard Hinnant21413152013-03-08 19:06:24 +0000664 if (__a_end == __a || (__a_end[-1] & 0x5F) == (__exp & 0x7F))
Howard Hinnant5132e192012-02-15 19:19:37 +0000665 {
666 *__a_end++ = __x;
667 return 0;
668 }
669 return -1;
670 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000671 if (__x == 'x' || __x == 'X')
672 __exp = 'P';
Howard Hinnant21413152013-03-08 19:06:24 +0000673 else if ((__x & 0x5F) == __exp)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000674 {
Howard Hinnant21413152013-03-08 19:06:24 +0000675 __exp |= 0x80;
676 if (__in_units)
677 {
678 __in_units = false;
679 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
680 *__g_end++ = __dc;
681 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000682 }
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000683 *__a_end++ = __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000684 if (__f >= 22)
685 return 0;
686 ++__dc;
687 return 0;
688}
689
Howard Hinnant8ea98242013-08-23 17:37:05 +0000690_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_TYPE_VIS __num_get<char>)
691_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_TYPE_VIS __num_get<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000692
693template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000694class _LIBCPP_TYPE_VIS_ONLY num_get
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695 : public locale::facet,
696 private __num_get<_CharT>
697{
698public:
699 typedef _CharT char_type;
700 typedef _InputIterator iter_type;
701
702 _LIBCPP_ALWAYS_INLINE
703 explicit num_get(size_t __refs = 0)
704 : locale::facet(__refs) {}
705
706 _LIBCPP_ALWAYS_INLINE
707 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
708 ios_base::iostate& __err, bool& __v) const
709 {
710 return do_get(__b, __e, __iob, __err, __v);
711 }
712
713 _LIBCPP_ALWAYS_INLINE
714 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
715 ios_base::iostate& __err, long& __v) const
716 {
717 return do_get(__b, __e, __iob, __err, __v);
718 }
719
720 _LIBCPP_ALWAYS_INLINE
721 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
722 ios_base::iostate& __err, long long& __v) const
723 {
724 return do_get(__b, __e, __iob, __err, __v);
725 }
726
727 _LIBCPP_ALWAYS_INLINE
728 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
729 ios_base::iostate& __err, unsigned short& __v) const
730 {
731 return do_get(__b, __e, __iob, __err, __v);
732 }
733
734 _LIBCPP_ALWAYS_INLINE
735 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
736 ios_base::iostate& __err, unsigned int& __v) const
737 {
738 return do_get(__b, __e, __iob, __err, __v);
739 }
740
741 _LIBCPP_ALWAYS_INLINE
742 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
743 ios_base::iostate& __err, unsigned long& __v) const
744 {
745 return do_get(__b, __e, __iob, __err, __v);
746 }
747
748 _LIBCPP_ALWAYS_INLINE
749 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
750 ios_base::iostate& __err, unsigned long long& __v) const
751 {
752 return do_get(__b, __e, __iob, __err, __v);
753 }
754
755 _LIBCPP_ALWAYS_INLINE
756 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
757 ios_base::iostate& __err, float& __v) const
758 {
759 return do_get(__b, __e, __iob, __err, __v);
760 }
761
762 _LIBCPP_ALWAYS_INLINE
763 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
764 ios_base::iostate& __err, double& __v) const
765 {
766 return do_get(__b, __e, __iob, __err, __v);
767 }
768
769 _LIBCPP_ALWAYS_INLINE
770 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
771 ios_base::iostate& __err, long double& __v) const
772 {
773 return do_get(__b, __e, __iob, __err, __v);
774 }
775
776 _LIBCPP_ALWAYS_INLINE
777 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
778 ios_base::iostate& __err, void*& __v) const
779 {
780 return do_get(__b, __e, __iob, __err, __v);
781 }
782
783 static locale::id id;
784
785protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000786 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +0000787 ~num_get() {}
788
789 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
790 ios_base::iostate& __err, bool& __v) const;
791 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
792 ios_base::iostate& __err, long& __v) const;
793 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
794 ios_base::iostate& __err, long long& __v) const;
795 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
796 ios_base::iostate& __err, unsigned short& __v) const;
797 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
798 ios_base::iostate& __err, unsigned int& __v) const;
799 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
800 ios_base::iostate& __err, unsigned long& __v) const;
801 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
802 ios_base::iostate& __err, unsigned long long& __v) const;
803 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
804 ios_base::iostate& __err, float& __v) const;
805 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
806 ios_base::iostate& __err, double& __v) const;
807 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
808 ios_base::iostate& __err, long double& __v) const;
809 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
810 ios_base::iostate& __err, void*& __v) const;
811};
812
813template <class _CharT, class _InputIterator>
814locale::id
815num_get<_CharT, _InputIterator>::id;
816
817template <class _Tp>
818_Tp
819__num_get_signed_integral(const char* __a, const char* __a_end,
820 ios_base::iostate& __err, int __base)
821{
822 if (__a != __a_end)
823 {
Howard Hinnantca8923c2013-01-22 17:26:08 +0000824 typename remove_reference<decltype(errno)>::type __save_errno = errno;
Howard Hinnant05c71342011-02-25 19:52:41 +0000825 errno = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000826 char *__p2;
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000827 long long __ll = strtoll_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
Howard Hinnantca8923c2013-01-22 17:26:08 +0000828 typename remove_reference<decltype(errno)>::type __current_errno = errno;
Howard Hinnant05c71342011-02-25 19:52:41 +0000829 if (__current_errno == 0)
830 errno = __save_errno;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000831 if (__p2 != __a_end)
832 {
833 __err = ios_base::failbit;
834 return 0;
835 }
Howard Hinnant05c71342011-02-25 19:52:41 +0000836 else if (__current_errno == ERANGE ||
837 __ll < numeric_limits<_Tp>::min() ||
838 numeric_limits<_Tp>::max() < __ll)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000839 {
840 __err = ios_base::failbit;
Howard Hinnant05c71342011-02-25 19:52:41 +0000841 if (__ll > 0)
842 return numeric_limits<_Tp>::max();
843 else
844 return numeric_limits<_Tp>::min();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000845 }
846 return static_cast<_Tp>(__ll);
847 }
848 __err = ios_base::failbit;
849 return 0;
850}
851
852template <class _Tp>
853_Tp
854__num_get_unsigned_integral(const char* __a, const char* __a_end,
855 ios_base::iostate& __err, int __base)
856{
857 if (__a != __a_end)
858 {
Howard Hinnant05c71342011-02-25 19:52:41 +0000859 if (*__a == '-')
860 {
861 __err = ios_base::failbit;
862 return 0;
863 }
Howard Hinnantca8923c2013-01-22 17:26:08 +0000864 typename remove_reference<decltype(errno)>::type __save_errno = errno;
Howard Hinnant05c71342011-02-25 19:52:41 +0000865 errno = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000866 char *__p2;
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000867 unsigned long long __ll = strtoull_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
Howard Hinnantca8923c2013-01-22 17:26:08 +0000868 typename remove_reference<decltype(errno)>::type __current_errno = errno;
Howard Hinnant05c71342011-02-25 19:52:41 +0000869 if (__current_errno == 0)
870 errno = __save_errno;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000871 if (__p2 != __a_end)
872 {
873 __err = ios_base::failbit;
874 return 0;
875 }
Howard Hinnant05c71342011-02-25 19:52:41 +0000876 else if (__current_errno == ERANGE ||
877 numeric_limits<_Tp>::max() < __ll)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000878 {
879 __err = ios_base::failbit;
880 return numeric_limits<_Tp>::max();
881 }
882 return static_cast<_Tp>(__ll);
883 }
884 __err = ios_base::failbit;
885 return 0;
886}
887
888template <class _Tp>
889_Tp
890__num_get_float(const char* __a, const char* __a_end, ios_base::iostate& __err)
891{
892 if (__a != __a_end)
893 {
Howard Hinnantc9567812013-04-13 18:19:25 +0000894 typename remove_reference<decltype(errno)>::type __save_errno = errno;
895 errno = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000896 char *__p2;
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000897 long double __ld = strtold_l(__a, &__p2, _LIBCPP_GET_C_LOCALE);
Howard Hinnantc9567812013-04-13 18:19:25 +0000898 typename remove_reference<decltype(errno)>::type __current_errno = errno;
899 if (__current_errno == 0)
900 errno = __save_errno;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000901 if (__p2 != __a_end)
902 {
903 __err = ios_base::failbit;
904 return 0;
905 }
Howard Hinnantc9567812013-04-13 18:19:25 +0000906 else if (__current_errno == ERANGE)
907 __err = ios_base::failbit;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000908 return static_cast<_Tp>(__ld);
909 }
910 __err = ios_base::failbit;
911 return 0;
912}
913
914template <class _CharT, class _InputIterator>
915_InputIterator
916num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
917 ios_base& __iob,
918 ios_base::iostate& __err,
919 bool& __v) const
920{
921 if ((__iob.flags() & ios_base::boolalpha) == 0)
922 {
923 long __lv = -1;
924 __b = do_get(__b, __e, __iob, __err, __lv);
925 switch (__lv)
926 {
927 case 0:
928 __v = false;
929 break;
930 case 1:
931 __v = true;
932 break;
933 default:
934 __v = true;
935 __err = ios_base::failbit;
936 break;
937 }
938 return __b;
939 }
940 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__iob.getloc());
941 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__iob.getloc());
942 typedef typename numpunct<_CharT>::string_type string_type;
943 const string_type __names[2] = {__np.truename(), __np.falsename()};
944 const string_type* __i = __scan_keyword(__b, __e, __names, __names+2,
945 __ct, __err);
946 __v = __i == __names;
947 return __b;
948}
949
950template <class _CharT, class _InputIterator>
951_InputIterator
952num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
953 ios_base& __iob,
954 ios_base::iostate& __err,
955 long& __v) const
956{
957 // Stage 1
958 int __base = this->__get_base(__iob);
959 // Stage 2
960 char_type __atoms[26];
961 char_type __thousands_sep;
962 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000963 string __buf;
964 __buf.resize(__buf.capacity());
965 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000966 char* __a_end = __a;
967 unsigned __g[__num_get_base::__num_get_buf_sz];
968 unsigned* __g_end = __g;
969 unsigned __dc = 0;
970 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000971 {
972 if (__a_end - __a == __buf.size())
973 {
974 size_t __tmp = __buf.size();
975 __buf.resize(2*__buf.size());
976 __buf.resize(__buf.capacity());
977 __a = &__buf[0];
978 __a_end = __a + __tmp;
979 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000980 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000981 __thousands_sep, __grouping, __g, __g_end,
982 __atoms))
983 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000984 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
986 *__g_end++ = __dc;
987 // Stage 3
988 __v = __num_get_signed_integral<long>(__a, __a_end, __err, __base);
989 // Digit grouping checked
990 __check_grouping(__grouping, __g, __g_end, __err);
991 // EOF checked
992 if (__b == __e)
993 __err |= ios_base::eofbit;
994 return __b;
995}
996
997template <class _CharT, class _InputIterator>
998_InputIterator
999num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1000 ios_base& __iob,
1001 ios_base::iostate& __err,
1002 long long& __v) const
1003{
1004 // Stage 1
1005 int __base = this->__get_base(__iob);
1006 // Stage 2
1007 char_type __atoms[26];
1008 char_type __thousands_sep;
1009 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001010 string __buf;
1011 __buf.resize(__buf.capacity());
1012 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013 char* __a_end = __a;
1014 unsigned __g[__num_get_base::__num_get_buf_sz];
1015 unsigned* __g_end = __g;
1016 unsigned __dc = 0;
1017 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001018 {
1019 if (__a_end - __a == __buf.size())
1020 {
1021 size_t __tmp = __buf.size();
1022 __buf.resize(2*__buf.size());
1023 __buf.resize(__buf.capacity());
1024 __a = &__buf[0];
1025 __a_end = __a + __tmp;
1026 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001027 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1028 __thousands_sep, __grouping, __g, __g_end,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029 __atoms))
1030 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001031 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1033 *__g_end++ = __dc;
1034 // Stage 3
1035 __v = __num_get_signed_integral<long long>(__a, __a_end, __err, __base);
1036 // Digit grouping checked
1037 __check_grouping(__grouping, __g, __g_end, __err);
1038 // EOF checked
1039 if (__b == __e)
1040 __err |= ios_base::eofbit;
1041 return __b;
1042}
1043
1044template <class _CharT, class _InputIterator>
1045_InputIterator
1046num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1047 ios_base& __iob,
1048 ios_base::iostate& __err,
1049 unsigned short& __v) const
1050{
1051 // Stage 1
1052 int __base = this->__get_base(__iob);
1053 // Stage 2
1054 char_type __atoms[26];
1055 char_type __thousands_sep;
1056 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001057 string __buf;
1058 __buf.resize(__buf.capacity());
1059 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001060 char* __a_end = __a;
1061 unsigned __g[__num_get_base::__num_get_buf_sz];
1062 unsigned* __g_end = __g;
1063 unsigned __dc = 0;
1064 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001065 {
1066 if (__a_end - __a == __buf.size())
1067 {
1068 size_t __tmp = __buf.size();
1069 __buf.resize(2*__buf.size());
1070 __buf.resize(__buf.capacity());
1071 __a = &__buf[0];
1072 __a_end = __a + __tmp;
1073 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001074 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075 __thousands_sep, __grouping, __g, __g_end,
1076 __atoms))
1077 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001078 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1080 *__g_end++ = __dc;
1081 // Stage 3
1082 __v = __num_get_unsigned_integral<unsigned short>(__a, __a_end, __err, __base);
1083 // Digit grouping checked
1084 __check_grouping(__grouping, __g, __g_end, __err);
1085 // EOF checked
1086 if (__b == __e)
1087 __err |= ios_base::eofbit;
1088 return __b;
1089}
1090
1091template <class _CharT, class _InputIterator>
1092_InputIterator
1093num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1094 ios_base& __iob,
1095 ios_base::iostate& __err,
1096 unsigned int& __v) const
1097{
1098 // Stage 1
1099 int __base = this->__get_base(__iob);
1100 // Stage 2
1101 char_type __atoms[26];
1102 char_type __thousands_sep;
1103 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001104 string __buf;
1105 __buf.resize(__buf.capacity());
1106 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001107 char* __a_end = __a;
1108 unsigned __g[__num_get_base::__num_get_buf_sz];
1109 unsigned* __g_end = __g;
1110 unsigned __dc = 0;
1111 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001112 {
1113 if (__a_end - __a == __buf.size())
1114 {
1115 size_t __tmp = __buf.size();
1116 __buf.resize(2*__buf.size());
1117 __buf.resize(__buf.capacity());
1118 __a = &__buf[0];
1119 __a_end = __a + __tmp;
1120 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001121 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122 __thousands_sep, __grouping, __g, __g_end,
1123 __atoms))
1124 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001125 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1127 *__g_end++ = __dc;
1128 // Stage 3
1129 __v = __num_get_unsigned_integral<unsigned int>(__a, __a_end, __err, __base);
1130 // Digit grouping checked
1131 __check_grouping(__grouping, __g, __g_end, __err);
1132 // EOF checked
1133 if (__b == __e)
1134 __err |= ios_base::eofbit;
1135 return __b;
1136}
1137
1138template <class _CharT, class _InputIterator>
1139_InputIterator
1140num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1141 ios_base& __iob,
1142 ios_base::iostate& __err,
1143 unsigned long& __v) const
1144{
1145 // Stage 1
1146 int __base = this->__get_base(__iob);
1147 // Stage 2
1148 char_type __atoms[26];
1149 char_type __thousands_sep;
1150 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001151 string __buf;
1152 __buf.resize(__buf.capacity());
1153 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154 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 Hinnant5d4013d2013-04-15 20:40:06 +00001159 {
1160 if (__a_end - __a == __buf.size())
1161 {
1162 size_t __tmp = __buf.size();
1163 __buf.resize(2*__buf.size());
1164 __buf.resize(__buf.capacity());
1165 __a = &__buf[0];
1166 __a_end = __a + __tmp;
1167 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001168 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169 __thousands_sep, __grouping, __g, __g_end,
1170 __atoms))
1171 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001172 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1174 *__g_end++ = __dc;
1175 // Stage 3
1176 __v = __num_get_unsigned_integral<unsigned long>(__a, __a_end, __err, __base);
1177 // Digit grouping checked
1178 __check_grouping(__grouping, __g, __g_end, __err);
1179 // EOF checked
1180 if (__b == __e)
1181 __err |= ios_base::eofbit;
1182 return __b;
1183}
1184
1185template <class _CharT, class _InputIterator>
1186_InputIterator
1187num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1188 ios_base& __iob,
1189 ios_base::iostate& __err,
1190 unsigned long long& __v) const
1191{
1192 // Stage 1
1193 int __base = this->__get_base(__iob);
1194 // Stage 2
1195 char_type __atoms[26];
1196 char_type __thousands_sep;
1197 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001198 string __buf;
1199 __buf.resize(__buf.capacity());
1200 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201 char* __a_end = __a;
1202 unsigned __g[__num_get_base::__num_get_buf_sz];
1203 unsigned* __g_end = __g;
1204 unsigned __dc = 0;
1205 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001206 {
1207 if (__a_end - __a == __buf.size())
1208 {
1209 size_t __tmp = __buf.size();
1210 __buf.resize(2*__buf.size());
1211 __buf.resize(__buf.capacity());
1212 __a = &__buf[0];
1213 __a_end = __a + __tmp;
1214 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001215 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001216 __thousands_sep, __grouping, __g, __g_end,
1217 __atoms))
1218 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001219 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1221 *__g_end++ = __dc;
1222 // Stage 3
1223 __v = __num_get_unsigned_integral<unsigned long long>(__a, __a_end, __err, __base);
1224 // Digit grouping checked
1225 __check_grouping(__grouping, __g, __g_end, __err);
1226 // EOF checked
1227 if (__b == __e)
1228 __err |= ios_base::eofbit;
1229 return __b;
1230}
1231
1232template <class _CharT, class _InputIterator>
1233_InputIterator
1234num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1235 ios_base& __iob,
1236 ios_base::iostate& __err,
1237 float& __v) const
1238{
1239 // Stage 1, nothing to do
1240 // Stage 2
1241 char_type __atoms[32];
1242 char_type __decimal_point;
1243 char_type __thousands_sep;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001244 string __grouping = this->__stage2_float_prep(__iob, __atoms,
1245 __decimal_point,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246 __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001247 string __buf;
1248 __buf.resize(__buf.capacity());
1249 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001250 char* __a_end = __a;
1251 unsigned __g[__num_get_base::__num_get_buf_sz];
1252 unsigned* __g_end = __g;
1253 unsigned __dc = 0;
1254 bool __in_units = true;
1255 char __exp = 'E';
1256 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001257 {
1258 if (__a_end - __a == __buf.size())
1259 {
1260 size_t __tmp = __buf.size();
1261 __buf.resize(2*__buf.size());
1262 __buf.resize(__buf.capacity());
1263 __a = &__buf[0];
1264 __a_end = __a + __tmp;
1265 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001266 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1267 __decimal_point, __thousands_sep,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001268 __grouping, __g, __g_end,
1269 __dc, __atoms))
1270 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001271 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001272 if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1273 *__g_end++ = __dc;
1274 // Stage 3
1275 __v = __num_get_float<float>(__a, __a_end, __err);
1276 // Digit grouping checked
1277 __check_grouping(__grouping, __g, __g_end, __err);
1278 // EOF checked
1279 if (__b == __e)
1280 __err |= ios_base::eofbit;
1281 return __b;
1282}
1283
1284template <class _CharT, class _InputIterator>
1285_InputIterator
1286num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1287 ios_base& __iob,
1288 ios_base::iostate& __err,
1289 double& __v) const
1290{
1291 // Stage 1, nothing to do
1292 // Stage 2
1293 char_type __atoms[32];
1294 char_type __decimal_point;
1295 char_type __thousands_sep;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001296 string __grouping = this->__stage2_float_prep(__iob, __atoms,
1297 __decimal_point,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001298 __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001299 string __buf;
1300 __buf.resize(__buf.capacity());
1301 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302 char* __a_end = __a;
1303 unsigned __g[__num_get_base::__num_get_buf_sz];
1304 unsigned* __g_end = __g;
1305 unsigned __dc = 0;
1306 bool __in_units = true;
1307 char __exp = 'E';
1308 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001309 {
1310 if (__a_end - __a == __buf.size())
1311 {
1312 size_t __tmp = __buf.size();
1313 __buf.resize(2*__buf.size());
1314 __buf.resize(__buf.capacity());
1315 __a = &__buf[0];
1316 __a_end = __a + __tmp;
1317 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001318 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1319 __decimal_point, __thousands_sep,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001320 __grouping, __g, __g_end,
1321 __dc, __atoms))
1322 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001323 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001324 if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1325 *__g_end++ = __dc;
1326 // Stage 3
1327 __v = __num_get_float<double>(__a, __a_end, __err);
1328 // Digit grouping checked
1329 __check_grouping(__grouping, __g, __g_end, __err);
1330 // EOF checked
1331 if (__b == __e)
1332 __err |= ios_base::eofbit;
1333 return __b;
1334}
1335
1336template <class _CharT, class _InputIterator>
1337_InputIterator
1338num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1339 ios_base& __iob,
1340 ios_base::iostate& __err,
1341 long double& __v) const
1342{
1343 // Stage 1, nothing to do
1344 // Stage 2
1345 char_type __atoms[32];
1346 char_type __decimal_point;
1347 char_type __thousands_sep;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001348 string __grouping = this->__stage2_float_prep(__iob, __atoms,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001349 __decimal_point,
1350 __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001351 string __buf;
1352 __buf.resize(__buf.capacity());
1353 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001354 char* __a_end = __a;
1355 unsigned __g[__num_get_base::__num_get_buf_sz];
1356 unsigned* __g_end = __g;
1357 unsigned __dc = 0;
1358 bool __in_units = true;
1359 char __exp = 'E';
1360 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001361 {
1362 if (__a_end - __a == __buf.size())
1363 {
1364 size_t __tmp = __buf.size();
1365 __buf.resize(2*__buf.size());
1366 __buf.resize(__buf.capacity());
1367 __a = &__buf[0];
1368 __a_end = __a + __tmp;
1369 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001370 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1371 __decimal_point, __thousands_sep,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001372 __grouping, __g, __g_end,
1373 __dc, __atoms))
1374 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001375 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001376 if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1377 *__g_end++ = __dc;
1378 // Stage 3
1379 __v = __num_get_float<long double>(__a, __a_end, __err);
1380 // Digit grouping checked
1381 __check_grouping(__grouping, __g, __g_end, __err);
1382 // EOF checked
1383 if (__b == __e)
1384 __err |= ios_base::eofbit;
1385 return __b;
1386}
1387
1388template <class _CharT, class _InputIterator>
1389_InputIterator
1390num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1391 ios_base& __iob,
1392 ios_base::iostate& __err,
1393 void*& __v) const
1394{
1395 // Stage 1
1396 int __base = 16;
1397 // Stage 2
1398 char_type __atoms[26];
Howard Hinnant28b24882011-12-01 20:21:04 +00001399 char_type __thousands_sep = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400 string __grouping;
1401 use_facet<ctype<_CharT> >(__iob.getloc()).widen(__num_get_base::__src,
1402 __num_get_base::__src + 26, __atoms);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001403 string __buf;
1404 __buf.resize(__buf.capacity());
1405 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001406 char* __a_end = __a;
1407 unsigned __g[__num_get_base::__num_get_buf_sz];
1408 unsigned* __g_end = __g;
1409 unsigned __dc = 0;
1410 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001411 {
1412 if (__a_end - __a == __buf.size())
1413 {
1414 size_t __tmp = __buf.size();
1415 __buf.resize(2*__buf.size());
1416 __buf.resize(__buf.capacity());
1417 __a = &__buf[0];
1418 __a_end = __a + __tmp;
1419 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001420 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1421 __thousands_sep, __grouping,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001422 __g, __g_end, __atoms))
1423 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001424 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001425 // Stage 3
1426 __a[sizeof(__a)-1] = 0;
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001427#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001428 if (sscanf_l(__a, _LIBCPP_GET_C_LOCALE, "%p", &__v) != 1)
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001429#else
1430 if (__sscanf_l(__a, __cloc(), "%p", &__v) != 1)
1431#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001432 __err = ios_base::failbit;
1433 // EOF checked
1434 if (__b == __e)
1435 __err |= ios_base::eofbit;
1436 return __b;
1437}
1438
Howard Hinnant8ea98242013-08-23 17:37:05 +00001439_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS num_get<char>)
1440_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS num_get<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001441
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001442struct _LIBCPP_TYPE_VIS __num_put_base
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443{
1444protected:
1445 static void __format_int(char* __fmt, const char* __len, bool __signd,
1446 ios_base::fmtflags __flags);
1447 static bool __format_float(char* __fmt, const char* __len,
1448 ios_base::fmtflags __flags);
1449 static char* __identify_padding(char* __nb, char* __ne,
1450 const ios_base& __iob);
1451};
1452
1453template <class _CharT>
1454struct __num_put
1455 : protected __num_put_base
1456{
1457 static void __widen_and_group_int(char* __nb, char* __np, char* __ne,
1458 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1459 const locale& __loc);
1460 static void __widen_and_group_float(char* __nb, char* __np, char* __ne,
1461 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1462 const locale& __loc);
1463};
1464
1465template <class _CharT>
1466void
1467__num_put<_CharT>::__widen_and_group_int(char* __nb, char* __np, char* __ne,
1468 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1469 const locale& __loc)
1470{
1471 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1472 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1473 string __grouping = __npt.grouping();
1474 if (__grouping.empty())
1475 {
1476 __ct.widen(__nb, __ne, __ob);
1477 __oe = __ob + (__ne - __nb);
1478 }
1479 else
1480 {
1481 __oe = __ob;
1482 char* __nf = __nb;
1483 if (*__nf == '-' || *__nf == '+')
1484 *__oe++ = __ct.widen(*__nf++);
1485 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1486 __nf[1] == 'X'))
1487 {
1488 *__oe++ = __ct.widen(*__nf++);
1489 *__oe++ = __ct.widen(*__nf++);
1490 }
1491 reverse(__nf, __ne);
1492 _CharT __thousands_sep = __npt.thousands_sep();
1493 unsigned __dc = 0;
1494 unsigned __dg = 0;
1495 for (char* __p = __nf; __p < __ne; ++__p)
1496 {
1497 if (static_cast<unsigned>(__grouping[__dg]) > 0 &&
1498 __dc == static_cast<unsigned>(__grouping[__dg]))
1499 {
1500 *__oe++ = __thousands_sep;
1501 __dc = 0;
1502 if (__dg < __grouping.size()-1)
1503 ++__dg;
1504 }
1505 *__oe++ = __ct.widen(*__p);
1506 ++__dc;
1507 }
1508 reverse(__ob + (__nf - __nb), __oe);
1509 }
1510 if (__np == __ne)
1511 __op = __oe;
1512 else
1513 __op = __ob + (__np - __nb);
1514}
1515
1516template <class _CharT>
1517void
1518__num_put<_CharT>::__widen_and_group_float(char* __nb, char* __np, char* __ne,
1519 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1520 const locale& __loc)
1521{
1522 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1523 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1524 string __grouping = __npt.grouping();
1525 __oe = __ob;
1526 char* __nf = __nb;
1527 if (*__nf == '-' || *__nf == '+')
1528 *__oe++ = __ct.widen(*__nf++);
1529 char* __ns;
1530 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1531 __nf[1] == 'X'))
1532 {
1533 *__oe++ = __ct.widen(*__nf++);
1534 *__oe++ = __ct.widen(*__nf++);
1535 for (__ns = __nf; __ns < __ne; ++__ns)
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001536 if (!isxdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001537 break;
1538 }
1539 else
1540 {
1541 for (__ns = __nf; __ns < __ne; ++__ns)
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001542 if (!isdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 break;
1544 }
1545 if (__grouping.empty())
1546 {
1547 __ct.widen(__nf, __ns, __oe);
1548 __oe += __ns - __nf;
1549 }
1550 else
1551 {
1552 reverse(__nf, __ns);
1553 _CharT __thousands_sep = __npt.thousands_sep();
1554 unsigned __dc = 0;
1555 unsigned __dg = 0;
1556 for (char* __p = __nf; __p < __ns; ++__p)
1557 {
1558 if (__grouping[__dg] > 0 && __dc == static_cast<unsigned>(__grouping[__dg]))
1559 {
1560 *__oe++ = __thousands_sep;
1561 __dc = 0;
1562 if (__dg < __grouping.size()-1)
1563 ++__dg;
1564 }
1565 *__oe++ = __ct.widen(*__p);
1566 ++__dc;
1567 }
1568 reverse(__ob + (__nf - __nb), __oe);
1569 }
1570 for (__nf = __ns; __nf < __ne; ++__nf)
1571 {
1572 if (*__nf == '.')
1573 {
1574 *__oe++ = __npt.decimal_point();
1575 ++__nf;
1576 break;
1577 }
1578 else
1579 *__oe++ = __ct.widen(*__nf);
1580 }
1581 __ct.widen(__nf, __ne, __oe);
1582 __oe += __ne - __nf;
1583 if (__np == __ne)
1584 __op = __oe;
1585 else
1586 __op = __ob + (__np - __nb);
1587}
1588
Howard Hinnant8ea98242013-08-23 17:37:05 +00001589_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_TYPE_VIS __num_put<char>)
1590_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_TYPE_VIS __num_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591
1592template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001593class _LIBCPP_TYPE_VIS_ONLY num_put
Howard Hinnantc51e1022010-05-11 19:42:16 +00001594 : public locale::facet,
1595 private __num_put<_CharT>
1596{
1597public:
1598 typedef _CharT char_type;
1599 typedef _OutputIterator iter_type;
1600
1601 _LIBCPP_ALWAYS_INLINE
1602 explicit num_put(size_t __refs = 0)
1603 : locale::facet(__refs) {}
1604
1605 _LIBCPP_ALWAYS_INLINE
1606 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1607 bool __v) const
1608 {
1609 return do_put(__s, __iob, __fl, __v);
1610 }
1611
1612 _LIBCPP_ALWAYS_INLINE
1613 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1614 long __v) const
1615 {
1616 return do_put(__s, __iob, __fl, __v);
1617 }
1618
1619 _LIBCPP_ALWAYS_INLINE
1620 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1621 long long __v) const
1622 {
1623 return do_put(__s, __iob, __fl, __v);
1624 }
1625
1626 _LIBCPP_ALWAYS_INLINE
1627 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1628 unsigned long __v) const
1629 {
1630 return do_put(__s, __iob, __fl, __v);
1631 }
1632
1633 _LIBCPP_ALWAYS_INLINE
1634 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1635 unsigned long long __v) const
1636 {
1637 return do_put(__s, __iob, __fl, __v);
1638 }
1639
1640 _LIBCPP_ALWAYS_INLINE
1641 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1642 double __v) const
1643 {
1644 return do_put(__s, __iob, __fl, __v);
1645 }
1646
1647 _LIBCPP_ALWAYS_INLINE
1648 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1649 long double __v) const
1650 {
1651 return do_put(__s, __iob, __fl, __v);
1652 }
1653
1654 _LIBCPP_ALWAYS_INLINE
1655 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1656 const void* __v) const
1657 {
1658 return do_put(__s, __iob, __fl, __v);
1659 }
1660
1661 static locale::id id;
1662
1663protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00001664 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001665 ~num_put() {}
1666
1667 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1668 bool __v) const;
1669 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1670 long __v) const;
1671 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1672 long long __v) const;
1673 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1674 unsigned long) const;
1675 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1676 unsigned long long) const;
1677 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1678 double __v) const;
1679 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1680 long double __v) const;
1681 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1682 const void* __v) const;
1683};
1684
1685template <class _CharT, class _OutputIterator>
1686locale::id
1687num_put<_CharT, _OutputIterator>::id;
1688
1689template <class _CharT, class _OutputIterator>
1690_LIBCPP_HIDDEN
1691_OutputIterator
1692__pad_and_output(_OutputIterator __s,
1693 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1694 ios_base& __iob, _CharT __fl)
1695{
1696 streamsize __sz = __oe - __ob;
1697 streamsize __ns = __iob.width();
1698 if (__ns > __sz)
1699 __ns -= __sz;
1700 else
1701 __ns = 0;
1702 for (;__ob < __op; ++__ob, ++__s)
1703 *__s = *__ob;
1704 for (; __ns; --__ns, ++__s)
1705 *__s = __fl;
1706 for (; __ob < __oe; ++__ob, ++__s)
1707 *__s = *__ob;
1708 __iob.width(0);
1709 return __s;
1710}
1711
Howard Hinnant48fd5d52012-11-14 21:17:15 +00001712#if !defined(__APPLE__) || \
1713 (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED > __MAC_10_8) || \
1714 (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_6_0)
1715
Howard Hinnant97955172012-09-19 19:14:15 +00001716template <class _CharT, class _Traits>
1717_LIBCPP_HIDDEN
1718ostreambuf_iterator<_CharT, _Traits>
1719__pad_and_output(ostreambuf_iterator<_CharT, _Traits> __s,
1720 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1721 ios_base& __iob, _CharT __fl)
1722{
1723 if (__s.__sbuf_ == nullptr)
1724 return __s;
1725 streamsize __sz = __oe - __ob;
1726 streamsize __ns = __iob.width();
1727 if (__ns > __sz)
1728 __ns -= __sz;
1729 else
1730 __ns = 0;
1731 streamsize __np = __op - __ob;
1732 if (__np > 0)
1733 {
1734 if (__s.__sbuf_->sputn(__ob, __np) != __np)
1735 {
1736 __s.__sbuf_ = nullptr;
1737 return __s;
1738 }
1739 }
1740 if (__ns > 0)
1741 {
1742 basic_string<_CharT, _Traits> __sp(__ns, __fl);
1743 if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns)
1744 {
1745 __s.__sbuf_ = nullptr;
1746 return __s;
1747 }
1748 }
1749 __np = __oe - __op;
1750 if (__np > 0)
1751 {
1752 if (__s.__sbuf_->sputn(__op, __np) != __np)
1753 {
1754 __s.__sbuf_ = nullptr;
1755 return __s;
1756 }
1757 }
1758 __iob.width(0);
1759 return __s;
1760}
1761
Howard Hinnant48fd5d52012-11-14 21:17:15 +00001762#endif
1763
Howard Hinnantc51e1022010-05-11 19:42:16 +00001764template <class _CharT, class _OutputIterator>
1765_OutputIterator
1766num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1767 char_type __fl, bool __v) const
1768{
1769 if ((__iob.flags() & ios_base::boolalpha) == 0)
1770 return do_put(__s, __iob, __fl, (unsigned long)__v);
1771 const numpunct<char_type>& __np = use_facet<numpunct<char_type> >(__iob.getloc());
1772 typedef typename numpunct<char_type>::string_type string_type;
Howard Hinnant8ea98242013-08-23 17:37:05 +00001773#if _LIBCPP_DEBUG_LEVEL >= 2
1774 string_type __tmp(__v ? __np.truename() : __np.falsename());
1775 string_type __nm = _VSTD::move(__tmp);
1776#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777 string_type __nm = __v ? __np.truename() : __np.falsename();
Howard Hinnant8ea98242013-08-23 17:37:05 +00001778#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001779 for (typename string_type::iterator __i = __nm.begin(); __i != __nm.end(); ++__i, ++__s)
1780 *__s = *__i;
1781 return __s;
1782}
1783
1784template <class _CharT, class _OutputIterator>
1785_OutputIterator
1786num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1787 char_type __fl, long __v) const
1788{
1789 // Stage 1 - Get number in narrow char
1790 char __fmt[6] = {'%', 0};
1791 const char* __len = "l";
1792 this->__format_int(__fmt+1, __len, true, __iob.flags());
1793 const unsigned __nbuf = (numeric_limits<long>::digits / 3)
1794 + ((numeric_limits<long>::digits % 3) != 0)
1795 + 1;
1796 char __nar[__nbuf];
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001797#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001798 int __nc = snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001799#else
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001800 int __nc = __snprintf_l(__nar, sizeof(__nar), __cloc(), __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001801#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802 char* __ne = __nar + __nc;
1803 char* __np = this->__identify_padding(__nar, __ne, __iob);
1804 // Stage 2 - Widen __nar while adding thousands separators
1805 char_type __o[2*(__nbuf-1) - 1];
1806 char_type* __op; // pad here
1807 char_type* __oe; // end of output
1808 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1809 // [__o, __oe) contains thousands_sep'd wide number
1810 // Stage 3 & 4
1811 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1812}
1813
1814template <class _CharT, class _OutputIterator>
1815_OutputIterator
1816num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1817 char_type __fl, long long __v) const
1818{
1819 // Stage 1 - Get number in narrow char
1820 char __fmt[8] = {'%', 0};
1821 const char* __len = "ll";
1822 this->__format_int(__fmt+1, __len, true, __iob.flags());
1823 const unsigned __nbuf = (numeric_limits<long long>::digits / 3)
1824 + ((numeric_limits<long long>::digits % 3) != 0)
1825 + 1;
1826 char __nar[__nbuf];
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001827#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001828 int __nc = snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001829#else
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001830 int __nc = __snprintf_l(__nar, sizeof(__nar), __cloc(), __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001831#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001832 char* __ne = __nar + __nc;
1833 char* __np = this->__identify_padding(__nar, __ne, __iob);
1834 // Stage 2 - Widen __nar while adding thousands separators
1835 char_type __o[2*(__nbuf-1) - 1];
1836 char_type* __op; // pad here
1837 char_type* __oe; // end of output
1838 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1839 // [__o, __oe) contains thousands_sep'd wide number
1840 // Stage 3 & 4
1841 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1842}
1843
1844template <class _CharT, class _OutputIterator>
1845_OutputIterator
1846num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1847 char_type __fl, unsigned long __v) const
1848{
1849 // Stage 1 - Get number in narrow char
1850 char __fmt[6] = {'%', 0};
1851 const char* __len = "l";
1852 this->__format_int(__fmt+1, __len, false, __iob.flags());
1853 const unsigned __nbuf = (numeric_limits<unsigned long>::digits / 3)
1854 + ((numeric_limits<unsigned long>::digits % 3) != 0)
1855 + 1;
1856 char __nar[__nbuf];
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001857#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001858 int __nc = snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001859#else
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001860 int __nc = __snprintf_l(__nar, sizeof(__nar), __cloc(), __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001861#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001862 char* __ne = __nar + __nc;
1863 char* __np = this->__identify_padding(__nar, __ne, __iob);
1864 // Stage 2 - Widen __nar while adding thousands separators
1865 char_type __o[2*(__nbuf-1) - 1];
1866 char_type* __op; // pad here
1867 char_type* __oe; // end of output
1868 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1869 // [__o, __oe) contains thousands_sep'd wide number
1870 // Stage 3 & 4
1871 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1872}
1873
1874template <class _CharT, class _OutputIterator>
1875_OutputIterator
1876num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1877 char_type __fl, unsigned long long __v) const
1878{
1879 // Stage 1 - Get number in narrow char
1880 char __fmt[8] = {'%', 0};
1881 const char* __len = "ll";
1882 this->__format_int(__fmt+1, __len, false, __iob.flags());
1883 const unsigned __nbuf = (numeric_limits<unsigned long long>::digits / 3)
1884 + ((numeric_limits<unsigned long long>::digits % 3) != 0)
1885 + 1;
1886 char __nar[__nbuf];
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001887#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001888 int __nc = snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001889#else
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001890 int __nc = __snprintf_l(__nar, sizeof(__nar), __cloc(), __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001891#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001892 char* __ne = __nar + __nc;
1893 char* __np = this->__identify_padding(__nar, __ne, __iob);
1894 // Stage 2 - Widen __nar while adding thousands separators
1895 char_type __o[2*(__nbuf-1) - 1];
1896 char_type* __op; // pad here
1897 char_type* __oe; // end of output
1898 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1899 // [__o, __oe) contains thousands_sep'd wide number
1900 // Stage 3 & 4
1901 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1902}
1903
1904template <class _CharT, class _OutputIterator>
1905_OutputIterator
1906num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1907 char_type __fl, double __v) const
1908{
1909 // Stage 1 - Get number in narrow char
1910 char __fmt[8] = {'%', 0};
1911 const char* __len = "";
1912 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1913 const unsigned __nbuf = 30;
1914 char __nar[__nbuf];
1915 char* __nb = __nar;
1916 int __nc;
1917 if (__specify_precision)
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001918#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001919 __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
Howard Hinnant155c2af2010-05-24 17:49:41 +00001920 (int)__iob.precision(), __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001921#else
1922 __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt,
1923 (int)__iob.precision(), __v);
1924#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001925 else
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001926#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001927 __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001928#else
1929 __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt, __v);
1930#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001931 unique_ptr<char, void(*)(void*)> __nbh(0, free);
1932 if (__nc > static_cast<int>(__nbuf-1))
1933 {
1934 if (__specify_precision)
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001935#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001936 __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001937#else
1938 __nc = __asprintf_l(&__nb, __cloc(), __fmt,
David Chisnall1d581062011-09-21 08:39:44 +00001939 (int)__iob.precision(), __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001940#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001941 else
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001942#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001943 __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001944#else
David Chisnall1d581062011-09-21 08:39:44 +00001945 __nc = __asprintf_l(&__nb, __cloc(), __fmt, (int)__iob.precision(), __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001946#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001947 if (__nb == 0)
1948 __throw_bad_alloc();
1949 __nbh.reset(__nb);
1950 }
1951 char* __ne = __nb + __nc;
1952 char* __np = this->__identify_padding(__nb, __ne, __iob);
1953 // Stage 2 - Widen __nar while adding thousands separators
1954 char_type __o[2*(__nbuf-1) - 1];
1955 char_type* __ob = __o;
1956 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1957 if (__nb != __nar)
1958 {
Howard Hinnant28b24882011-12-01 20:21:04 +00001959 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001960 if (__ob == 0)
1961 __throw_bad_alloc();
1962 __obh.reset(__ob);
1963 }
1964 char_type* __op; // pad here
1965 char_type* __oe; // end of output
1966 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1967 // [__o, __oe) contains thousands_sep'd wide number
1968 // Stage 3 & 4
1969 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1970 return __s;
1971}
1972
1973template <class _CharT, class _OutputIterator>
1974_OutputIterator
1975num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1976 char_type __fl, long double __v) const
1977{
1978 // Stage 1 - Get number in narrow char
1979 char __fmt[8] = {'%', 0};
1980 const char* __len = "L";
1981 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1982 const unsigned __nbuf = 30;
1983 char __nar[__nbuf];
1984 char* __nb = __nar;
1985 int __nc;
1986 if (__specify_precision)
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001987#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001988 __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
Howard Hinnant155c2af2010-05-24 17:49:41 +00001989 (int)__iob.precision(), __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001990#else
1991 __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt,
1992 (int)__iob.precision(), __v);
1993#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001994 else
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001995#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001996 __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001997#else
1998 __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt, __v);
1999#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002000 unique_ptr<char, void(*)(void*)> __nbh(0, free);
2001 if (__nc > static_cast<int>(__nbuf-1))
2002 {
2003 if (__specify_precision)
Howard Hinnantf312e3e2011-09-28 23:39:33 +00002004#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00002005 __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00002006#else
2007 __nc = __asprintf_l(&__nb, __cloc(), __fmt,
David Chisnall1d581062011-09-21 08:39:44 +00002008 (int)__iob.precision(), __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00002009#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002010 else
Howard Hinnantf312e3e2011-09-28 23:39:33 +00002011#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00002012 __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00002013#else
David Chisnall1d581062011-09-21 08:39:44 +00002014 __nc = __asprintf_l(&__nb, __cloc(), __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00002015#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002016 if (__nb == 0)
2017 __throw_bad_alloc();
2018 __nbh.reset(__nb);
2019 }
2020 char* __ne = __nb + __nc;
2021 char* __np = this->__identify_padding(__nb, __ne, __iob);
2022 // Stage 2 - Widen __nar while adding thousands separators
2023 char_type __o[2*(__nbuf-1) - 1];
2024 char_type* __ob = __o;
2025 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
2026 if (__nb != __nar)
2027 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002028 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002029 if (__ob == 0)
2030 __throw_bad_alloc();
2031 __obh.reset(__ob);
2032 }
2033 char_type* __op; // pad here
2034 char_type* __oe; // end of output
2035 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
2036 // [__o, __oe) contains thousands_sep'd wide number
2037 // Stage 3 & 4
2038 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
2039 return __s;
2040}
2041
2042template <class _CharT, class _OutputIterator>
2043_OutputIterator
2044num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
2045 char_type __fl, const void* __v) const
2046{
2047 // Stage 1 - Get pointer in narrow char
2048 char __fmt[6] = "%p";
2049 const unsigned __nbuf = 20;
2050 char __nar[__nbuf];
Howard Hinnantf312e3e2011-09-28 23:39:33 +00002051#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00002052 int __nc = snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00002053#else
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00002054 int __nc = __snprintf_l(__nar, sizeof(__nar), __cloc(), __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00002055#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002056 char* __ne = __nar + __nc;
2057 char* __np = this->__identify_padding(__nar, __ne, __iob);
2058 // Stage 2 - Widen __nar
2059 char_type __o[2*(__nbuf-1) - 1];
2060 char_type* __op; // pad here
2061 char_type* __oe; // end of output
2062 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2063 __ct.widen(__nar, __ne, __o);
2064 __oe = __o + (__ne - __nar);
2065 if (__np == __ne)
2066 __op = __oe;
2067 else
2068 __op = __o + (__np - __nar);
2069 // [__o, __oe) contains wide number
2070 // Stage 3 & 4
2071 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
2072}
2073
Howard Hinnant8ea98242013-08-23 17:37:05 +00002074_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS num_put<char>)
2075_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS num_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002076
2077template <class _CharT, class _InputIterator>
2078_LIBCPP_HIDDEN
2079int
2080__get_up_to_n_digits(_InputIterator& __b, _InputIterator __e,
2081 ios_base::iostate& __err, const ctype<_CharT>& __ct, int __n)
2082{
2083 // Precondition: __n >= 1
2084 if (__b == __e)
2085 {
2086 __err |= ios_base::eofbit | ios_base::failbit;
2087 return 0;
2088 }
2089 // get first digit
2090 _CharT __c = *__b;
2091 if (!__ct.is(ctype_base::digit, __c))
2092 {
2093 __err |= ios_base::failbit;
2094 return 0;
2095 }
2096 int __r = __ct.narrow(__c, 0) - '0';
2097 for (++__b, --__n; __b != __e && __n > 0; ++__b, --__n)
2098 {
2099 // get next digit
2100 __c = *__b;
2101 if (!__ct.is(ctype_base::digit, __c))
2102 return __r;
2103 __r = __r * 10 + __ct.narrow(__c, 0) - '0';
2104 }
2105 if (__b == __e)
2106 __err |= ios_base::eofbit;
2107 return __r;
2108}
2109
Howard Hinnant8331b762013-03-06 23:30:19 +00002110class _LIBCPP_TYPE_VIS time_base
Howard Hinnantc51e1022010-05-11 19:42:16 +00002111{
2112public:
2113 enum dateorder {no_order, dmy, mdy, ymd, ydm};
2114};
2115
2116template <class _CharT>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002117class _LIBCPP_TYPE_VIS __time_get_c_storage
Howard Hinnantc51e1022010-05-11 19:42:16 +00002118{
2119protected:
2120 typedef basic_string<_CharT> string_type;
2121
2122 virtual const string_type* __weeks() const;
2123 virtual const string_type* __months() const;
2124 virtual const string_type* __am_pm() const;
2125 virtual const string_type& __c() const;
2126 virtual const string_type& __r() const;
2127 virtual const string_type& __x() const;
2128 virtual const string_type& __X() const;
2129};
2130
2131template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002132class _LIBCPP_TYPE_VIS_ONLY time_get
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133 : public locale::facet,
2134 public time_base,
2135 private __time_get_c_storage<_CharT>
2136{
2137public:
2138 typedef _CharT char_type;
2139 typedef _InputIterator iter_type;
2140 typedef time_base::dateorder dateorder;
2141 typedef basic_string<char_type> string_type;
2142
2143 _LIBCPP_ALWAYS_INLINE
2144 explicit time_get(size_t __refs = 0)
2145 : locale::facet(__refs) {}
2146
2147 _LIBCPP_ALWAYS_INLINE
2148 dateorder date_order() const
2149 {
2150 return this->do_date_order();
2151 }
2152
2153 _LIBCPP_ALWAYS_INLINE
2154 iter_type get_time(iter_type __b, iter_type __e, ios_base& __iob,
2155 ios_base::iostate& __err, tm* __tm) const
2156 {
2157 return do_get_time(__b, __e, __iob, __err, __tm);
2158 }
2159
2160 _LIBCPP_ALWAYS_INLINE
2161 iter_type get_date(iter_type __b, iter_type __e, ios_base& __iob,
2162 ios_base::iostate& __err, tm* __tm) const
2163 {
2164 return do_get_date(__b, __e, __iob, __err, __tm);
2165 }
2166
2167 _LIBCPP_ALWAYS_INLINE
2168 iter_type get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
2169 ios_base::iostate& __err, tm* __tm) const
2170 {
2171 return do_get_weekday(__b, __e, __iob, __err, __tm);
2172 }
2173
2174 _LIBCPP_ALWAYS_INLINE
2175 iter_type get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
2176 ios_base::iostate& __err, tm* __tm) const
2177 {
2178 return do_get_monthname(__b, __e, __iob, __err, __tm);
2179 }
2180
2181 _LIBCPP_ALWAYS_INLINE
2182 iter_type get_year(iter_type __b, iter_type __e, ios_base& __iob,
2183 ios_base::iostate& __err, tm* __tm) const
2184 {
2185 return do_get_year(__b, __e, __iob, __err, __tm);
2186 }
2187
2188 _LIBCPP_ALWAYS_INLINE
2189 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
2190 ios_base::iostate& __err, tm *__tm,
2191 char __fmt, char __mod = 0) const
2192 {
2193 return do_get(__b, __e, __iob, __err, __tm, __fmt, __mod);
2194 }
2195
2196 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
2197 ios_base::iostate& __err, tm* __tm,
2198 const char_type* __fmtb, const char_type* __fmte) const;
2199
2200 static locale::id id;
2201
2202protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002203 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002204 ~time_get() {}
2205
2206 virtual dateorder do_date_order() const;
2207 virtual iter_type do_get_time(iter_type __b, iter_type __e, ios_base& __iob,
2208 ios_base::iostate& __err, tm* __tm) const;
2209 virtual iter_type do_get_date(iter_type __b, iter_type __e, ios_base& __iob,
2210 ios_base::iostate& __err, tm* __tm) const;
2211 virtual iter_type do_get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
2212 ios_base::iostate& __err, tm* __tm) const;
2213 virtual iter_type do_get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
2214 ios_base::iostate& __err, tm* __tm) const;
2215 virtual iter_type do_get_year(iter_type __b, iter_type __e, ios_base& __iob,
2216 ios_base::iostate& __err, tm* __tm) const;
2217 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
2218 ios_base::iostate& __err, tm* __tm,
2219 char __fmt, char __mod) const;
2220private:
2221 void __get_white_space(iter_type& __b, iter_type __e,
2222 ios_base::iostate& __err, const ctype<char_type>& __ct) const;
2223 void __get_percent(iter_type& __b, iter_type __e, ios_base::iostate& __err,
2224 const ctype<char_type>& __ct) const;
2225
2226 void __get_weekdayname(int& __m,
2227 iter_type& __b, iter_type __e,
2228 ios_base::iostate& __err,
2229 const ctype<char_type>& __ct) const;
2230 void __get_monthname(int& __m,
2231 iter_type& __b, iter_type __e,
2232 ios_base::iostate& __err,
2233 const ctype<char_type>& __ct) const;
2234 void __get_day(int& __d,
2235 iter_type& __b, iter_type __e,
2236 ios_base::iostate& __err,
2237 const ctype<char_type>& __ct) const;
2238 void __get_month(int& __m,
2239 iter_type& __b, iter_type __e,
2240 ios_base::iostate& __err,
2241 const ctype<char_type>& __ct) const;
2242 void __get_year(int& __y,
2243 iter_type& __b, iter_type __e,
2244 ios_base::iostate& __err,
2245 const ctype<char_type>& __ct) const;
2246 void __get_year4(int& __y,
2247 iter_type& __b, iter_type __e,
2248 ios_base::iostate& __err,
2249 const ctype<char_type>& __ct) const;
2250 void __get_hour(int& __d,
2251 iter_type& __b, iter_type __e,
2252 ios_base::iostate& __err,
2253 const ctype<char_type>& __ct) const;
2254 void __get_12_hour(int& __h,
2255 iter_type& __b, iter_type __e,
2256 ios_base::iostate& __err,
2257 const ctype<char_type>& __ct) const;
2258 void __get_am_pm(int& __h,
2259 iter_type& __b, iter_type __e,
2260 ios_base::iostate& __err,
2261 const ctype<char_type>& __ct) const;
2262 void __get_minute(int& __m,
2263 iter_type& __b, iter_type __e,
2264 ios_base::iostate& __err,
2265 const ctype<char_type>& __ct) const;
2266 void __get_second(int& __s,
2267 iter_type& __b, iter_type __e,
2268 ios_base::iostate& __err,
2269 const ctype<char_type>& __ct) const;
2270 void __get_weekday(int& __w,
2271 iter_type& __b, iter_type __e,
2272 ios_base::iostate& __err,
2273 const ctype<char_type>& __ct) const;
2274 void __get_day_year_num(int& __w,
2275 iter_type& __b, iter_type __e,
2276 ios_base::iostate& __err,
2277 const ctype<char_type>& __ct) const;
2278};
2279
2280template <class _CharT, class _InputIterator>
2281locale::id
2282time_get<_CharT, _InputIterator>::id;
2283
2284// time_get primatives
2285
2286template <class _CharT, class _InputIterator>
2287void
2288time_get<_CharT, _InputIterator>::__get_weekdayname(int& __w,
2289 iter_type& __b, iter_type __e,
2290 ios_base::iostate& __err,
2291 const ctype<char_type>& __ct) const
2292{
2293 // Note: ignoring case comes from the POSIX strptime spec
2294 const string_type* __wk = this->__weeks();
Howard Hinnant28b24882011-12-01 20:21:04 +00002295 ptrdiff_t __i = __scan_keyword(__b, __e, __wk, __wk+14, __ct, __err, false) - __wk;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296 if (__i < 14)
2297 __w = __i % 7;
2298}
2299
2300template <class _CharT, class _InputIterator>
2301void
2302time_get<_CharT, _InputIterator>::__get_monthname(int& __m,
2303 iter_type& __b, iter_type __e,
2304 ios_base::iostate& __err,
2305 const ctype<char_type>& __ct) const
2306{
2307 // Note: ignoring case comes from the POSIX strptime spec
2308 const string_type* __month = this->__months();
Howard Hinnant28b24882011-12-01 20:21:04 +00002309 ptrdiff_t __i = __scan_keyword(__b, __e, __month, __month+24, __ct, __err, false) - __month;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002310 if (__i < 24)
2311 __m = __i % 12;
2312}
2313
2314template <class _CharT, class _InputIterator>
2315void
2316time_get<_CharT, _InputIterator>::__get_day(int& __d,
2317 iter_type& __b, iter_type __e,
2318 ios_base::iostate& __err,
2319 const ctype<char_type>& __ct) const
2320{
2321 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2322 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 31)
2323 __d = __t;
2324 else
2325 __err |= ios_base::failbit;
2326}
2327
2328template <class _CharT, class _InputIterator>
2329void
2330time_get<_CharT, _InputIterator>::__get_month(int& __m,
2331 iter_type& __b, iter_type __e,
2332 ios_base::iostate& __err,
2333 const ctype<char_type>& __ct) const
2334{
2335 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2) - 1;
2336 if (!(__err & ios_base::failbit) && __t <= 11)
2337 __m = __t;
2338 else
2339 __err |= ios_base::failbit;
2340}
2341
2342template <class _CharT, class _InputIterator>
2343void
2344time_get<_CharT, _InputIterator>::__get_year(int& __y,
2345 iter_type& __b, iter_type __e,
2346 ios_base::iostate& __err,
2347 const ctype<char_type>& __ct) const
2348{
2349 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
2350 if (!(__err & ios_base::failbit))
2351 {
2352 if (__t < 69)
2353 __t += 2000;
2354 else if (69 <= __t && __t <= 99)
2355 __t += 1900;
2356 __y = __t - 1900;
2357 }
2358}
2359
2360template <class _CharT, class _InputIterator>
2361void
2362time_get<_CharT, _InputIterator>::__get_year4(int& __y,
2363 iter_type& __b, iter_type __e,
2364 ios_base::iostate& __err,
2365 const ctype<char_type>& __ct) const
2366{
2367 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
2368 if (!(__err & ios_base::failbit))
2369 __y = __t - 1900;
2370}
2371
2372template <class _CharT, class _InputIterator>
2373void
2374time_get<_CharT, _InputIterator>::__get_hour(int& __h,
2375 iter_type& __b, iter_type __e,
2376 ios_base::iostate& __err,
2377 const ctype<char_type>& __ct) const
2378{
2379 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2380 if (!(__err & ios_base::failbit) && __t <= 23)
2381 __h = __t;
2382 else
2383 __err |= ios_base::failbit;
2384}
2385
2386template <class _CharT, class _InputIterator>
2387void
2388time_get<_CharT, _InputIterator>::__get_12_hour(int& __h,
2389 iter_type& __b, iter_type __e,
2390 ios_base::iostate& __err,
2391 const ctype<char_type>& __ct) const
2392{
2393 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2394 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 12)
2395 __h = __t;
2396 else
2397 __err |= ios_base::failbit;
2398}
2399
2400template <class _CharT, class _InputIterator>
2401void
2402time_get<_CharT, _InputIterator>::__get_minute(int& __m,
2403 iter_type& __b, iter_type __e,
2404 ios_base::iostate& __err,
2405 const ctype<char_type>& __ct) const
2406{
2407 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2408 if (!(__err & ios_base::failbit) && __t <= 59)
2409 __m = __t;
2410 else
2411 __err |= ios_base::failbit;
2412}
2413
2414template <class _CharT, class _InputIterator>
2415void
2416time_get<_CharT, _InputIterator>::__get_second(int& __s,
2417 iter_type& __b, iter_type __e,
2418 ios_base::iostate& __err,
2419 const ctype<char_type>& __ct) const
2420{
2421 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2422 if (!(__err & ios_base::failbit) && __t <= 60)
2423 __s = __t;
2424 else
2425 __err |= ios_base::failbit;
2426}
2427
2428template <class _CharT, class _InputIterator>
2429void
2430time_get<_CharT, _InputIterator>::__get_weekday(int& __w,
2431 iter_type& __b, iter_type __e,
2432 ios_base::iostate& __err,
2433 const ctype<char_type>& __ct) const
2434{
2435 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 1);
2436 if (!(__err & ios_base::failbit) && __t <= 6)
2437 __w = __t;
2438 else
2439 __err |= ios_base::failbit;
2440}
2441
2442template <class _CharT, class _InputIterator>
2443void
2444time_get<_CharT, _InputIterator>::__get_day_year_num(int& __d,
2445 iter_type& __b, iter_type __e,
2446 ios_base::iostate& __err,
2447 const ctype<char_type>& __ct) const
2448{
2449 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 3);
2450 if (!(__err & ios_base::failbit) && __t <= 365)
2451 __d = __t;
2452 else
2453 __err |= ios_base::failbit;
2454}
2455
2456template <class _CharT, class _InputIterator>
2457void
2458time_get<_CharT, _InputIterator>::__get_white_space(iter_type& __b, iter_type __e,
2459 ios_base::iostate& __err,
2460 const ctype<char_type>& __ct) const
2461{
2462 for (; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2463 ;
2464 if (__b == __e)
2465 __err |= ios_base::eofbit;
2466}
2467
2468template <class _CharT, class _InputIterator>
2469void
2470time_get<_CharT, _InputIterator>::__get_am_pm(int& __h,
2471 iter_type& __b, iter_type __e,
2472 ios_base::iostate& __err,
2473 const ctype<char_type>& __ct) const
2474{
2475 const string_type* __ap = this->__am_pm();
2476 if (__ap[0].size() + __ap[1].size() == 0)
2477 {
2478 __err |= ios_base::failbit;
2479 return;
2480 }
Howard Hinnant28b24882011-12-01 20:21:04 +00002481 ptrdiff_t __i = __scan_keyword(__b, __e, __ap, __ap+2, __ct, __err, false) - __ap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002482 if (__i == 0 && __h == 12)
2483 __h = 0;
2484 else if (__i == 1 && __h < 12)
2485 __h += 12;
2486}
2487
2488template <class _CharT, class _InputIterator>
2489void
2490time_get<_CharT, _InputIterator>::__get_percent(iter_type& __b, iter_type __e,
2491 ios_base::iostate& __err,
2492 const ctype<char_type>& __ct) const
2493{
2494 if (__b == __e)
2495 {
2496 __err |= ios_base::eofbit | ios_base::failbit;
2497 return;
2498 }
2499 if (__ct.narrow(*__b, 0) != '%')
2500 __err |= ios_base::failbit;
2501 else if(++__b == __e)
2502 __err |= ios_base::eofbit;
2503}
2504
2505// time_get end primatives
2506
2507template <class _CharT, class _InputIterator>
2508_InputIterator
2509time_get<_CharT, _InputIterator>::get(iter_type __b, iter_type __e,
2510 ios_base& __iob,
2511 ios_base::iostate& __err, tm* __tm,
2512 const char_type* __fmtb, const char_type* __fmte) const
2513{
2514 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2515 __err = ios_base::goodbit;
2516 while (__fmtb != __fmte && __err == ios_base::goodbit)
2517 {
2518 if (__b == __e)
2519 {
2520 __err = ios_base::failbit;
2521 break;
2522 }
2523 if (__ct.narrow(*__fmtb, 0) == '%')
2524 {
2525 if (++__fmtb == __fmte)
2526 {
2527 __err = ios_base::failbit;
2528 break;
2529 }
2530 char __cmd = __ct.narrow(*__fmtb, 0);
2531 char __opt = '\0';
2532 if (__cmd == 'E' || __cmd == '0')
2533 {
2534 if (++__fmtb == __fmte)
2535 {
2536 __err = ios_base::failbit;
2537 break;
2538 }
2539 __opt = __cmd;
2540 __cmd = __ct.narrow(*__fmtb, 0);
2541 }
2542 __b = do_get(__b, __e, __iob, __err, __tm, __cmd, __opt);
2543 ++__fmtb;
2544 }
2545 else if (__ct.is(ctype_base::space, *__fmtb))
2546 {
2547 for (++__fmtb; __fmtb != __fmte && __ct.is(ctype_base::space, *__fmtb); ++__fmtb)
2548 ;
2549 for ( ; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2550 ;
2551 }
2552 else if (__ct.toupper(*__b) == __ct.toupper(*__fmtb))
2553 {
2554 ++__b;
2555 ++__fmtb;
2556 }
2557 else
2558 __err = ios_base::failbit;
2559 }
2560 if (__b == __e)
2561 __err |= ios_base::eofbit;
2562 return __b;
2563}
2564
2565template <class _CharT, class _InputIterator>
2566typename time_get<_CharT, _InputIterator>::dateorder
2567time_get<_CharT, _InputIterator>::do_date_order() const
2568{
2569 return mdy;
2570}
2571
2572template <class _CharT, class _InputIterator>
2573_InputIterator
2574time_get<_CharT, _InputIterator>::do_get_time(iter_type __b, iter_type __e,
2575 ios_base& __iob,
2576 ios_base::iostate& __err,
2577 tm* __tm) const
2578{
2579 const char_type __fmt[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2580 return get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt)/sizeof(__fmt[0]));
2581}
2582
2583template <class _CharT, class _InputIterator>
2584_InputIterator
2585time_get<_CharT, _InputIterator>::do_get_date(iter_type __b, iter_type __e,
2586 ios_base& __iob,
2587 ios_base::iostate& __err,
2588 tm* __tm) const
2589{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590 const string_type& __fmt = this->__x();
2591 return get(__b, __e, __iob, __err, __tm, __fmt.data(), __fmt.data() + __fmt.size());
2592}
2593
2594template <class _CharT, class _InputIterator>
2595_InputIterator
2596time_get<_CharT, _InputIterator>::do_get_weekday(iter_type __b, iter_type __e,
2597 ios_base& __iob,
2598 ios_base::iostate& __err,
2599 tm* __tm) const
2600{
2601 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2602 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2603 return __b;
2604}
2605
2606template <class _CharT, class _InputIterator>
2607_InputIterator
2608time_get<_CharT, _InputIterator>::do_get_monthname(iter_type __b, iter_type __e,
2609 ios_base& __iob,
2610 ios_base::iostate& __err,
2611 tm* __tm) const
2612{
2613 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2614 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2615 return __b;
2616}
2617
2618template <class _CharT, class _InputIterator>
2619_InputIterator
2620time_get<_CharT, _InputIterator>::do_get_year(iter_type __b, iter_type __e,
2621 ios_base& __iob,
2622 ios_base::iostate& __err,
2623 tm* __tm) const
2624{
2625 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2626 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2627 return __b;
2628}
2629
2630template <class _CharT, class _InputIterator>
2631_InputIterator
2632time_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
2633 ios_base& __iob,
2634 ios_base::iostate& __err, tm* __tm,
2635 char __fmt, char) const
2636{
2637 __err = ios_base::goodbit;
2638 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2639 switch (__fmt)
2640 {
2641 case 'a':
2642 case 'A':
2643 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2644 break;
2645 case 'b':
2646 case 'B':
2647 case 'h':
2648 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2649 break;
2650 case 'c':
2651 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002652 const string_type& __fm = this->__c();
2653 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002654 }
2655 break;
2656 case 'd':
2657 case 'e':
2658 __get_day(__tm->tm_mday, __b, __e, __err, __ct);
2659 break;
2660 case 'D':
2661 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002662 const char_type __fm[] = {'%', 'm', '/', '%', 'd', '/', '%', 'y'};
2663 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002664 }
2665 break;
Howard Hinnantf60ff4e2011-04-10 17:54:14 +00002666 case 'F':
2667 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002668 const char_type __fm[] = {'%', 'Y', '-', '%', 'm', '-', '%', 'd'};
2669 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantf60ff4e2011-04-10 17:54:14 +00002670 }
2671 break;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002672 case 'H':
2673 __get_hour(__tm->tm_hour, __b, __e, __err, __ct);
2674 break;
2675 case 'I':
2676 __get_12_hour(__tm->tm_hour, __b, __e, __err, __ct);
2677 break;
2678 case 'j':
2679 __get_day_year_num(__tm->tm_yday, __b, __e, __err, __ct);
2680 break;
2681 case 'm':
2682 __get_month(__tm->tm_mon, __b, __e, __err, __ct);
2683 break;
2684 case 'M':
2685 __get_minute(__tm->tm_min, __b, __e, __err, __ct);
2686 break;
2687 case 'n':
2688 case 't':
2689 __get_white_space(__b, __e, __err, __ct);
2690 break;
2691 case 'p':
2692 __get_am_pm(__tm->tm_hour, __b, __e, __err, __ct);
2693 break;
2694 case 'r':
2695 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002696 const char_type __fm[] = {'%', 'I', ':', '%', 'M', ':', '%', 'S', ' ', '%', 'p'};
2697 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698 }
2699 break;
2700 case 'R':
2701 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002702 const char_type __fm[] = {'%', 'H', ':', '%', 'M'};
2703 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704 }
2705 break;
2706 case 'S':
2707 __get_second(__tm->tm_sec, __b, __e, __err, __ct);
2708 break;
2709 case 'T':
2710 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002711 const char_type __fm[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2712 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002713 }
2714 break;
2715 case 'w':
2716 __get_weekday(__tm->tm_wday, __b, __e, __err, __ct);
2717 break;
2718 case 'x':
2719 return do_get_date(__b, __e, __iob, __err, __tm);
2720 case 'X':
2721 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002722 const string_type& __fm = this->__X();
2723 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002724 }
2725 break;
2726 case 'y':
2727 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2728 break;
2729 case 'Y':
2730 __get_year4(__tm->tm_year, __b, __e, __err, __ct);
2731 break;
2732 case '%':
2733 __get_percent(__b, __e, __err, __ct);
2734 break;
2735 default:
2736 __err |= ios_base::failbit;
2737 }
2738 return __b;
2739}
2740
Howard Hinnant8ea98242013-08-23 17:37:05 +00002741_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_get<char>)
2742_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_get<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002743
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002744class _LIBCPP_TYPE_VIS __time_get
Howard Hinnantc51e1022010-05-11 19:42:16 +00002745{
2746protected:
2747 locale_t __loc_;
2748
2749 __time_get(const char* __nm);
2750 __time_get(const string& __nm);
2751 ~__time_get();
2752};
2753
2754template <class _CharT>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002755class _LIBCPP_TYPE_VIS __time_get_storage
Howard Hinnantc51e1022010-05-11 19:42:16 +00002756 : public __time_get
2757{
2758protected:
2759 typedef basic_string<_CharT> string_type;
2760
2761 string_type __weeks_[14];
2762 string_type __months_[24];
2763 string_type __am_pm_[2];
2764 string_type __c_;
2765 string_type __r_;
2766 string_type __x_;
2767 string_type __X_;
2768
2769 explicit __time_get_storage(const char* __nm);
2770 explicit __time_get_storage(const string& __nm);
2771
2772 _LIBCPP_ALWAYS_INLINE ~__time_get_storage() {}
2773
2774 time_base::dateorder __do_date_order() const;
2775
2776private:
2777 void init(const ctype<_CharT>&);
2778 string_type __analyze(char __fmt, const ctype<_CharT>&);
2779};
2780
2781template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002782class _LIBCPP_TYPE_VIS_ONLY time_get_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00002783 : public time_get<_CharT, _InputIterator>,
2784 private __time_get_storage<_CharT>
2785{
2786public:
2787 typedef time_base::dateorder dateorder;
2788 typedef _InputIterator iter_type;
2789 typedef _CharT char_type;
2790 typedef basic_string<char_type> string_type;
2791
Howard Hinnant756c69b2010-09-22 16:48:34 +00002792 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793 explicit time_get_byname(const char* __nm, size_t __refs = 0)
2794 : time_get<_CharT, _InputIterator>(__refs),
2795 __time_get_storage<_CharT>(__nm) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002796 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002797 explicit time_get_byname(const string& __nm, size_t __refs = 0)
2798 : time_get<_CharT, _InputIterator>(__refs),
2799 __time_get_storage<_CharT>(__nm) {}
2800
2801protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002803 ~time_get_byname() {}
2804
Howard Hinnant756c69b2010-09-22 16:48:34 +00002805 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002806 virtual dateorder do_date_order() const {return this->__do_date_order();}
2807private:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002809 virtual const string_type* __weeks() const {return this->__weeks_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002811 virtual const string_type* __months() const {return this->__months_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002812 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002813 virtual const string_type* __am_pm() const {return this->__am_pm_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002814 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002815 virtual const string_type& __c() const {return this->__c_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002816 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002817 virtual const string_type& __r() const {return this->__r_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002818 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002819 virtual const string_type& __x() const {return this->__x_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002820 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002821 virtual const string_type& __X() const {return this->__X_;}
2822};
2823
Howard Hinnant8ea98242013-08-23 17:37:05 +00002824_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_get_byname<char>)
2825_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_get_byname<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002826
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002827class _LIBCPP_TYPE_VIS __time_put
Howard Hinnantc51e1022010-05-11 19:42:16 +00002828{
2829 locale_t __loc_;
2830protected:
Howard Hinnant1ab52da2011-09-28 21:05:01 +00002831 _LIBCPP_ALWAYS_INLINE __time_put() : __loc_(_LIBCPP_GET_C_LOCALE) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832 __time_put(const char* __nm);
2833 __time_put(const string& __nm);
2834 ~__time_put();
2835 void __do_put(char* __nb, char*& __ne, const tm* __tm,
2836 char __fmt, char __mod) const;
2837 void __do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm,
2838 char __fmt, char __mod) const;
2839};
2840
2841template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002842class _LIBCPP_TYPE_VIS_ONLY time_put
Howard Hinnantc51e1022010-05-11 19:42:16 +00002843 : public locale::facet,
2844 private __time_put
2845{
2846public:
2847 typedef _CharT char_type;
2848 typedef _OutputIterator iter_type;
2849
2850 _LIBCPP_ALWAYS_INLINE
2851 explicit time_put(size_t __refs = 0)
2852 : locale::facet(__refs) {}
2853
2854 iter_type put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm,
2855 const char_type* __pb, const char_type* __pe) const;
2856
2857 _LIBCPP_ALWAYS_INLINE
2858 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
2859 const tm* __tm, char __fmt, char __mod = 0) const
2860 {
2861 return do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2862 }
2863
2864 static locale::id id;
2865
2866protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002867 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002868 ~time_put() {}
2869 virtual iter_type do_put(iter_type __s, ios_base&, char_type, const tm* __tm,
2870 char __fmt, char __mod) const;
2871
Howard Hinnant756c69b2010-09-22 16:48:34 +00002872 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873 explicit time_put(const char* __nm, size_t __refs)
2874 : locale::facet(__refs),
2875 __time_put(__nm) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002876 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002877 explicit time_put(const string& __nm, size_t __refs)
2878 : locale::facet(__refs),
2879 __time_put(__nm) {}
2880};
2881
2882template <class _CharT, class _OutputIterator>
2883locale::id
2884time_put<_CharT, _OutputIterator>::id;
2885
2886template <class _CharT, class _OutputIterator>
2887_OutputIterator
2888time_put<_CharT, _OutputIterator>::put(iter_type __s, ios_base& __iob,
2889 char_type __fl, const tm* __tm,
2890 const char_type* __pb,
2891 const char_type* __pe) const
2892{
2893 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2894 for (; __pb != __pe; ++__pb)
2895 {
2896 if (__ct.narrow(*__pb, 0) == '%')
2897 {
2898 if (++__pb == __pe)
2899 {
2900 *__s++ = __pb[-1];
2901 break;
2902 }
2903 char __mod = 0;
2904 char __fmt = __ct.narrow(*__pb, 0);
2905 if (__fmt == 'E' || __fmt == 'O')
2906 {
2907 if (++__pb == __pe)
2908 {
2909 *__s++ = __pb[-2];
2910 *__s++ = __pb[-1];
2911 break;
2912 }
2913 __mod = __fmt;
2914 __fmt = __ct.narrow(*__pb, 0);
2915 }
2916 __s = do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2917 }
2918 else
2919 *__s++ = *__pb;
2920 }
2921 return __s;
2922}
2923
2924template <class _CharT, class _OutputIterator>
2925_OutputIterator
Howard Hinnant28b24882011-12-01 20:21:04 +00002926time_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927 char_type, const tm* __tm,
2928 char __fmt, char __mod) const
2929{
2930 char_type __nar[100];
2931 char_type* __nb = __nar;
2932 char_type* __ne = __nb + 100;
2933 __do_put(__nb, __ne, __tm, __fmt, __mod);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002934 return _VSTD::copy(__nb, __ne, __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002935}
2936
Howard Hinnant8ea98242013-08-23 17:37:05 +00002937_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_put<char>)
2938_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002939
2940template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002941class _LIBCPP_TYPE_VIS_ONLY time_put_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00002942 : public time_put<_CharT, _OutputIterator>
2943{
2944public:
2945 _LIBCPP_ALWAYS_INLINE
2946 explicit time_put_byname(const char* __nm, size_t __refs = 0)
2947 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2948
2949 _LIBCPP_ALWAYS_INLINE
2950 explicit time_put_byname(const string& __nm, size_t __refs = 0)
2951 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2952
2953protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002954 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002955 ~time_put_byname() {}
2956};
2957
Howard Hinnant8ea98242013-08-23 17:37:05 +00002958_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_put_byname<char>)
2959_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_put_byname<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002960
2961// money_base
2962
Howard Hinnant8331b762013-03-06 23:30:19 +00002963class _LIBCPP_TYPE_VIS money_base
Howard Hinnantc51e1022010-05-11 19:42:16 +00002964{
2965public:
2966 enum part {none, space, symbol, sign, value};
2967 struct pattern {char field[4];};
2968
2969 _LIBCPP_ALWAYS_INLINE money_base() {}
2970};
2971
2972// moneypunct
2973
2974template <class _CharT, bool _International = false>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002975class _LIBCPP_TYPE_VIS_ONLY moneypunct
Howard Hinnantc51e1022010-05-11 19:42:16 +00002976 : public locale::facet,
2977 public money_base
2978{
2979public:
2980 typedef _CharT char_type;
2981 typedef basic_string<char_type> string_type;
2982
Howard Hinnant756c69b2010-09-22 16:48:34 +00002983 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002984 explicit moneypunct(size_t __refs = 0)
2985 : locale::facet(__refs) {}
2986
2987 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
2988 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
2989 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
2990 _LIBCPP_ALWAYS_INLINE string_type curr_symbol() const {return do_curr_symbol();}
2991 _LIBCPP_ALWAYS_INLINE string_type positive_sign() const {return do_positive_sign();}
2992 _LIBCPP_ALWAYS_INLINE string_type negative_sign() const {return do_negative_sign();}
2993 _LIBCPP_ALWAYS_INLINE int frac_digits() const {return do_frac_digits();}
2994 _LIBCPP_ALWAYS_INLINE pattern pos_format() const {return do_pos_format();}
2995 _LIBCPP_ALWAYS_INLINE pattern neg_format() const {return do_neg_format();}
2996
2997 static locale::id id;
2998 static const bool intl = _International;
2999
3000protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003001 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00003002 ~moneypunct() {}
3003
3004 virtual char_type do_decimal_point() const {return numeric_limits<char_type>::max();}
3005 virtual char_type do_thousands_sep() const {return numeric_limits<char_type>::max();}
3006 virtual string do_grouping() const {return string();}
3007 virtual string_type do_curr_symbol() const {return string_type();}
3008 virtual string_type do_positive_sign() const {return string_type();}
3009 virtual string_type do_negative_sign() const {return string_type(1, '-');}
3010 virtual int do_frac_digits() const {return 0;}
3011 virtual pattern do_pos_format() const
Howard Hinnantb10e6cf2012-11-06 21:48:33 +00003012 {pattern __p = {{symbol, sign, none, value}}; return __p;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003013 virtual pattern do_neg_format() const
Howard Hinnantb10e6cf2012-11-06 21:48:33 +00003014 {pattern __p = {{symbol, sign, none, value}}; return __p;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003015};
3016
3017template <class _CharT, bool _International>
3018locale::id
3019moneypunct<_CharT, _International>::id;
3020
Howard Hinnant2c45cb42012-12-12 21:14:28 +00003021template <class _CharT, bool _International>
3022const bool
3023moneypunct<_CharT, _International>::intl;
3024
Howard Hinnant8ea98242013-08-23 17:37:05 +00003025_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct<char, false>)
3026_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct<char, true>)
3027_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct<wchar_t, false>)
3028_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct<wchar_t, true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003029
3030// moneypunct_byname
3031
3032template <class _CharT, bool _International = false>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003033class _LIBCPP_TYPE_VIS_ONLY moneypunct_byname
Howard Hinnant756c69b2010-09-22 16:48:34 +00003034 : public moneypunct<_CharT, _International>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003035{
3036public:
3037 typedef money_base::pattern pattern;
3038 typedef _CharT char_type;
3039 typedef basic_string<char_type> string_type;
3040
3041 _LIBCPP_ALWAYS_INLINE
3042 explicit moneypunct_byname(const char* __nm, size_t __refs = 0)
3043 : moneypunct<_CharT, _International>(__refs) {init(__nm);}
3044
3045 _LIBCPP_ALWAYS_INLINE
3046 explicit moneypunct_byname(const string& __nm, size_t __refs = 0)
3047 : moneypunct<_CharT, _International>(__refs) {init(__nm.c_str());}
3048
3049protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003050 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00003051 ~moneypunct_byname() {}
3052
3053 virtual char_type do_decimal_point() const {return __decimal_point_;}
3054 virtual char_type do_thousands_sep() const {return __thousands_sep_;}
3055 virtual string do_grouping() const {return __grouping_;}
3056 virtual string_type do_curr_symbol() const {return __curr_symbol_;}
3057 virtual string_type do_positive_sign() const {return __positive_sign_;}
3058 virtual string_type do_negative_sign() const {return __negative_sign_;}
3059 virtual int do_frac_digits() const {return __frac_digits_;}
3060 virtual pattern do_pos_format() const {return __pos_format_;}
3061 virtual pattern do_neg_format() const {return __neg_format_;}
3062
3063private:
3064 char_type __decimal_point_;
3065 char_type __thousands_sep_;
3066 string __grouping_;
3067 string_type __curr_symbol_;
3068 string_type __positive_sign_;
3069 string_type __negative_sign_;
3070 int __frac_digits_;
3071 pattern __pos_format_;
3072 pattern __neg_format_;
3073
3074 void init(const char*);
3075};
3076
3077template<> void moneypunct_byname<char, false>::init(const char*);
3078template<> void moneypunct_byname<char, true>::init(const char*);
3079template<> void moneypunct_byname<wchar_t, false>::init(const char*);
3080template<> void moneypunct_byname<wchar_t, true>::init(const char*);
3081
Howard Hinnant8ea98242013-08-23 17:37:05 +00003082_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct_byname<char, false>)
3083_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct_byname<char, true>)
3084_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct_byname<wchar_t, false>)
3085_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct_byname<wchar_t, true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003086
3087// money_get
3088
3089template <class _CharT>
3090class __money_get
3091{
3092protected:
3093 typedef _CharT char_type;
3094 typedef basic_string<char_type> string_type;
3095
3096 _LIBCPP_ALWAYS_INLINE __money_get() {}
3097
3098 static void __gather_info(bool __intl, const locale& __loc,
3099 money_base::pattern& __pat, char_type& __dp,
3100 char_type& __ts, string& __grp,
3101 string_type& __sym, string_type& __psn,
3102 string_type& __nsn, int& __fd);
3103};
3104
3105template <class _CharT>
3106void
3107__money_get<_CharT>::__gather_info(bool __intl, const locale& __loc,
3108 money_base::pattern& __pat, char_type& __dp,
3109 char_type& __ts, string& __grp,
3110 string_type& __sym, string_type& __psn,
3111 string_type& __nsn, int& __fd)
3112{
3113 if (__intl)
3114 {
3115 const moneypunct<char_type, true>& __mp =
3116 use_facet<moneypunct<char_type, true> >(__loc);
3117 __pat = __mp.neg_format();
3118 __nsn = __mp.negative_sign();
3119 __psn = __mp.positive_sign();
3120 __dp = __mp.decimal_point();
3121 __ts = __mp.thousands_sep();
3122 __grp = __mp.grouping();
3123 __sym = __mp.curr_symbol();
3124 __fd = __mp.frac_digits();
3125 }
3126 else
3127 {
3128 const moneypunct<char_type, false>& __mp =
3129 use_facet<moneypunct<char_type, false> >(__loc);
3130 __pat = __mp.neg_format();
3131 __nsn = __mp.negative_sign();
3132 __psn = __mp.positive_sign();
3133 __dp = __mp.decimal_point();
3134 __ts = __mp.thousands_sep();
3135 __grp = __mp.grouping();
3136 __sym = __mp.curr_symbol();
3137 __fd = __mp.frac_digits();
3138 }
3139}
3140
Howard Hinnant8ea98242013-08-23 17:37:05 +00003141_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS __money_get<char>)
3142_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS __money_get<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003143
3144template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003145class _LIBCPP_TYPE_VIS_ONLY money_get
Howard Hinnantc51e1022010-05-11 19:42:16 +00003146 : public locale::facet,
3147 private __money_get<_CharT>
3148{
3149public:
3150 typedef _CharT char_type;
3151 typedef _InputIterator iter_type;
3152 typedef basic_string<char_type> string_type;
3153
3154 _LIBCPP_ALWAYS_INLINE
3155 explicit money_get(size_t __refs = 0)
3156 : locale::facet(__refs) {}
3157
3158 _LIBCPP_ALWAYS_INLINE
3159 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
3160 ios_base::iostate& __err, long double& __v) const
3161 {
3162 return do_get(__b, __e, __intl, __iob, __err, __v);
3163 }
3164
3165 _LIBCPP_ALWAYS_INLINE
3166 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
3167 ios_base::iostate& __err, string_type& __v) const
3168 {
3169 return do_get(__b, __e, __intl, __iob, __err, __v);
3170 }
3171
3172 static locale::id id;
3173
3174protected:
3175
Howard Hinnant756c69b2010-09-22 16:48:34 +00003176 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00003177 ~money_get() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003178
Howard Hinnantc51e1022010-05-11 19:42:16 +00003179 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
3180 ios_base& __iob, ios_base::iostate& __err,
3181 long double& __v) const;
3182 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
3183 ios_base& __iob, ios_base::iostate& __err,
3184 string_type& __v) const;
3185
3186private:
3187 static bool __do_get(iter_type& __b, iter_type __e,
3188 bool __intl, const locale& __loc,
3189 ios_base::fmtflags __flags, ios_base::iostate& __err,
3190 bool& __neg, const ctype<char_type>& __ct,
3191 unique_ptr<char_type, void(*)(void*)>& __wb,
3192 char_type*& __wn, char_type* __we);
3193};
3194
3195template <class _CharT, class _InputIterator>
3196locale::id
3197money_get<_CharT, _InputIterator>::id;
3198
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003199_LIBCPP_FUNC_VIS void __do_nothing(void*);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003200
3201template <class _Tp>
3202_LIBCPP_HIDDEN
3203void
3204__double_or_nothing(unique_ptr<_Tp, void(*)(void*)>& __b, _Tp*& __n, _Tp*& __e)
3205{
3206 bool __owns = __b.get_deleter() != __do_nothing;
Howard Hinnant28b24882011-12-01 20:21:04 +00003207 size_t __cur_cap = static_cast<size_t>(__e-__b.get()) * sizeof(_Tp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003208 size_t __new_cap = __cur_cap < numeric_limits<size_t>::max() / 2 ?
3209 2 * __cur_cap : numeric_limits<size_t>::max();
Howard Hinnant28b24882011-12-01 20:21:04 +00003210 size_t __n_off = static_cast<size_t>(__n - __b.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003211 _Tp* __t = (_Tp*)realloc(__owns ? __b.get() : 0, __new_cap);
3212 if (__t == 0)
3213 __throw_bad_alloc();
3214 if (__owns)
3215 __b.release();
3216 __b = unique_ptr<_Tp, void(*)(void*)>(__t, free);
3217 __new_cap /= sizeof(_Tp);
3218 __n = __b.get() + __n_off;
3219 __e = __b.get() + __new_cap;
3220}
3221
3222// true == success
3223template <class _CharT, class _InputIterator>
3224bool
3225money_get<_CharT, _InputIterator>::__do_get(iter_type& __b, iter_type __e,
3226 bool __intl, const locale& __loc,
3227 ios_base::fmtflags __flags,
3228 ios_base::iostate& __err,
3229 bool& __neg,
3230 const ctype<char_type>& __ct,
3231 unique_ptr<char_type, void(*)(void*)>& __wb,
3232 char_type*& __wn, char_type* __we)
3233{
3234 const unsigned __bz = 100;
3235 unsigned __gbuf[__bz];
3236 unique_ptr<unsigned, void(*)(void*)> __gb(__gbuf, __do_nothing);
3237 unsigned* __gn = __gb.get();
3238 unsigned* __ge = __gn + __bz;
3239 money_base::pattern __pat;
3240 char_type __dp;
3241 char_type __ts;
3242 string __grp;
3243 string_type __sym;
3244 string_type __psn;
3245 string_type __nsn;
Jeffrey Yasskin6b045fb2012-03-10 18:31:43 +00003246 // Capture the spaces read into money_base::{space,none} so they
3247 // can be compared to initial spaces in __sym.
3248 string_type __spaces;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003249 int __fd;
3250 __money_get<_CharT>::__gather_info(__intl, __loc, __pat, __dp, __ts, __grp,
3251 __sym, __psn, __nsn, __fd);
3252 const string_type* __trailing_sign = 0;
3253 __wn = __wb.get();
3254 for (unsigned __p = 0; __p < 4 && __b != __e; ++__p)
3255 {
3256 switch (__pat.field[__p])
3257 {
3258 case money_base::space:
3259 if (__p != 3)
3260 {
3261 if (__ct.is(ctype_base::space, *__b))
Jeffrey Yasskin6b045fb2012-03-10 18:31:43 +00003262 __spaces.push_back(*__b++);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003263 else
3264 {
3265 __err |= ios_base::failbit;
3266 return false;
3267 }
3268 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003269 // drop through
Howard Hinnantc51e1022010-05-11 19:42:16 +00003270 case money_base::none:
3271 if (__p != 3)
3272 {
3273 while (__b != __e && __ct.is(ctype_base::space, *__b))
Jeffrey Yasskin6b045fb2012-03-10 18:31:43 +00003274 __spaces.push_back(*__b++);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003275 }
3276 break;
3277 case money_base::sign:
3278 if (__psn.size() + __nsn.size() > 0)
3279 {
3280 if (__psn.size() == 0 || __nsn.size() == 0)
3281 { // sign is optional
3282 if (__psn.size() > 0)
3283 { // __nsn.size() == 0
3284 if (*__b == __psn[0])
3285 {
3286 ++__b;
3287 if (__psn.size() > 1)
3288 __trailing_sign = &__psn;
3289 }
3290 else
3291 __neg = true;
3292 }
3293 else if (*__b == __nsn[0]) // __nsn.size() > 0 && __psn.size() == 0
3294 {
3295 ++__b;
3296 __neg = true;
3297 if (__nsn.size() > 1)
3298 __trailing_sign = &__nsn;
3299 }
3300 }
3301 else // sign is required
3302 {
3303 if (*__b == __psn[0])
3304 {
3305 ++__b;
3306 if (__psn.size() > 1)
3307 __trailing_sign = &__psn;
3308 }
3309 else if (*__b == __nsn[0])
3310 {
3311 ++__b;
3312 __neg = true;
3313 if (__nsn.size() > 1)
3314 __trailing_sign = &__nsn;
3315 }
3316 else
3317 {
3318 __err |= ios_base::failbit;
3319 return false;
3320 }
3321 }
3322 }
3323 break;
3324 case money_base::symbol:
3325 {
3326 bool __more_needed = __trailing_sign ||
3327 (__p < 2) ||
3328 (__p == 2 && __pat.field[3] != static_cast<char>(money_base::none));
3329 bool __sb = __flags & ios_base::showbase;
3330 if (__sb || __more_needed)
3331 {
Jeffrey Yasskin6b045fb2012-03-10 18:31:43 +00003332 typename string_type::const_iterator __sym_space_end = __sym.begin();
3333 if (__p > 0 && (__pat.field[__p - 1] == money_base::none ||
3334 __pat.field[__p - 1] == money_base::space)) {
3335 // Match spaces we've already read against spaces at
3336 // the beginning of __sym.
3337 while (__sym_space_end != __sym.end() &&
3338 __ct.is(ctype_base::space, *__sym_space_end))
3339 ++__sym_space_end;
3340 const size_t __num_spaces = __sym_space_end - __sym.begin();
3341 if (__num_spaces > __spaces.size() ||
3342 !equal(__spaces.end() - __num_spaces, __spaces.end(),
3343 __sym.begin())) {
3344 // No match. Put __sym_space_end back at the
3345 // beginning of __sym, which will prevent a
3346 // match in the next loop.
3347 __sym_space_end = __sym.begin();
3348 }
3349 }
3350 typename string_type::const_iterator __sym_curr_char = __sym_space_end;
3351 while (__sym_curr_char != __sym.end() && __b != __e &&
3352 *__b == *__sym_curr_char) {
3353 ++__b;
3354 ++__sym_curr_char;
3355 }
3356 if (__sb && __sym_curr_char != __sym.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003357 {
3358 __err |= ios_base::failbit;
3359 return false;
3360 }
3361 }
3362 }
3363 break;
3364 case money_base::value:
3365 {
3366 unsigned __ng = 0;
3367 for (; __b != __e; ++__b)
3368 {
3369 char_type __c = *__b;
3370 if (__ct.is(ctype_base::digit, __c))
3371 {
3372 if (__wn == __we)
3373 __double_or_nothing(__wb, __wn, __we);
3374 *__wn++ = __c;
3375 ++__ng;
3376 }
3377 else if (__grp.size() > 0 && __ng > 0 && __c == __ts)
3378 {
3379 if (__gn == __ge)
3380 __double_or_nothing(__gb, __gn, __ge);
3381 *__gn++ = __ng;
3382 __ng = 0;
3383 }
3384 else
3385 break;
3386 }
3387 if (__gb.get() != __gn && __ng > 0)
3388 {
3389 if (__gn == __ge)
3390 __double_or_nothing(__gb, __gn, __ge);
3391 *__gn++ = __ng;
3392 }
3393 if (__fd > 0)
3394 {
3395 if (__b == __e || *__b != __dp)
3396 {
3397 __err |= ios_base::failbit;
3398 return false;
3399 }
3400 for (++__b; __fd > 0; --__fd, ++__b)
3401 {
3402 if (__b == __e || !__ct.is(ctype_base::digit, *__b))
3403 {
3404 __err |= ios_base::failbit;
3405 return false;
3406 }
3407 if (__wn == __we)
3408 __double_or_nothing(__wb, __wn, __we);
3409 *__wn++ = *__b;
3410 }
3411 }
3412 if (__wn == __wb.get())
3413 {
3414 __err |= ios_base::failbit;
3415 return false;
3416 }
3417 }
3418 break;
3419 }
3420 }
3421 if (__trailing_sign)
3422 {
3423 for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, ++__b)
3424 {
3425 if (__b == __e || *__b != (*__trailing_sign)[__i])
3426 {
3427 __err |= ios_base::failbit;
3428 return false;
3429 }
3430 }
3431 }
3432 if (__gb.get() != __gn)
3433 {
3434 ios_base::iostate __et = ios_base::goodbit;
3435 __check_grouping(__grp, __gb.get(), __gn, __et);
3436 if (__et)
3437 {
3438 __err |= ios_base::failbit;
3439 return false;
3440 }
3441 }
3442 return true;
3443}
3444
3445template <class _CharT, class _InputIterator>
3446_InputIterator
3447money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3448 bool __intl, ios_base& __iob,
3449 ios_base::iostate& __err,
3450 long double& __v) const
3451{
Howard Hinnant28b24882011-12-01 20:21:04 +00003452 const int __bz = 100;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003453 char_type __wbuf[__bz];
3454 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3455 char_type* __wn;
3456 char_type* __we = __wbuf + __bz;
3457 locale __loc = __iob.getloc();
3458 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3459 bool __neg = false;
3460 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3461 __wb, __wn, __we))
3462 {
3463 const char __src[] = "0123456789";
3464 char_type __atoms[sizeof(__src)-1];
3465 __ct.widen(__src, __src + (sizeof(__src)-1), __atoms);
3466 char __nbuf[__bz];
3467 char* __nc = __nbuf;
3468 unique_ptr<char, void(*)(void*)> __h(0, free);
3469 if (__wn - __wb.get() > __bz-2)
3470 {
Howard Hinnant28b24882011-12-01 20:21:04 +00003471 __h.reset((char*)malloc(static_cast<size_t>(__wn - __wb.get() + 2)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003472 if (__h.get() == 0)
3473 __throw_bad_alloc();
3474 __nc = __h.get();
3475 }
3476 if (__neg)
3477 *__nc++ = '-';
3478 for (const char_type* __w = __wb.get(); __w < __wn; ++__w, ++__nc)
Marshall Clow1e68fd42013-03-22 02:14:40 +00003479 *__nc = __src[find(__atoms, _VSTD::end(__atoms), *__w) - __atoms];
Howard Hinnantc51e1022010-05-11 19:42:16 +00003480 *__nc = char();
3481 if (sscanf(__nbuf, "%Lf", &__v) != 1)
3482 __throw_runtime_error("money_get error");
3483 }
3484 if (__b == __e)
3485 __err |= ios_base::eofbit;
3486 return __b;
3487}
3488
3489template <class _CharT, class _InputIterator>
3490_InputIterator
3491money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3492 bool __intl, ios_base& __iob,
3493 ios_base::iostate& __err,
3494 string_type& __v) const
3495{
Howard Hinnant28b24882011-12-01 20:21:04 +00003496 const int __bz = 100;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003497 char_type __wbuf[__bz];
3498 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3499 char_type* __wn;
3500 char_type* __we = __wbuf + __bz;
3501 locale __loc = __iob.getloc();
3502 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3503 bool __neg = false;
3504 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3505 __wb, __wn, __we))
3506 {
3507 __v.clear();
3508 if (__neg)
3509 __v.push_back(__ct.widen('-'));
3510 char_type __z = __ct.widen('0');
3511 char_type* __w;
3512 for (__w = __wb.get(); __w < __wn-1; ++__w)
3513 if (*__w != __z)
3514 break;
3515 __v.append(__w, __wn);
3516 }
3517 if (__b == __e)
3518 __err |= ios_base::eofbit;
3519 return __b;
3520}
3521
Howard Hinnant8ea98242013-08-23 17:37:05 +00003522_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS money_get<char>)
3523_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS money_get<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003524
3525// money_put
3526
3527template <class _CharT>
3528class __money_put
3529{
3530protected:
3531 typedef _CharT char_type;
3532 typedef basic_string<char_type> string_type;
3533
3534 _LIBCPP_ALWAYS_INLINE __money_put() {}
3535
3536 static void __gather_info(bool __intl, bool __neg, const locale& __loc,
3537 money_base::pattern& __pat, char_type& __dp,
3538 char_type& __ts, string& __grp,
3539 string_type& __sym, string_type& __sn,
3540 int& __fd);
3541 static void __format(char_type* __mb, char_type*& __mi, char_type*& __me,
3542 ios_base::fmtflags __flags,
3543 const char_type* __db, const char_type* __de,
3544 const ctype<char_type>& __ct, bool __neg,
3545 const money_base::pattern& __pat, char_type __dp,
3546 char_type __ts, const string& __grp,
3547 const string_type& __sym, const string_type& __sn,
3548 int __fd);
3549};
3550
3551template <class _CharT>
3552void
3553__money_put<_CharT>::__gather_info(bool __intl, bool __neg, const locale& __loc,
3554 money_base::pattern& __pat, char_type& __dp,
3555 char_type& __ts, string& __grp,
3556 string_type& __sym, string_type& __sn,
3557 int& __fd)
3558{
3559 if (__intl)
3560 {
3561 const moneypunct<char_type, true>& __mp =
3562 use_facet<moneypunct<char_type, true> >(__loc);
3563 if (__neg)
3564 {
3565 __pat = __mp.neg_format();
3566 __sn = __mp.negative_sign();
3567 }
3568 else
3569 {
3570 __pat = __mp.pos_format();
3571 __sn = __mp.positive_sign();
3572 }
3573 __dp = __mp.decimal_point();
3574 __ts = __mp.thousands_sep();
3575 __grp = __mp.grouping();
3576 __sym = __mp.curr_symbol();
3577 __fd = __mp.frac_digits();
3578 }
3579 else
3580 {
3581 const moneypunct<char_type, false>& __mp =
3582 use_facet<moneypunct<char_type, false> >(__loc);
3583 if (__neg)
3584 {
3585 __pat = __mp.neg_format();
3586 __sn = __mp.negative_sign();
3587 }
3588 else
3589 {
3590 __pat = __mp.pos_format();
3591 __sn = __mp.positive_sign();
3592 }
3593 __dp = __mp.decimal_point();
3594 __ts = __mp.thousands_sep();
3595 __grp = __mp.grouping();
3596 __sym = __mp.curr_symbol();
3597 __fd = __mp.frac_digits();
3598 }
3599}
3600
3601template <class _CharT>
3602void
3603__money_put<_CharT>::__format(char_type* __mb, char_type*& __mi, char_type*& __me,
3604 ios_base::fmtflags __flags,
3605 const char_type* __db, const char_type* __de,
3606 const ctype<char_type>& __ct, bool __neg,
3607 const money_base::pattern& __pat, char_type __dp,
3608 char_type __ts, const string& __grp,
3609 const string_type& __sym, const string_type& __sn,
3610 int __fd)
3611{
3612 __me = __mb;
3613 for (unsigned __p = 0; __p < 4; ++__p)
3614 {
3615 switch (__pat.field[__p])
3616 {
3617 case money_base::none:
3618 __mi = __me;
3619 break;
3620 case money_base::space:
3621 __mi = __me;
3622 *__me++ = __ct.widen(' ');
3623 break;
3624 case money_base::sign:
3625 if (!__sn.empty())
3626 *__me++ = __sn[0];
3627 break;
3628 case money_base::symbol:
3629 if (!__sym.empty() && (__flags & ios_base::showbase))
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003630 __me = _VSTD::copy(__sym.begin(), __sym.end(), __me);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003631 break;
3632 case money_base::value:
3633 {
3634 // remember start of value so we can reverse it
3635 char_type* __t = __me;
3636 // find beginning of digits
3637 if (__neg)
3638 ++__db;
3639 // find end of digits
3640 const char_type* __d;
3641 for (__d = __db; __d < __de; ++__d)
3642 if (!__ct.is(ctype_base::digit, *__d))
3643 break;
3644 // print fractional part
3645 if (__fd > 0)
3646 {
3647 int __f;
3648 for (__f = __fd; __d > __db && __f > 0; --__f)
3649 *__me++ = *--__d;
3650 char_type __z = __f > 0 ? __ct.widen('0') : char_type();
3651 for (; __f > 0; --__f)
3652 *__me++ = __z;
3653 *__me++ = __dp;
3654 }
3655 // print units part
3656 if (__d == __db)
3657 {
3658 *__me++ = __ct.widen('0');
3659 }
3660 else
3661 {
3662 unsigned __ng = 0;
3663 unsigned __ig = 0;
3664 unsigned __gl = __grp.empty() ? numeric_limits<unsigned>::max()
3665 : static_cast<unsigned>(__grp[__ig]);
3666 while (__d != __db)
3667 {
3668 if (__ng == __gl)
3669 {
3670 *__me++ = __ts;
3671 __ng = 0;
3672 if (++__ig < __grp.size())
3673 __gl = __grp[__ig] == numeric_limits<char>::max() ?
3674 numeric_limits<unsigned>::max() :
3675 static_cast<unsigned>(__grp[__ig]);
3676 }
3677 *__me++ = *--__d;
3678 ++__ng;
3679 }
3680 }
3681 // reverse it
3682 reverse(__t, __me);
3683 }
3684 break;
3685 }
3686 }
3687 // print rest of sign, if any
3688 if (__sn.size() > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003689 __me = _VSTD::copy(__sn.begin()+1, __sn.end(), __me);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003690 // set alignment
3691 if ((__flags & ios_base::adjustfield) == ios_base::left)
3692 __mi = __me;
3693 else if ((__flags & ios_base::adjustfield) != ios_base::internal)
3694 __mi = __mb;
3695}
3696
Howard Hinnant8ea98242013-08-23 17:37:05 +00003697_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS __money_put<char>)
3698_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS __money_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003699
3700template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003701class _LIBCPP_TYPE_VIS_ONLY money_put
Howard Hinnantc51e1022010-05-11 19:42:16 +00003702 : public locale::facet,
3703 private __money_put<_CharT>
3704{
3705public:
3706 typedef _CharT char_type;
3707 typedef _OutputIterator iter_type;
3708 typedef basic_string<char_type> string_type;
3709
3710 _LIBCPP_ALWAYS_INLINE
3711 explicit money_put(size_t __refs = 0)
3712 : locale::facet(__refs) {}
3713
3714 _LIBCPP_ALWAYS_INLINE
3715 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3716 long double __units) const
3717 {
3718 return do_put(__s, __intl, __iob, __fl, __units);
3719 }
3720
3721 _LIBCPP_ALWAYS_INLINE
3722 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3723 const string_type& __digits) const
3724 {
3725 return do_put(__s, __intl, __iob, __fl, __digits);
3726 }
3727
3728 static locale::id id;
3729
3730protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003731 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00003732 ~money_put() {}
3733
3734 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3735 char_type __fl, long double __units) const;
3736 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3737 char_type __fl, const string_type& __digits) const;
3738};
3739
3740template <class _CharT, class _OutputIterator>
3741locale::id
3742money_put<_CharT, _OutputIterator>::id;
3743
3744template <class _CharT, class _OutputIterator>
3745_OutputIterator
3746money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3747 ios_base& __iob, char_type __fl,
3748 long double __units) const
3749{
3750 // convert to char
3751 const size_t __bs = 100;
3752 char __buf[__bs];
3753 char* __bb = __buf;
3754 char_type __digits[__bs];
3755 char_type* __db = __digits;
Howard Hinnant28b24882011-12-01 20:21:04 +00003756 size_t __n = static_cast<size_t>(snprintf(__bb, __bs, "%.0Lf", __units));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003757 unique_ptr<char, void(*)(void*)> __hn(0, free);
3758 unique_ptr<char_type, void(*)(void*)> __hd(0, free);
3759 // secure memory for digit storage
3760 if (__n > __bs-1)
3761 {
Howard Hinnantf312e3e2011-09-28 23:39:33 +00003762#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant28b24882011-12-01 20:21:04 +00003763 __n = static_cast<size_t>(asprintf_l(&__bb, _LIBCPP_GET_C_LOCALE, "%.0Lf", __units));
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00003764#else
3765 __n = __asprintf_l(&__bb, __cloc(), "%.0Lf", __units);
3766#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003767 if (__bb == 0)
3768 __throw_bad_alloc();
3769 __hn.reset(__bb);
3770 __hd.reset((char_type*)malloc(__n * sizeof(char_type)));
Howard Hinnant03de6f92012-03-07 20:37:43 +00003771 if (__hd == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003772 __throw_bad_alloc();
3773 __db = __hd.get();
3774 }
3775 // gather info
3776 locale __loc = __iob.getloc();
3777 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3778 __ct.widen(__bb, __bb + __n, __db);
3779 bool __neg = __n > 0 && __bb[0] == '-';
3780 money_base::pattern __pat;
3781 char_type __dp;
3782 char_type __ts;
3783 string __grp;
3784 string_type __sym;
3785 string_type __sn;
3786 int __fd;
3787 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3788 // secure memory for formatting
3789 char_type __mbuf[__bs];
3790 char_type* __mb = __mbuf;
3791 unique_ptr<char_type, void(*)(void*)> __hw(0, free);
3792 size_t __exn = static_cast<int>(__n) > __fd ?
Howard Hinnant28b24882011-12-01 20:21:04 +00003793 (__n - static_cast<size_t>(__fd)) * 2 + __sn.size() +
3794 __sym.size() + static_cast<size_t>(__fd) + 1
3795 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003796 if (__exn > __bs)
3797 {
3798 __hw.reset((char_type*)malloc(__exn * sizeof(char_type)));
3799 __mb = __hw.get();
3800 if (__mb == 0)
3801 __throw_bad_alloc();
3802 }
3803 // format
3804 char_type* __mi;
3805 char_type* __me;
3806 this->__format(__mb, __mi, __me, __iob.flags(),
3807 __db, __db + __n, __ct,
3808 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3809 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3810}
3811
3812template <class _CharT, class _OutputIterator>
3813_OutputIterator
3814money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3815 ios_base& __iob, char_type __fl,
3816 const string_type& __digits) const
3817{
3818 // gather info
3819 locale __loc = __iob.getloc();
3820 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3821 bool __neg = __digits.size() > 0 && __digits[0] == __ct.widen('-');
3822 money_base::pattern __pat;
3823 char_type __dp;
3824 char_type __ts;
3825 string __grp;
3826 string_type __sym;
3827 string_type __sn;
3828 int __fd;
3829 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3830 // secure memory for formatting
3831 char_type __mbuf[100];
3832 char_type* __mb = __mbuf;
3833 unique_ptr<char_type, void(*)(void*)> __h(0, free);
Howard Hinnant28b24882011-12-01 20:21:04 +00003834 size_t __exn = static_cast<int>(__digits.size()) > __fd ?
3835 (__digits.size() - static_cast<size_t>(__fd)) * 2 +
3836 __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 1
3837 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003838 if (__exn > 100)
3839 {
3840 __h.reset((char_type*)malloc(__exn * sizeof(char_type)));
3841 __mb = __h.get();
3842 if (__mb == 0)
3843 __throw_bad_alloc();
3844 }
3845 // format
3846 char_type* __mi;
3847 char_type* __me;
3848 this->__format(__mb, __mi, __me, __iob.flags(),
3849 __digits.data(), __digits.data() + __digits.size(), __ct,
3850 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3851 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3852}
3853
Howard Hinnant8ea98242013-08-23 17:37:05 +00003854_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS money_put<char>)
3855_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS money_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003856
3857// messages
3858
Howard Hinnant8331b762013-03-06 23:30:19 +00003859class _LIBCPP_TYPE_VIS messages_base
Howard Hinnantc51e1022010-05-11 19:42:16 +00003860{
3861public:
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003862 typedef ptrdiff_t catalog;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003863
3864 _LIBCPP_ALWAYS_INLINE messages_base() {}
3865};
3866
3867template <class _CharT>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003868class _LIBCPP_TYPE_VIS_ONLY messages
Howard Hinnantc51e1022010-05-11 19:42:16 +00003869 : public locale::facet,
3870 public messages_base
3871{
3872public:
3873 typedef _CharT char_type;
3874 typedef basic_string<_CharT> string_type;
3875
3876 _LIBCPP_ALWAYS_INLINE
3877 explicit messages(size_t __refs = 0)
3878 : locale::facet(__refs) {}
3879
3880 _LIBCPP_ALWAYS_INLINE
3881 catalog open(const basic_string<char>& __nm, const locale& __loc) const
3882 {
3883 return do_open(__nm, __loc);
3884 }
3885
3886 _LIBCPP_ALWAYS_INLINE
3887 string_type get(catalog __c, int __set, int __msgid,
3888 const string_type& __dflt) const
3889 {
3890 return do_get(__c, __set, __msgid, __dflt);
3891 }
3892
3893 _LIBCPP_ALWAYS_INLINE
3894 void close(catalog __c) const
3895 {
3896 do_close(__c);
3897 }
3898
3899 static locale::id id;
3900
3901protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003902 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00003903 ~messages() {}
3904
3905 virtual catalog do_open(const basic_string<char>&, const locale&) const;
3906 virtual string_type do_get(catalog, int __set, int __msgid,
3907 const string_type& __dflt) const;
3908 virtual void do_close(catalog) const;
3909};
3910
3911template <class _CharT>
3912locale::id
3913messages<_CharT>::id;
3914
3915template <class _CharT>
3916typename messages<_CharT>::catalog
3917messages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const
3918{
Marshall Clow1f257322013-03-18 17:04:29 +00003919#ifdef _WIN32
Howard Hinnanteb505f62011-09-23 16:11:27 +00003920 return -1;
3921#else // _WIN32
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +00003922 catalog __cat = (catalog)catopen(__nm.c_str(), NL_CAT_LOCALE);
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003923 if (__cat != -1)
3924 __cat = static_cast<catalog>((static_cast<size_t>(__cat) >> 1));
3925 return __cat;
Howard Hinnanteb505f62011-09-23 16:11:27 +00003926#endif // _WIN32
Howard Hinnantc51e1022010-05-11 19:42:16 +00003927}
3928
3929template <class _CharT>
3930typename messages<_CharT>::string_type
3931messages<_CharT>::do_get(catalog __c, int __set, int __msgid,
3932 const string_type& __dflt) const
3933{
Marshall Clow1f257322013-03-18 17:04:29 +00003934#ifdef _WIN32
Howard Hinnanteb505f62011-09-23 16:11:27 +00003935 return __dflt;
3936#else // _WIN32
Howard Hinnantc51e1022010-05-11 19:42:16 +00003937 string __ndflt;
3938 __narrow_to_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__ndflt),
3939 __dflt.c_str(),
3940 __dflt.c_str() + __dflt.size());
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003941 if (__c != -1)
3942 __c <<= 1;
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +00003943 nl_catd __cat = (nl_catd)__c;
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003944 char* __n = catgets(__cat, __set, __msgid, __ndflt.c_str());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003945 string_type __w;
3946 __widen_from_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__w),
3947 __n, __n + strlen(__n));
3948 return __w;
Howard Hinnanteb505f62011-09-23 16:11:27 +00003949#endif // _WIN32
Howard Hinnantc51e1022010-05-11 19:42:16 +00003950}
3951
3952template <class _CharT>
3953void
3954messages<_CharT>::do_close(catalog __c) const
3955{
Marshall Clow1f257322013-03-18 17:04:29 +00003956#if !defined(_WIN32)
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003957 if (__c != -1)
3958 __c <<= 1;
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +00003959 nl_catd __cat = (nl_catd)__c;
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003960 catclose(__cat);
Howard Hinnanteb505f62011-09-23 16:11:27 +00003961#endif // !_WIN32
Howard Hinnantc51e1022010-05-11 19:42:16 +00003962}
3963
Howard Hinnant8ea98242013-08-23 17:37:05 +00003964_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS messages<char>)
3965_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS messages<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003966
3967template <class _CharT>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003968class _LIBCPP_TYPE_VIS_ONLY messages_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00003969 : public messages<_CharT>
3970{
3971public:
3972 typedef messages_base::catalog catalog;
3973 typedef basic_string<_CharT> string_type;
3974
3975 _LIBCPP_ALWAYS_INLINE
3976 explicit messages_byname(const char*, size_t __refs = 0)
3977 : messages<_CharT>(__refs) {}
3978
3979 _LIBCPP_ALWAYS_INLINE
3980 explicit messages_byname(const string&, size_t __refs = 0)
3981 : messages<_CharT>(__refs) {}
3982
3983protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003984 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00003985 ~messages_byname() {}
3986};
3987
Howard Hinnant8ea98242013-08-23 17:37:05 +00003988_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS messages_byname<char>)
3989_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS messages_byname<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003990
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003991template<class _Codecvt, class _Elem = wchar_t,
3992 class _Wide_alloc = allocator<_Elem>,
3993 class _Byte_alloc = allocator<char> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003994class _LIBCPP_TYPE_VIS_ONLY wstring_convert
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003995{
3996public:
3997 typedef basic_string<char, char_traits<char>, _Byte_alloc> byte_string;
3998 typedef basic_string<_Elem, char_traits<_Elem>, _Wide_alloc> wide_string;
3999 typedef typename _Codecvt::state_type state_type;
4000 typedef typename wide_string::traits_type::int_type int_type;
4001
4002private:
4003 byte_string __byte_err_string_;
4004 wide_string __wide_err_string_;
4005 _Codecvt* __cvtptr_;
4006 state_type __cvtstate_;
4007 size_t __cvtcount_;
4008
4009 wstring_convert(const wstring_convert& __wc);
4010 wstring_convert& operator=(const wstring_convert& __wc);
4011public:
4012 wstring_convert(_Codecvt* __pcvt = new _Codecvt);
4013 wstring_convert(_Codecvt* __pcvt, state_type __state);
4014 wstring_convert(const byte_string& __byte_err,
4015 const wide_string& __wide_err = wide_string());
Howard Hinnant74279a52010-09-04 23:28:19 +00004016#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004017 wstring_convert(wstring_convert&& __wc);
4018#endif
4019 ~wstring_convert();
4020
Howard Hinnant756c69b2010-09-22 16:48:34 +00004021 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004022 wide_string from_bytes(char __byte)
4023 {return from_bytes(&__byte, &__byte+1);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004024 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004025 wide_string from_bytes(const char* __ptr)
4026 {return from_bytes(__ptr, __ptr + char_traits<char>::length(__ptr));}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004027 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004028 wide_string from_bytes(const byte_string& __str)
4029 {return from_bytes(__str.data(), __str.data() + __str.size());}
4030 wide_string from_bytes(const char* __first, const char* __last);
4031
Howard Hinnant756c69b2010-09-22 16:48:34 +00004032 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004033 byte_string to_bytes(_Elem __wchar)
4034 {return to_bytes(&__wchar, &__wchar+1);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004035 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004036 byte_string to_bytes(const _Elem* __wptr)
4037 {return to_bytes(__wptr, __wptr + char_traits<_Elem>::length(__wptr));}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004038 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004039 byte_string to_bytes(const wide_string& __wstr)
4040 {return to_bytes(__wstr.data(), __wstr.data() + __wstr.size());}
4041 byte_string to_bytes(const _Elem* __first, const _Elem* __last);
4042
Howard Hinnant756c69b2010-09-22 16:48:34 +00004043 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004044 size_t converted() const {return __cvtcount_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004045 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004046 state_type state() const {return __cvtstate_;}
4047};
4048
4049template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004050inline _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004051wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4052 wstring_convert(_Codecvt* __pcvt)
4053 : __cvtptr_(__pcvt), __cvtstate_(), __cvtcount_(0)
4054{
4055}
4056
4057template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004058inline _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004059wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4060 wstring_convert(_Codecvt* __pcvt, state_type __state)
4061 : __cvtptr_(__pcvt), __cvtstate_(__state), __cvtcount_(0)
4062{
4063}
4064
4065template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
4066wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4067 wstring_convert(const byte_string& __byte_err, const wide_string& __wide_err)
4068 : __byte_err_string_(__byte_err), __wide_err_string_(__wide_err),
4069 __cvtstate_(), __cvtcount_(0)
4070{
4071 __cvtptr_ = new _Codecvt;
4072}
4073
Howard Hinnant74279a52010-09-04 23:28:19 +00004074#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004075
4076template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004077inline _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004078wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4079 wstring_convert(wstring_convert&& __wc)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004080 : __byte_err_string_(_VSTD::move(__wc.__byte_err_string_)),
4081 __wide_err_string_(_VSTD::move(__wc.__wide_err_string_)),
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004082 __cvtptr_(__wc.__cvtptr_),
4083 __cvtstate_(__wc.__cvtstate_), __cvtcount_(__wc.__cvtstate_)
4084{
4085 __wc.__cvtptr_ = nullptr;
4086}
4087
Howard Hinnant5dc89112010-09-04 23:46:48 +00004088#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004089
4090template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
4091wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::~wstring_convert()
4092{
4093 delete __cvtptr_;
4094}
4095
4096template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
4097typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::wide_string
4098wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4099 from_bytes(const char* __frm, const char* __frm_end)
4100{
4101 __cvtcount_ = 0;
4102 if (__cvtptr_ != nullptr)
4103 {
4104 wide_string __ws(2*(__frm_end - __frm), _Elem());
Howard Hinnant16d54f22012-07-12 18:07:41 +00004105 if (__frm != __frm_end)
4106 __ws.resize(__ws.capacity());
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004107 codecvt_base::result __r = codecvt_base::ok;
4108 state_type __st = __cvtstate_;
4109 if (__frm != __frm_end)
4110 {
4111 _Elem* __to = &__ws[0];
4112 _Elem* __to_end = __to + __ws.size();
4113 const char* __frm_nxt;
4114 do
4115 {
4116 _Elem* __to_nxt;
4117 __r = __cvtptr_->in(__st, __frm, __frm_end, __frm_nxt,
4118 __to, __to_end, __to_nxt);
4119 __cvtcount_ += __frm_nxt - __frm;
4120 if (__frm_nxt == __frm)
4121 {
4122 __r = codecvt_base::error;
4123 }
4124 else if (__r == codecvt_base::noconv)
4125 {
4126 __ws.resize(__to - &__ws[0]);
4127 // This only gets executed if _Elem is char
4128 __ws.append((const _Elem*)__frm, (const _Elem*)__frm_end);
4129 __frm = __frm_nxt;
4130 __r = codecvt_base::ok;
4131 }
4132 else if (__r == codecvt_base::ok)
4133 {
4134 __ws.resize(__to_nxt - &__ws[0]);
4135 __frm = __frm_nxt;
4136 }
4137 else if (__r == codecvt_base::partial)
4138 {
4139 ptrdiff_t __s = __to_nxt - &__ws[0];
4140 __ws.resize(2 * __s);
4141 __to = &__ws[0] + __s;
4142 __to_end = &__ws[0] + __ws.size();
4143 __frm = __frm_nxt;
4144 }
4145 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
4146 }
4147 if (__r == codecvt_base::ok)
4148 return __ws;
4149 }
Howard Hinnant72f73582010-08-11 17:04:31 +00004150#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004151 if (__wide_err_string_.empty())
4152 throw range_error("wstring_convert: from_bytes error");
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004153#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004154 return __wide_err_string_;
4155}
4156
4157template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
4158typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::byte_string
4159wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4160 to_bytes(const _Elem* __frm, const _Elem* __frm_end)
4161{
4162 __cvtcount_ = 0;
4163 if (__cvtptr_ != nullptr)
4164 {
4165 byte_string __bs(2*(__frm_end - __frm), char());
Howard Hinnant16d54f22012-07-12 18:07:41 +00004166 if (__frm != __frm_end)
4167 __bs.resize(__bs.capacity());
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004168 codecvt_base::result __r = codecvt_base::ok;
4169 state_type __st = __cvtstate_;
4170 if (__frm != __frm_end)
4171 {
4172 char* __to = &__bs[0];
4173 char* __to_end = __to + __bs.size();
4174 const _Elem* __frm_nxt;
4175 do
4176 {
4177 char* __to_nxt;
4178 __r = __cvtptr_->out(__st, __frm, __frm_end, __frm_nxt,
4179 __to, __to_end, __to_nxt);
4180 __cvtcount_ += __frm_nxt - __frm;
4181 if (__frm_nxt == __frm)
4182 {
4183 __r = codecvt_base::error;
4184 }
4185 else if (__r == codecvt_base::noconv)
4186 {
4187 __bs.resize(__to - &__bs[0]);
4188 // This only gets executed if _Elem is char
4189 __bs.append((const char*)__frm, (const char*)__frm_end);
4190 __frm = __frm_nxt;
4191 __r = codecvt_base::ok;
4192 }
4193 else if (__r == codecvt_base::ok)
4194 {
4195 __bs.resize(__to_nxt - &__bs[0]);
4196 __frm = __frm_nxt;
4197 }
4198 else if (__r == codecvt_base::partial)
4199 {
4200 ptrdiff_t __s = __to_nxt - &__bs[0];
4201 __bs.resize(2 * __s);
4202 __to = &__bs[0] + __s;
4203 __to_end = &__bs[0] + __bs.size();
4204 __frm = __frm_nxt;
4205 }
4206 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
4207 }
4208 if (__r == codecvt_base::ok)
4209 {
4210 size_t __s = __bs.size();
4211 __bs.resize(__bs.capacity());
4212 char* __to = &__bs[0] + __s;
4213 char* __to_end = __to + __bs.size();
4214 do
4215 {
4216 char* __to_nxt;
4217 __r = __cvtptr_->unshift(__st, __to, __to_end, __to_nxt);
4218 if (__r == codecvt_base::noconv)
4219 {
4220 __bs.resize(__to - &__bs[0]);
4221 __r = codecvt_base::ok;
4222 }
4223 else if (__r == codecvt_base::ok)
4224 {
4225 __bs.resize(__to_nxt - &__bs[0]);
4226 }
4227 else if (__r == codecvt_base::partial)
4228 {
Howard Hinnant28b24882011-12-01 20:21:04 +00004229 ptrdiff_t __sp = __to_nxt - &__bs[0];
4230 __bs.resize(2 * __sp);
4231 __to = &__bs[0] + __sp;
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004232 __to_end = &__bs[0] + __bs.size();
4233 }
4234 } while (__r == codecvt_base::partial);
4235 if (__r == codecvt_base::ok)
4236 return __bs;
4237 }
4238 }
Howard Hinnant72f73582010-08-11 17:04:31 +00004239#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004240 if (__byte_err_string_.empty())
4241 throw range_error("wstring_convert: to_bytes error");
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004242#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004243 return __byte_err_string_;
4244}
4245
4246template <class _Codecvt, class _Elem = wchar_t, class _Tr = char_traits<_Elem> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004247class _LIBCPP_TYPE_VIS_ONLY wbuffer_convert
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004248 : public basic_streambuf<_Elem, _Tr>
4249{
4250public:
4251 // types:
4252 typedef _Elem char_type;
4253 typedef _Tr traits_type;
4254 typedef typename traits_type::int_type int_type;
4255 typedef typename traits_type::pos_type pos_type;
4256 typedef typename traits_type::off_type off_type;
4257 typedef typename _Codecvt::state_type state_type;
4258
4259private:
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004260 char* __extbuf_;
4261 const char* __extbufnext_;
4262 const char* __extbufend_;
4263 char __extbuf_min_[8];
4264 size_t __ebs_;
4265 char_type* __intbuf_;
4266 size_t __ibs_;
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004267 streambuf* __bufptr_;
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004268 _Codecvt* __cv_;
4269 state_type __st_;
4270 ios_base::openmode __cm_;
4271 bool __owns_eb_;
4272 bool __owns_ib_;
4273 bool __always_noconv_;
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004274
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004275 wbuffer_convert(const wbuffer_convert&);
4276 wbuffer_convert& operator=(const wbuffer_convert&);
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004277public:
4278 wbuffer_convert(streambuf* __bytebuf = 0, _Codecvt* __pcvt = new _Codecvt,
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004279 state_type __state = state_type());
4280 ~wbuffer_convert();
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004281
Howard Hinnant756c69b2010-09-22 16:48:34 +00004282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004283 streambuf* rdbuf() const {return __bufptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004285 streambuf* rdbuf(streambuf* __bytebuf)
4286 {
4287 streambuf* __r = __bufptr_;
4288 __bufptr_ = __bytebuf;
4289 return __r;
4290 }
4291
Howard Hinnant756c69b2010-09-22 16:48:34 +00004292 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004293 state_type state() const {return __st_;}
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004294
4295protected:
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004296 virtual int_type underflow();
4297 virtual int_type pbackfail(int_type __c = traits_type::eof());
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004298 virtual int_type overflow (int_type __c = traits_type::eof());
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004299 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s,
4300 streamsize __n);
4301 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
4302 ios_base::openmode __wch = ios_base::in | ios_base::out);
4303 virtual pos_type seekpos(pos_type __sp,
4304 ios_base::openmode __wch = ios_base::in | ios_base::out);
4305 virtual int sync();
4306
4307private:
4308 bool __read_mode();
4309 void __write_mode();
4310 wbuffer_convert* __close();
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004311};
4312
4313template <class _Codecvt, class _Elem, class _Tr>
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004314wbuffer_convert<_Codecvt, _Elem, _Tr>::
4315 wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt, state_type __state)
4316 : __extbuf_(0),
4317 __extbufnext_(0),
4318 __extbufend_(0),
4319 __ebs_(0),
4320 __intbuf_(0),
4321 __ibs_(0),
4322 __bufptr_(__bytebuf),
4323 __cv_(__pcvt),
4324 __st_(__state),
4325 __cm_(0),
4326 __owns_eb_(false),
4327 __owns_ib_(false),
4328 __always_noconv_(__cv_ ? __cv_->always_noconv() : false)
4329{
4330 setbuf(0, 4096);
4331}
4332
4333template <class _Codecvt, class _Elem, class _Tr>
4334wbuffer_convert<_Codecvt, _Elem, _Tr>::~wbuffer_convert()
4335{
4336 __close();
4337 delete __cv_;
4338 if (__owns_eb_)
4339 delete [] __extbuf_;
4340 if (__owns_ib_)
4341 delete [] __intbuf_;
4342}
4343
4344template <class _Codecvt, class _Elem, class _Tr>
4345typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4346wbuffer_convert<_Codecvt, _Elem, _Tr>::underflow()
4347{
4348 if (__cv_ == 0 || __bufptr_ == 0)
4349 return traits_type::eof();
4350 bool __initial = __read_mode();
4351 char_type __1buf;
4352 if (this->gptr() == 0)
4353 this->setg(&__1buf, &__1buf+1, &__1buf+1);
4354 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
4355 int_type __c = traits_type::eof();
4356 if (this->gptr() == this->egptr())
4357 {
4358 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
4359 if (__always_noconv_)
4360 {
4361 streamsize __nmemb = static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz);
4362 __nmemb = __bufptr_->sgetn((char*)this->eback() + __unget_sz, __nmemb);
4363 if (__nmemb != 0)
4364 {
4365 this->setg(this->eback(),
4366 this->eback() + __unget_sz,
4367 this->eback() + __unget_sz + __nmemb);
4368 __c = *this->gptr();
4369 }
4370 }
4371 else
4372 {
4373 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
4374 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
4375 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004376 streamsize __nmemb = _VSTD::min(static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz),
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004377 static_cast<streamsize>(__extbufend_ - __extbufnext_));
4378 codecvt_base::result __r;
4379 state_type __svs = __st_;
4380 streamsize __nr = __bufptr_->sgetn(const_cast<char*>(__extbufnext_), __nmemb);
4381 if (__nr != 0)
4382 {
4383 __extbufend_ = __extbufnext_ + __nr;
4384 char_type* __inext;
4385 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
4386 this->eback() + __unget_sz,
4387 this->egptr(), __inext);
4388 if (__r == codecvt_base::noconv)
4389 {
4390 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)__extbufend_);
4391 __c = *this->gptr();
4392 }
4393 else if (__inext != this->eback() + __unget_sz)
4394 {
4395 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
4396 __c = *this->gptr();
4397 }
4398 }
4399 }
4400 }
4401 else
4402 __c = *this->gptr();
4403 if (this->eback() == &__1buf)
4404 this->setg(0, 0, 0);
4405 return __c;
4406}
4407
4408template <class _Codecvt, class _Elem, class _Tr>
4409typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4410wbuffer_convert<_Codecvt, _Elem, _Tr>::pbackfail(int_type __c)
4411{
4412 if (__cv_ != 0 && __bufptr_ != 0 && this->eback() < this->gptr())
4413 {
4414 if (traits_type::eq_int_type(__c, traits_type::eof()))
4415 {
4416 this->gbump(-1);
4417 return traits_type::not_eof(__c);
4418 }
4419 if (traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
4420 {
4421 this->gbump(-1);
4422 *this->gptr() = traits_type::to_char_type(__c);
4423 return __c;
4424 }
4425 }
4426 return traits_type::eof();
4427}
4428
4429template <class _Codecvt, class _Elem, class _Tr>
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004430typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4431wbuffer_convert<_Codecvt, _Elem, _Tr>::overflow(int_type __c)
4432{
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004433 if (__cv_ == 0 || __bufptr_ == 0)
4434 return traits_type::eof();
4435 __write_mode();
4436 char_type __1buf;
4437 char_type* __pb_save = this->pbase();
4438 char_type* __epb_save = this->epptr();
4439 if (!traits_type::eq_int_type(__c, traits_type::eof()))
4440 {
4441 if (this->pptr() == 0)
4442 this->setp(&__1buf, &__1buf+1);
4443 *this->pptr() = traits_type::to_char_type(__c);
4444 this->pbump(1);
4445 }
4446 if (this->pptr() != this->pbase())
4447 {
4448 if (__always_noconv_)
4449 {
4450 streamsize __nmemb = static_cast<streamsize>(this->pptr() - this->pbase());
4451 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4452 return traits_type::eof();
4453 }
4454 else
4455 {
4456 char* __extbe = __extbuf_;
4457 codecvt_base::result __r;
4458 do
4459 {
4460 const char_type* __e;
4461 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
4462 __extbuf_, __extbuf_ + __ebs_, __extbe);
4463 if (__e == this->pbase())
4464 return traits_type::eof();
4465 if (__r == codecvt_base::noconv)
4466 {
4467 streamsize __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
4468 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4469 return traits_type::eof();
4470 }
4471 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
4472 {
4473 streamsize __nmemb = static_cast<size_t>(__extbe - __extbuf_);
4474 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4475 return traits_type::eof();
4476 if (__r == codecvt_base::partial)
4477 {
4478 this->setp((char_type*)__e, this->pptr());
4479 this->pbump(this->epptr() - this->pbase());
4480 }
4481 }
4482 else
4483 return traits_type::eof();
4484 } while (__r == codecvt_base::partial);
4485 }
4486 this->setp(__pb_save, __epb_save);
4487 }
4488 return traits_type::not_eof(__c);
4489}
4490
4491template <class _Codecvt, class _Elem, class _Tr>
4492basic_streambuf<_Elem, _Tr>*
4493wbuffer_convert<_Codecvt, _Elem, _Tr>::setbuf(char_type* __s, streamsize __n)
4494{
4495 this->setg(0, 0, 0);
4496 this->setp(0, 0);
4497 if (__owns_eb_)
4498 delete [] __extbuf_;
4499 if (__owns_ib_)
4500 delete [] __intbuf_;
4501 __ebs_ = __n;
4502 if (__ebs_ > sizeof(__extbuf_min_))
4503 {
4504 if (__always_noconv_ && __s)
4505 {
4506 __extbuf_ = (char*)__s;
4507 __owns_eb_ = false;
4508 }
4509 else
4510 {
4511 __extbuf_ = new char[__ebs_];
4512 __owns_eb_ = true;
4513 }
4514 }
4515 else
4516 {
4517 __extbuf_ = __extbuf_min_;
4518 __ebs_ = sizeof(__extbuf_min_);
4519 __owns_eb_ = false;
4520 }
4521 if (!__always_noconv_)
4522 {
4523 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
4524 if (__s && __ibs_ >= sizeof(__extbuf_min_))
4525 {
4526 __intbuf_ = __s;
4527 __owns_ib_ = false;
4528 }
4529 else
4530 {
4531 __intbuf_ = new char_type[__ibs_];
4532 __owns_ib_ = true;
4533 }
4534 }
4535 else
4536 {
4537 __ibs_ = 0;
4538 __intbuf_ = 0;
4539 __owns_ib_ = false;
4540 }
4541 return this;
4542}
4543
4544template <class _Codecvt, class _Elem, class _Tr>
4545typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4546wbuffer_convert<_Codecvt, _Elem, _Tr>::seekoff(off_type __off, ios_base::seekdir __way,
4547 ios_base::openmode __om)
4548{
4549 int __width = __cv_->encoding();
4550 if (__cv_ == 0 || __bufptr_ == 0 || (__width <= 0 && __off != 0) || sync())
4551 return pos_type(off_type(-1));
4552 // __width > 0 || __off == 0
4553 switch (__way)
4554 {
4555 case ios_base::beg:
4556 break;
4557 case ios_base::cur:
4558 break;
4559 case ios_base::end:
4560 break;
4561 default:
4562 return pos_type(off_type(-1));
4563 }
4564 pos_type __r = __bufptr_->pubseekoff(__width * __off, __way, __om);
4565 __r.state(__st_);
4566 return __r;
4567}
4568
4569template <class _Codecvt, class _Elem, class _Tr>
4570typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4571wbuffer_convert<_Codecvt, _Elem, _Tr>::seekpos(pos_type __sp, ios_base::openmode __wch)
4572{
4573 if (__cv_ == 0 || __bufptr_ == 0 || sync())
4574 return pos_type(off_type(-1));
4575 if (__bufptr_->pubseekpos(__sp, __wch) == pos_type(off_type(-1)))
4576 return pos_type(off_type(-1));
4577 return __sp;
4578}
4579
4580template <class _Codecvt, class _Elem, class _Tr>
4581int
4582wbuffer_convert<_Codecvt, _Elem, _Tr>::sync()
4583{
4584 if (__cv_ == 0 || __bufptr_ == 0)
4585 return 0;
4586 if (__cm_ & ios_base::out)
4587 {
4588 if (this->pptr() != this->pbase())
4589 if (overflow() == traits_type::eof())
4590 return -1;
4591 codecvt_base::result __r;
4592 do
4593 {
4594 char* __extbe;
4595 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
4596 streamsize __nmemb = static_cast<streamsize>(__extbe - __extbuf_);
4597 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4598 return -1;
4599 } while (__r == codecvt_base::partial);
4600 if (__r == codecvt_base::error)
4601 return -1;
4602 if (__bufptr_->pubsync())
4603 return -1;
4604 }
4605 else if (__cm_ & ios_base::in)
4606 {
4607 off_type __c;
4608 if (__always_noconv_)
4609 __c = this->egptr() - this->gptr();
4610 else
4611 {
4612 int __width = __cv_->encoding();
4613 __c = __extbufend_ - __extbufnext_;
4614 if (__width > 0)
4615 __c += __width * (this->egptr() - this->gptr());
4616 else
4617 {
4618 if (this->gptr() != this->egptr())
4619 {
4620 reverse(this->gptr(), this->egptr());
4621 codecvt_base::result __r;
4622 const char_type* __e = this->gptr();
4623 char* __extbe;
4624 do
4625 {
4626 __r = __cv_->out(__st_, __e, this->egptr(), __e,
4627 __extbuf_, __extbuf_ + __ebs_, __extbe);
4628 switch (__r)
4629 {
4630 case codecvt_base::noconv:
4631 __c += this->egptr() - this->gptr();
4632 break;
4633 case codecvt_base::ok:
4634 case codecvt_base::partial:
4635 __c += __extbe - __extbuf_;
4636 break;
4637 default:
4638 return -1;
4639 }
4640 } while (__r == codecvt_base::partial);
4641 }
4642 }
4643 }
4644 if (__bufptr_->pubseekoff(-__c, ios_base::cur, __cm_) == pos_type(off_type(-1)))
4645 return -1;
4646 this->setg(0, 0, 0);
4647 __cm_ = 0;
4648 }
4649 return 0;
4650}
4651
4652template <class _Codecvt, class _Elem, class _Tr>
4653bool
4654wbuffer_convert<_Codecvt, _Elem, _Tr>::__read_mode()
4655{
4656 if (!(__cm_ & ios_base::in))
4657 {
4658 this->setp(0, 0);
4659 if (__always_noconv_)
4660 this->setg((char_type*)__extbuf_,
4661 (char_type*)__extbuf_ + __ebs_,
4662 (char_type*)__extbuf_ + __ebs_);
4663 else
4664 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
4665 __cm_ = ios_base::in;
4666 return true;
4667 }
4668 return false;
4669}
4670
4671template <class _Codecvt, class _Elem, class _Tr>
4672void
4673wbuffer_convert<_Codecvt, _Elem, _Tr>::__write_mode()
4674{
4675 if (!(__cm_ & ios_base::out))
4676 {
4677 this->setg(0, 0, 0);
4678 if (__ebs_ > sizeof(__extbuf_min_))
4679 {
4680 if (__always_noconv_)
4681 this->setp((char_type*)__extbuf_,
4682 (char_type*)__extbuf_ + (__ebs_ - 1));
4683 else
4684 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
4685 }
4686 else
4687 this->setp(0, 0);
4688 __cm_ = ios_base::out;
4689 }
4690}
4691
4692template <class _Codecvt, class _Elem, class _Tr>
4693wbuffer_convert<_Codecvt, _Elem, _Tr>*
4694wbuffer_convert<_Codecvt, _Elem, _Tr>::__close()
4695{
4696 wbuffer_convert* __rt = 0;
4697 if (__cv_ != 0 && __bufptr_ != 0)
4698 {
4699 __rt = this;
4700 if ((__cm_ & ios_base::out) && sync())
4701 __rt = 0;
4702 }
4703 return __rt;
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004704}
4705
Howard Hinnantc51e1022010-05-11 19:42:16 +00004706_LIBCPP_END_NAMESPACE_STD
4707
4708#endif // _LIBCPP_LOCALE