blob: 4dd7737c0a414a298d59d7465ee1def74d95a91a [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- locale ------------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_LOCALE
12#define _LIBCPP_LOCALE
13
14/*
15 locale synopsis
16
17namespace std
18{
19
20class locale
21{
22public:
23 // types:
24 class facet;
25 class id;
26
27 typedef int category;
28 static const category // values assigned here are for exposition only
29 none = 0x000,
30 collate = 0x010,
31 ctype = 0x020,
32 monetary = 0x040,
33 numeric = 0x080,
34 time = 0x100,
35 messages = 0x200,
36 all = collate | ctype | monetary | numeric | time | messages;
37
38 // construct/copy/destroy:
Howard Hinnant7c9e5732011-05-31 15:34:58 +000039 locale() noexcept;
40 locale(const locale& other) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000041 explicit locale(const char* std_name);
42 explicit locale(const string& std_name);
43 locale(const locale& other, const char* std_name, category);
44 locale(const locale& other, const string& std_name, category);
45 template <class Facet> locale(const locale& other, Facet* f);
46 locale(const locale& other, const locale& one, category);
47
Howard Hinnant7c9e5732011-05-31 15:34:58 +000048 ~locale(); // not virtual
Howard Hinnantc51e1022010-05-11 19:42:16 +000049
Howard Hinnant7c9e5732011-05-31 15:34:58 +000050 const locale& operator=(const locale& other) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000051
52 template <class Facet> locale combine(const locale& other) const;
53
54 // locale operations:
55 basic_string<char> name() const;
56 bool operator==(const locale& other) const;
57 bool operator!=(const locale& other) const;
58 template <class charT, class Traits, class Allocator>
59 bool operator()(const basic_string<charT,Traits,Allocator>& s1,
60 const basic_string<charT,Traits,Allocator>& s2) const;
61
62 // global locale objects:
63 static locale global(const locale&);
64 static const locale& classic();
65};
66
67template <class Facet> const Facet& use_facet(const locale&);
Howard Hinnant7c9e5732011-05-31 15:34:58 +000068template <class Facet> bool has_facet(const locale&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000069
70// 22.3.3, convenience interfaces:
71template <class charT> bool isspace (charT c, const locale& loc);
72template <class charT> bool isprint (charT c, const locale& loc);
73template <class charT> bool iscntrl (charT c, const locale& loc);
74template <class charT> bool isupper (charT c, const locale& loc);
75template <class charT> bool islower (charT c, const locale& loc);
76template <class charT> bool isalpha (charT c, const locale& loc);
77template <class charT> bool isdigit (charT c, const locale& loc);
78template <class charT> bool ispunct (charT c, const locale& loc);
79template <class charT> bool isxdigit(charT c, const locale& loc);
80template <class charT> bool isalnum (charT c, const locale& loc);
81template <class charT> bool isgraph (charT c, const locale& loc);
82template <class charT> charT toupper(charT c, const locale& loc);
83template <class charT> charT tolower(charT c, const locale& loc);
Howard Hinnant9dd7e892010-05-31 20:58:54 +000084
85template<class Codecvt, class Elem = wchar_t,
86 class Wide_alloc = allocator<Elem>,
87 class Byte_alloc = allocator<char>>
88class wstring_convert
89{
90public:
91 typedef basic_string<char, char_traits<char>, Byte_alloc> byte_string;
92 typedef basic_string<Elem, char_traits<Elem>, Wide_alloc> wide_string;
93 typedef typename Codecvt::state_type state_type;
94 typedef typename wide_string::traits_type::int_type int_type;
95
Marshall Clowccaa0fb2013-08-27 20:18:59 +000096 explicit wstring_convert(Codecvt* pcvt = new Codecvt); // explicit in C++14
Howard Hinnant9dd7e892010-05-31 20:58:54 +000097 wstring_convert(Codecvt* pcvt, state_type state);
Marshall Clowccaa0fb2013-08-27 20:18:59 +000098 explicit wstring_convert(const byte_string& byte_err, // explicit in C++14
Howard Hinnant9dd7e892010-05-31 20:58:54 +000099 const wide_string& wide_err = wide_string());
Marshall Clowccaa0fb2013-08-27 20:18:59 +0000100 wstring_convert(const wstring_convert&) = delete; // C++14
101 wstring_convert & operator=(const wstring_convert &) = delete; // C++14
Howard Hinnant9dd7e892010-05-31 20:58:54 +0000102 ~wstring_convert();
103
104 wide_string from_bytes(char byte);
105 wide_string from_bytes(const char* ptr);
106 wide_string from_bytes(const byte_string& str);
107 wide_string from_bytes(const char* first, const char* last);
108
109 byte_string to_bytes(Elem wchar);
110 byte_string to_bytes(const Elem* wptr);
111 byte_string to_bytes(const wide_string& wstr);
112 byte_string to_bytes(const Elem* first, const Elem* last);
113
Marshall Clowccaa0fb2013-08-27 20:18:59 +0000114 size_t converted() const; // noexcept in C++14
Howard Hinnant9dd7e892010-05-31 20:58:54 +0000115 state_type state() const;
116};
117
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118template <class Codecvt, class Elem = wchar_t, class Tr = char_traits<Elem>>
Howard Hinnant9dd7e892010-05-31 20:58:54 +0000119class wbuffer_convert
120 : public basic_streambuf<Elem, Tr>
121{
122public:
123 typedef typename Tr::state_type state_type;
124
Marshall Clowccaa0fb2013-08-27 20:18:59 +0000125 explicit wbuffer_convert(streambuf* bytebuf = 0, Codecvt* pcvt = new Codecvt,
126 state_type state = state_type()); // explicit in C++14
127 wbuffer_convert(const wbuffer_convert&) = delete; // C++14
128 wbuffer_convert & operator=(const wbuffer_convert &) = delete; // C++14
129 ~wbuffer_convert(); // C++14
130
Howard Hinnant9dd7e892010-05-31 20:58:54 +0000131 streambuf* rdbuf() const;
132 streambuf* rdbuf(streambuf* bytebuf);
133
134 state_type state() const;
135};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000136
137// 22.4.1 and 22.4.1.3, ctype:
138class ctype_base;
139template <class charT> class ctype;
140template <> class ctype<char>; // specialization
141template <class charT> class ctype_byname;
142template <> class ctype_byname<char>; // specialization
143
144class codecvt_base;
145template <class internT, class externT, class stateT> class codecvt;
146template <class internT, class externT, class stateT> class codecvt_byname;
147
148// 22.4.2 and 22.4.3, numeric:
149template <class charT, class InputIterator> class num_get;
150template <class charT, class OutputIterator> class num_put;
151template <class charT> class numpunct;
152template <class charT> class numpunct_byname;
153
154// 22.4.4, col lation:
155template <class charT> class collate;
156template <class charT> class collate_byname;
157
158// 22.4.5, date and time:
159class time_base;
160template <class charT, class InputIterator> class time_get;
161template <class charT, class InputIterator> class time_get_byname;
162template <class charT, class OutputIterator> class time_put;
163template <class charT, class OutputIterator> class time_put_byname;
164
165// 22.4.6, money:
166class money_base;
167template <class charT, class InputIterator> class money_get;
168template <class charT, class OutputIterator> class money_put;
169template <class charT, bool Intl> class moneypunct;
170template <class charT, bool Intl> class moneypunct_byname;
171
172// 22.4.7, message retrieval:
173class messages_base;
174template <class charT> class messages;
175template <class charT> class messages_byname;
176
177} // std
178
179*/
180
181#include <__config>
182#include <__locale>
183#include <algorithm>
184#include <memory>
185#include <ios>
186#include <streambuf>
187#include <iterator>
188#include <limits>
Marshall Clowdde4bfe2013-03-18 17:45:34 +0000189#ifndef __APPLE__
Howard Hinnant155c2af2010-05-24 17:49:41 +0000190#include <cstdarg>
191#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000192#include <cstdlib>
193#include <ctime>
Howard Hinnant8ad70912013-09-17 01:34:47 +0000194#if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
Howard Hinnantae0f80b2011-09-29 20:33:10 +0000195#include <support/win32/locale_win32.h>
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000196#else // _LIBCPP_MSVCRT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000197#include <nl_types.h>
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000198#endif // !_LIBCPP_MSVCRT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000199
Marshall Clowdde4bfe2013-03-18 17:45:34 +0000200#ifdef __APPLE__
Howard Hinnant48fd5d52012-11-14 21:17:15 +0000201#include <Availability.h>
202#endif
203
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000204#include <__undef_min_max>
205
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000206#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000208#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000209
210_LIBCPP_BEGIN_NAMESPACE_STD
211
Marshall Clow82378c02013-03-18 19:34:07 +0000212#if defined(__APPLE__) || defined(__FreeBSD__)
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000213# define _LIBCPP_GET_C_LOCALE 0
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000214#elif defined(__NetBSD__)
215# define _LIBCPP_GET_C_LOCALE LC_C_LOCALE
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000216#else
217# define _LIBCPP_GET_C_LOCALE __cloc()
Howard Hinnantf312e3e2011-09-28 23:39:33 +0000218 // Get the C locale object
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000219 _LIBCPP_FUNC_VIS locale_t __cloc();
Howard Hinnantf312e3e2011-09-28 23:39:33 +0000220#define __cloc_defined
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000221#endif
222
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000223typedef _VSTD::remove_pointer<locale_t>::type __locale_struct;
224typedef _VSTD::unique_ptr<__locale_struct, decltype(&freelocale)> __locale_unique_ptr;
David Chisnall4d958ed2012-02-29 13:00:07 +0000225#ifndef _LIBCPP_LOCALE__L_EXTENSIONS
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000226typedef _VSTD::unique_ptr<__locale_struct, decltype(&uselocale)> __locale_raii;
David Chisnall4d958ed2012-02-29 13:00:07 +0000227#endif
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000228
Howard Hinnant155c2af2010-05-24 17:49:41 +0000229// OSX has nice foo_l() functions that let you turn off use of the global
230// locale. Linux, not so much. The following functions avoid the locale when
231// that's possible and otherwise do the wrong thing. FIXME.
Howard Hinnantea382952013-08-14 18:00:20 +0000232#if defined(__linux__) || defined(EMSCRIPTEN) || defined(_AIX)
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000233
234#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
235decltype(MB_CUR_MAX_L(_VSTD::declval<locale_t>()))
Howard Hinnant756c69b2010-09-22 16:48:34 +0000236inline _LIBCPP_INLINE_VISIBILITY
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000237__mb_cur_max_l(locale_t __l)
Howard Hinnant155c2af2010-05-24 17:49:41 +0000238{
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000239 return MB_CUR_MAX_L(__l);
240}
241#else // _LIBCPP_LOCALE__L_EXTENSIONS
242_LIBCPP_ALWAYS_INLINE inline
243decltype(MB_CUR_MAX) __mb_cur_max_l(locale_t __l)
244{
245 __locale_raii __current(uselocale(__l), uselocale);
246 return MB_CUR_MAX;
247}
248#endif // _LIBCPP_LOCALE__L_EXTENSIONS
249
250_LIBCPP_ALWAYS_INLINE inline
251wint_t __btowc_l(int __c, locale_t __l)
252{
Howard Hinnantf312e3e2011-09-28 23:39:33 +0000253#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000254 return btowc_l(__c, __l);
255#else
256 __locale_raii __current(uselocale(__l), uselocale);
257 return btowc(__c);
258#endif
Alexis Huntd4a24912011-07-13 06:40:50 +0000259}
Howard Hinnant2de5c472011-07-13 15:48:16 +0000260
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000261_LIBCPP_ALWAYS_INLINE inline
262int __wctob_l(wint_t __c, locale_t __l)
Alexis Huntd4a24912011-07-13 06:40:50 +0000263{
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000264#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
265 return wctob_l(__c, __l);
266#else
267 __locale_raii __current(uselocale(__l), uselocale);
268 return wctob(__c);
269#endif
Alexis Huntd4a24912011-07-13 06:40:50 +0000270}
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000271
272_LIBCPP_ALWAYS_INLINE inline
273size_t __wcsnrtombs_l(char *__dest, const wchar_t **__src, size_t __nwc,
274 size_t __len, mbstate_t *__ps, locale_t __l)
275{
276#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
277 return wcsnrtombs_l(__dest, __src, __nwc, __len, __ps, __l);
278#else
279 __locale_raii __current(uselocale(__l), uselocale);
280 return wcsnrtombs(__dest, __src, __nwc, __len, __ps);
281#endif
282}
283
284_LIBCPP_ALWAYS_INLINE inline
285size_t __wcrtomb_l(char *__s, wchar_t __wc, mbstate_t *__ps, locale_t __l)
286{
287#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
288 return wcrtomb_l(__s, __wc, __ps, __l);
289#else
290 __locale_raii __current(uselocale(__l), uselocale);
291 return wcrtomb(__s, __wc, __ps);
292#endif
293}
294
295_LIBCPP_ALWAYS_INLINE inline
296size_t __mbsnrtowcs_l(wchar_t * __dest, const char **__src, size_t __nms,
297 size_t __len, mbstate_t *__ps, locale_t __l)
298{
299#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
David Chisnall1d581062011-09-21 08:39:44 +0000300 return mbsnrtowcs_l(__dest, __src, __nms, __len, __ps, __l);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000301#else
302 __locale_raii __current(uselocale(__l), uselocale);
303 return mbsnrtowcs(__dest, __src, __nms, __len, __ps);
304#endif
305}
306
307_LIBCPP_ALWAYS_INLINE inline
308size_t __mbrtowc_l(wchar_t *__pwc, const char *__s, size_t __n,
309 mbstate_t *__ps, locale_t __l)
310{
311#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
312 return mbrtowc_l(__pwc, __s, __n, __ps, __l);
313#else
314 __locale_raii __current(uselocale(__l), uselocale);
315 return mbrtowc(__pwc, __s, __n, __ps);
316#endif
317}
318
319_LIBCPP_ALWAYS_INLINE inline
320int __mbtowc_l(wchar_t *__pwc, const char *__pmb, size_t __max, locale_t __l)
321{
322#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
David Chisnall1d581062011-09-21 08:39:44 +0000323 return mbtowc_l(__pwc, __pmb, __max, __l);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000324#else
325 __locale_raii __current(uselocale(__l), uselocale);
326 return mbtowc(__pwc, __pmb, __max);
327#endif
328}
329
330_LIBCPP_ALWAYS_INLINE inline
331size_t __mbrlen_l(const char *__s, size_t __n, mbstate_t *__ps, locale_t __l)
332{
333#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
334 return mbrlen_l(__s, __n, __ps, __l);
335#else
336 __locale_raii __current(uselocale(__l), uselocale);
337 return mbrlen(__s, __n, __ps);
338#endif
339}
340
341_LIBCPP_ALWAYS_INLINE inline
342lconv *__localeconv_l(locale_t __l)
343{
344#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
345 return localeconv_l(__l);
346#else
347 __locale_raii __current(uselocale(__l), uselocale);
348 return localeconv();
349#endif
350}
351
352_LIBCPP_ALWAYS_INLINE inline
353size_t __mbsrtowcs_l(wchar_t *__dest, const char **__src, size_t __len,
354 mbstate_t *__ps, locale_t __l)
355{
356#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
357 return mbsrtowcs_l(__dest, __src, __len, __ps, __l);
358#else
359 __locale_raii __current(uselocale(__l), uselocale);
360 return mbsrtowcs(__dest, __src, __len, __ps);
361#endif
362}
363
Chandler Carruth952bdc82012-12-31 06:09:54 +0000364inline
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000365int __snprintf_l(char *__s, size_t __n, locale_t __l, const char *__format, ...) {
366 va_list __va;
367 va_start(__va, __format);
368#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
369 int __res = vsnprintf_l(__s, __n, __l, __format, __va);
370#else
371 __locale_raii __current(uselocale(__l), uselocale);
372 int __res = vsnprintf(__s, __n, __format, __va);
373#endif
374 va_end(__va);
375 return __res;
376}
377
Chandler Carruth952bdc82012-12-31 06:09:54 +0000378inline
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000379int __asprintf_l(char **__s, locale_t __l, const char *__format, ...) {
380 va_list __va;
381 va_start(__va, __format);
382#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
383 int __res = vasprintf_l(__s, __l, __format, __va);
384#else
385 __locale_raii __current(uselocale(__l), uselocale);
386 int __res = vasprintf(__s, __format, __va);
387#endif
388 va_end(__va);
389 return __res;
390}
391
Chandler Carruth952bdc82012-12-31 06:09:54 +0000392inline
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000393int __sscanf_l(const char *__s, locale_t __l, const char *__format, ...) {
394 va_list __va;
395 va_start(__va, __format);
396#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
397 int __res = vsscanf_l(__s, __l, __format, __va);
398#else
399 __locale_raii __current(uselocale(__l), uselocale);
400 int __res = vsscanf(__s, __format, __va);
401#endif
402 va_end(__va);
403 return __res;
404}
405
Howard Hinnantf312e3e2011-09-28 23:39:33 +0000406#endif // __linux__
Howard Hinnant155c2af2010-05-24 17:49:41 +0000407
Howard Hinnantc51e1022010-05-11 19:42:16 +0000408// __scan_keyword
409// Scans [__b, __e) until a match is found in the basic_strings range
410// [__kb, __ke) or until it can be shown that there is no match in [__kb, __ke).
411// __b will be incremented (visibly), consuming CharT until a match is found
412// or proved to not exist. A keyword may be "", in which will match anything.
413// If one keyword is a prefix of another, and the next CharT in the input
414// might match another keyword, the algorithm will attempt to find the longest
415// matching keyword. If the longer matching keyword ends up not matching, then
416// no keyword match is found. If no keyword match is found, __ke is returned
417// and failbit is set in __err.
418// Else an iterator pointing to the matching keyword is found. If more than
419// one keyword matches, an iterator to the first matching keyword is returned.
420// If on exit __b == __e, eofbit is set in __err. If __case_senstive is false,
421// __ct is used to force to lower case before comparing characters.
422// Examples:
423// Keywords: "a", "abb"
424// If the input is "a", the first keyword matches and eofbit is set.
425// If the input is "abc", no match is found and "ab" are consumed.
426template <class _InputIterator, class _ForwardIterator, class _Ctype>
427_LIBCPP_HIDDEN
428_ForwardIterator
429__scan_keyword(_InputIterator& __b, _InputIterator __e,
430 _ForwardIterator __kb, _ForwardIterator __ke,
431 const _Ctype& __ct, ios_base::iostate& __err,
432 bool __case_sensitive = true)
433{
434 typedef typename iterator_traits<_InputIterator>::value_type _CharT;
Howard Hinnant28b24882011-12-01 20:21:04 +0000435 size_t __nkw = static_cast<size_t>(_VSTD::distance(__kb, __ke));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000436 const unsigned char __doesnt_match = '\0';
437 const unsigned char __might_match = '\1';
438 const unsigned char __does_match = '\2';
439 unsigned char __statbuf[100];
440 unsigned char* __status = __statbuf;
441 unique_ptr<unsigned char, void(*)(void*)> __stat_hold(0, free);
442 if (__nkw > sizeof(__statbuf))
443 {
444 __status = (unsigned char*)malloc(__nkw);
445 if (__status == 0)
446 __throw_bad_alloc();
447 __stat_hold.reset(__status);
448 }
449 size_t __n_might_match = __nkw; // At this point, any keyword might match
450 size_t __n_does_match = 0; // but none of them definitely do
451 // Initialize all statuses to __might_match, except for "" keywords are __does_match
452 unsigned char* __st = __status;
453 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, ++__st)
454 {
455 if (!__ky->empty())
456 *__st = __might_match;
457 else
458 {
459 *__st = __does_match;
460 --__n_might_match;
461 ++__n_does_match;
462 }
463 }
464 // While there might be a match, test keywords against the next CharT
465 for (size_t __indx = 0; __b != __e && __n_might_match > 0; ++__indx)
466 {
467 // Peek at the next CharT but don't consume it
468 _CharT __c = *__b;
469 if (!__case_sensitive)
470 __c = __ct.toupper(__c);
471 bool __consume = false;
472 // For each keyword which might match, see if the __indx character is __c
473 // If a match if found, consume __c
474 // If a match is found, and that is the last character in the keyword,
475 // then that keyword matches.
476 // If the keyword doesn't match this character, then change the keyword
477 // to doesn't match
478 __st = __status;
479 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, ++__st)
480 {
481 if (*__st == __might_match)
482 {
483 _CharT __kc = (*__ky)[__indx];
484 if (!__case_sensitive)
485 __kc = __ct.toupper(__kc);
486 if (__c == __kc)
487 {
488 __consume = true;
489 if (__ky->size() == __indx+1)
490 {
491 *__st = __does_match;
492 --__n_might_match;
493 ++__n_does_match;
494 }
495 }
496 else
497 {
498 *__st = __doesnt_match;
499 --__n_might_match;
500 }
501 }
502 }
503 // consume if we matched a character
504 if (__consume)
505 {
506 ++__b;
507 // If we consumed a character and there might be a matched keyword that
508 // was marked matched on a previous iteration, then such keywords
509 // which are now marked as not matching.
510 if (__n_might_match + __n_does_match > 1)
511 {
512 __st = __status;
513 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, ++__st)
514 {
515 if (*__st == __does_match && __ky->size() != __indx+1)
516 {
517 *__st = __doesnt_match;
518 --__n_does_match;
519 }
520 }
521 }
522 }
523 }
524 // We've exited the loop because we hit eof and/or we have no more "might matches".
525 if (__b == __e)
526 __err |= ios_base::eofbit;
527 // Return the first matching result
528 for (__st = __status; __kb != __ke; ++__kb, ++__st)
529 if (*__st == __does_match)
530 break;
531 if (__kb == __ke)
532 __err |= ios_base::failbit;
533 return __kb;
534}
535
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000536struct _LIBCPP_TYPE_VIS __num_get_base
Howard Hinnantc51e1022010-05-11 19:42:16 +0000537{
538 static const int __num_get_buf_sz = 40;
539
540 static int __get_base(ios_base&);
541 static const char __src[33];
542};
543
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000544_LIBCPP_FUNC_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000545void __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end,
546 ios_base::iostate& __err);
547
Howard Hinnantc51e1022010-05-11 19:42:16 +0000548template <class _CharT>
549struct __num_get
550 : protected __num_get_base
551{
552 static string __stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep);
553 static string __stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
554 _CharT& __thousands_sep);
555 static int __stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
556 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
557 unsigned* __g, unsigned*& __g_end, _CharT* __atoms);
558 static int __stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp,
559 char* __a, char*& __a_end,
560 _CharT __decimal_point, _CharT __thousands_sep,
561 const string& __grouping, unsigned* __g,
562 unsigned*& __g_end, unsigned& __dc, _CharT* __atoms);
563};
564
565template <class _CharT>
566string
567__num_get<_CharT>::__stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep)
568{
569 locale __loc = __iob.getloc();
570 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 26, __atoms);
571 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
572 __thousands_sep = __np.thousands_sep();
573 return __np.grouping();
574}
575
576template <class _CharT>
577string
578__num_get<_CharT>::__stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
579 _CharT& __thousands_sep)
580{
581 locale __loc = __iob.getloc();
582 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 32, __atoms);
583 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
584 __decimal_point = __np.decimal_point();
585 __thousands_sep = __np.thousands_sep();
586 return __np.grouping();
587}
588
589template <class _CharT>
590int
591__num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
592 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
593 unsigned* __g, unsigned*& __g_end, _CharT* __atoms)
594{
Howard Hinnantf57b3fc2011-03-09 01:03:19 +0000595 if (__a_end == __a && (__ct == __atoms[24] || __ct == __atoms[25]))
596 {
597 *__a_end++ = __ct == __atoms[24] ? '+' : '-';
598 __dc = 0;
599 return 0;
600 }
Howard Hinnant28b24882011-12-01 20:21:04 +0000601 if (__grouping.size() != 0 && __ct == __thousands_sep)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000602 {
603 if (__g_end-__g < __num_get_buf_sz)
604 {
605 *__g_end++ = __dc;
606 __dc = 0;
607 }
608 return 0;
609 }
610 ptrdiff_t __f = find(__atoms, __atoms + 26, __ct) - __atoms;
Howard Hinnantf57b3fc2011-03-09 01:03:19 +0000611 if (__f >= 24)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612 return -1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000613 switch (__base)
614 {
615 case 8:
616 case 10:
617 if (__f >= __base)
Howard Hinnantf57b3fc2011-03-09 01:03:19 +0000618 return -1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000619 break;
Howard Hinnantf57b3fc2011-03-09 01:03:19 +0000620 case 16:
621 if (__f < 22)
622 break;
623 if (__a_end != __a && __a_end - __a <= 2 && __a_end[-1] == '0')
624 {
625 __dc = 0;
626 *__a_end++ = __src[__f];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000627 return 0;
Howard Hinnantf57b3fc2011-03-09 01:03:19 +0000628 }
629 return -1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000630 }
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000631 *__a_end++ = __src[__f];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000632 ++__dc;
633 return 0;
634}
635
636template <class _CharT>
637int
638__num_get<_CharT>::__stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp, char* __a, char*& __a_end,
639 _CharT __decimal_point, _CharT __thousands_sep, const string& __grouping,
640 unsigned* __g, unsigned*& __g_end, unsigned& __dc, _CharT* __atoms)
641{
642 if (__ct == __decimal_point)
643 {
644 if (!__in_units)
645 return -1;
646 __in_units = false;
647 *__a_end++ = '.';
648 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
649 *__g_end++ = __dc;
650 return 0;
651 }
652 if (__ct == __thousands_sep && __grouping.size() != 0)
653 {
654 if (!__in_units)
655 return -1;
656 if (__g_end-__g < __num_get_buf_sz)
657 {
658 *__g_end++ = __dc;
659 __dc = 0;
660 }
661 return 0;
662 }
663 ptrdiff_t __f = find(__atoms, __atoms + 32, __ct) - __atoms;
664 if (__f >= 32)
665 return -1;
666 char __x = __src[__f];
Howard Hinnant5132e192012-02-15 19:19:37 +0000667 if (__x == '-' || __x == '+')
668 {
Howard Hinnant21413152013-03-08 19:06:24 +0000669 if (__a_end == __a || (__a_end[-1] & 0x5F) == (__exp & 0x7F))
Howard Hinnant5132e192012-02-15 19:19:37 +0000670 {
671 *__a_end++ = __x;
672 return 0;
673 }
674 return -1;
675 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000676 if (__x == 'x' || __x == 'X')
677 __exp = 'P';
Howard Hinnant21413152013-03-08 19:06:24 +0000678 else if ((__x & 0x5F) == __exp)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000679 {
Howard Hinnant21413152013-03-08 19:06:24 +0000680 __exp |= 0x80;
681 if (__in_units)
682 {
683 __in_units = false;
684 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
685 *__g_end++ = __dc;
686 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000687 }
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000688 *__a_end++ = __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000689 if (__f >= 22)
690 return 0;
691 ++__dc;
692 return 0;
693}
694
Howard Hinnant8ea98242013-08-23 17:37:05 +0000695_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_TYPE_VIS __num_get<char>)
696_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_TYPE_VIS __num_get<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000697
698template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000699class _LIBCPP_TYPE_VIS_ONLY num_get
Howard Hinnantc51e1022010-05-11 19:42:16 +0000700 : public locale::facet,
701 private __num_get<_CharT>
702{
703public:
704 typedef _CharT char_type;
705 typedef _InputIterator iter_type;
706
707 _LIBCPP_ALWAYS_INLINE
708 explicit num_get(size_t __refs = 0)
709 : locale::facet(__refs) {}
710
711 _LIBCPP_ALWAYS_INLINE
712 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
713 ios_base::iostate& __err, bool& __v) const
714 {
715 return do_get(__b, __e, __iob, __err, __v);
716 }
717
718 _LIBCPP_ALWAYS_INLINE
719 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
720 ios_base::iostate& __err, long& __v) const
721 {
722 return do_get(__b, __e, __iob, __err, __v);
723 }
724
725 _LIBCPP_ALWAYS_INLINE
726 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
727 ios_base::iostate& __err, long long& __v) const
728 {
729 return do_get(__b, __e, __iob, __err, __v);
730 }
731
732 _LIBCPP_ALWAYS_INLINE
733 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
734 ios_base::iostate& __err, unsigned short& __v) const
735 {
736 return do_get(__b, __e, __iob, __err, __v);
737 }
738
739 _LIBCPP_ALWAYS_INLINE
740 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
741 ios_base::iostate& __err, unsigned int& __v) const
742 {
743 return do_get(__b, __e, __iob, __err, __v);
744 }
745
746 _LIBCPP_ALWAYS_INLINE
747 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
748 ios_base::iostate& __err, unsigned long& __v) const
749 {
750 return do_get(__b, __e, __iob, __err, __v);
751 }
752
753 _LIBCPP_ALWAYS_INLINE
754 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
755 ios_base::iostate& __err, unsigned long long& __v) const
756 {
757 return do_get(__b, __e, __iob, __err, __v);
758 }
759
760 _LIBCPP_ALWAYS_INLINE
761 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
762 ios_base::iostate& __err, float& __v) const
763 {
764 return do_get(__b, __e, __iob, __err, __v);
765 }
766
767 _LIBCPP_ALWAYS_INLINE
768 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
769 ios_base::iostate& __err, double& __v) const
770 {
771 return do_get(__b, __e, __iob, __err, __v);
772 }
773
774 _LIBCPP_ALWAYS_INLINE
775 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
776 ios_base::iostate& __err, long double& __v) const
777 {
778 return do_get(__b, __e, __iob, __err, __v);
779 }
780
781 _LIBCPP_ALWAYS_INLINE
782 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
783 ios_base::iostate& __err, void*& __v) const
784 {
785 return do_get(__b, __e, __iob, __err, __v);
786 }
787
788 static locale::id id;
789
790protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000791 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +0000792 ~num_get() {}
793
794 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
795 ios_base::iostate& __err, bool& __v) const;
796 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
797 ios_base::iostate& __err, long& __v) const;
798 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
799 ios_base::iostate& __err, long long& __v) const;
800 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
801 ios_base::iostate& __err, unsigned short& __v) const;
802 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
803 ios_base::iostate& __err, unsigned int& __v) const;
804 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
805 ios_base::iostate& __err, unsigned long& __v) const;
806 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
807 ios_base::iostate& __err, unsigned long long& __v) const;
808 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
809 ios_base::iostate& __err, float& __v) const;
810 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
811 ios_base::iostate& __err, double& __v) const;
812 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
813 ios_base::iostate& __err, long double& __v) const;
814 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
815 ios_base::iostate& __err, void*& __v) const;
816};
817
818template <class _CharT, class _InputIterator>
819locale::id
820num_get<_CharT, _InputIterator>::id;
821
822template <class _Tp>
823_Tp
824__num_get_signed_integral(const char* __a, const char* __a_end,
825 ios_base::iostate& __err, int __base)
826{
827 if (__a != __a_end)
828 {
Howard Hinnantca8923c2013-01-22 17:26:08 +0000829 typename remove_reference<decltype(errno)>::type __save_errno = errno;
Howard Hinnant05c71342011-02-25 19:52:41 +0000830 errno = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000831 char *__p2;
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000832 long long __ll = strtoll_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
Howard Hinnantca8923c2013-01-22 17:26:08 +0000833 typename remove_reference<decltype(errno)>::type __current_errno = errno;
Howard Hinnant05c71342011-02-25 19:52:41 +0000834 if (__current_errno == 0)
835 errno = __save_errno;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000836 if (__p2 != __a_end)
837 {
838 __err = ios_base::failbit;
839 return 0;
840 }
Howard Hinnant05c71342011-02-25 19:52:41 +0000841 else if (__current_errno == ERANGE ||
842 __ll < numeric_limits<_Tp>::min() ||
843 numeric_limits<_Tp>::max() < __ll)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000844 {
845 __err = ios_base::failbit;
Howard Hinnant05c71342011-02-25 19:52:41 +0000846 if (__ll > 0)
847 return numeric_limits<_Tp>::max();
848 else
849 return numeric_limits<_Tp>::min();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000850 }
851 return static_cast<_Tp>(__ll);
852 }
853 __err = ios_base::failbit;
854 return 0;
855}
856
857template <class _Tp>
858_Tp
859__num_get_unsigned_integral(const char* __a, const char* __a_end,
860 ios_base::iostate& __err, int __base)
861{
862 if (__a != __a_end)
863 {
Howard Hinnant05c71342011-02-25 19:52:41 +0000864 if (*__a == '-')
865 {
866 __err = ios_base::failbit;
867 return 0;
868 }
Howard Hinnantca8923c2013-01-22 17:26:08 +0000869 typename remove_reference<decltype(errno)>::type __save_errno = errno;
Howard Hinnant05c71342011-02-25 19:52:41 +0000870 errno = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000871 char *__p2;
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000872 unsigned long long __ll = strtoull_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
Howard Hinnantca8923c2013-01-22 17:26:08 +0000873 typename remove_reference<decltype(errno)>::type __current_errno = errno;
Howard Hinnant05c71342011-02-25 19:52:41 +0000874 if (__current_errno == 0)
875 errno = __save_errno;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000876 if (__p2 != __a_end)
877 {
878 __err = ios_base::failbit;
879 return 0;
880 }
Howard Hinnant05c71342011-02-25 19:52:41 +0000881 else if (__current_errno == ERANGE ||
882 numeric_limits<_Tp>::max() < __ll)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000883 {
884 __err = ios_base::failbit;
885 return numeric_limits<_Tp>::max();
886 }
887 return static_cast<_Tp>(__ll);
888 }
889 __err = ios_base::failbit;
890 return 0;
891}
892
893template <class _Tp>
894_Tp
895__num_get_float(const char* __a, const char* __a_end, ios_base::iostate& __err)
896{
897 if (__a != __a_end)
898 {
Howard Hinnantc9567812013-04-13 18:19:25 +0000899 typename remove_reference<decltype(errno)>::type __save_errno = errno;
900 errno = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000901 char *__p2;
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000902 long double __ld = strtold_l(__a, &__p2, _LIBCPP_GET_C_LOCALE);
Howard Hinnantc9567812013-04-13 18:19:25 +0000903 typename remove_reference<decltype(errno)>::type __current_errno = errno;
904 if (__current_errno == 0)
905 errno = __save_errno;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000906 if (__p2 != __a_end)
907 {
908 __err = ios_base::failbit;
909 return 0;
910 }
Howard Hinnantc9567812013-04-13 18:19:25 +0000911 else if (__current_errno == ERANGE)
912 __err = ios_base::failbit;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000913 return static_cast<_Tp>(__ld);
914 }
915 __err = ios_base::failbit;
916 return 0;
917}
918
919template <class _CharT, class _InputIterator>
920_InputIterator
921num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
922 ios_base& __iob,
923 ios_base::iostate& __err,
924 bool& __v) const
925{
926 if ((__iob.flags() & ios_base::boolalpha) == 0)
927 {
928 long __lv = -1;
929 __b = do_get(__b, __e, __iob, __err, __lv);
930 switch (__lv)
931 {
932 case 0:
933 __v = false;
934 break;
935 case 1:
936 __v = true;
937 break;
938 default:
939 __v = true;
940 __err = ios_base::failbit;
941 break;
942 }
943 return __b;
944 }
945 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__iob.getloc());
946 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__iob.getloc());
947 typedef typename numpunct<_CharT>::string_type string_type;
948 const string_type __names[2] = {__np.truename(), __np.falsename()};
949 const string_type* __i = __scan_keyword(__b, __e, __names, __names+2,
950 __ct, __err);
951 __v = __i == __names;
952 return __b;
953}
954
955template <class _CharT, class _InputIterator>
956_InputIterator
957num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
958 ios_base& __iob,
959 ios_base::iostate& __err,
960 long& __v) const
961{
962 // Stage 1
963 int __base = this->__get_base(__iob);
964 // Stage 2
965 char_type __atoms[26];
966 char_type __thousands_sep;
967 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000968 string __buf;
969 __buf.resize(__buf.capacity());
970 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971 char* __a_end = __a;
972 unsigned __g[__num_get_base::__num_get_buf_sz];
973 unsigned* __g_end = __g;
974 unsigned __dc = 0;
975 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000976 {
977 if (__a_end - __a == __buf.size())
978 {
979 size_t __tmp = __buf.size();
980 __buf.resize(2*__buf.size());
981 __buf.resize(__buf.capacity());
982 __a = &__buf[0];
983 __a_end = __a + __tmp;
984 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000985 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986 __thousands_sep, __grouping, __g, __g_end,
987 __atoms))
988 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000989 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
991 *__g_end++ = __dc;
992 // Stage 3
993 __v = __num_get_signed_integral<long>(__a, __a_end, __err, __base);
994 // Digit grouping checked
995 __check_grouping(__grouping, __g, __g_end, __err);
996 // EOF checked
997 if (__b == __e)
998 __err |= ios_base::eofbit;
999 return __b;
1000}
1001
1002template <class _CharT, class _InputIterator>
1003_InputIterator
1004num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1005 ios_base& __iob,
1006 ios_base::iostate& __err,
1007 long long& __v) const
1008{
1009 // Stage 1
1010 int __base = this->__get_base(__iob);
1011 // Stage 2
1012 char_type __atoms[26];
1013 char_type __thousands_sep;
1014 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001015 string __buf;
1016 __buf.resize(__buf.capacity());
1017 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018 char* __a_end = __a;
1019 unsigned __g[__num_get_base::__num_get_buf_sz];
1020 unsigned* __g_end = __g;
1021 unsigned __dc = 0;
1022 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001023 {
1024 if (__a_end - __a == __buf.size())
1025 {
1026 size_t __tmp = __buf.size();
1027 __buf.resize(2*__buf.size());
1028 __buf.resize(__buf.capacity());
1029 __a = &__buf[0];
1030 __a_end = __a + __tmp;
1031 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001032 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1033 __thousands_sep, __grouping, __g, __g_end,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001034 __atoms))
1035 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001036 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1038 *__g_end++ = __dc;
1039 // Stage 3
1040 __v = __num_get_signed_integral<long long>(__a, __a_end, __err, __base);
1041 // Digit grouping checked
1042 __check_grouping(__grouping, __g, __g_end, __err);
1043 // EOF checked
1044 if (__b == __e)
1045 __err |= ios_base::eofbit;
1046 return __b;
1047}
1048
1049template <class _CharT, class _InputIterator>
1050_InputIterator
1051num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1052 ios_base& __iob,
1053 ios_base::iostate& __err,
1054 unsigned short& __v) const
1055{
1056 // Stage 1
1057 int __base = this->__get_base(__iob);
1058 // Stage 2
1059 char_type __atoms[26];
1060 char_type __thousands_sep;
1061 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001062 string __buf;
1063 __buf.resize(__buf.capacity());
1064 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001065 char* __a_end = __a;
1066 unsigned __g[__num_get_base::__num_get_buf_sz];
1067 unsigned* __g_end = __g;
1068 unsigned __dc = 0;
1069 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001070 {
1071 if (__a_end - __a == __buf.size())
1072 {
1073 size_t __tmp = __buf.size();
1074 __buf.resize(2*__buf.size());
1075 __buf.resize(__buf.capacity());
1076 __a = &__buf[0];
1077 __a_end = __a + __tmp;
1078 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001079 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001080 __thousands_sep, __grouping, __g, __g_end,
1081 __atoms))
1082 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001083 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001084 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1085 *__g_end++ = __dc;
1086 // Stage 3
1087 __v = __num_get_unsigned_integral<unsigned short>(__a, __a_end, __err, __base);
1088 // Digit grouping checked
1089 __check_grouping(__grouping, __g, __g_end, __err);
1090 // EOF checked
1091 if (__b == __e)
1092 __err |= ios_base::eofbit;
1093 return __b;
1094}
1095
1096template <class _CharT, class _InputIterator>
1097_InputIterator
1098num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1099 ios_base& __iob,
1100 ios_base::iostate& __err,
1101 unsigned int& __v) const
1102{
1103 // Stage 1
1104 int __base = this->__get_base(__iob);
1105 // Stage 2
1106 char_type __atoms[26];
1107 char_type __thousands_sep;
1108 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001109 string __buf;
1110 __buf.resize(__buf.capacity());
1111 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001112 char* __a_end = __a;
1113 unsigned __g[__num_get_base::__num_get_buf_sz];
1114 unsigned* __g_end = __g;
1115 unsigned __dc = 0;
1116 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001117 {
1118 if (__a_end - __a == __buf.size())
1119 {
1120 size_t __tmp = __buf.size();
1121 __buf.resize(2*__buf.size());
1122 __buf.resize(__buf.capacity());
1123 __a = &__buf[0];
1124 __a_end = __a + __tmp;
1125 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001126 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127 __thousands_sep, __grouping, __g, __g_end,
1128 __atoms))
1129 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001130 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1132 *__g_end++ = __dc;
1133 // Stage 3
1134 __v = __num_get_unsigned_integral<unsigned int>(__a, __a_end, __err, __base);
1135 // Digit grouping checked
1136 __check_grouping(__grouping, __g, __g_end, __err);
1137 // EOF checked
1138 if (__b == __e)
1139 __err |= ios_base::eofbit;
1140 return __b;
1141}
1142
1143template <class _CharT, class _InputIterator>
1144_InputIterator
1145num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1146 ios_base& __iob,
1147 ios_base::iostate& __err,
1148 unsigned long& __v) const
1149{
1150 // Stage 1
1151 int __base = this->__get_base(__iob);
1152 // Stage 2
1153 char_type __atoms[26];
1154 char_type __thousands_sep;
1155 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001156 string __buf;
1157 __buf.resize(__buf.capacity());
1158 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 char* __a_end = __a;
1160 unsigned __g[__num_get_base::__num_get_buf_sz];
1161 unsigned* __g_end = __g;
1162 unsigned __dc = 0;
1163 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001164 {
1165 if (__a_end - __a == __buf.size())
1166 {
1167 size_t __tmp = __buf.size();
1168 __buf.resize(2*__buf.size());
1169 __buf.resize(__buf.capacity());
1170 __a = &__buf[0];
1171 __a_end = __a + __tmp;
1172 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001173 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174 __thousands_sep, __grouping, __g, __g_end,
1175 __atoms))
1176 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001177 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1179 *__g_end++ = __dc;
1180 // Stage 3
1181 __v = __num_get_unsigned_integral<unsigned long>(__a, __a_end, __err, __base);
1182 // Digit grouping checked
1183 __check_grouping(__grouping, __g, __g_end, __err);
1184 // EOF checked
1185 if (__b == __e)
1186 __err |= ios_base::eofbit;
1187 return __b;
1188}
1189
1190template <class _CharT, class _InputIterator>
1191_InputIterator
1192num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1193 ios_base& __iob,
1194 ios_base::iostate& __err,
1195 unsigned long long& __v) const
1196{
1197 // Stage 1
1198 int __base = this->__get_base(__iob);
1199 // Stage 2
1200 char_type __atoms[26];
1201 char_type __thousands_sep;
1202 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001203 string __buf;
1204 __buf.resize(__buf.capacity());
1205 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206 char* __a_end = __a;
1207 unsigned __g[__num_get_base::__num_get_buf_sz];
1208 unsigned* __g_end = __g;
1209 unsigned __dc = 0;
1210 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001211 {
1212 if (__a_end - __a == __buf.size())
1213 {
1214 size_t __tmp = __buf.size();
1215 __buf.resize(2*__buf.size());
1216 __buf.resize(__buf.capacity());
1217 __a = &__buf[0];
1218 __a_end = __a + __tmp;
1219 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001220 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221 __thousands_sep, __grouping, __g, __g_end,
1222 __atoms))
1223 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001224 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001225 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1226 *__g_end++ = __dc;
1227 // Stage 3
1228 __v = __num_get_unsigned_integral<unsigned long long>(__a, __a_end, __err, __base);
1229 // Digit grouping checked
1230 __check_grouping(__grouping, __g, __g_end, __err);
1231 // EOF checked
1232 if (__b == __e)
1233 __err |= ios_base::eofbit;
1234 return __b;
1235}
1236
1237template <class _CharT, class _InputIterator>
1238_InputIterator
1239num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1240 ios_base& __iob,
1241 ios_base::iostate& __err,
1242 float& __v) const
1243{
1244 // Stage 1, nothing to do
1245 // Stage 2
1246 char_type __atoms[32];
1247 char_type __decimal_point;
1248 char_type __thousands_sep;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001249 string __grouping = this->__stage2_float_prep(__iob, __atoms,
1250 __decimal_point,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251 __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001252 string __buf;
1253 __buf.resize(__buf.capacity());
1254 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001255 char* __a_end = __a;
1256 unsigned __g[__num_get_base::__num_get_buf_sz];
1257 unsigned* __g_end = __g;
1258 unsigned __dc = 0;
1259 bool __in_units = true;
1260 char __exp = 'E';
1261 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001262 {
1263 if (__a_end - __a == __buf.size())
1264 {
1265 size_t __tmp = __buf.size();
1266 __buf.resize(2*__buf.size());
1267 __buf.resize(__buf.capacity());
1268 __a = &__buf[0];
1269 __a_end = __a + __tmp;
1270 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001271 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1272 __decimal_point, __thousands_sep,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001273 __grouping, __g, __g_end,
1274 __dc, __atoms))
1275 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001276 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001277 if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1278 *__g_end++ = __dc;
1279 // Stage 3
1280 __v = __num_get_float<float>(__a, __a_end, __err);
1281 // Digit grouping checked
1282 __check_grouping(__grouping, __g, __g_end, __err);
1283 // EOF checked
1284 if (__b == __e)
1285 __err |= ios_base::eofbit;
1286 return __b;
1287}
1288
1289template <class _CharT, class _InputIterator>
1290_InputIterator
1291num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1292 ios_base& __iob,
1293 ios_base::iostate& __err,
1294 double& __v) const
1295{
1296 // Stage 1, nothing to do
1297 // Stage 2
1298 char_type __atoms[32];
1299 char_type __decimal_point;
1300 char_type __thousands_sep;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001301 string __grouping = this->__stage2_float_prep(__iob, __atoms,
1302 __decimal_point,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001303 __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001304 string __buf;
1305 __buf.resize(__buf.capacity());
1306 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001307 char* __a_end = __a;
1308 unsigned __g[__num_get_base::__num_get_buf_sz];
1309 unsigned* __g_end = __g;
1310 unsigned __dc = 0;
1311 bool __in_units = true;
1312 char __exp = 'E';
1313 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001314 {
1315 if (__a_end - __a == __buf.size())
1316 {
1317 size_t __tmp = __buf.size();
1318 __buf.resize(2*__buf.size());
1319 __buf.resize(__buf.capacity());
1320 __a = &__buf[0];
1321 __a_end = __a + __tmp;
1322 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001323 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1324 __decimal_point, __thousands_sep,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325 __grouping, __g, __g_end,
1326 __dc, __atoms))
1327 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001328 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001329 if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1330 *__g_end++ = __dc;
1331 // Stage 3
1332 __v = __num_get_float<double>(__a, __a_end, __err);
1333 // Digit grouping checked
1334 __check_grouping(__grouping, __g, __g_end, __err);
1335 // EOF checked
1336 if (__b == __e)
1337 __err |= ios_base::eofbit;
1338 return __b;
1339}
1340
1341template <class _CharT, class _InputIterator>
1342_InputIterator
1343num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1344 ios_base& __iob,
1345 ios_base::iostate& __err,
1346 long double& __v) const
1347{
1348 // Stage 1, nothing to do
1349 // Stage 2
1350 char_type __atoms[32];
1351 char_type __decimal_point;
1352 char_type __thousands_sep;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001353 string __grouping = this->__stage2_float_prep(__iob, __atoms,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001354 __decimal_point,
1355 __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001356 string __buf;
1357 __buf.resize(__buf.capacity());
1358 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001359 char* __a_end = __a;
1360 unsigned __g[__num_get_base::__num_get_buf_sz];
1361 unsigned* __g_end = __g;
1362 unsigned __dc = 0;
1363 bool __in_units = true;
1364 char __exp = 'E';
1365 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001366 {
1367 if (__a_end - __a == __buf.size())
1368 {
1369 size_t __tmp = __buf.size();
1370 __buf.resize(2*__buf.size());
1371 __buf.resize(__buf.capacity());
1372 __a = &__buf[0];
1373 __a_end = __a + __tmp;
1374 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001375 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1376 __decimal_point, __thousands_sep,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377 __grouping, __g, __g_end,
1378 __dc, __atoms))
1379 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001380 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001381 if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1382 *__g_end++ = __dc;
1383 // Stage 3
1384 __v = __num_get_float<long double>(__a, __a_end, __err);
1385 // Digit grouping checked
1386 __check_grouping(__grouping, __g, __g_end, __err);
1387 // EOF checked
1388 if (__b == __e)
1389 __err |= ios_base::eofbit;
1390 return __b;
1391}
1392
1393template <class _CharT, class _InputIterator>
1394_InputIterator
1395num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1396 ios_base& __iob,
1397 ios_base::iostate& __err,
1398 void*& __v) const
1399{
1400 // Stage 1
1401 int __base = 16;
1402 // Stage 2
1403 char_type __atoms[26];
Howard Hinnant28b24882011-12-01 20:21:04 +00001404 char_type __thousands_sep = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001405 string __grouping;
1406 use_facet<ctype<_CharT> >(__iob.getloc()).widen(__num_get_base::__src,
1407 __num_get_base::__src + 26, __atoms);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001408 string __buf;
1409 __buf.resize(__buf.capacity());
1410 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001411 char* __a_end = __a;
1412 unsigned __g[__num_get_base::__num_get_buf_sz];
1413 unsigned* __g_end = __g;
1414 unsigned __dc = 0;
1415 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001416 {
1417 if (__a_end - __a == __buf.size())
1418 {
1419 size_t __tmp = __buf.size();
1420 __buf.resize(2*__buf.size());
1421 __buf.resize(__buf.capacity());
1422 __a = &__buf[0];
1423 __a_end = __a + __tmp;
1424 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001425 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1426 __thousands_sep, __grouping,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001427 __g, __g_end, __atoms))
1428 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001429 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001430 // Stage 3
1431 __a[sizeof(__a)-1] = 0;
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001432#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001433 if (sscanf_l(__a, _LIBCPP_GET_C_LOCALE, "%p", &__v) != 1)
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001434#else
1435 if (__sscanf_l(__a, __cloc(), "%p", &__v) != 1)
1436#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001437 __err = ios_base::failbit;
1438 // EOF checked
1439 if (__b == __e)
1440 __err |= ios_base::eofbit;
1441 return __b;
1442}
1443
Howard Hinnant8ea98242013-08-23 17:37:05 +00001444_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS num_get<char>)
1445_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS num_get<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001446
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001447struct _LIBCPP_TYPE_VIS __num_put_base
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448{
1449protected:
1450 static void __format_int(char* __fmt, const char* __len, bool __signd,
1451 ios_base::fmtflags __flags);
1452 static bool __format_float(char* __fmt, const char* __len,
1453 ios_base::fmtflags __flags);
1454 static char* __identify_padding(char* __nb, char* __ne,
1455 const ios_base& __iob);
1456};
1457
1458template <class _CharT>
1459struct __num_put
1460 : protected __num_put_base
1461{
1462 static void __widen_and_group_int(char* __nb, char* __np, char* __ne,
1463 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1464 const locale& __loc);
1465 static void __widen_and_group_float(char* __nb, char* __np, char* __ne,
1466 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1467 const locale& __loc);
1468};
1469
1470template <class _CharT>
1471void
1472__num_put<_CharT>::__widen_and_group_int(char* __nb, char* __np, char* __ne,
1473 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1474 const locale& __loc)
1475{
1476 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1477 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1478 string __grouping = __npt.grouping();
1479 if (__grouping.empty())
1480 {
1481 __ct.widen(__nb, __ne, __ob);
1482 __oe = __ob + (__ne - __nb);
1483 }
1484 else
1485 {
1486 __oe = __ob;
1487 char* __nf = __nb;
1488 if (*__nf == '-' || *__nf == '+')
1489 *__oe++ = __ct.widen(*__nf++);
1490 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1491 __nf[1] == 'X'))
1492 {
1493 *__oe++ = __ct.widen(*__nf++);
1494 *__oe++ = __ct.widen(*__nf++);
1495 }
1496 reverse(__nf, __ne);
1497 _CharT __thousands_sep = __npt.thousands_sep();
1498 unsigned __dc = 0;
1499 unsigned __dg = 0;
1500 for (char* __p = __nf; __p < __ne; ++__p)
1501 {
1502 if (static_cast<unsigned>(__grouping[__dg]) > 0 &&
1503 __dc == static_cast<unsigned>(__grouping[__dg]))
1504 {
1505 *__oe++ = __thousands_sep;
1506 __dc = 0;
1507 if (__dg < __grouping.size()-1)
1508 ++__dg;
1509 }
1510 *__oe++ = __ct.widen(*__p);
1511 ++__dc;
1512 }
1513 reverse(__ob + (__nf - __nb), __oe);
1514 }
1515 if (__np == __ne)
1516 __op = __oe;
1517 else
1518 __op = __ob + (__np - __nb);
1519}
1520
1521template <class _CharT>
1522void
1523__num_put<_CharT>::__widen_and_group_float(char* __nb, char* __np, char* __ne,
1524 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1525 const locale& __loc)
1526{
1527 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1528 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1529 string __grouping = __npt.grouping();
1530 __oe = __ob;
1531 char* __nf = __nb;
1532 if (*__nf == '-' || *__nf == '+')
1533 *__oe++ = __ct.widen(*__nf++);
1534 char* __ns;
1535 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1536 __nf[1] == 'X'))
1537 {
1538 *__oe++ = __ct.widen(*__nf++);
1539 *__oe++ = __ct.widen(*__nf++);
1540 for (__ns = __nf; __ns < __ne; ++__ns)
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001541 if (!isxdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001542 break;
1543 }
1544 else
1545 {
1546 for (__ns = __nf; __ns < __ne; ++__ns)
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001547 if (!isdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548 break;
1549 }
1550 if (__grouping.empty())
1551 {
1552 __ct.widen(__nf, __ns, __oe);
1553 __oe += __ns - __nf;
1554 }
1555 else
1556 {
1557 reverse(__nf, __ns);
1558 _CharT __thousands_sep = __npt.thousands_sep();
1559 unsigned __dc = 0;
1560 unsigned __dg = 0;
1561 for (char* __p = __nf; __p < __ns; ++__p)
1562 {
1563 if (__grouping[__dg] > 0 && __dc == static_cast<unsigned>(__grouping[__dg]))
1564 {
1565 *__oe++ = __thousands_sep;
1566 __dc = 0;
1567 if (__dg < __grouping.size()-1)
1568 ++__dg;
1569 }
1570 *__oe++ = __ct.widen(*__p);
1571 ++__dc;
1572 }
1573 reverse(__ob + (__nf - __nb), __oe);
1574 }
1575 for (__nf = __ns; __nf < __ne; ++__nf)
1576 {
1577 if (*__nf == '.')
1578 {
1579 *__oe++ = __npt.decimal_point();
1580 ++__nf;
1581 break;
1582 }
1583 else
1584 *__oe++ = __ct.widen(*__nf);
1585 }
1586 __ct.widen(__nf, __ne, __oe);
1587 __oe += __ne - __nf;
1588 if (__np == __ne)
1589 __op = __oe;
1590 else
1591 __op = __ob + (__np - __nb);
1592}
1593
Howard Hinnant8ea98242013-08-23 17:37:05 +00001594_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_TYPE_VIS __num_put<char>)
1595_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_TYPE_VIS __num_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596
1597template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001598class _LIBCPP_TYPE_VIS_ONLY num_put
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599 : public locale::facet,
1600 private __num_put<_CharT>
1601{
1602public:
1603 typedef _CharT char_type;
1604 typedef _OutputIterator iter_type;
1605
1606 _LIBCPP_ALWAYS_INLINE
1607 explicit num_put(size_t __refs = 0)
1608 : locale::facet(__refs) {}
1609
1610 _LIBCPP_ALWAYS_INLINE
1611 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1612 bool __v) const
1613 {
1614 return do_put(__s, __iob, __fl, __v);
1615 }
1616
1617 _LIBCPP_ALWAYS_INLINE
1618 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1619 long __v) const
1620 {
1621 return do_put(__s, __iob, __fl, __v);
1622 }
1623
1624 _LIBCPP_ALWAYS_INLINE
1625 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1626 long long __v) const
1627 {
1628 return do_put(__s, __iob, __fl, __v);
1629 }
1630
1631 _LIBCPP_ALWAYS_INLINE
1632 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1633 unsigned long __v) const
1634 {
1635 return do_put(__s, __iob, __fl, __v);
1636 }
1637
1638 _LIBCPP_ALWAYS_INLINE
1639 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1640 unsigned long long __v) const
1641 {
1642 return do_put(__s, __iob, __fl, __v);
1643 }
1644
1645 _LIBCPP_ALWAYS_INLINE
1646 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1647 double __v) const
1648 {
1649 return do_put(__s, __iob, __fl, __v);
1650 }
1651
1652 _LIBCPP_ALWAYS_INLINE
1653 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1654 long double __v) const
1655 {
1656 return do_put(__s, __iob, __fl, __v);
1657 }
1658
1659 _LIBCPP_ALWAYS_INLINE
1660 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1661 const void* __v) const
1662 {
1663 return do_put(__s, __iob, __fl, __v);
1664 }
1665
1666 static locale::id id;
1667
1668protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00001669 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001670 ~num_put() {}
1671
1672 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1673 bool __v) const;
1674 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1675 long __v) const;
1676 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1677 long long __v) const;
1678 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1679 unsigned long) const;
1680 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1681 unsigned long long) const;
1682 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1683 double __v) const;
1684 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1685 long double __v) const;
1686 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1687 const void* __v) const;
1688};
1689
1690template <class _CharT, class _OutputIterator>
1691locale::id
1692num_put<_CharT, _OutputIterator>::id;
1693
1694template <class _CharT, class _OutputIterator>
1695_LIBCPP_HIDDEN
1696_OutputIterator
1697__pad_and_output(_OutputIterator __s,
1698 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1699 ios_base& __iob, _CharT __fl)
1700{
1701 streamsize __sz = __oe - __ob;
1702 streamsize __ns = __iob.width();
1703 if (__ns > __sz)
1704 __ns -= __sz;
1705 else
1706 __ns = 0;
1707 for (;__ob < __op; ++__ob, ++__s)
1708 *__s = *__ob;
1709 for (; __ns; --__ns, ++__s)
1710 *__s = __fl;
1711 for (; __ob < __oe; ++__ob, ++__s)
1712 *__s = *__ob;
1713 __iob.width(0);
1714 return __s;
1715}
1716
Howard Hinnant48fd5d52012-11-14 21:17:15 +00001717#if !defined(__APPLE__) || \
1718 (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED > __MAC_10_8) || \
1719 (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_6_0)
1720
Howard Hinnant97955172012-09-19 19:14:15 +00001721template <class _CharT, class _Traits>
1722_LIBCPP_HIDDEN
1723ostreambuf_iterator<_CharT, _Traits>
1724__pad_and_output(ostreambuf_iterator<_CharT, _Traits> __s,
1725 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1726 ios_base& __iob, _CharT __fl)
1727{
1728 if (__s.__sbuf_ == nullptr)
1729 return __s;
1730 streamsize __sz = __oe - __ob;
1731 streamsize __ns = __iob.width();
1732 if (__ns > __sz)
1733 __ns -= __sz;
1734 else
1735 __ns = 0;
1736 streamsize __np = __op - __ob;
1737 if (__np > 0)
1738 {
1739 if (__s.__sbuf_->sputn(__ob, __np) != __np)
1740 {
1741 __s.__sbuf_ = nullptr;
1742 return __s;
1743 }
1744 }
1745 if (__ns > 0)
1746 {
1747 basic_string<_CharT, _Traits> __sp(__ns, __fl);
1748 if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns)
1749 {
1750 __s.__sbuf_ = nullptr;
1751 return __s;
1752 }
1753 }
1754 __np = __oe - __op;
1755 if (__np > 0)
1756 {
1757 if (__s.__sbuf_->sputn(__op, __np) != __np)
1758 {
1759 __s.__sbuf_ = nullptr;
1760 return __s;
1761 }
1762 }
1763 __iob.width(0);
1764 return __s;
1765}
1766
Howard Hinnant48fd5d52012-11-14 21:17:15 +00001767#endif
1768
Howard Hinnantc51e1022010-05-11 19:42:16 +00001769template <class _CharT, class _OutputIterator>
1770_OutputIterator
1771num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1772 char_type __fl, bool __v) const
1773{
1774 if ((__iob.flags() & ios_base::boolalpha) == 0)
1775 return do_put(__s, __iob, __fl, (unsigned long)__v);
1776 const numpunct<char_type>& __np = use_facet<numpunct<char_type> >(__iob.getloc());
1777 typedef typename numpunct<char_type>::string_type string_type;
Howard Hinnant8ea98242013-08-23 17:37:05 +00001778#if _LIBCPP_DEBUG_LEVEL >= 2
1779 string_type __tmp(__v ? __np.truename() : __np.falsename());
1780 string_type __nm = _VSTD::move(__tmp);
1781#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001782 string_type __nm = __v ? __np.truename() : __np.falsename();
Howard Hinnant8ea98242013-08-23 17:37:05 +00001783#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001784 for (typename string_type::iterator __i = __nm.begin(); __i != __nm.end(); ++__i, ++__s)
1785 *__s = *__i;
1786 return __s;
1787}
1788
1789template <class _CharT, class _OutputIterator>
1790_OutputIterator
1791num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1792 char_type __fl, long __v) const
1793{
1794 // Stage 1 - Get number in narrow char
1795 char __fmt[6] = {'%', 0};
1796 const char* __len = "l";
1797 this->__format_int(__fmt+1, __len, true, __iob.flags());
1798 const unsigned __nbuf = (numeric_limits<long>::digits / 3)
1799 + ((numeric_limits<long>::digits % 3) != 0)
1800 + 1;
1801 char __nar[__nbuf];
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001802#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001803 int __nc = snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001804#else
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001805 int __nc = __snprintf_l(__nar, sizeof(__nar), __cloc(), __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001806#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001807 char* __ne = __nar + __nc;
1808 char* __np = this->__identify_padding(__nar, __ne, __iob);
1809 // Stage 2 - Widen __nar while adding thousands separators
1810 char_type __o[2*(__nbuf-1) - 1];
1811 char_type* __op; // pad here
1812 char_type* __oe; // end of output
1813 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1814 // [__o, __oe) contains thousands_sep'd wide number
1815 // Stage 3 & 4
1816 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1817}
1818
1819template <class _CharT, class _OutputIterator>
1820_OutputIterator
1821num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1822 char_type __fl, long long __v) const
1823{
1824 // Stage 1 - Get number in narrow char
1825 char __fmt[8] = {'%', 0};
1826 const char* __len = "ll";
1827 this->__format_int(__fmt+1, __len, true, __iob.flags());
1828 const unsigned __nbuf = (numeric_limits<long long>::digits / 3)
1829 + ((numeric_limits<long long>::digits % 3) != 0)
1830 + 1;
1831 char __nar[__nbuf];
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001832#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001833 int __nc = snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001834#else
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001835 int __nc = __snprintf_l(__nar, sizeof(__nar), __cloc(), __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001836#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001837 char* __ne = __nar + __nc;
1838 char* __np = this->__identify_padding(__nar, __ne, __iob);
1839 // Stage 2 - Widen __nar while adding thousands separators
1840 char_type __o[2*(__nbuf-1) - 1];
1841 char_type* __op; // pad here
1842 char_type* __oe; // end of output
1843 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1844 // [__o, __oe) contains thousands_sep'd wide number
1845 // Stage 3 & 4
1846 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1847}
1848
1849template <class _CharT, class _OutputIterator>
1850_OutputIterator
1851num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1852 char_type __fl, unsigned long __v) const
1853{
1854 // Stage 1 - Get number in narrow char
1855 char __fmt[6] = {'%', 0};
1856 const char* __len = "l";
1857 this->__format_int(__fmt+1, __len, false, __iob.flags());
1858 const unsigned __nbuf = (numeric_limits<unsigned long>::digits / 3)
1859 + ((numeric_limits<unsigned long>::digits % 3) != 0)
1860 + 1;
1861 char __nar[__nbuf];
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001862#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001863 int __nc = snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001864#else
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001865 int __nc = __snprintf_l(__nar, sizeof(__nar), __cloc(), __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001866#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001867 char* __ne = __nar + __nc;
1868 char* __np = this->__identify_padding(__nar, __ne, __iob);
1869 // Stage 2 - Widen __nar while adding thousands separators
1870 char_type __o[2*(__nbuf-1) - 1];
1871 char_type* __op; // pad here
1872 char_type* __oe; // end of output
1873 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1874 // [__o, __oe) contains thousands_sep'd wide number
1875 // Stage 3 & 4
1876 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1877}
1878
1879template <class _CharT, class _OutputIterator>
1880_OutputIterator
1881num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1882 char_type __fl, unsigned long long __v) const
1883{
1884 // Stage 1 - Get number in narrow char
1885 char __fmt[8] = {'%', 0};
1886 const char* __len = "ll";
1887 this->__format_int(__fmt+1, __len, false, __iob.flags());
1888 const unsigned __nbuf = (numeric_limits<unsigned long long>::digits / 3)
1889 + ((numeric_limits<unsigned long long>::digits % 3) != 0)
1890 + 1;
1891 char __nar[__nbuf];
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001892#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001893 int __nc = snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001894#else
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00001895 int __nc = __snprintf_l(__nar, sizeof(__nar), __cloc(), __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001896#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001897 char* __ne = __nar + __nc;
1898 char* __np = this->__identify_padding(__nar, __ne, __iob);
1899 // Stage 2 - Widen __nar while adding thousands separators
1900 char_type __o[2*(__nbuf-1) - 1];
1901 char_type* __op; // pad here
1902 char_type* __oe; // end of output
1903 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1904 // [__o, __oe) contains thousands_sep'd wide number
1905 // Stage 3 & 4
1906 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1907}
1908
1909template <class _CharT, class _OutputIterator>
1910_OutputIterator
1911num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1912 char_type __fl, double __v) const
1913{
1914 // Stage 1 - Get number in narrow char
1915 char __fmt[8] = {'%', 0};
1916 const char* __len = "";
1917 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1918 const unsigned __nbuf = 30;
1919 char __nar[__nbuf];
1920 char* __nb = __nar;
1921 int __nc;
1922 if (__specify_precision)
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001923#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001924 __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
Howard Hinnant155c2af2010-05-24 17:49:41 +00001925 (int)__iob.precision(), __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001926#else
1927 __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt,
1928 (int)__iob.precision(), __v);
1929#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001930 else
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001931#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001932 __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001933#else
1934 __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt, __v);
1935#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001936 unique_ptr<char, void(*)(void*)> __nbh(0, free);
1937 if (__nc > static_cast<int>(__nbuf-1))
1938 {
1939 if (__specify_precision)
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001940#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001941 __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001942#else
1943 __nc = __asprintf_l(&__nb, __cloc(), __fmt,
David Chisnall1d581062011-09-21 08:39:44 +00001944 (int)__iob.precision(), __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001945#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001946 else
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001947#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001948 __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001949#else
David Chisnall1d581062011-09-21 08:39:44 +00001950 __nc = __asprintf_l(&__nb, __cloc(), __fmt, (int)__iob.precision(), __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001951#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001952 if (__nb == 0)
1953 __throw_bad_alloc();
1954 __nbh.reset(__nb);
1955 }
1956 char* __ne = __nb + __nc;
1957 char* __np = this->__identify_padding(__nb, __ne, __iob);
1958 // Stage 2 - Widen __nar while adding thousands separators
1959 char_type __o[2*(__nbuf-1) - 1];
1960 char_type* __ob = __o;
1961 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1962 if (__nb != __nar)
1963 {
Howard Hinnant28b24882011-12-01 20:21:04 +00001964 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001965 if (__ob == 0)
1966 __throw_bad_alloc();
1967 __obh.reset(__ob);
1968 }
1969 char_type* __op; // pad here
1970 char_type* __oe; // end of output
1971 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1972 // [__o, __oe) contains thousands_sep'd wide number
1973 // Stage 3 & 4
1974 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1975 return __s;
1976}
1977
1978template <class _CharT, class _OutputIterator>
1979_OutputIterator
1980num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1981 char_type __fl, long double __v) const
1982{
1983 // Stage 1 - Get number in narrow char
1984 char __fmt[8] = {'%', 0};
1985 const char* __len = "L";
1986 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1987 const unsigned __nbuf = 30;
1988 char __nar[__nbuf];
1989 char* __nb = __nar;
1990 int __nc;
1991 if (__specify_precision)
Howard Hinnantf312e3e2011-09-28 23:39:33 +00001992#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001993 __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
Howard Hinnant155c2af2010-05-24 17:49:41 +00001994 (int)__iob.precision(), __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00001995#else
1996 __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt,
1997 (int)__iob.precision(), __v);
1998#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001999 else
Howard Hinnantf312e3e2011-09-28 23:39:33 +00002000#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00002001 __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00002002#else
2003 __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt, __v);
2004#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002005 unique_ptr<char, void(*)(void*)> __nbh(0, free);
2006 if (__nc > static_cast<int>(__nbuf-1))
2007 {
2008 if (__specify_precision)
Howard Hinnantf312e3e2011-09-28 23:39:33 +00002009#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00002010 __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00002011#else
2012 __nc = __asprintf_l(&__nb, __cloc(), __fmt,
David Chisnall1d581062011-09-21 08:39:44 +00002013 (int)__iob.precision(), __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00002014#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015 else
Howard Hinnantf312e3e2011-09-28 23:39:33 +00002016#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant1ab52da2011-09-28 21:05:01 +00002017 __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00002018#else
David Chisnall1d581062011-09-21 08:39:44 +00002019 __nc = __asprintf_l(&__nb, __cloc(), __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00002020#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002021 if (__nb == 0)
2022 __throw_bad_alloc();
2023 __nbh.reset(__nb);
2024 }
2025 char* __ne = __nb + __nc;
2026 char* __np = this->__identify_padding(__nb, __ne, __iob);
2027 // Stage 2 - Widen __nar while adding thousands separators
2028 char_type __o[2*(__nbuf-1) - 1];
2029 char_type* __ob = __o;
2030 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
2031 if (__nb != __nar)
2032 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002033 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002034 if (__ob == 0)
2035 __throw_bad_alloc();
2036 __obh.reset(__ob);
2037 }
2038 char_type* __op; // pad here
2039 char_type* __oe; // end of output
2040 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
2041 // [__o, __oe) contains thousands_sep'd wide number
2042 // Stage 3 & 4
2043 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
2044 return __s;
2045}
2046
2047template <class _CharT, class _OutputIterator>
2048_OutputIterator
2049num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
2050 char_type __fl, const void* __v) const
2051{
2052 // Stage 1 - Get pointer in narrow char
2053 char __fmt[6] = "%p";
2054 const unsigned __nbuf = 20;
2055 char __nar[__nbuf];
Howard Hinnantf312e3e2011-09-28 23:39:33 +00002056#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00002057 int __nc = snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00002058#else
Howard Hinnantb85f2ef2013-07-02 18:42:28 +00002059 int __nc = __snprintf_l(__nar, sizeof(__nar), __cloc(), __fmt, __v);
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00002060#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002061 char* __ne = __nar + __nc;
2062 char* __np = this->__identify_padding(__nar, __ne, __iob);
2063 // Stage 2 - Widen __nar
2064 char_type __o[2*(__nbuf-1) - 1];
2065 char_type* __op; // pad here
2066 char_type* __oe; // end of output
2067 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2068 __ct.widen(__nar, __ne, __o);
2069 __oe = __o + (__ne - __nar);
2070 if (__np == __ne)
2071 __op = __oe;
2072 else
2073 __op = __o + (__np - __nar);
2074 // [__o, __oe) contains wide number
2075 // Stage 3 & 4
2076 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
2077}
2078
Howard Hinnant8ea98242013-08-23 17:37:05 +00002079_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS num_put<char>)
2080_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS num_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002081
2082template <class _CharT, class _InputIterator>
2083_LIBCPP_HIDDEN
2084int
2085__get_up_to_n_digits(_InputIterator& __b, _InputIterator __e,
2086 ios_base::iostate& __err, const ctype<_CharT>& __ct, int __n)
2087{
2088 // Precondition: __n >= 1
2089 if (__b == __e)
2090 {
2091 __err |= ios_base::eofbit | ios_base::failbit;
2092 return 0;
2093 }
2094 // get first digit
2095 _CharT __c = *__b;
2096 if (!__ct.is(ctype_base::digit, __c))
2097 {
2098 __err |= ios_base::failbit;
2099 return 0;
2100 }
2101 int __r = __ct.narrow(__c, 0) - '0';
2102 for (++__b, --__n; __b != __e && __n > 0; ++__b, --__n)
2103 {
2104 // get next digit
2105 __c = *__b;
2106 if (!__ct.is(ctype_base::digit, __c))
2107 return __r;
2108 __r = __r * 10 + __ct.narrow(__c, 0) - '0';
2109 }
2110 if (__b == __e)
2111 __err |= ios_base::eofbit;
2112 return __r;
2113}
2114
Howard Hinnant8331b762013-03-06 23:30:19 +00002115class _LIBCPP_TYPE_VIS time_base
Howard Hinnantc51e1022010-05-11 19:42:16 +00002116{
2117public:
2118 enum dateorder {no_order, dmy, mdy, ymd, ydm};
2119};
2120
2121template <class _CharT>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002122class _LIBCPP_TYPE_VIS __time_get_c_storage
Howard Hinnantc51e1022010-05-11 19:42:16 +00002123{
2124protected:
2125 typedef basic_string<_CharT> string_type;
2126
2127 virtual const string_type* __weeks() const;
2128 virtual const string_type* __months() const;
2129 virtual const string_type* __am_pm() const;
2130 virtual const string_type& __c() const;
2131 virtual const string_type& __r() const;
2132 virtual const string_type& __x() const;
2133 virtual const string_type& __X() const;
2134};
2135
2136template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002137class _LIBCPP_TYPE_VIS_ONLY time_get
Howard Hinnantc51e1022010-05-11 19:42:16 +00002138 : public locale::facet,
2139 public time_base,
2140 private __time_get_c_storage<_CharT>
2141{
2142public:
2143 typedef _CharT char_type;
2144 typedef _InputIterator iter_type;
2145 typedef time_base::dateorder dateorder;
2146 typedef basic_string<char_type> string_type;
2147
2148 _LIBCPP_ALWAYS_INLINE
2149 explicit time_get(size_t __refs = 0)
2150 : locale::facet(__refs) {}
2151
2152 _LIBCPP_ALWAYS_INLINE
2153 dateorder date_order() const
2154 {
2155 return this->do_date_order();
2156 }
2157
2158 _LIBCPP_ALWAYS_INLINE
2159 iter_type get_time(iter_type __b, iter_type __e, ios_base& __iob,
2160 ios_base::iostate& __err, tm* __tm) const
2161 {
2162 return do_get_time(__b, __e, __iob, __err, __tm);
2163 }
2164
2165 _LIBCPP_ALWAYS_INLINE
2166 iter_type get_date(iter_type __b, iter_type __e, ios_base& __iob,
2167 ios_base::iostate& __err, tm* __tm) const
2168 {
2169 return do_get_date(__b, __e, __iob, __err, __tm);
2170 }
2171
2172 _LIBCPP_ALWAYS_INLINE
2173 iter_type get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
2174 ios_base::iostate& __err, tm* __tm) const
2175 {
2176 return do_get_weekday(__b, __e, __iob, __err, __tm);
2177 }
2178
2179 _LIBCPP_ALWAYS_INLINE
2180 iter_type get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
2181 ios_base::iostate& __err, tm* __tm) const
2182 {
2183 return do_get_monthname(__b, __e, __iob, __err, __tm);
2184 }
2185
2186 _LIBCPP_ALWAYS_INLINE
2187 iter_type get_year(iter_type __b, iter_type __e, ios_base& __iob,
2188 ios_base::iostate& __err, tm* __tm) const
2189 {
2190 return do_get_year(__b, __e, __iob, __err, __tm);
2191 }
2192
2193 _LIBCPP_ALWAYS_INLINE
2194 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
2195 ios_base::iostate& __err, tm *__tm,
2196 char __fmt, char __mod = 0) const
2197 {
2198 return do_get(__b, __e, __iob, __err, __tm, __fmt, __mod);
2199 }
2200
2201 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
2202 ios_base::iostate& __err, tm* __tm,
2203 const char_type* __fmtb, const char_type* __fmte) const;
2204
2205 static locale::id id;
2206
2207protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002208 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002209 ~time_get() {}
2210
2211 virtual dateorder do_date_order() const;
2212 virtual iter_type do_get_time(iter_type __b, iter_type __e, ios_base& __iob,
2213 ios_base::iostate& __err, tm* __tm) const;
2214 virtual iter_type do_get_date(iter_type __b, iter_type __e, ios_base& __iob,
2215 ios_base::iostate& __err, tm* __tm) const;
2216 virtual iter_type do_get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
2217 ios_base::iostate& __err, tm* __tm) const;
2218 virtual iter_type do_get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
2219 ios_base::iostate& __err, tm* __tm) const;
2220 virtual iter_type do_get_year(iter_type __b, iter_type __e, ios_base& __iob,
2221 ios_base::iostate& __err, tm* __tm) const;
2222 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
2223 ios_base::iostate& __err, tm* __tm,
2224 char __fmt, char __mod) const;
2225private:
2226 void __get_white_space(iter_type& __b, iter_type __e,
2227 ios_base::iostate& __err, const ctype<char_type>& __ct) const;
2228 void __get_percent(iter_type& __b, iter_type __e, ios_base::iostate& __err,
2229 const ctype<char_type>& __ct) const;
2230
2231 void __get_weekdayname(int& __m,
2232 iter_type& __b, iter_type __e,
2233 ios_base::iostate& __err,
2234 const ctype<char_type>& __ct) const;
2235 void __get_monthname(int& __m,
2236 iter_type& __b, iter_type __e,
2237 ios_base::iostate& __err,
2238 const ctype<char_type>& __ct) const;
2239 void __get_day(int& __d,
2240 iter_type& __b, iter_type __e,
2241 ios_base::iostate& __err,
2242 const ctype<char_type>& __ct) const;
2243 void __get_month(int& __m,
2244 iter_type& __b, iter_type __e,
2245 ios_base::iostate& __err,
2246 const ctype<char_type>& __ct) const;
2247 void __get_year(int& __y,
2248 iter_type& __b, iter_type __e,
2249 ios_base::iostate& __err,
2250 const ctype<char_type>& __ct) const;
2251 void __get_year4(int& __y,
2252 iter_type& __b, iter_type __e,
2253 ios_base::iostate& __err,
2254 const ctype<char_type>& __ct) const;
2255 void __get_hour(int& __d,
2256 iter_type& __b, iter_type __e,
2257 ios_base::iostate& __err,
2258 const ctype<char_type>& __ct) const;
2259 void __get_12_hour(int& __h,
2260 iter_type& __b, iter_type __e,
2261 ios_base::iostate& __err,
2262 const ctype<char_type>& __ct) const;
2263 void __get_am_pm(int& __h,
2264 iter_type& __b, iter_type __e,
2265 ios_base::iostate& __err,
2266 const ctype<char_type>& __ct) const;
2267 void __get_minute(int& __m,
2268 iter_type& __b, iter_type __e,
2269 ios_base::iostate& __err,
2270 const ctype<char_type>& __ct) const;
2271 void __get_second(int& __s,
2272 iter_type& __b, iter_type __e,
2273 ios_base::iostate& __err,
2274 const ctype<char_type>& __ct) const;
2275 void __get_weekday(int& __w,
2276 iter_type& __b, iter_type __e,
2277 ios_base::iostate& __err,
2278 const ctype<char_type>& __ct) const;
2279 void __get_day_year_num(int& __w,
2280 iter_type& __b, iter_type __e,
2281 ios_base::iostate& __err,
2282 const ctype<char_type>& __ct) const;
2283};
2284
2285template <class _CharT, class _InputIterator>
2286locale::id
2287time_get<_CharT, _InputIterator>::id;
2288
2289// time_get primatives
2290
2291template <class _CharT, class _InputIterator>
2292void
2293time_get<_CharT, _InputIterator>::__get_weekdayname(int& __w,
2294 iter_type& __b, iter_type __e,
2295 ios_base::iostate& __err,
2296 const ctype<char_type>& __ct) const
2297{
2298 // Note: ignoring case comes from the POSIX strptime spec
2299 const string_type* __wk = this->__weeks();
Howard Hinnant28b24882011-12-01 20:21:04 +00002300 ptrdiff_t __i = __scan_keyword(__b, __e, __wk, __wk+14, __ct, __err, false) - __wk;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002301 if (__i < 14)
2302 __w = __i % 7;
2303}
2304
2305template <class _CharT, class _InputIterator>
2306void
2307time_get<_CharT, _InputIterator>::__get_monthname(int& __m,
2308 iter_type& __b, iter_type __e,
2309 ios_base::iostate& __err,
2310 const ctype<char_type>& __ct) const
2311{
2312 // Note: ignoring case comes from the POSIX strptime spec
2313 const string_type* __month = this->__months();
Howard Hinnant28b24882011-12-01 20:21:04 +00002314 ptrdiff_t __i = __scan_keyword(__b, __e, __month, __month+24, __ct, __err, false) - __month;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002315 if (__i < 24)
2316 __m = __i % 12;
2317}
2318
2319template <class _CharT, class _InputIterator>
2320void
2321time_get<_CharT, _InputIterator>::__get_day(int& __d,
2322 iter_type& __b, iter_type __e,
2323 ios_base::iostate& __err,
2324 const ctype<char_type>& __ct) const
2325{
2326 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2327 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 31)
2328 __d = __t;
2329 else
2330 __err |= ios_base::failbit;
2331}
2332
2333template <class _CharT, class _InputIterator>
2334void
2335time_get<_CharT, _InputIterator>::__get_month(int& __m,
2336 iter_type& __b, iter_type __e,
2337 ios_base::iostate& __err,
2338 const ctype<char_type>& __ct) const
2339{
2340 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2) - 1;
2341 if (!(__err & ios_base::failbit) && __t <= 11)
2342 __m = __t;
2343 else
2344 __err |= ios_base::failbit;
2345}
2346
2347template <class _CharT, class _InputIterator>
2348void
2349time_get<_CharT, _InputIterator>::__get_year(int& __y,
2350 iter_type& __b, iter_type __e,
2351 ios_base::iostate& __err,
2352 const ctype<char_type>& __ct) const
2353{
2354 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
2355 if (!(__err & ios_base::failbit))
2356 {
2357 if (__t < 69)
2358 __t += 2000;
2359 else if (69 <= __t && __t <= 99)
2360 __t += 1900;
2361 __y = __t - 1900;
2362 }
2363}
2364
2365template <class _CharT, class _InputIterator>
2366void
2367time_get<_CharT, _InputIterator>::__get_year4(int& __y,
2368 iter_type& __b, iter_type __e,
2369 ios_base::iostate& __err,
2370 const ctype<char_type>& __ct) const
2371{
2372 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
2373 if (!(__err & ios_base::failbit))
2374 __y = __t - 1900;
2375}
2376
2377template <class _CharT, class _InputIterator>
2378void
2379time_get<_CharT, _InputIterator>::__get_hour(int& __h,
2380 iter_type& __b, iter_type __e,
2381 ios_base::iostate& __err,
2382 const ctype<char_type>& __ct) const
2383{
2384 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2385 if (!(__err & ios_base::failbit) && __t <= 23)
2386 __h = __t;
2387 else
2388 __err |= ios_base::failbit;
2389}
2390
2391template <class _CharT, class _InputIterator>
2392void
2393time_get<_CharT, _InputIterator>::__get_12_hour(int& __h,
2394 iter_type& __b, iter_type __e,
2395 ios_base::iostate& __err,
2396 const ctype<char_type>& __ct) const
2397{
2398 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2399 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 12)
2400 __h = __t;
2401 else
2402 __err |= ios_base::failbit;
2403}
2404
2405template <class _CharT, class _InputIterator>
2406void
2407time_get<_CharT, _InputIterator>::__get_minute(int& __m,
2408 iter_type& __b, iter_type __e,
2409 ios_base::iostate& __err,
2410 const ctype<char_type>& __ct) const
2411{
2412 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2413 if (!(__err & ios_base::failbit) && __t <= 59)
2414 __m = __t;
2415 else
2416 __err |= ios_base::failbit;
2417}
2418
2419template <class _CharT, class _InputIterator>
2420void
2421time_get<_CharT, _InputIterator>::__get_second(int& __s,
2422 iter_type& __b, iter_type __e,
2423 ios_base::iostate& __err,
2424 const ctype<char_type>& __ct) const
2425{
2426 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2427 if (!(__err & ios_base::failbit) && __t <= 60)
2428 __s = __t;
2429 else
2430 __err |= ios_base::failbit;
2431}
2432
2433template <class _CharT, class _InputIterator>
2434void
2435time_get<_CharT, _InputIterator>::__get_weekday(int& __w,
2436 iter_type& __b, iter_type __e,
2437 ios_base::iostate& __err,
2438 const ctype<char_type>& __ct) const
2439{
2440 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 1);
2441 if (!(__err & ios_base::failbit) && __t <= 6)
2442 __w = __t;
2443 else
2444 __err |= ios_base::failbit;
2445}
2446
2447template <class _CharT, class _InputIterator>
2448void
2449time_get<_CharT, _InputIterator>::__get_day_year_num(int& __d,
2450 iter_type& __b, iter_type __e,
2451 ios_base::iostate& __err,
2452 const ctype<char_type>& __ct) const
2453{
2454 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 3);
2455 if (!(__err & ios_base::failbit) && __t <= 365)
2456 __d = __t;
2457 else
2458 __err |= ios_base::failbit;
2459}
2460
2461template <class _CharT, class _InputIterator>
2462void
2463time_get<_CharT, _InputIterator>::__get_white_space(iter_type& __b, iter_type __e,
2464 ios_base::iostate& __err,
2465 const ctype<char_type>& __ct) const
2466{
2467 for (; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2468 ;
2469 if (__b == __e)
2470 __err |= ios_base::eofbit;
2471}
2472
2473template <class _CharT, class _InputIterator>
2474void
2475time_get<_CharT, _InputIterator>::__get_am_pm(int& __h,
2476 iter_type& __b, iter_type __e,
2477 ios_base::iostate& __err,
2478 const ctype<char_type>& __ct) const
2479{
2480 const string_type* __ap = this->__am_pm();
2481 if (__ap[0].size() + __ap[1].size() == 0)
2482 {
2483 __err |= ios_base::failbit;
2484 return;
2485 }
Howard Hinnant28b24882011-12-01 20:21:04 +00002486 ptrdiff_t __i = __scan_keyword(__b, __e, __ap, __ap+2, __ct, __err, false) - __ap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487 if (__i == 0 && __h == 12)
2488 __h = 0;
2489 else if (__i == 1 && __h < 12)
2490 __h += 12;
2491}
2492
2493template <class _CharT, class _InputIterator>
2494void
2495time_get<_CharT, _InputIterator>::__get_percent(iter_type& __b, iter_type __e,
2496 ios_base::iostate& __err,
2497 const ctype<char_type>& __ct) const
2498{
2499 if (__b == __e)
2500 {
2501 __err |= ios_base::eofbit | ios_base::failbit;
2502 return;
2503 }
2504 if (__ct.narrow(*__b, 0) != '%')
2505 __err |= ios_base::failbit;
2506 else if(++__b == __e)
2507 __err |= ios_base::eofbit;
2508}
2509
2510// time_get end primatives
2511
2512template <class _CharT, class _InputIterator>
2513_InputIterator
2514time_get<_CharT, _InputIterator>::get(iter_type __b, iter_type __e,
2515 ios_base& __iob,
2516 ios_base::iostate& __err, tm* __tm,
2517 const char_type* __fmtb, const char_type* __fmte) const
2518{
2519 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2520 __err = ios_base::goodbit;
2521 while (__fmtb != __fmte && __err == ios_base::goodbit)
2522 {
2523 if (__b == __e)
2524 {
2525 __err = ios_base::failbit;
2526 break;
2527 }
2528 if (__ct.narrow(*__fmtb, 0) == '%')
2529 {
2530 if (++__fmtb == __fmte)
2531 {
2532 __err = ios_base::failbit;
2533 break;
2534 }
2535 char __cmd = __ct.narrow(*__fmtb, 0);
2536 char __opt = '\0';
2537 if (__cmd == 'E' || __cmd == '0')
2538 {
2539 if (++__fmtb == __fmte)
2540 {
2541 __err = ios_base::failbit;
2542 break;
2543 }
2544 __opt = __cmd;
2545 __cmd = __ct.narrow(*__fmtb, 0);
2546 }
2547 __b = do_get(__b, __e, __iob, __err, __tm, __cmd, __opt);
2548 ++__fmtb;
2549 }
2550 else if (__ct.is(ctype_base::space, *__fmtb))
2551 {
2552 for (++__fmtb; __fmtb != __fmte && __ct.is(ctype_base::space, *__fmtb); ++__fmtb)
2553 ;
2554 for ( ; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2555 ;
2556 }
2557 else if (__ct.toupper(*__b) == __ct.toupper(*__fmtb))
2558 {
2559 ++__b;
2560 ++__fmtb;
2561 }
2562 else
2563 __err = ios_base::failbit;
2564 }
2565 if (__b == __e)
2566 __err |= ios_base::eofbit;
2567 return __b;
2568}
2569
2570template <class _CharT, class _InputIterator>
2571typename time_get<_CharT, _InputIterator>::dateorder
2572time_get<_CharT, _InputIterator>::do_date_order() const
2573{
2574 return mdy;
2575}
2576
2577template <class _CharT, class _InputIterator>
2578_InputIterator
2579time_get<_CharT, _InputIterator>::do_get_time(iter_type __b, iter_type __e,
2580 ios_base& __iob,
2581 ios_base::iostate& __err,
2582 tm* __tm) const
2583{
2584 const char_type __fmt[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2585 return get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt)/sizeof(__fmt[0]));
2586}
2587
2588template <class _CharT, class _InputIterator>
2589_InputIterator
2590time_get<_CharT, _InputIterator>::do_get_date(iter_type __b, iter_type __e,
2591 ios_base& __iob,
2592 ios_base::iostate& __err,
2593 tm* __tm) const
2594{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002595 const string_type& __fmt = this->__x();
2596 return get(__b, __e, __iob, __err, __tm, __fmt.data(), __fmt.data() + __fmt.size());
2597}
2598
2599template <class _CharT, class _InputIterator>
2600_InputIterator
2601time_get<_CharT, _InputIterator>::do_get_weekday(iter_type __b, iter_type __e,
2602 ios_base& __iob,
2603 ios_base::iostate& __err,
2604 tm* __tm) const
2605{
2606 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2607 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2608 return __b;
2609}
2610
2611template <class _CharT, class _InputIterator>
2612_InputIterator
2613time_get<_CharT, _InputIterator>::do_get_monthname(iter_type __b, iter_type __e,
2614 ios_base& __iob,
2615 ios_base::iostate& __err,
2616 tm* __tm) const
2617{
2618 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2619 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2620 return __b;
2621}
2622
2623template <class _CharT, class _InputIterator>
2624_InputIterator
2625time_get<_CharT, _InputIterator>::do_get_year(iter_type __b, iter_type __e,
2626 ios_base& __iob,
2627 ios_base::iostate& __err,
2628 tm* __tm) const
2629{
2630 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2631 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2632 return __b;
2633}
2634
2635template <class _CharT, class _InputIterator>
2636_InputIterator
2637time_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
2638 ios_base& __iob,
2639 ios_base::iostate& __err, tm* __tm,
2640 char __fmt, char) const
2641{
2642 __err = ios_base::goodbit;
2643 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2644 switch (__fmt)
2645 {
2646 case 'a':
2647 case 'A':
2648 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2649 break;
2650 case 'b':
2651 case 'B':
2652 case 'h':
2653 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2654 break;
2655 case 'c':
2656 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002657 const string_type& __fm = this->__c();
2658 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659 }
2660 break;
2661 case 'd':
2662 case 'e':
2663 __get_day(__tm->tm_mday, __b, __e, __err, __ct);
2664 break;
2665 case 'D':
2666 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002667 const char_type __fm[] = {'%', 'm', '/', '%', 'd', '/', '%', 'y'};
2668 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002669 }
2670 break;
Howard Hinnantf60ff4e2011-04-10 17:54:14 +00002671 case 'F':
2672 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002673 const char_type __fm[] = {'%', 'Y', '-', '%', 'm', '-', '%', 'd'};
2674 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantf60ff4e2011-04-10 17:54:14 +00002675 }
2676 break;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002677 case 'H':
2678 __get_hour(__tm->tm_hour, __b, __e, __err, __ct);
2679 break;
2680 case 'I':
2681 __get_12_hour(__tm->tm_hour, __b, __e, __err, __ct);
2682 break;
2683 case 'j':
2684 __get_day_year_num(__tm->tm_yday, __b, __e, __err, __ct);
2685 break;
2686 case 'm':
2687 __get_month(__tm->tm_mon, __b, __e, __err, __ct);
2688 break;
2689 case 'M':
2690 __get_minute(__tm->tm_min, __b, __e, __err, __ct);
2691 break;
2692 case 'n':
2693 case 't':
2694 __get_white_space(__b, __e, __err, __ct);
2695 break;
2696 case 'p':
2697 __get_am_pm(__tm->tm_hour, __b, __e, __err, __ct);
2698 break;
2699 case 'r':
2700 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002701 const char_type __fm[] = {'%', 'I', ':', '%', 'M', ':', '%', 'S', ' ', '%', 'p'};
2702 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002703 }
2704 break;
2705 case 'R':
2706 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002707 const char_type __fm[] = {'%', 'H', ':', '%', 'M'};
2708 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002709 }
2710 break;
2711 case 'S':
2712 __get_second(__tm->tm_sec, __b, __e, __err, __ct);
2713 break;
2714 case 'T':
2715 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002716 const char_type __fm[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2717 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002718 }
2719 break;
2720 case 'w':
2721 __get_weekday(__tm->tm_wday, __b, __e, __err, __ct);
2722 break;
2723 case 'x':
2724 return do_get_date(__b, __e, __iob, __err, __tm);
2725 case 'X':
2726 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002727 const string_type& __fm = this->__X();
2728 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002729 }
2730 break;
2731 case 'y':
2732 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2733 break;
2734 case 'Y':
2735 __get_year4(__tm->tm_year, __b, __e, __err, __ct);
2736 break;
2737 case '%':
2738 __get_percent(__b, __e, __err, __ct);
2739 break;
2740 default:
2741 __err |= ios_base::failbit;
2742 }
2743 return __b;
2744}
2745
Howard Hinnant8ea98242013-08-23 17:37:05 +00002746_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_get<char>)
2747_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_get<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002748
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002749class _LIBCPP_TYPE_VIS __time_get
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750{
2751protected:
2752 locale_t __loc_;
2753
2754 __time_get(const char* __nm);
2755 __time_get(const string& __nm);
2756 ~__time_get();
2757};
2758
2759template <class _CharT>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002760class _LIBCPP_TYPE_VIS __time_get_storage
Howard Hinnantc51e1022010-05-11 19:42:16 +00002761 : public __time_get
2762{
2763protected:
2764 typedef basic_string<_CharT> string_type;
2765
2766 string_type __weeks_[14];
2767 string_type __months_[24];
2768 string_type __am_pm_[2];
2769 string_type __c_;
2770 string_type __r_;
2771 string_type __x_;
2772 string_type __X_;
2773
2774 explicit __time_get_storage(const char* __nm);
2775 explicit __time_get_storage(const string& __nm);
2776
2777 _LIBCPP_ALWAYS_INLINE ~__time_get_storage() {}
2778
2779 time_base::dateorder __do_date_order() const;
2780
2781private:
2782 void init(const ctype<_CharT>&);
2783 string_type __analyze(char __fmt, const ctype<_CharT>&);
2784};
2785
2786template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002787class _LIBCPP_TYPE_VIS_ONLY time_get_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00002788 : public time_get<_CharT, _InputIterator>,
2789 private __time_get_storage<_CharT>
2790{
2791public:
2792 typedef time_base::dateorder dateorder;
2793 typedef _InputIterator iter_type;
2794 typedef _CharT char_type;
2795 typedef basic_string<char_type> string_type;
2796
Howard Hinnant756c69b2010-09-22 16:48:34 +00002797 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002798 explicit time_get_byname(const char* __nm, size_t __refs = 0)
2799 : time_get<_CharT, _InputIterator>(__refs),
2800 __time_get_storage<_CharT>(__nm) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002802 explicit time_get_byname(const string& __nm, size_t __refs = 0)
2803 : time_get<_CharT, _InputIterator>(__refs),
2804 __time_get_storage<_CharT>(__nm) {}
2805
2806protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002807 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002808 ~time_get_byname() {}
2809
Howard Hinnant756c69b2010-09-22 16:48:34 +00002810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002811 virtual dateorder do_date_order() const {return this->__do_date_order();}
2812private:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002813 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002814 virtual const string_type* __weeks() const {return this->__weeks_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002815 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816 virtual const string_type* __months() const {return this->__months_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002817 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002818 virtual const string_type* __am_pm() const {return this->__am_pm_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002819 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002820 virtual const string_type& __c() const {return this->__c_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002821 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822 virtual const string_type& __r() const {return this->__r_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824 virtual const string_type& __x() const {return this->__x_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002825 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002826 virtual const string_type& __X() const {return this->__X_;}
2827};
2828
Howard Hinnant8ea98242013-08-23 17:37:05 +00002829_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_get_byname<char>)
2830_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_get_byname<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002831
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002832class _LIBCPP_TYPE_VIS __time_put
Howard Hinnantc51e1022010-05-11 19:42:16 +00002833{
2834 locale_t __loc_;
2835protected:
Howard Hinnant1ab52da2011-09-28 21:05:01 +00002836 _LIBCPP_ALWAYS_INLINE __time_put() : __loc_(_LIBCPP_GET_C_LOCALE) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002837 __time_put(const char* __nm);
2838 __time_put(const string& __nm);
2839 ~__time_put();
2840 void __do_put(char* __nb, char*& __ne, const tm* __tm,
2841 char __fmt, char __mod) const;
2842 void __do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm,
2843 char __fmt, char __mod) const;
2844};
2845
2846template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002847class _LIBCPP_TYPE_VIS_ONLY time_put
Howard Hinnantc51e1022010-05-11 19:42:16 +00002848 : public locale::facet,
2849 private __time_put
2850{
2851public:
2852 typedef _CharT char_type;
2853 typedef _OutputIterator iter_type;
2854
2855 _LIBCPP_ALWAYS_INLINE
2856 explicit time_put(size_t __refs = 0)
2857 : locale::facet(__refs) {}
2858
2859 iter_type put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm,
2860 const char_type* __pb, const char_type* __pe) const;
2861
2862 _LIBCPP_ALWAYS_INLINE
2863 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
2864 const tm* __tm, char __fmt, char __mod = 0) const
2865 {
2866 return do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2867 }
2868
2869 static locale::id id;
2870
2871protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002872 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873 ~time_put() {}
2874 virtual iter_type do_put(iter_type __s, ios_base&, char_type, const tm* __tm,
2875 char __fmt, char __mod) const;
2876
Howard Hinnant756c69b2010-09-22 16:48:34 +00002877 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002878 explicit time_put(const char* __nm, size_t __refs)
2879 : locale::facet(__refs),
2880 __time_put(__nm) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002881 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002882 explicit time_put(const string& __nm, size_t __refs)
2883 : locale::facet(__refs),
2884 __time_put(__nm) {}
2885};
2886
2887template <class _CharT, class _OutputIterator>
2888locale::id
2889time_put<_CharT, _OutputIterator>::id;
2890
2891template <class _CharT, class _OutputIterator>
2892_OutputIterator
2893time_put<_CharT, _OutputIterator>::put(iter_type __s, ios_base& __iob,
2894 char_type __fl, const tm* __tm,
2895 const char_type* __pb,
2896 const char_type* __pe) const
2897{
2898 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2899 for (; __pb != __pe; ++__pb)
2900 {
2901 if (__ct.narrow(*__pb, 0) == '%')
2902 {
2903 if (++__pb == __pe)
2904 {
2905 *__s++ = __pb[-1];
2906 break;
2907 }
2908 char __mod = 0;
2909 char __fmt = __ct.narrow(*__pb, 0);
2910 if (__fmt == 'E' || __fmt == 'O')
2911 {
2912 if (++__pb == __pe)
2913 {
2914 *__s++ = __pb[-2];
2915 *__s++ = __pb[-1];
2916 break;
2917 }
2918 __mod = __fmt;
2919 __fmt = __ct.narrow(*__pb, 0);
2920 }
2921 __s = do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2922 }
2923 else
2924 *__s++ = *__pb;
2925 }
2926 return __s;
2927}
2928
2929template <class _CharT, class _OutputIterator>
2930_OutputIterator
Howard Hinnant28b24882011-12-01 20:21:04 +00002931time_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002932 char_type, const tm* __tm,
2933 char __fmt, char __mod) const
2934{
2935 char_type __nar[100];
2936 char_type* __nb = __nar;
2937 char_type* __ne = __nb + 100;
2938 __do_put(__nb, __ne, __tm, __fmt, __mod);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002939 return _VSTD::copy(__nb, __ne, __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002940}
2941
Howard Hinnant8ea98242013-08-23 17:37:05 +00002942_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_put<char>)
2943_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002944
2945template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002946class _LIBCPP_TYPE_VIS_ONLY time_put_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00002947 : public time_put<_CharT, _OutputIterator>
2948{
2949public:
2950 _LIBCPP_ALWAYS_INLINE
2951 explicit time_put_byname(const char* __nm, size_t __refs = 0)
2952 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2953
2954 _LIBCPP_ALWAYS_INLINE
2955 explicit time_put_byname(const string& __nm, size_t __refs = 0)
2956 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2957
2958protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002959 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002960 ~time_put_byname() {}
2961};
2962
Howard Hinnant8ea98242013-08-23 17:37:05 +00002963_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_put_byname<char>)
2964_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS time_put_byname<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002965
2966// money_base
2967
Howard Hinnant8331b762013-03-06 23:30:19 +00002968class _LIBCPP_TYPE_VIS money_base
Howard Hinnantc51e1022010-05-11 19:42:16 +00002969{
2970public:
2971 enum part {none, space, symbol, sign, value};
2972 struct pattern {char field[4];};
2973
2974 _LIBCPP_ALWAYS_INLINE money_base() {}
2975};
2976
2977// moneypunct
2978
2979template <class _CharT, bool _International = false>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002980class _LIBCPP_TYPE_VIS_ONLY moneypunct
Howard Hinnantc51e1022010-05-11 19:42:16 +00002981 : public locale::facet,
2982 public money_base
2983{
2984public:
2985 typedef _CharT char_type;
2986 typedef basic_string<char_type> string_type;
2987
Howard Hinnant756c69b2010-09-22 16:48:34 +00002988 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002989 explicit moneypunct(size_t __refs = 0)
2990 : locale::facet(__refs) {}
2991
2992 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
2993 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
2994 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
2995 _LIBCPP_ALWAYS_INLINE string_type curr_symbol() const {return do_curr_symbol();}
2996 _LIBCPP_ALWAYS_INLINE string_type positive_sign() const {return do_positive_sign();}
2997 _LIBCPP_ALWAYS_INLINE string_type negative_sign() const {return do_negative_sign();}
2998 _LIBCPP_ALWAYS_INLINE int frac_digits() const {return do_frac_digits();}
2999 _LIBCPP_ALWAYS_INLINE pattern pos_format() const {return do_pos_format();}
3000 _LIBCPP_ALWAYS_INLINE pattern neg_format() const {return do_neg_format();}
3001
3002 static locale::id id;
3003 static const bool intl = _International;
3004
3005protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003006 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00003007 ~moneypunct() {}
3008
3009 virtual char_type do_decimal_point() const {return numeric_limits<char_type>::max();}
3010 virtual char_type do_thousands_sep() const {return numeric_limits<char_type>::max();}
3011 virtual string do_grouping() const {return string();}
3012 virtual string_type do_curr_symbol() const {return string_type();}
3013 virtual string_type do_positive_sign() const {return string_type();}
3014 virtual string_type do_negative_sign() const {return string_type(1, '-');}
3015 virtual int do_frac_digits() const {return 0;}
3016 virtual pattern do_pos_format() const
Howard Hinnantb10e6cf2012-11-06 21:48:33 +00003017 {pattern __p = {{symbol, sign, none, value}}; return __p;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003018 virtual pattern do_neg_format() const
Howard Hinnantb10e6cf2012-11-06 21:48:33 +00003019 {pattern __p = {{symbol, sign, none, value}}; return __p;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003020};
3021
3022template <class _CharT, bool _International>
3023locale::id
3024moneypunct<_CharT, _International>::id;
3025
Howard Hinnant2c45cb42012-12-12 21:14:28 +00003026template <class _CharT, bool _International>
3027const bool
3028moneypunct<_CharT, _International>::intl;
3029
Howard Hinnant8ea98242013-08-23 17:37:05 +00003030_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct<char, false>)
3031_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct<char, true>)
3032_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct<wchar_t, false>)
3033_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct<wchar_t, true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003034
3035// moneypunct_byname
3036
3037template <class _CharT, bool _International = false>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003038class _LIBCPP_TYPE_VIS_ONLY moneypunct_byname
Howard Hinnant756c69b2010-09-22 16:48:34 +00003039 : public moneypunct<_CharT, _International>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003040{
3041public:
3042 typedef money_base::pattern pattern;
3043 typedef _CharT char_type;
3044 typedef basic_string<char_type> string_type;
3045
3046 _LIBCPP_ALWAYS_INLINE
3047 explicit moneypunct_byname(const char* __nm, size_t __refs = 0)
3048 : moneypunct<_CharT, _International>(__refs) {init(__nm);}
3049
3050 _LIBCPP_ALWAYS_INLINE
3051 explicit moneypunct_byname(const string& __nm, size_t __refs = 0)
3052 : moneypunct<_CharT, _International>(__refs) {init(__nm.c_str());}
3053
3054protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003055 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00003056 ~moneypunct_byname() {}
3057
3058 virtual char_type do_decimal_point() const {return __decimal_point_;}
3059 virtual char_type do_thousands_sep() const {return __thousands_sep_;}
3060 virtual string do_grouping() const {return __grouping_;}
3061 virtual string_type do_curr_symbol() const {return __curr_symbol_;}
3062 virtual string_type do_positive_sign() const {return __positive_sign_;}
3063 virtual string_type do_negative_sign() const {return __negative_sign_;}
3064 virtual int do_frac_digits() const {return __frac_digits_;}
3065 virtual pattern do_pos_format() const {return __pos_format_;}
3066 virtual pattern do_neg_format() const {return __neg_format_;}
3067
3068private:
3069 char_type __decimal_point_;
3070 char_type __thousands_sep_;
3071 string __grouping_;
3072 string_type __curr_symbol_;
3073 string_type __positive_sign_;
3074 string_type __negative_sign_;
3075 int __frac_digits_;
3076 pattern __pos_format_;
3077 pattern __neg_format_;
3078
3079 void init(const char*);
3080};
3081
3082template<> void moneypunct_byname<char, false>::init(const char*);
3083template<> void moneypunct_byname<char, true>::init(const char*);
3084template<> void moneypunct_byname<wchar_t, false>::init(const char*);
3085template<> void moneypunct_byname<wchar_t, true>::init(const char*);
3086
Howard Hinnant8ea98242013-08-23 17:37:05 +00003087_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct_byname<char, false>)
3088_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct_byname<char, true>)
3089_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct_byname<wchar_t, false>)
3090_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS moneypunct_byname<wchar_t, true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003091
3092// money_get
3093
3094template <class _CharT>
3095class __money_get
3096{
3097protected:
3098 typedef _CharT char_type;
3099 typedef basic_string<char_type> string_type;
3100
3101 _LIBCPP_ALWAYS_INLINE __money_get() {}
3102
3103 static void __gather_info(bool __intl, const locale& __loc,
3104 money_base::pattern& __pat, char_type& __dp,
3105 char_type& __ts, string& __grp,
3106 string_type& __sym, string_type& __psn,
3107 string_type& __nsn, int& __fd);
3108};
3109
3110template <class _CharT>
3111void
3112__money_get<_CharT>::__gather_info(bool __intl, const locale& __loc,
3113 money_base::pattern& __pat, char_type& __dp,
3114 char_type& __ts, string& __grp,
3115 string_type& __sym, string_type& __psn,
3116 string_type& __nsn, int& __fd)
3117{
3118 if (__intl)
3119 {
3120 const moneypunct<char_type, true>& __mp =
3121 use_facet<moneypunct<char_type, true> >(__loc);
3122 __pat = __mp.neg_format();
3123 __nsn = __mp.negative_sign();
3124 __psn = __mp.positive_sign();
3125 __dp = __mp.decimal_point();
3126 __ts = __mp.thousands_sep();
3127 __grp = __mp.grouping();
3128 __sym = __mp.curr_symbol();
3129 __fd = __mp.frac_digits();
3130 }
3131 else
3132 {
3133 const moneypunct<char_type, false>& __mp =
3134 use_facet<moneypunct<char_type, false> >(__loc);
3135 __pat = __mp.neg_format();
3136 __nsn = __mp.negative_sign();
3137 __psn = __mp.positive_sign();
3138 __dp = __mp.decimal_point();
3139 __ts = __mp.thousands_sep();
3140 __grp = __mp.grouping();
3141 __sym = __mp.curr_symbol();
3142 __fd = __mp.frac_digits();
3143 }
3144}
3145
Howard Hinnant8ea98242013-08-23 17:37:05 +00003146_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS __money_get<char>)
3147_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS __money_get<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003148
3149template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003150class _LIBCPP_TYPE_VIS_ONLY money_get
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151 : public locale::facet,
3152 private __money_get<_CharT>
3153{
3154public:
3155 typedef _CharT char_type;
3156 typedef _InputIterator iter_type;
3157 typedef basic_string<char_type> string_type;
3158
3159 _LIBCPP_ALWAYS_INLINE
3160 explicit money_get(size_t __refs = 0)
3161 : locale::facet(__refs) {}
3162
3163 _LIBCPP_ALWAYS_INLINE
3164 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
3165 ios_base::iostate& __err, long double& __v) const
3166 {
3167 return do_get(__b, __e, __intl, __iob, __err, __v);
3168 }
3169
3170 _LIBCPP_ALWAYS_INLINE
3171 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
3172 ios_base::iostate& __err, string_type& __v) const
3173 {
3174 return do_get(__b, __e, __intl, __iob, __err, __v);
3175 }
3176
3177 static locale::id id;
3178
3179protected:
3180
Howard Hinnant756c69b2010-09-22 16:48:34 +00003181 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00003182 ~money_get() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003183
Howard Hinnantc51e1022010-05-11 19:42:16 +00003184 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
3185 ios_base& __iob, ios_base::iostate& __err,
3186 long double& __v) const;
3187 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
3188 ios_base& __iob, ios_base::iostate& __err,
3189 string_type& __v) const;
3190
3191private:
3192 static bool __do_get(iter_type& __b, iter_type __e,
3193 bool __intl, const locale& __loc,
3194 ios_base::fmtflags __flags, ios_base::iostate& __err,
3195 bool& __neg, const ctype<char_type>& __ct,
3196 unique_ptr<char_type, void(*)(void*)>& __wb,
3197 char_type*& __wn, char_type* __we);
3198};
3199
3200template <class _CharT, class _InputIterator>
3201locale::id
3202money_get<_CharT, _InputIterator>::id;
3203
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003204_LIBCPP_FUNC_VIS void __do_nothing(void*);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003205
3206template <class _Tp>
3207_LIBCPP_HIDDEN
3208void
3209__double_or_nothing(unique_ptr<_Tp, void(*)(void*)>& __b, _Tp*& __n, _Tp*& __e)
3210{
3211 bool __owns = __b.get_deleter() != __do_nothing;
Howard Hinnant28b24882011-12-01 20:21:04 +00003212 size_t __cur_cap = static_cast<size_t>(__e-__b.get()) * sizeof(_Tp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003213 size_t __new_cap = __cur_cap < numeric_limits<size_t>::max() / 2 ?
3214 2 * __cur_cap : numeric_limits<size_t>::max();
Howard Hinnant28b24882011-12-01 20:21:04 +00003215 size_t __n_off = static_cast<size_t>(__n - __b.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003216 _Tp* __t = (_Tp*)realloc(__owns ? __b.get() : 0, __new_cap);
3217 if (__t == 0)
3218 __throw_bad_alloc();
3219 if (__owns)
3220 __b.release();
3221 __b = unique_ptr<_Tp, void(*)(void*)>(__t, free);
3222 __new_cap /= sizeof(_Tp);
3223 __n = __b.get() + __n_off;
3224 __e = __b.get() + __new_cap;
3225}
3226
3227// true == success
3228template <class _CharT, class _InputIterator>
3229bool
3230money_get<_CharT, _InputIterator>::__do_get(iter_type& __b, iter_type __e,
3231 bool __intl, const locale& __loc,
3232 ios_base::fmtflags __flags,
3233 ios_base::iostate& __err,
3234 bool& __neg,
3235 const ctype<char_type>& __ct,
3236 unique_ptr<char_type, void(*)(void*)>& __wb,
3237 char_type*& __wn, char_type* __we)
3238{
3239 const unsigned __bz = 100;
3240 unsigned __gbuf[__bz];
3241 unique_ptr<unsigned, void(*)(void*)> __gb(__gbuf, __do_nothing);
3242 unsigned* __gn = __gb.get();
3243 unsigned* __ge = __gn + __bz;
3244 money_base::pattern __pat;
3245 char_type __dp;
3246 char_type __ts;
3247 string __grp;
3248 string_type __sym;
3249 string_type __psn;
3250 string_type __nsn;
Jeffrey Yasskin6b045fb2012-03-10 18:31:43 +00003251 // Capture the spaces read into money_base::{space,none} so they
3252 // can be compared to initial spaces in __sym.
3253 string_type __spaces;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003254 int __fd;
3255 __money_get<_CharT>::__gather_info(__intl, __loc, __pat, __dp, __ts, __grp,
3256 __sym, __psn, __nsn, __fd);
3257 const string_type* __trailing_sign = 0;
3258 __wn = __wb.get();
3259 for (unsigned __p = 0; __p < 4 && __b != __e; ++__p)
3260 {
3261 switch (__pat.field[__p])
3262 {
3263 case money_base::space:
3264 if (__p != 3)
3265 {
3266 if (__ct.is(ctype_base::space, *__b))
Jeffrey Yasskin6b045fb2012-03-10 18:31:43 +00003267 __spaces.push_back(*__b++);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003268 else
3269 {
3270 __err |= ios_base::failbit;
3271 return false;
3272 }
3273 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003274 // drop through
Howard Hinnantc51e1022010-05-11 19:42:16 +00003275 case money_base::none:
3276 if (__p != 3)
3277 {
3278 while (__b != __e && __ct.is(ctype_base::space, *__b))
Jeffrey Yasskin6b045fb2012-03-10 18:31:43 +00003279 __spaces.push_back(*__b++);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003280 }
3281 break;
3282 case money_base::sign:
3283 if (__psn.size() + __nsn.size() > 0)
3284 {
3285 if (__psn.size() == 0 || __nsn.size() == 0)
3286 { // sign is optional
3287 if (__psn.size() > 0)
3288 { // __nsn.size() == 0
3289 if (*__b == __psn[0])
3290 {
3291 ++__b;
3292 if (__psn.size() > 1)
3293 __trailing_sign = &__psn;
3294 }
3295 else
3296 __neg = true;
3297 }
3298 else if (*__b == __nsn[0]) // __nsn.size() > 0 && __psn.size() == 0
3299 {
3300 ++__b;
3301 __neg = true;
3302 if (__nsn.size() > 1)
3303 __trailing_sign = &__nsn;
3304 }
3305 }
3306 else // sign is required
3307 {
3308 if (*__b == __psn[0])
3309 {
3310 ++__b;
3311 if (__psn.size() > 1)
3312 __trailing_sign = &__psn;
3313 }
3314 else if (*__b == __nsn[0])
3315 {
3316 ++__b;
3317 __neg = true;
3318 if (__nsn.size() > 1)
3319 __trailing_sign = &__nsn;
3320 }
3321 else
3322 {
3323 __err |= ios_base::failbit;
3324 return false;
3325 }
3326 }
3327 }
3328 break;
3329 case money_base::symbol:
3330 {
3331 bool __more_needed = __trailing_sign ||
3332 (__p < 2) ||
3333 (__p == 2 && __pat.field[3] != static_cast<char>(money_base::none));
3334 bool __sb = __flags & ios_base::showbase;
3335 if (__sb || __more_needed)
3336 {
Jeffrey Yasskin6b045fb2012-03-10 18:31:43 +00003337 typename string_type::const_iterator __sym_space_end = __sym.begin();
3338 if (__p > 0 && (__pat.field[__p - 1] == money_base::none ||
3339 __pat.field[__p - 1] == money_base::space)) {
3340 // Match spaces we've already read against spaces at
3341 // the beginning of __sym.
3342 while (__sym_space_end != __sym.end() &&
3343 __ct.is(ctype_base::space, *__sym_space_end))
3344 ++__sym_space_end;
3345 const size_t __num_spaces = __sym_space_end - __sym.begin();
3346 if (__num_spaces > __spaces.size() ||
3347 !equal(__spaces.end() - __num_spaces, __spaces.end(),
3348 __sym.begin())) {
3349 // No match. Put __sym_space_end back at the
3350 // beginning of __sym, which will prevent a
3351 // match in the next loop.
3352 __sym_space_end = __sym.begin();
3353 }
3354 }
3355 typename string_type::const_iterator __sym_curr_char = __sym_space_end;
3356 while (__sym_curr_char != __sym.end() && __b != __e &&
3357 *__b == *__sym_curr_char) {
3358 ++__b;
3359 ++__sym_curr_char;
3360 }
3361 if (__sb && __sym_curr_char != __sym.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003362 {
3363 __err |= ios_base::failbit;
3364 return false;
3365 }
3366 }
3367 }
3368 break;
3369 case money_base::value:
3370 {
3371 unsigned __ng = 0;
3372 for (; __b != __e; ++__b)
3373 {
3374 char_type __c = *__b;
3375 if (__ct.is(ctype_base::digit, __c))
3376 {
3377 if (__wn == __we)
3378 __double_or_nothing(__wb, __wn, __we);
3379 *__wn++ = __c;
3380 ++__ng;
3381 }
3382 else if (__grp.size() > 0 && __ng > 0 && __c == __ts)
3383 {
3384 if (__gn == __ge)
3385 __double_or_nothing(__gb, __gn, __ge);
3386 *__gn++ = __ng;
3387 __ng = 0;
3388 }
3389 else
3390 break;
3391 }
3392 if (__gb.get() != __gn && __ng > 0)
3393 {
3394 if (__gn == __ge)
3395 __double_or_nothing(__gb, __gn, __ge);
3396 *__gn++ = __ng;
3397 }
3398 if (__fd > 0)
3399 {
3400 if (__b == __e || *__b != __dp)
3401 {
3402 __err |= ios_base::failbit;
3403 return false;
3404 }
3405 for (++__b; __fd > 0; --__fd, ++__b)
3406 {
3407 if (__b == __e || !__ct.is(ctype_base::digit, *__b))
3408 {
3409 __err |= ios_base::failbit;
3410 return false;
3411 }
3412 if (__wn == __we)
3413 __double_or_nothing(__wb, __wn, __we);
3414 *__wn++ = *__b;
3415 }
3416 }
3417 if (__wn == __wb.get())
3418 {
3419 __err |= ios_base::failbit;
3420 return false;
3421 }
3422 }
3423 break;
3424 }
3425 }
3426 if (__trailing_sign)
3427 {
3428 for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, ++__b)
3429 {
3430 if (__b == __e || *__b != (*__trailing_sign)[__i])
3431 {
3432 __err |= ios_base::failbit;
3433 return false;
3434 }
3435 }
3436 }
3437 if (__gb.get() != __gn)
3438 {
3439 ios_base::iostate __et = ios_base::goodbit;
3440 __check_grouping(__grp, __gb.get(), __gn, __et);
3441 if (__et)
3442 {
3443 __err |= ios_base::failbit;
3444 return false;
3445 }
3446 }
3447 return true;
3448}
3449
3450template <class _CharT, class _InputIterator>
3451_InputIterator
3452money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3453 bool __intl, ios_base& __iob,
3454 ios_base::iostate& __err,
3455 long double& __v) const
3456{
Howard Hinnant28b24882011-12-01 20:21:04 +00003457 const int __bz = 100;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003458 char_type __wbuf[__bz];
3459 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3460 char_type* __wn;
3461 char_type* __we = __wbuf + __bz;
3462 locale __loc = __iob.getloc();
3463 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3464 bool __neg = false;
3465 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3466 __wb, __wn, __we))
3467 {
3468 const char __src[] = "0123456789";
3469 char_type __atoms[sizeof(__src)-1];
3470 __ct.widen(__src, __src + (sizeof(__src)-1), __atoms);
3471 char __nbuf[__bz];
3472 char* __nc = __nbuf;
3473 unique_ptr<char, void(*)(void*)> __h(0, free);
3474 if (__wn - __wb.get() > __bz-2)
3475 {
Howard Hinnant28b24882011-12-01 20:21:04 +00003476 __h.reset((char*)malloc(static_cast<size_t>(__wn - __wb.get() + 2)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003477 if (__h.get() == 0)
3478 __throw_bad_alloc();
3479 __nc = __h.get();
3480 }
3481 if (__neg)
3482 *__nc++ = '-';
3483 for (const char_type* __w = __wb.get(); __w < __wn; ++__w, ++__nc)
Marshall Clow1e68fd42013-03-22 02:14:40 +00003484 *__nc = __src[find(__atoms, _VSTD::end(__atoms), *__w) - __atoms];
Howard Hinnantc51e1022010-05-11 19:42:16 +00003485 *__nc = char();
3486 if (sscanf(__nbuf, "%Lf", &__v) != 1)
3487 __throw_runtime_error("money_get error");
3488 }
3489 if (__b == __e)
3490 __err |= ios_base::eofbit;
3491 return __b;
3492}
3493
3494template <class _CharT, class _InputIterator>
3495_InputIterator
3496money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3497 bool __intl, ios_base& __iob,
3498 ios_base::iostate& __err,
3499 string_type& __v) const
3500{
Howard Hinnant28b24882011-12-01 20:21:04 +00003501 const int __bz = 100;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003502 char_type __wbuf[__bz];
3503 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3504 char_type* __wn;
3505 char_type* __we = __wbuf + __bz;
3506 locale __loc = __iob.getloc();
3507 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3508 bool __neg = false;
3509 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3510 __wb, __wn, __we))
3511 {
3512 __v.clear();
3513 if (__neg)
3514 __v.push_back(__ct.widen('-'));
3515 char_type __z = __ct.widen('0');
3516 char_type* __w;
3517 for (__w = __wb.get(); __w < __wn-1; ++__w)
3518 if (*__w != __z)
3519 break;
3520 __v.append(__w, __wn);
3521 }
3522 if (__b == __e)
3523 __err |= ios_base::eofbit;
3524 return __b;
3525}
3526
Howard Hinnant8ea98242013-08-23 17:37:05 +00003527_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS money_get<char>)
3528_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS money_get<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003529
3530// money_put
3531
3532template <class _CharT>
3533class __money_put
3534{
3535protected:
3536 typedef _CharT char_type;
3537 typedef basic_string<char_type> string_type;
3538
3539 _LIBCPP_ALWAYS_INLINE __money_put() {}
3540
3541 static void __gather_info(bool __intl, bool __neg, const locale& __loc,
3542 money_base::pattern& __pat, char_type& __dp,
3543 char_type& __ts, string& __grp,
3544 string_type& __sym, string_type& __sn,
3545 int& __fd);
3546 static void __format(char_type* __mb, char_type*& __mi, char_type*& __me,
3547 ios_base::fmtflags __flags,
3548 const char_type* __db, const char_type* __de,
3549 const ctype<char_type>& __ct, bool __neg,
3550 const money_base::pattern& __pat, char_type __dp,
3551 char_type __ts, const string& __grp,
3552 const string_type& __sym, const string_type& __sn,
3553 int __fd);
3554};
3555
3556template <class _CharT>
3557void
3558__money_put<_CharT>::__gather_info(bool __intl, bool __neg, const locale& __loc,
3559 money_base::pattern& __pat, char_type& __dp,
3560 char_type& __ts, string& __grp,
3561 string_type& __sym, string_type& __sn,
3562 int& __fd)
3563{
3564 if (__intl)
3565 {
3566 const moneypunct<char_type, true>& __mp =
3567 use_facet<moneypunct<char_type, true> >(__loc);
3568 if (__neg)
3569 {
3570 __pat = __mp.neg_format();
3571 __sn = __mp.negative_sign();
3572 }
3573 else
3574 {
3575 __pat = __mp.pos_format();
3576 __sn = __mp.positive_sign();
3577 }
3578 __dp = __mp.decimal_point();
3579 __ts = __mp.thousands_sep();
3580 __grp = __mp.grouping();
3581 __sym = __mp.curr_symbol();
3582 __fd = __mp.frac_digits();
3583 }
3584 else
3585 {
3586 const moneypunct<char_type, false>& __mp =
3587 use_facet<moneypunct<char_type, false> >(__loc);
3588 if (__neg)
3589 {
3590 __pat = __mp.neg_format();
3591 __sn = __mp.negative_sign();
3592 }
3593 else
3594 {
3595 __pat = __mp.pos_format();
3596 __sn = __mp.positive_sign();
3597 }
3598 __dp = __mp.decimal_point();
3599 __ts = __mp.thousands_sep();
3600 __grp = __mp.grouping();
3601 __sym = __mp.curr_symbol();
3602 __fd = __mp.frac_digits();
3603 }
3604}
3605
3606template <class _CharT>
3607void
3608__money_put<_CharT>::__format(char_type* __mb, char_type*& __mi, char_type*& __me,
3609 ios_base::fmtflags __flags,
3610 const char_type* __db, const char_type* __de,
3611 const ctype<char_type>& __ct, bool __neg,
3612 const money_base::pattern& __pat, char_type __dp,
3613 char_type __ts, const string& __grp,
3614 const string_type& __sym, const string_type& __sn,
3615 int __fd)
3616{
3617 __me = __mb;
3618 for (unsigned __p = 0; __p < 4; ++__p)
3619 {
3620 switch (__pat.field[__p])
3621 {
3622 case money_base::none:
3623 __mi = __me;
3624 break;
3625 case money_base::space:
3626 __mi = __me;
3627 *__me++ = __ct.widen(' ');
3628 break;
3629 case money_base::sign:
3630 if (!__sn.empty())
3631 *__me++ = __sn[0];
3632 break;
3633 case money_base::symbol:
3634 if (!__sym.empty() && (__flags & ios_base::showbase))
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003635 __me = _VSTD::copy(__sym.begin(), __sym.end(), __me);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003636 break;
3637 case money_base::value:
3638 {
3639 // remember start of value so we can reverse it
3640 char_type* __t = __me;
3641 // find beginning of digits
3642 if (__neg)
3643 ++__db;
3644 // find end of digits
3645 const char_type* __d;
3646 for (__d = __db; __d < __de; ++__d)
3647 if (!__ct.is(ctype_base::digit, *__d))
3648 break;
3649 // print fractional part
3650 if (__fd > 0)
3651 {
3652 int __f;
3653 for (__f = __fd; __d > __db && __f > 0; --__f)
3654 *__me++ = *--__d;
3655 char_type __z = __f > 0 ? __ct.widen('0') : char_type();
3656 for (; __f > 0; --__f)
3657 *__me++ = __z;
3658 *__me++ = __dp;
3659 }
3660 // print units part
3661 if (__d == __db)
3662 {
3663 *__me++ = __ct.widen('0');
3664 }
3665 else
3666 {
3667 unsigned __ng = 0;
3668 unsigned __ig = 0;
3669 unsigned __gl = __grp.empty() ? numeric_limits<unsigned>::max()
3670 : static_cast<unsigned>(__grp[__ig]);
3671 while (__d != __db)
3672 {
3673 if (__ng == __gl)
3674 {
3675 *__me++ = __ts;
3676 __ng = 0;
3677 if (++__ig < __grp.size())
3678 __gl = __grp[__ig] == numeric_limits<char>::max() ?
3679 numeric_limits<unsigned>::max() :
3680 static_cast<unsigned>(__grp[__ig]);
3681 }
3682 *__me++ = *--__d;
3683 ++__ng;
3684 }
3685 }
3686 // reverse it
3687 reverse(__t, __me);
3688 }
3689 break;
3690 }
3691 }
3692 // print rest of sign, if any
3693 if (__sn.size() > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003694 __me = _VSTD::copy(__sn.begin()+1, __sn.end(), __me);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003695 // set alignment
3696 if ((__flags & ios_base::adjustfield) == ios_base::left)
3697 __mi = __me;
3698 else if ((__flags & ios_base::adjustfield) != ios_base::internal)
3699 __mi = __mb;
3700}
3701
Howard Hinnant8ea98242013-08-23 17:37:05 +00003702_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS __money_put<char>)
3703_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS __money_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003704
3705template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003706class _LIBCPP_TYPE_VIS_ONLY money_put
Howard Hinnantc51e1022010-05-11 19:42:16 +00003707 : public locale::facet,
3708 private __money_put<_CharT>
3709{
3710public:
3711 typedef _CharT char_type;
3712 typedef _OutputIterator iter_type;
3713 typedef basic_string<char_type> string_type;
3714
3715 _LIBCPP_ALWAYS_INLINE
3716 explicit money_put(size_t __refs = 0)
3717 : locale::facet(__refs) {}
3718
3719 _LIBCPP_ALWAYS_INLINE
3720 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3721 long double __units) const
3722 {
3723 return do_put(__s, __intl, __iob, __fl, __units);
3724 }
3725
3726 _LIBCPP_ALWAYS_INLINE
3727 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3728 const string_type& __digits) const
3729 {
3730 return do_put(__s, __intl, __iob, __fl, __digits);
3731 }
3732
3733 static locale::id id;
3734
3735protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003736 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00003737 ~money_put() {}
3738
3739 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3740 char_type __fl, long double __units) const;
3741 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3742 char_type __fl, const string_type& __digits) const;
3743};
3744
3745template <class _CharT, class _OutputIterator>
3746locale::id
3747money_put<_CharT, _OutputIterator>::id;
3748
3749template <class _CharT, class _OutputIterator>
3750_OutputIterator
3751money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3752 ios_base& __iob, char_type __fl,
3753 long double __units) const
3754{
3755 // convert to char
3756 const size_t __bs = 100;
3757 char __buf[__bs];
3758 char* __bb = __buf;
3759 char_type __digits[__bs];
3760 char_type* __db = __digits;
Howard Hinnant28b24882011-12-01 20:21:04 +00003761 size_t __n = static_cast<size_t>(snprintf(__bb, __bs, "%.0Lf", __units));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003762 unique_ptr<char, void(*)(void*)> __hn(0, free);
3763 unique_ptr<char_type, void(*)(void*)> __hd(0, free);
3764 // secure memory for digit storage
3765 if (__n > __bs-1)
3766 {
Howard Hinnantf312e3e2011-09-28 23:39:33 +00003767#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnant28b24882011-12-01 20:21:04 +00003768 __n = static_cast<size_t>(asprintf_l(&__bb, _LIBCPP_GET_C_LOCALE, "%.0Lf", __units));
Alexis Hunt1adf2aa2011-07-15 05:40:33 +00003769#else
3770 __n = __asprintf_l(&__bb, __cloc(), "%.0Lf", __units);
3771#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003772 if (__bb == 0)
3773 __throw_bad_alloc();
3774 __hn.reset(__bb);
3775 __hd.reset((char_type*)malloc(__n * sizeof(char_type)));
Howard Hinnant03de6f92012-03-07 20:37:43 +00003776 if (__hd == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003777 __throw_bad_alloc();
3778 __db = __hd.get();
3779 }
3780 // gather info
3781 locale __loc = __iob.getloc();
3782 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3783 __ct.widen(__bb, __bb + __n, __db);
3784 bool __neg = __n > 0 && __bb[0] == '-';
3785 money_base::pattern __pat;
3786 char_type __dp;
3787 char_type __ts;
3788 string __grp;
3789 string_type __sym;
3790 string_type __sn;
3791 int __fd;
3792 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3793 // secure memory for formatting
3794 char_type __mbuf[__bs];
3795 char_type* __mb = __mbuf;
3796 unique_ptr<char_type, void(*)(void*)> __hw(0, free);
3797 size_t __exn = static_cast<int>(__n) > __fd ?
Howard Hinnant28b24882011-12-01 20:21:04 +00003798 (__n - static_cast<size_t>(__fd)) * 2 + __sn.size() +
3799 __sym.size() + static_cast<size_t>(__fd) + 1
3800 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003801 if (__exn > __bs)
3802 {
3803 __hw.reset((char_type*)malloc(__exn * sizeof(char_type)));
3804 __mb = __hw.get();
3805 if (__mb == 0)
3806 __throw_bad_alloc();
3807 }
3808 // format
3809 char_type* __mi;
3810 char_type* __me;
3811 this->__format(__mb, __mi, __me, __iob.flags(),
3812 __db, __db + __n, __ct,
3813 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3814 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3815}
3816
3817template <class _CharT, class _OutputIterator>
3818_OutputIterator
3819money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3820 ios_base& __iob, char_type __fl,
3821 const string_type& __digits) const
3822{
3823 // gather info
3824 locale __loc = __iob.getloc();
3825 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3826 bool __neg = __digits.size() > 0 && __digits[0] == __ct.widen('-');
3827 money_base::pattern __pat;
3828 char_type __dp;
3829 char_type __ts;
3830 string __grp;
3831 string_type __sym;
3832 string_type __sn;
3833 int __fd;
3834 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3835 // secure memory for formatting
3836 char_type __mbuf[100];
3837 char_type* __mb = __mbuf;
3838 unique_ptr<char_type, void(*)(void*)> __h(0, free);
Howard Hinnant28b24882011-12-01 20:21:04 +00003839 size_t __exn = static_cast<int>(__digits.size()) > __fd ?
3840 (__digits.size() - static_cast<size_t>(__fd)) * 2 +
3841 __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 1
3842 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003843 if (__exn > 100)
3844 {
3845 __h.reset((char_type*)malloc(__exn * sizeof(char_type)));
3846 __mb = __h.get();
3847 if (__mb == 0)
3848 __throw_bad_alloc();
3849 }
3850 // format
3851 char_type* __mi;
3852 char_type* __me;
3853 this->__format(__mb, __mi, __me, __iob.flags(),
3854 __digits.data(), __digits.data() + __digits.size(), __ct,
3855 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3856 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3857}
3858
Howard Hinnant8ea98242013-08-23 17:37:05 +00003859_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS money_put<char>)
3860_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS money_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003861
3862// messages
3863
Howard Hinnant8331b762013-03-06 23:30:19 +00003864class _LIBCPP_TYPE_VIS messages_base
Howard Hinnantc51e1022010-05-11 19:42:16 +00003865{
3866public:
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003867 typedef ptrdiff_t catalog;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003868
3869 _LIBCPP_ALWAYS_INLINE messages_base() {}
3870};
3871
3872template <class _CharT>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003873class _LIBCPP_TYPE_VIS_ONLY messages
Howard Hinnantc51e1022010-05-11 19:42:16 +00003874 : public locale::facet,
3875 public messages_base
3876{
3877public:
3878 typedef _CharT char_type;
3879 typedef basic_string<_CharT> string_type;
3880
3881 _LIBCPP_ALWAYS_INLINE
3882 explicit messages(size_t __refs = 0)
3883 : locale::facet(__refs) {}
3884
3885 _LIBCPP_ALWAYS_INLINE
3886 catalog open(const basic_string<char>& __nm, const locale& __loc) const
3887 {
3888 return do_open(__nm, __loc);
3889 }
3890
3891 _LIBCPP_ALWAYS_INLINE
3892 string_type get(catalog __c, int __set, int __msgid,
3893 const string_type& __dflt) const
3894 {
3895 return do_get(__c, __set, __msgid, __dflt);
3896 }
3897
3898 _LIBCPP_ALWAYS_INLINE
3899 void close(catalog __c) const
3900 {
3901 do_close(__c);
3902 }
3903
3904 static locale::id id;
3905
3906protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003907 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00003908 ~messages() {}
3909
3910 virtual catalog do_open(const basic_string<char>&, const locale&) const;
3911 virtual string_type do_get(catalog, int __set, int __msgid,
3912 const string_type& __dflt) const;
3913 virtual void do_close(catalog) const;
3914};
3915
3916template <class _CharT>
3917locale::id
3918messages<_CharT>::id;
3919
3920template <class _CharT>
3921typename messages<_CharT>::catalog
3922messages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const
3923{
Marshall Clow1f257322013-03-18 17:04:29 +00003924#ifdef _WIN32
Howard Hinnanteb505f62011-09-23 16:11:27 +00003925 return -1;
3926#else // _WIN32
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +00003927 catalog __cat = (catalog)catopen(__nm.c_str(), NL_CAT_LOCALE);
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003928 if (__cat != -1)
3929 __cat = static_cast<catalog>((static_cast<size_t>(__cat) >> 1));
3930 return __cat;
Howard Hinnanteb505f62011-09-23 16:11:27 +00003931#endif // _WIN32
Howard Hinnantc51e1022010-05-11 19:42:16 +00003932}
3933
3934template <class _CharT>
3935typename messages<_CharT>::string_type
3936messages<_CharT>::do_get(catalog __c, int __set, int __msgid,
3937 const string_type& __dflt) const
3938{
Marshall Clow1f257322013-03-18 17:04:29 +00003939#ifdef _WIN32
Howard Hinnanteb505f62011-09-23 16:11:27 +00003940 return __dflt;
3941#else // _WIN32
Howard Hinnantc51e1022010-05-11 19:42:16 +00003942 string __ndflt;
3943 __narrow_to_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__ndflt),
3944 __dflt.c_str(),
3945 __dflt.c_str() + __dflt.size());
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003946 if (__c != -1)
3947 __c <<= 1;
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +00003948 nl_catd __cat = (nl_catd)__c;
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003949 char* __n = catgets(__cat, __set, __msgid, __ndflt.c_str());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003950 string_type __w;
3951 __widen_from_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__w),
3952 __n, __n + strlen(__n));
3953 return __w;
Howard Hinnanteb505f62011-09-23 16:11:27 +00003954#endif // _WIN32
Howard Hinnantc51e1022010-05-11 19:42:16 +00003955}
3956
3957template <class _CharT>
3958void
3959messages<_CharT>::do_close(catalog __c) const
3960{
Marshall Clow1f257322013-03-18 17:04:29 +00003961#if !defined(_WIN32)
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003962 if (__c != -1)
3963 __c <<= 1;
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +00003964 nl_catd __cat = (nl_catd)__c;
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003965 catclose(__cat);
Howard Hinnanteb505f62011-09-23 16:11:27 +00003966#endif // !_WIN32
Howard Hinnantc51e1022010-05-11 19:42:16 +00003967}
3968
Howard Hinnant8ea98242013-08-23 17:37:05 +00003969_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS messages<char>)
3970_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS messages<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003971
3972template <class _CharT>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003973class _LIBCPP_TYPE_VIS_ONLY messages_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00003974 : public messages<_CharT>
3975{
3976public:
3977 typedef messages_base::catalog catalog;
3978 typedef basic_string<_CharT> string_type;
3979
3980 _LIBCPP_ALWAYS_INLINE
3981 explicit messages_byname(const char*, size_t __refs = 0)
3982 : messages<_CharT>(__refs) {}
3983
3984 _LIBCPP_ALWAYS_INLINE
3985 explicit messages_byname(const string&, size_t __refs = 0)
3986 : messages<_CharT>(__refs) {}
3987
3988protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003989 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00003990 ~messages_byname() {}
3991};
3992
Howard Hinnant8ea98242013-08-23 17:37:05 +00003993_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS messages_byname<char>)
3994_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS messages_byname<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003995
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003996template<class _Codecvt, class _Elem = wchar_t,
3997 class _Wide_alloc = allocator<_Elem>,
3998 class _Byte_alloc = allocator<char> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003999class _LIBCPP_TYPE_VIS_ONLY wstring_convert
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004000{
4001public:
4002 typedef basic_string<char, char_traits<char>, _Byte_alloc> byte_string;
4003 typedef basic_string<_Elem, char_traits<_Elem>, _Wide_alloc> wide_string;
4004 typedef typename _Codecvt::state_type state_type;
4005 typedef typename wide_string::traits_type::int_type int_type;
4006
4007private:
4008 byte_string __byte_err_string_;
4009 wide_string __wide_err_string_;
4010 _Codecvt* __cvtptr_;
4011 state_type __cvtstate_;
4012 size_t __cvtcount_;
4013
4014 wstring_convert(const wstring_convert& __wc);
4015 wstring_convert& operator=(const wstring_convert& __wc);
4016public:
Marshall Clowccaa0fb2013-08-27 20:18:59 +00004017 _LIBCPP_EXPLICIT_AFTER_CXX11 wstring_convert(_Codecvt* __pcvt = new _Codecvt);
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004018 wstring_convert(_Codecvt* __pcvt, state_type __state);
Marshall Clowccaa0fb2013-08-27 20:18:59 +00004019 _LIBCPP_EXPLICIT_AFTER_CXX11 wstring_convert(const byte_string& __byte_err,
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004020 const wide_string& __wide_err = wide_string());
Howard Hinnant74279a52010-09-04 23:28:19 +00004021#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004022 wstring_convert(wstring_convert&& __wc);
4023#endif
4024 ~wstring_convert();
4025
Howard Hinnant756c69b2010-09-22 16:48:34 +00004026 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004027 wide_string from_bytes(char __byte)
4028 {return from_bytes(&__byte, &__byte+1);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004029 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004030 wide_string from_bytes(const char* __ptr)
4031 {return from_bytes(__ptr, __ptr + char_traits<char>::length(__ptr));}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004032 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004033 wide_string from_bytes(const byte_string& __str)
4034 {return from_bytes(__str.data(), __str.data() + __str.size());}
4035 wide_string from_bytes(const char* __first, const char* __last);
4036
Howard Hinnant756c69b2010-09-22 16:48:34 +00004037 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004038 byte_string to_bytes(_Elem __wchar)
4039 {return to_bytes(&__wchar, &__wchar+1);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004040 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004041 byte_string to_bytes(const _Elem* __wptr)
4042 {return to_bytes(__wptr, __wptr + char_traits<_Elem>::length(__wptr));}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004043 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004044 byte_string to_bytes(const wide_string& __wstr)
4045 {return to_bytes(__wstr.data(), __wstr.data() + __wstr.size());}
4046 byte_string to_bytes(const _Elem* __first, const _Elem* __last);
4047
Howard Hinnant756c69b2010-09-22 16:48:34 +00004048 _LIBCPP_ALWAYS_INLINE
Marshall Clowccaa0fb2013-08-27 20:18:59 +00004049 size_t converted() const _NOEXCEPT {return __cvtcount_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004050 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004051 state_type state() const {return __cvtstate_;}
4052};
4053
4054template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004055inline _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004056wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4057 wstring_convert(_Codecvt* __pcvt)
4058 : __cvtptr_(__pcvt), __cvtstate_(), __cvtcount_(0)
4059{
4060}
4061
4062template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004063inline _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004064wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4065 wstring_convert(_Codecvt* __pcvt, state_type __state)
4066 : __cvtptr_(__pcvt), __cvtstate_(__state), __cvtcount_(0)
4067{
4068}
4069
4070template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
4071wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4072 wstring_convert(const byte_string& __byte_err, const wide_string& __wide_err)
4073 : __byte_err_string_(__byte_err), __wide_err_string_(__wide_err),
4074 __cvtstate_(), __cvtcount_(0)
4075{
4076 __cvtptr_ = new _Codecvt;
4077}
4078
Howard Hinnant74279a52010-09-04 23:28:19 +00004079#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004080
4081template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004082inline _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004083wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4084 wstring_convert(wstring_convert&& __wc)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004085 : __byte_err_string_(_VSTD::move(__wc.__byte_err_string_)),
4086 __wide_err_string_(_VSTD::move(__wc.__wide_err_string_)),
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004087 __cvtptr_(__wc.__cvtptr_),
4088 __cvtstate_(__wc.__cvtstate_), __cvtcount_(__wc.__cvtstate_)
4089{
4090 __wc.__cvtptr_ = nullptr;
4091}
4092
Howard Hinnant5dc89112010-09-04 23:46:48 +00004093#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004094
4095template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
4096wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::~wstring_convert()
4097{
4098 delete __cvtptr_;
4099}
4100
4101template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
4102typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::wide_string
4103wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4104 from_bytes(const char* __frm, const char* __frm_end)
4105{
4106 __cvtcount_ = 0;
4107 if (__cvtptr_ != nullptr)
4108 {
4109 wide_string __ws(2*(__frm_end - __frm), _Elem());
Howard Hinnant16d54f22012-07-12 18:07:41 +00004110 if (__frm != __frm_end)
4111 __ws.resize(__ws.capacity());
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004112 codecvt_base::result __r = codecvt_base::ok;
4113 state_type __st = __cvtstate_;
4114 if (__frm != __frm_end)
4115 {
4116 _Elem* __to = &__ws[0];
4117 _Elem* __to_end = __to + __ws.size();
4118 const char* __frm_nxt;
4119 do
4120 {
4121 _Elem* __to_nxt;
4122 __r = __cvtptr_->in(__st, __frm, __frm_end, __frm_nxt,
4123 __to, __to_end, __to_nxt);
4124 __cvtcount_ += __frm_nxt - __frm;
4125 if (__frm_nxt == __frm)
4126 {
4127 __r = codecvt_base::error;
4128 }
4129 else if (__r == codecvt_base::noconv)
4130 {
4131 __ws.resize(__to - &__ws[0]);
4132 // This only gets executed if _Elem is char
4133 __ws.append((const _Elem*)__frm, (const _Elem*)__frm_end);
4134 __frm = __frm_nxt;
4135 __r = codecvt_base::ok;
4136 }
4137 else if (__r == codecvt_base::ok)
4138 {
4139 __ws.resize(__to_nxt - &__ws[0]);
4140 __frm = __frm_nxt;
4141 }
4142 else if (__r == codecvt_base::partial)
4143 {
4144 ptrdiff_t __s = __to_nxt - &__ws[0];
4145 __ws.resize(2 * __s);
4146 __to = &__ws[0] + __s;
4147 __to_end = &__ws[0] + __ws.size();
4148 __frm = __frm_nxt;
4149 }
4150 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
4151 }
4152 if (__r == codecvt_base::ok)
4153 return __ws;
4154 }
Howard Hinnant72f73582010-08-11 17:04:31 +00004155#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004156 if (__wide_err_string_.empty())
4157 throw range_error("wstring_convert: from_bytes error");
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004158#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004159 return __wide_err_string_;
4160}
4161
4162template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
4163typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::byte_string
4164wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4165 to_bytes(const _Elem* __frm, const _Elem* __frm_end)
4166{
4167 __cvtcount_ = 0;
4168 if (__cvtptr_ != nullptr)
4169 {
4170 byte_string __bs(2*(__frm_end - __frm), char());
Howard Hinnant16d54f22012-07-12 18:07:41 +00004171 if (__frm != __frm_end)
4172 __bs.resize(__bs.capacity());
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004173 codecvt_base::result __r = codecvt_base::ok;
4174 state_type __st = __cvtstate_;
4175 if (__frm != __frm_end)
4176 {
4177 char* __to = &__bs[0];
4178 char* __to_end = __to + __bs.size();
4179 const _Elem* __frm_nxt;
4180 do
4181 {
4182 char* __to_nxt;
4183 __r = __cvtptr_->out(__st, __frm, __frm_end, __frm_nxt,
4184 __to, __to_end, __to_nxt);
4185 __cvtcount_ += __frm_nxt - __frm;
4186 if (__frm_nxt == __frm)
4187 {
4188 __r = codecvt_base::error;
4189 }
4190 else if (__r == codecvt_base::noconv)
4191 {
4192 __bs.resize(__to - &__bs[0]);
4193 // This only gets executed if _Elem is char
4194 __bs.append((const char*)__frm, (const char*)__frm_end);
4195 __frm = __frm_nxt;
4196 __r = codecvt_base::ok;
4197 }
4198 else if (__r == codecvt_base::ok)
4199 {
4200 __bs.resize(__to_nxt - &__bs[0]);
4201 __frm = __frm_nxt;
4202 }
4203 else if (__r == codecvt_base::partial)
4204 {
4205 ptrdiff_t __s = __to_nxt - &__bs[0];
4206 __bs.resize(2 * __s);
4207 __to = &__bs[0] + __s;
4208 __to_end = &__bs[0] + __bs.size();
4209 __frm = __frm_nxt;
4210 }
4211 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
4212 }
4213 if (__r == codecvt_base::ok)
4214 {
4215 size_t __s = __bs.size();
4216 __bs.resize(__bs.capacity());
4217 char* __to = &__bs[0] + __s;
4218 char* __to_end = __to + __bs.size();
4219 do
4220 {
4221 char* __to_nxt;
4222 __r = __cvtptr_->unshift(__st, __to, __to_end, __to_nxt);
4223 if (__r == codecvt_base::noconv)
4224 {
4225 __bs.resize(__to - &__bs[0]);
4226 __r = codecvt_base::ok;
4227 }
4228 else if (__r == codecvt_base::ok)
4229 {
4230 __bs.resize(__to_nxt - &__bs[0]);
4231 }
4232 else if (__r == codecvt_base::partial)
4233 {
Howard Hinnant28b24882011-12-01 20:21:04 +00004234 ptrdiff_t __sp = __to_nxt - &__bs[0];
4235 __bs.resize(2 * __sp);
4236 __to = &__bs[0] + __sp;
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004237 __to_end = &__bs[0] + __bs.size();
4238 }
4239 } while (__r == codecvt_base::partial);
4240 if (__r == codecvt_base::ok)
4241 return __bs;
4242 }
4243 }
Howard Hinnant72f73582010-08-11 17:04:31 +00004244#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004245 if (__byte_err_string_.empty())
4246 throw range_error("wstring_convert: to_bytes error");
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004247#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004248 return __byte_err_string_;
4249}
4250
4251template <class _Codecvt, class _Elem = wchar_t, class _Tr = char_traits<_Elem> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004252class _LIBCPP_TYPE_VIS_ONLY wbuffer_convert
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004253 : public basic_streambuf<_Elem, _Tr>
4254{
4255public:
4256 // types:
4257 typedef _Elem char_type;
4258 typedef _Tr traits_type;
4259 typedef typename traits_type::int_type int_type;
4260 typedef typename traits_type::pos_type pos_type;
4261 typedef typename traits_type::off_type off_type;
4262 typedef typename _Codecvt::state_type state_type;
4263
4264private:
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004265 char* __extbuf_;
4266 const char* __extbufnext_;
4267 const char* __extbufend_;
4268 char __extbuf_min_[8];
4269 size_t __ebs_;
4270 char_type* __intbuf_;
4271 size_t __ibs_;
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004272 streambuf* __bufptr_;
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004273 _Codecvt* __cv_;
4274 state_type __st_;
4275 ios_base::openmode __cm_;
4276 bool __owns_eb_;
4277 bool __owns_ib_;
4278 bool __always_noconv_;
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004279
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004280 wbuffer_convert(const wbuffer_convert&);
4281 wbuffer_convert& operator=(const wbuffer_convert&);
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004282public:
Marshall Clowccaa0fb2013-08-27 20:18:59 +00004283 _LIBCPP_EXPLICIT_AFTER_CXX11 wbuffer_convert(streambuf* __bytebuf = 0,
4284 _Codecvt* __pcvt = new _Codecvt, state_type __state = state_type());
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004285 ~wbuffer_convert();
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004286
Howard Hinnant756c69b2010-09-22 16:48:34 +00004287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004288 streambuf* rdbuf() const {return __bufptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004290 streambuf* rdbuf(streambuf* __bytebuf)
4291 {
4292 streambuf* __r = __bufptr_;
4293 __bufptr_ = __bytebuf;
4294 return __r;
4295 }
4296
Howard Hinnant756c69b2010-09-22 16:48:34 +00004297 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004298 state_type state() const {return __st_;}
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004299
4300protected:
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004301 virtual int_type underflow();
4302 virtual int_type pbackfail(int_type __c = traits_type::eof());
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004303 virtual int_type overflow (int_type __c = traits_type::eof());
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004304 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s,
4305 streamsize __n);
4306 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
4307 ios_base::openmode __wch = ios_base::in | ios_base::out);
4308 virtual pos_type seekpos(pos_type __sp,
4309 ios_base::openmode __wch = ios_base::in | ios_base::out);
4310 virtual int sync();
4311
4312private:
4313 bool __read_mode();
4314 void __write_mode();
4315 wbuffer_convert* __close();
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004316};
4317
4318template <class _Codecvt, class _Elem, class _Tr>
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004319wbuffer_convert<_Codecvt, _Elem, _Tr>::
4320 wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt, state_type __state)
4321 : __extbuf_(0),
4322 __extbufnext_(0),
4323 __extbufend_(0),
4324 __ebs_(0),
4325 __intbuf_(0),
4326 __ibs_(0),
4327 __bufptr_(__bytebuf),
4328 __cv_(__pcvt),
4329 __st_(__state),
4330 __cm_(0),
4331 __owns_eb_(false),
4332 __owns_ib_(false),
4333 __always_noconv_(__cv_ ? __cv_->always_noconv() : false)
4334{
4335 setbuf(0, 4096);
4336}
4337
4338template <class _Codecvt, class _Elem, class _Tr>
4339wbuffer_convert<_Codecvt, _Elem, _Tr>::~wbuffer_convert()
4340{
4341 __close();
4342 delete __cv_;
4343 if (__owns_eb_)
4344 delete [] __extbuf_;
4345 if (__owns_ib_)
4346 delete [] __intbuf_;
4347}
4348
4349template <class _Codecvt, class _Elem, class _Tr>
4350typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4351wbuffer_convert<_Codecvt, _Elem, _Tr>::underflow()
4352{
4353 if (__cv_ == 0 || __bufptr_ == 0)
4354 return traits_type::eof();
4355 bool __initial = __read_mode();
4356 char_type __1buf;
4357 if (this->gptr() == 0)
4358 this->setg(&__1buf, &__1buf+1, &__1buf+1);
4359 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
4360 int_type __c = traits_type::eof();
4361 if (this->gptr() == this->egptr())
4362 {
4363 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
4364 if (__always_noconv_)
4365 {
4366 streamsize __nmemb = static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz);
4367 __nmemb = __bufptr_->sgetn((char*)this->eback() + __unget_sz, __nmemb);
4368 if (__nmemb != 0)
4369 {
4370 this->setg(this->eback(),
4371 this->eback() + __unget_sz,
4372 this->eback() + __unget_sz + __nmemb);
4373 __c = *this->gptr();
4374 }
4375 }
4376 else
4377 {
4378 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
4379 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
4380 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004381 streamsize __nmemb = _VSTD::min(static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz),
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004382 static_cast<streamsize>(__extbufend_ - __extbufnext_));
4383 codecvt_base::result __r;
4384 state_type __svs = __st_;
4385 streamsize __nr = __bufptr_->sgetn(const_cast<char*>(__extbufnext_), __nmemb);
4386 if (__nr != 0)
4387 {
4388 __extbufend_ = __extbufnext_ + __nr;
4389 char_type* __inext;
4390 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
4391 this->eback() + __unget_sz,
4392 this->egptr(), __inext);
4393 if (__r == codecvt_base::noconv)
4394 {
4395 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)__extbufend_);
4396 __c = *this->gptr();
4397 }
4398 else if (__inext != this->eback() + __unget_sz)
4399 {
4400 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
4401 __c = *this->gptr();
4402 }
4403 }
4404 }
4405 }
4406 else
4407 __c = *this->gptr();
4408 if (this->eback() == &__1buf)
4409 this->setg(0, 0, 0);
4410 return __c;
4411}
4412
4413template <class _Codecvt, class _Elem, class _Tr>
4414typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4415wbuffer_convert<_Codecvt, _Elem, _Tr>::pbackfail(int_type __c)
4416{
4417 if (__cv_ != 0 && __bufptr_ != 0 && this->eback() < this->gptr())
4418 {
4419 if (traits_type::eq_int_type(__c, traits_type::eof()))
4420 {
4421 this->gbump(-1);
4422 return traits_type::not_eof(__c);
4423 }
4424 if (traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
4425 {
4426 this->gbump(-1);
4427 *this->gptr() = traits_type::to_char_type(__c);
4428 return __c;
4429 }
4430 }
4431 return traits_type::eof();
4432}
4433
4434template <class _Codecvt, class _Elem, class _Tr>
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004435typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4436wbuffer_convert<_Codecvt, _Elem, _Tr>::overflow(int_type __c)
4437{
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004438 if (__cv_ == 0 || __bufptr_ == 0)
4439 return traits_type::eof();
4440 __write_mode();
4441 char_type __1buf;
4442 char_type* __pb_save = this->pbase();
4443 char_type* __epb_save = this->epptr();
4444 if (!traits_type::eq_int_type(__c, traits_type::eof()))
4445 {
4446 if (this->pptr() == 0)
4447 this->setp(&__1buf, &__1buf+1);
4448 *this->pptr() = traits_type::to_char_type(__c);
4449 this->pbump(1);
4450 }
4451 if (this->pptr() != this->pbase())
4452 {
4453 if (__always_noconv_)
4454 {
4455 streamsize __nmemb = static_cast<streamsize>(this->pptr() - this->pbase());
4456 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4457 return traits_type::eof();
4458 }
4459 else
4460 {
4461 char* __extbe = __extbuf_;
4462 codecvt_base::result __r;
4463 do
4464 {
4465 const char_type* __e;
4466 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
4467 __extbuf_, __extbuf_ + __ebs_, __extbe);
4468 if (__e == this->pbase())
4469 return traits_type::eof();
4470 if (__r == codecvt_base::noconv)
4471 {
4472 streamsize __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
4473 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4474 return traits_type::eof();
4475 }
4476 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
4477 {
4478 streamsize __nmemb = static_cast<size_t>(__extbe - __extbuf_);
4479 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4480 return traits_type::eof();
4481 if (__r == codecvt_base::partial)
4482 {
4483 this->setp((char_type*)__e, this->pptr());
4484 this->pbump(this->epptr() - this->pbase());
4485 }
4486 }
4487 else
4488 return traits_type::eof();
4489 } while (__r == codecvt_base::partial);
4490 }
4491 this->setp(__pb_save, __epb_save);
4492 }
4493 return traits_type::not_eof(__c);
4494}
4495
4496template <class _Codecvt, class _Elem, class _Tr>
4497basic_streambuf<_Elem, _Tr>*
4498wbuffer_convert<_Codecvt, _Elem, _Tr>::setbuf(char_type* __s, streamsize __n)
4499{
4500 this->setg(0, 0, 0);
4501 this->setp(0, 0);
4502 if (__owns_eb_)
4503 delete [] __extbuf_;
4504 if (__owns_ib_)
4505 delete [] __intbuf_;
4506 __ebs_ = __n;
4507 if (__ebs_ > sizeof(__extbuf_min_))
4508 {
4509 if (__always_noconv_ && __s)
4510 {
4511 __extbuf_ = (char*)__s;
4512 __owns_eb_ = false;
4513 }
4514 else
4515 {
4516 __extbuf_ = new char[__ebs_];
4517 __owns_eb_ = true;
4518 }
4519 }
4520 else
4521 {
4522 __extbuf_ = __extbuf_min_;
4523 __ebs_ = sizeof(__extbuf_min_);
4524 __owns_eb_ = false;
4525 }
4526 if (!__always_noconv_)
4527 {
4528 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
4529 if (__s && __ibs_ >= sizeof(__extbuf_min_))
4530 {
4531 __intbuf_ = __s;
4532 __owns_ib_ = false;
4533 }
4534 else
4535 {
4536 __intbuf_ = new char_type[__ibs_];
4537 __owns_ib_ = true;
4538 }
4539 }
4540 else
4541 {
4542 __ibs_ = 0;
4543 __intbuf_ = 0;
4544 __owns_ib_ = false;
4545 }
4546 return this;
4547}
4548
4549template <class _Codecvt, class _Elem, class _Tr>
4550typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4551wbuffer_convert<_Codecvt, _Elem, _Tr>::seekoff(off_type __off, ios_base::seekdir __way,
4552 ios_base::openmode __om)
4553{
4554 int __width = __cv_->encoding();
4555 if (__cv_ == 0 || __bufptr_ == 0 || (__width <= 0 && __off != 0) || sync())
4556 return pos_type(off_type(-1));
4557 // __width > 0 || __off == 0
4558 switch (__way)
4559 {
4560 case ios_base::beg:
4561 break;
4562 case ios_base::cur:
4563 break;
4564 case ios_base::end:
4565 break;
4566 default:
4567 return pos_type(off_type(-1));
4568 }
4569 pos_type __r = __bufptr_->pubseekoff(__width * __off, __way, __om);
4570 __r.state(__st_);
4571 return __r;
4572}
4573
4574template <class _Codecvt, class _Elem, class _Tr>
4575typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4576wbuffer_convert<_Codecvt, _Elem, _Tr>::seekpos(pos_type __sp, ios_base::openmode __wch)
4577{
4578 if (__cv_ == 0 || __bufptr_ == 0 || sync())
4579 return pos_type(off_type(-1));
4580 if (__bufptr_->pubseekpos(__sp, __wch) == pos_type(off_type(-1)))
4581 return pos_type(off_type(-1));
4582 return __sp;
4583}
4584
4585template <class _Codecvt, class _Elem, class _Tr>
4586int
4587wbuffer_convert<_Codecvt, _Elem, _Tr>::sync()
4588{
4589 if (__cv_ == 0 || __bufptr_ == 0)
4590 return 0;
4591 if (__cm_ & ios_base::out)
4592 {
4593 if (this->pptr() != this->pbase())
4594 if (overflow() == traits_type::eof())
4595 return -1;
4596 codecvt_base::result __r;
4597 do
4598 {
4599 char* __extbe;
4600 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
4601 streamsize __nmemb = static_cast<streamsize>(__extbe - __extbuf_);
4602 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4603 return -1;
4604 } while (__r == codecvt_base::partial);
4605 if (__r == codecvt_base::error)
4606 return -1;
4607 if (__bufptr_->pubsync())
4608 return -1;
4609 }
4610 else if (__cm_ & ios_base::in)
4611 {
4612 off_type __c;
4613 if (__always_noconv_)
4614 __c = this->egptr() - this->gptr();
4615 else
4616 {
4617 int __width = __cv_->encoding();
4618 __c = __extbufend_ - __extbufnext_;
4619 if (__width > 0)
4620 __c += __width * (this->egptr() - this->gptr());
4621 else
4622 {
4623 if (this->gptr() != this->egptr())
4624 {
4625 reverse(this->gptr(), this->egptr());
4626 codecvt_base::result __r;
4627 const char_type* __e = this->gptr();
4628 char* __extbe;
4629 do
4630 {
4631 __r = __cv_->out(__st_, __e, this->egptr(), __e,
4632 __extbuf_, __extbuf_ + __ebs_, __extbe);
4633 switch (__r)
4634 {
4635 case codecvt_base::noconv:
4636 __c += this->egptr() - this->gptr();
4637 break;
4638 case codecvt_base::ok:
4639 case codecvt_base::partial:
4640 __c += __extbe - __extbuf_;
4641 break;
4642 default:
4643 return -1;
4644 }
4645 } while (__r == codecvt_base::partial);
4646 }
4647 }
4648 }
4649 if (__bufptr_->pubseekoff(-__c, ios_base::cur, __cm_) == pos_type(off_type(-1)))
4650 return -1;
4651 this->setg(0, 0, 0);
4652 __cm_ = 0;
4653 }
4654 return 0;
4655}
4656
4657template <class _Codecvt, class _Elem, class _Tr>
4658bool
4659wbuffer_convert<_Codecvt, _Elem, _Tr>::__read_mode()
4660{
4661 if (!(__cm_ & ios_base::in))
4662 {
4663 this->setp(0, 0);
4664 if (__always_noconv_)
4665 this->setg((char_type*)__extbuf_,
4666 (char_type*)__extbuf_ + __ebs_,
4667 (char_type*)__extbuf_ + __ebs_);
4668 else
4669 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
4670 __cm_ = ios_base::in;
4671 return true;
4672 }
4673 return false;
4674}
4675
4676template <class _Codecvt, class _Elem, class _Tr>
4677void
4678wbuffer_convert<_Codecvt, _Elem, _Tr>::__write_mode()
4679{
4680 if (!(__cm_ & ios_base::out))
4681 {
4682 this->setg(0, 0, 0);
4683 if (__ebs_ > sizeof(__extbuf_min_))
4684 {
4685 if (__always_noconv_)
4686 this->setp((char_type*)__extbuf_,
4687 (char_type*)__extbuf_ + (__ebs_ - 1));
4688 else
4689 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
4690 }
4691 else
4692 this->setp(0, 0);
4693 __cm_ = ios_base::out;
4694 }
4695}
4696
4697template <class _Codecvt, class _Elem, class _Tr>
4698wbuffer_convert<_Codecvt, _Elem, _Tr>*
4699wbuffer_convert<_Codecvt, _Elem, _Tr>::__close()
4700{
4701 wbuffer_convert* __rt = 0;
4702 if (__cv_ != 0 && __bufptr_ != 0)
4703 {
4704 __rt = this;
4705 if ((__cm_ & ios_base::out) && sync())
4706 __rt = 0;
4707 }
4708 return __rt;
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004709}
4710
Howard Hinnantc51e1022010-05-11 19:42:16 +00004711_LIBCPP_END_NAMESPACE_STD
4712
4713#endif // _LIBCPP_LOCALE