2 //===-------------------------- locale ------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_LOCALE
12 #define _LIBCPP_LOCALE
28 static const category // values assigned here are for exposition only
36 all = collate | ctype | monetary | numeric | time | messages;
38 // construct/copy/destroy:
40 locale(const locale& other) noexcept;
41 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);
48 ~locale(); // not virtual
50 const locale& operator=(const locale& other) noexcept;
52 template <class Facet> locale combine(const locale& other) const;
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;
62 // global locale objects:
63 static locale global(const locale&);
64 static const locale& classic();
67 template <class Facet> const Facet& use_facet(const locale&);
68 template <class Facet> bool has_facet(const locale&) noexcept;
70 // 22.3.3, convenience interfaces:
71 template <class charT> bool isspace (charT c, const locale& loc);
72 template <class charT> bool isprint (charT c, const locale& loc);
73 template <class charT> bool iscntrl (charT c, const locale& loc);
74 template <class charT> bool isupper (charT c, const locale& loc);
75 template <class charT> bool islower (charT c, const locale& loc);
76 template <class charT> bool isalpha (charT c, const locale& loc);
77 template <class charT> bool isdigit (charT c, const locale& loc);
78 template <class charT> bool ispunct (charT c, const locale& loc);
79 template <class charT> bool isxdigit(charT c, const locale& loc);
80 template <class charT> bool isalnum (charT c, const locale& loc);
81 template <class charT> bool isgraph (charT c, const locale& loc);
82 template <class charT> charT toupper(charT c, const locale& loc);
83 template <class charT> charT tolower(charT c, const locale& loc);
85 template<class Codecvt, class Elem = wchar_t,
86 class Wide_alloc = allocator<Elem>,
87 class Byte_alloc = allocator<char>>
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;
96 explicit wstring_convert(Codecvt* pcvt = new Codecvt); // explicit in C++14
97 wstring_convert(Codecvt* pcvt, state_type state);
98 explicit wstring_convert(const byte_string& byte_err, // explicit in C++14
99 const wide_string& wide_err = wide_string());
100 wstring_convert(const wstring_convert&) = delete; // C++14
101 wstring_convert & operator=(const wstring_convert &) = delete; // C++14
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);
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);
114 size_t converted() const; // noexcept in C++14
115 state_type state() const;
118 template <class Codecvt, class Elem = wchar_t, class Tr = char_traits<Elem>>
119 class wbuffer_convert
120 : public basic_streambuf<Elem, Tr>
123 typedef typename Tr::state_type state_type;
125 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
131 streambuf* rdbuf() const;
132 streambuf* rdbuf(streambuf* bytebuf);
134 state_type state() const;
137 // 22.4.1 and 22.4.1.3, ctype:
139 template <class charT> class ctype;
140 template <> class ctype<char>; // specialization
141 template <class charT> class ctype_byname;
142 template <> class ctype_byname<char>; // specialization
145 template <class internT, class externT, class stateT> class codecvt;
146 template <class internT, class externT, class stateT> class codecvt_byname;
148 // 22.4.2 and 22.4.3, numeric:
149 template <class charT, class InputIterator> class num_get;
150 template <class charT, class OutputIterator> class num_put;
151 template <class charT> class numpunct;
152 template <class charT> class numpunct_byname;
154 // 22.4.4, col lation:
155 template <class charT> class collate;
156 template <class charT> class collate_byname;
158 // 22.4.5, date and time:
160 template <class charT, class InputIterator> class time_get;
161 template <class charT, class InputIterator> class time_get_byname;
162 template <class charT, class OutputIterator> class time_put;
163 template <class charT, class OutputIterator> class time_put_byname;
167 template <class charT, class InputIterator> class money_get;
168 template <class charT, class OutputIterator> class money_put;
169 template <class charT, bool Intl> class moneypunct;
170 template <class charT, bool Intl> class moneypunct_byname;
172 // 22.4.7, message retrieval:
174 template <class charT> class messages;
175 template <class charT> class messages_byname;
195 #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
196 #include <support/win32/locale_win32.h>
197 #elif defined(_NEWLIB_VERSION)
198 // FIXME: replace all the uses of _NEWLIB_VERSION with __NEWLIB__ preceded by an
199 // include of <sys/cdefs.h> once https://sourceware.org/ml/newlib-cvs/2014-q3/msg00038.html
200 // has had a chance to bake for a bit
201 #include <support/newlib/xlocale.h>
203 #ifdef _LIBCPP_HAS_CATOPEN
204 #include <nl_types.h>
208 #include <Availability.h>
211 #include <__undef_min_max>
213 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
214 #pragma GCC system_header
217 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
218 #include <__bsd_locale_defaults.h>
220 #include <__bsd_locale_fallbacks.h>
223 _LIBCPP_BEGIN_NAMESPACE_STD
225 #if defined(__APPLE__) || defined(__FreeBSD__)
226 # define _LIBCPP_GET_C_LOCALE 0
227 #elif defined(__CloudABI__) || defined(__NetBSD__)
228 # define _LIBCPP_GET_C_LOCALE LC_C_LOCALE
230 # define _LIBCPP_GET_C_LOCALE __cloc()
231 // Get the C locale object
232 _LIBCPP_FUNC_VIS locale_t __cloc();
233 #define __cloc_defined
236 typedef _VSTD::remove_pointer<locale_t>::type __locale_struct;
237 typedef _VSTD::unique_ptr<__locale_struct, decltype(&freelocale)> __locale_unique_ptr;
240 // Scans [__b, __e) until a match is found in the basic_strings range
241 // [__kb, __ke) or until it can be shown that there is no match in [__kb, __ke).
242 // __b will be incremented (visibly), consuming CharT until a match is found
243 // or proved to not exist. A keyword may be "", in which will match anything.
244 // If one keyword is a prefix of another, and the next CharT in the input
245 // might match another keyword, the algorithm will attempt to find the longest
246 // matching keyword. If the longer matching keyword ends up not matching, then
247 // no keyword match is found. If no keyword match is found, __ke is returned
248 // and failbit is set in __err.
249 // Else an iterator pointing to the matching keyword is found. If more than
250 // one keyword matches, an iterator to the first matching keyword is returned.
251 // If on exit __b == __e, eofbit is set in __err. If __case_sensitive is false,
252 // __ct is used to force to lower case before comparing characters.
254 // Keywords: "a", "abb"
255 // If the input is "a", the first keyword matches and eofbit is set.
256 // If the input is "abc", no match is found and "ab" are consumed.
257 template <class _InputIterator, class _ForwardIterator, class _Ctype>
260 __scan_keyword(_InputIterator& __b, _InputIterator __e,
261 _ForwardIterator __kb, _ForwardIterator __ke,
262 const _Ctype& __ct, ios_base::iostate& __err,
263 bool __case_sensitive = true)
265 typedef typename iterator_traits<_InputIterator>::value_type _CharT;
266 size_t __nkw = static_cast<size_t>(_VSTD::distance(__kb, __ke));
267 const unsigned char __doesnt_match = '\0';
268 const unsigned char __might_match = '\1';
269 const unsigned char __does_match = '\2';
270 unsigned char __statbuf[100];
271 unsigned char* __status = __statbuf;
272 unique_ptr<unsigned char, void(*)(void*)> __stat_hold(0, free);
273 if (__nkw > sizeof(__statbuf))
275 __status = (unsigned char*)malloc(__nkw);
278 __stat_hold.reset(__status);
280 size_t __n_might_match = __nkw; // At this point, any keyword might match
281 size_t __n_does_match = 0; // but none of them definitely do
282 // Initialize all statuses to __might_match, except for "" keywords are __does_match
283 unsigned char* __st = __status;
284 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
287 *__st = __might_match;
290 *__st = __does_match;
295 // While there might be a match, test keywords against the next CharT
296 for (size_t __indx = 0; __b != __e && __n_might_match > 0; ++__indx)
298 // Peek at the next CharT but don't consume it
300 if (!__case_sensitive)
301 __c = __ct.toupper(__c);
302 bool __consume = false;
303 // For each keyword which might match, see if the __indx character is __c
304 // If a match if found, consume __c
305 // If a match is found, and that is the last character in the keyword,
306 // then that keyword matches.
307 // If the keyword doesn't match this character, then change the keyword
310 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
312 if (*__st == __might_match)
314 _CharT __kc = (*__ky)[__indx];
315 if (!__case_sensitive)
316 __kc = __ct.toupper(__kc);
320 if (__ky->size() == __indx+1)
322 *__st = __does_match;
329 *__st = __doesnt_match;
334 // consume if we matched a character
338 // If we consumed a character and there might be a matched keyword that
339 // was marked matched on a previous iteration, then such keywords
340 // which are now marked as not matching.
341 if (__n_might_match + __n_does_match > 1)
344 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
346 if (*__st == __does_match && __ky->size() != __indx+1)
348 *__st = __doesnt_match;
355 // We've exited the loop because we hit eof and/or we have no more "might matches".
357 __err |= ios_base::eofbit;
358 // Return the first matching result
359 for (__st = __status; __kb != __ke; ++__kb, (void) ++__st)
360 if (*__st == __does_match)
363 __err |= ios_base::failbit;
367 struct _LIBCPP_TYPE_VIS __num_get_base
369 static const int __num_get_buf_sz = 40;
371 static int __get_base(ios_base&);
372 static const char __src[33];
376 void __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end,
377 ios_base::iostate& __err);
379 template <class _CharT>
381 : protected __num_get_base
383 static string __stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep);
384 static string __stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
385 _CharT& __thousands_sep);
386 static int __stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
387 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
388 unsigned* __g, unsigned*& __g_end, _CharT* __atoms);
389 static int __stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp,
390 char* __a, char*& __a_end,
391 _CharT __decimal_point, _CharT __thousands_sep,
392 const string& __grouping, unsigned* __g,
393 unsigned*& __g_end, unsigned& __dc, _CharT* __atoms);
396 template <class _CharT>
398 __num_get<_CharT>::__stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep)
400 locale __loc = __iob.getloc();
401 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 26, __atoms);
402 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
403 __thousands_sep = __np.thousands_sep();
404 return __np.grouping();
407 template <class _CharT>
409 __num_get<_CharT>::__stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
410 _CharT& __thousands_sep)
412 locale __loc = __iob.getloc();
413 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 32, __atoms);
414 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
415 __decimal_point = __np.decimal_point();
416 __thousands_sep = __np.thousands_sep();
417 return __np.grouping();
420 template <class _CharT>
422 __num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
423 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
424 unsigned* __g, unsigned*& __g_end, _CharT* __atoms)
426 if (__a_end == __a && (__ct == __atoms[24] || __ct == __atoms[25]))
428 *__a_end++ = __ct == __atoms[24] ? '+' : '-';
432 if (__grouping.size() != 0 && __ct == __thousands_sep)
434 if (__g_end-__g < __num_get_buf_sz)
441 ptrdiff_t __f = find(__atoms, __atoms + 26, __ct) - __atoms;
454 if (__a_end != __a && __a_end - __a <= 2 && __a_end[-1] == '0')
457 *__a_end++ = __src[__f];
462 *__a_end++ = __src[__f];
467 template <class _CharT>
469 __num_get<_CharT>::__stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp, char* __a, char*& __a_end,
470 _CharT __decimal_point, _CharT __thousands_sep, const string& __grouping,
471 unsigned* __g, unsigned*& __g_end, unsigned& __dc, _CharT* __atoms)
473 if (__ct == __decimal_point)
479 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
483 if (__ct == __thousands_sep && __grouping.size() != 0)
487 if (__g_end-__g < __num_get_buf_sz)
494 ptrdiff_t __f = find(__atoms, __atoms + 32, __ct) - __atoms;
497 char __x = __src[__f];
498 if (__x == '-' || __x == '+')
500 if (__a_end == __a || (__a_end[-1] & 0x5F) == (__exp & 0x7F))
507 if (__x == 'x' || __x == 'X')
509 else if ((__x & 0x5F) == __exp)
515 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
526 _LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<char>)
527 _LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<wchar_t>)
529 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
530 class _LIBCPP_TEMPLATE_VIS num_get
531 : public locale::facet,
532 private __num_get<_CharT>
535 typedef _CharT char_type;
536 typedef _InputIterator iter_type;
538 _LIBCPP_ALWAYS_INLINE
539 explicit num_get(size_t __refs = 0)
540 : locale::facet(__refs) {}
542 _LIBCPP_ALWAYS_INLINE
543 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
544 ios_base::iostate& __err, bool& __v) const
546 return do_get(__b, __e, __iob, __err, __v);
549 _LIBCPP_ALWAYS_INLINE
550 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
551 ios_base::iostate& __err, long& __v) const
553 return do_get(__b, __e, __iob, __err, __v);
556 _LIBCPP_ALWAYS_INLINE
557 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
558 ios_base::iostate& __err, long long& __v) const
560 return do_get(__b, __e, __iob, __err, __v);
563 _LIBCPP_ALWAYS_INLINE
564 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
565 ios_base::iostate& __err, unsigned short& __v) const
567 return do_get(__b, __e, __iob, __err, __v);
570 _LIBCPP_ALWAYS_INLINE
571 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
572 ios_base::iostate& __err, unsigned int& __v) const
574 return do_get(__b, __e, __iob, __err, __v);
577 _LIBCPP_ALWAYS_INLINE
578 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
579 ios_base::iostate& __err, unsigned long& __v) const
581 return do_get(__b, __e, __iob, __err, __v);
584 _LIBCPP_ALWAYS_INLINE
585 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
586 ios_base::iostate& __err, unsigned long long& __v) const
588 return do_get(__b, __e, __iob, __err, __v);
591 _LIBCPP_ALWAYS_INLINE
592 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
593 ios_base::iostate& __err, float& __v) const
595 return do_get(__b, __e, __iob, __err, __v);
598 _LIBCPP_ALWAYS_INLINE
599 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
600 ios_base::iostate& __err, double& __v) const
602 return do_get(__b, __e, __iob, __err, __v);
605 _LIBCPP_ALWAYS_INLINE
606 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
607 ios_base::iostate& __err, long double& __v) const
609 return do_get(__b, __e, __iob, __err, __v);
612 _LIBCPP_ALWAYS_INLINE
613 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
614 ios_base::iostate& __err, void*& __v) const
616 return do_get(__b, __e, __iob, __err, __v);
619 static locale::id id;
622 _LIBCPP_ALWAYS_INLINE
626 iter_type __do_get_floating_point
627 (iter_type __b, iter_type __e, ios_base& __iob,
628 ios_base::iostate& __err, _Fp& __v) const;
630 template <class _Signed>
631 iter_type __do_get_signed
632 (iter_type __b, iter_type __e, ios_base& __iob,
633 ios_base::iostate& __err, _Signed& __v) const;
635 template <class _Unsigned>
636 iter_type __do_get_unsigned
637 (iter_type __b, iter_type __e, ios_base& __iob,
638 ios_base::iostate& __err, _Unsigned& __v) const;
641 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
642 ios_base::iostate& __err, bool& __v) const;
644 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
645 ios_base::iostate& __err, long& __v) const
646 { return this->__do_get_signed ( __b, __e, __iob, __err, __v ); }
648 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
649 ios_base::iostate& __err, long long& __v) const
650 { return this->__do_get_signed ( __b, __e, __iob, __err, __v ); }
652 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
653 ios_base::iostate& __err, unsigned short& __v) const
654 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
656 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
657 ios_base::iostate& __err, unsigned int& __v) const
658 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
660 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
661 ios_base::iostate& __err, unsigned long& __v) const
662 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
664 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
665 ios_base::iostate& __err, unsigned long long& __v) const
666 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
668 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
669 ios_base::iostate& __err, float& __v) const
670 { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
672 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
673 ios_base::iostate& __err, double& __v) const
674 { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
676 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
677 ios_base::iostate& __err, long double& __v) const
678 { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
680 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
681 ios_base::iostate& __err, void*& __v) const;
684 template <class _CharT, class _InputIterator>
686 num_get<_CharT, _InputIterator>::id;
690 __num_get_signed_integral(const char* __a, const char* __a_end,
691 ios_base::iostate& __err, int __base)
695 typename remove_reference<decltype(errno)>::type __save_errno = errno;
698 long long __ll = strtoll_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
699 typename remove_reference<decltype(errno)>::type __current_errno = errno;
700 if (__current_errno == 0)
701 errno = __save_errno;
704 __err = ios_base::failbit;
707 else if (__current_errno == ERANGE ||
708 __ll < numeric_limits<_Tp>::min() ||
709 numeric_limits<_Tp>::max() < __ll)
711 __err = ios_base::failbit;
713 return numeric_limits<_Tp>::max();
715 return numeric_limits<_Tp>::min();
717 return static_cast<_Tp>(__ll);
719 __err = ios_base::failbit;
725 __num_get_unsigned_integral(const char* __a, const char* __a_end,
726 ios_base::iostate& __err, int __base)
732 __err = ios_base::failbit;
735 typename remove_reference<decltype(errno)>::type __save_errno = errno;
738 unsigned long long __ll = strtoull_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
739 typename remove_reference<decltype(errno)>::type __current_errno = errno;
740 if (__current_errno == 0)
741 errno = __save_errno;
744 __err = ios_base::failbit;
747 else if (__current_errno == ERANGE ||
748 numeric_limits<_Tp>::max() < __ll)
750 __err = ios_base::failbit;
751 return numeric_limits<_Tp>::max();
753 return static_cast<_Tp>(__ll);
755 __err = ios_base::failbit;
760 _LIBCPP_INLINE_VISIBILITY
761 _Tp __do_strtod(const char* __a, char** __p2);
764 inline _LIBCPP_INLINE_VISIBILITY
765 float __do_strtod<float>(const char* __a, char** __p2) {
766 return strtof_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
770 inline _LIBCPP_INLINE_VISIBILITY
771 double __do_strtod<double>(const char* __a, char** __p2) {
772 return strtod_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
776 inline _LIBCPP_INLINE_VISIBILITY
777 long double __do_strtod<long double>(const char* __a, char** __p2) {
778 return strtold_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
784 __num_get_float(const char* __a, const char* __a_end, ios_base::iostate& __err)
788 typename remove_reference<decltype(errno)>::type __save_errno = errno;
791 _Tp __ld = __do_strtod<_Tp>(__a, &__p2);
792 typename remove_reference<decltype(errno)>::type __current_errno = errno;
793 if (__current_errno == 0)
794 errno = __save_errno;
797 __err = ios_base::failbit;
800 else if (__current_errno == ERANGE)
801 __err = ios_base::failbit;
804 __err = ios_base::failbit;
808 template <class _CharT, class _InputIterator>
810 num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
812 ios_base::iostate& __err,
815 if ((__iob.flags() & ios_base::boolalpha) == 0)
818 __b = do_get(__b, __e, __iob, __err, __lv);
829 __err = ios_base::failbit;
834 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__iob.getloc());
835 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__iob.getloc());
836 typedef typename numpunct<_CharT>::string_type string_type;
837 const string_type __names[2] = {__np.truename(), __np.falsename()};
838 const string_type* __i = __scan_keyword(__b, __e, __names, __names+2,
840 __v = __i == __names;
846 template <class _CharT, class _InputIterator>
847 template <class _Signed>
849 num_get<_CharT, _InputIterator>::__do_get_signed(iter_type __b, iter_type __e,
851 ios_base::iostate& __err,
855 int __base = this->__get_base(__iob);
857 char_type __atoms[26];
858 char_type __thousands_sep;
859 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
861 __buf.resize(__buf.capacity());
862 char* __a = &__buf[0];
864 unsigned __g[__num_get_base::__num_get_buf_sz];
865 unsigned* __g_end = __g;
867 for (; __b != __e; ++__b)
869 if (__a_end == __a + __buf.size())
871 size_t __tmp = __buf.size();
872 __buf.resize(2*__buf.size());
873 __buf.resize(__buf.capacity());
875 __a_end = __a + __tmp;
877 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
878 __thousands_sep, __grouping, __g, __g_end,
882 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
885 __v = __num_get_signed_integral<_Signed>(__a, __a_end, __err, __base);
886 // Digit grouping checked
887 __check_grouping(__grouping, __g, __g_end, __err);
890 __err |= ios_base::eofbit;
896 template <class _CharT, class _InputIterator>
897 template <class _Unsigned>
899 num_get<_CharT, _InputIterator>::__do_get_unsigned(iter_type __b, iter_type __e,
901 ios_base::iostate& __err,
902 _Unsigned& __v) const
905 int __base = this->__get_base(__iob);
907 char_type __atoms[26];
908 char_type __thousands_sep;
909 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
911 __buf.resize(__buf.capacity());
912 char* __a = &__buf[0];
914 unsigned __g[__num_get_base::__num_get_buf_sz];
915 unsigned* __g_end = __g;
917 for (; __b != __e; ++__b)
919 if (__a_end == __a + __buf.size())
921 size_t __tmp = __buf.size();
922 __buf.resize(2*__buf.size());
923 __buf.resize(__buf.capacity());
925 __a_end = __a + __tmp;
927 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
928 __thousands_sep, __grouping, __g, __g_end,
932 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
935 __v = __num_get_unsigned_integral<_Unsigned>(__a, __a_end, __err, __base);
936 // Digit grouping checked
937 __check_grouping(__grouping, __g, __g_end, __err);
940 __err |= ios_base::eofbit;
946 template <class _CharT, class _InputIterator>
949 num_get<_CharT, _InputIterator>::__do_get_floating_point(iter_type __b, iter_type __e,
951 ios_base::iostate& __err,
954 // Stage 1, nothing to do
956 char_type __atoms[32];
957 char_type __decimal_point;
958 char_type __thousands_sep;
959 string __grouping = this->__stage2_float_prep(__iob, __atoms,
963 __buf.resize(__buf.capacity());
964 char* __a = &__buf[0];
966 unsigned __g[__num_get_base::__num_get_buf_sz];
967 unsigned* __g_end = __g;
969 bool __in_units = true;
971 for (; __b != __e; ++__b)
973 if (__a_end == __a + __buf.size())
975 size_t __tmp = __buf.size();
976 __buf.resize(2*__buf.size());
977 __buf.resize(__buf.capacity());
979 __a_end = __a + __tmp;
981 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
982 __decimal_point, __thousands_sep,
983 __grouping, __g, __g_end,
987 if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
990 __v = __num_get_float<_Fp>(__a, __a_end, __err);
991 // Digit grouping checked
992 __check_grouping(__grouping, __g, __g_end, __err);
995 __err |= ios_base::eofbit;
999 template <class _CharT, class _InputIterator>
1001 num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1003 ios_base::iostate& __err,
1009 char_type __atoms[26];
1010 char_type __thousands_sep = 0;
1012 use_facet<ctype<_CharT> >(__iob.getloc()).widen(__num_get_base::__src,
1013 __num_get_base::__src + 26, __atoms);
1015 __buf.resize(__buf.capacity());
1016 char* __a = &__buf[0];
1017 char* __a_end = __a;
1018 unsigned __g[__num_get_base::__num_get_buf_sz];
1019 unsigned* __g_end = __g;
1021 for (; __b != __e; ++__b)
1023 if (__a_end == __a + __buf.size())
1025 size_t __tmp = __buf.size();
1026 __buf.resize(2*__buf.size());
1027 __buf.resize(__buf.capacity());
1029 __a_end = __a + __tmp;
1031 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1032 __thousands_sep, __grouping,
1033 __g, __g_end, __atoms))
1037 __buf.resize(__a_end - __a);
1038 if (__libcpp_sscanf_l(__buf.c_str(), _LIBCPP_GET_C_LOCALE, "%p", &__v) != 1)
1039 __err = ios_base::failbit;
1042 __err |= ios_base::eofbit;
1046 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<char>)
1047 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<wchar_t>)
1049 struct _LIBCPP_TYPE_VIS __num_put_base
1052 static void __format_int(char* __fmt, const char* __len, bool __signd,
1053 ios_base::fmtflags __flags);
1054 static bool __format_float(char* __fmt, const char* __len,
1055 ios_base::fmtflags __flags);
1056 static char* __identify_padding(char* __nb, char* __ne,
1057 const ios_base& __iob);
1060 template <class _CharT>
1062 : protected __num_put_base
1064 static void __widen_and_group_int(char* __nb, char* __np, char* __ne,
1065 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1066 const locale& __loc);
1067 static void __widen_and_group_float(char* __nb, char* __np, char* __ne,
1068 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1069 const locale& __loc);
1072 template <class _CharT>
1074 __num_put<_CharT>::__widen_and_group_int(char* __nb, char* __np, char* __ne,
1075 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1076 const locale& __loc)
1078 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1079 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1080 string __grouping = __npt.grouping();
1081 if (__grouping.empty())
1083 __ct.widen(__nb, __ne, __ob);
1084 __oe = __ob + (__ne - __nb);
1090 if (*__nf == '-' || *__nf == '+')
1091 *__oe++ = __ct.widen(*__nf++);
1092 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1095 *__oe++ = __ct.widen(*__nf++);
1096 *__oe++ = __ct.widen(*__nf++);
1098 reverse(__nf, __ne);
1099 _CharT __thousands_sep = __npt.thousands_sep();
1102 for (char* __p = __nf; __p < __ne; ++__p)
1104 if (static_cast<unsigned>(__grouping[__dg]) > 0 &&
1105 __dc == static_cast<unsigned>(__grouping[__dg]))
1107 *__oe++ = __thousands_sep;
1109 if (__dg < __grouping.size()-1)
1112 *__oe++ = __ct.widen(*__p);
1115 reverse(__ob + (__nf - __nb), __oe);
1120 __op = __ob + (__np - __nb);
1123 template <class _CharT>
1125 __num_put<_CharT>::__widen_and_group_float(char* __nb, char* __np, char* __ne,
1126 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1127 const locale& __loc)
1129 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1130 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1131 string __grouping = __npt.grouping();
1134 if (*__nf == '-' || *__nf == '+')
1135 *__oe++ = __ct.widen(*__nf++);
1137 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1140 *__oe++ = __ct.widen(*__nf++);
1141 *__oe++ = __ct.widen(*__nf++);
1142 for (__ns = __nf; __ns < __ne; ++__ns)
1143 if (!isxdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
1148 for (__ns = __nf; __ns < __ne; ++__ns)
1149 if (!isdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
1152 if (__grouping.empty())
1154 __ct.widen(__nf, __ns, __oe);
1155 __oe += __ns - __nf;
1159 reverse(__nf, __ns);
1160 _CharT __thousands_sep = __npt.thousands_sep();
1163 for (char* __p = __nf; __p < __ns; ++__p)
1165 if (__grouping[__dg] > 0 && __dc == static_cast<unsigned>(__grouping[__dg]))
1167 *__oe++ = __thousands_sep;
1169 if (__dg < __grouping.size()-1)
1172 *__oe++ = __ct.widen(*__p);
1175 reverse(__ob + (__nf - __nb), __oe);
1177 for (__nf = __ns; __nf < __ne; ++__nf)
1181 *__oe++ = __npt.decimal_point();
1186 *__oe++ = __ct.widen(*__nf);
1188 __ct.widen(__nf, __ne, __oe);
1189 __oe += __ne - __nf;
1193 __op = __ob + (__np - __nb);
1196 _LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<char>)
1197 _LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<wchar_t>)
1199 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
1200 class _LIBCPP_TEMPLATE_VIS num_put
1201 : public locale::facet,
1202 private __num_put<_CharT>
1205 typedef _CharT char_type;
1206 typedef _OutputIterator iter_type;
1208 _LIBCPP_ALWAYS_INLINE
1209 explicit num_put(size_t __refs = 0)
1210 : locale::facet(__refs) {}
1212 _LIBCPP_ALWAYS_INLINE
1213 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1216 return do_put(__s, __iob, __fl, __v);
1219 _LIBCPP_ALWAYS_INLINE
1220 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1223 return do_put(__s, __iob, __fl, __v);
1226 _LIBCPP_ALWAYS_INLINE
1227 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1228 long long __v) const
1230 return do_put(__s, __iob, __fl, __v);
1233 _LIBCPP_ALWAYS_INLINE
1234 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1235 unsigned long __v) const
1237 return do_put(__s, __iob, __fl, __v);
1240 _LIBCPP_ALWAYS_INLINE
1241 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1242 unsigned long long __v) const
1244 return do_put(__s, __iob, __fl, __v);
1247 _LIBCPP_ALWAYS_INLINE
1248 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1251 return do_put(__s, __iob, __fl, __v);
1254 _LIBCPP_ALWAYS_INLINE
1255 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1256 long double __v) const
1258 return do_put(__s, __iob, __fl, __v);
1261 _LIBCPP_ALWAYS_INLINE
1262 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1263 const void* __v) const
1265 return do_put(__s, __iob, __fl, __v);
1268 static locale::id id;
1271 _LIBCPP_ALWAYS_INLINE
1274 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1276 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1278 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1279 long long __v) const;
1280 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1281 unsigned long) const;
1282 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1283 unsigned long long) const;
1284 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1286 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1287 long double __v) const;
1288 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1289 const void* __v) const;
1292 template <class _CharT, class _OutputIterator>
1294 num_put<_CharT, _OutputIterator>::id;
1296 template <class _CharT, class _OutputIterator>
1299 __pad_and_output(_OutputIterator __s,
1300 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1301 ios_base& __iob, _CharT __fl)
1303 streamsize __sz = __oe - __ob;
1304 streamsize __ns = __iob.width();
1309 for (;__ob < __op; ++__ob, ++__s)
1311 for (; __ns; --__ns, ++__s)
1313 for (; __ob < __oe; ++__ob, ++__s)
1319 #if !defined(__APPLE__) || \
1320 (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED > __MAC_10_8) || \
1321 (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_6_0)
1323 template <class _CharT, class _Traits>
1325 ostreambuf_iterator<_CharT, _Traits>
1326 __pad_and_output(ostreambuf_iterator<_CharT, _Traits> __s,
1327 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1328 ios_base& __iob, _CharT __fl)
1330 if (__s.__sbuf_ == nullptr)
1332 streamsize __sz = __oe - __ob;
1333 streamsize __ns = __iob.width();
1338 streamsize __np = __op - __ob;
1341 if (__s.__sbuf_->sputn(__ob, __np) != __np)
1343 __s.__sbuf_ = nullptr;
1349 basic_string<_CharT, _Traits> __sp(__ns, __fl);
1350 if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns)
1352 __s.__sbuf_ = nullptr;
1359 if (__s.__sbuf_->sputn(__op, __np) != __np)
1361 __s.__sbuf_ = nullptr;
1371 template <class _CharT, class _OutputIterator>
1373 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1374 char_type __fl, bool __v) const
1376 if ((__iob.flags() & ios_base::boolalpha) == 0)
1377 return do_put(__s, __iob, __fl, (unsigned long)__v);
1378 const numpunct<char_type>& __np = use_facet<numpunct<char_type> >(__iob.getloc());
1379 typedef typename numpunct<char_type>::string_type string_type;
1380 #if _LIBCPP_DEBUG_LEVEL >= 2
1381 string_type __tmp(__v ? __np.truename() : __np.falsename());
1382 string_type __nm = _VSTD::move(__tmp);
1384 string_type __nm = __v ? __np.truename() : __np.falsename();
1386 for (typename string_type::iterator __i = __nm.begin(); __i != __nm.end(); ++__i, ++__s)
1391 template <class _CharT, class _OutputIterator>
1393 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1394 char_type __fl, long __v) const
1396 // Stage 1 - Get number in narrow char
1397 char __fmt[6] = {'%', 0};
1398 const char* __len = "l";
1399 this->__format_int(__fmt+1, __len, true, __iob.flags());
1400 const unsigned __nbuf = (numeric_limits<long>::digits / 3)
1401 + ((numeric_limits<long>::digits % 3) != 0)
1404 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1405 char* __ne = __nar + __nc;
1406 char* __np = this->__identify_padding(__nar, __ne, __iob);
1407 // Stage 2 - Widen __nar while adding thousands separators
1408 char_type __o[2*(__nbuf-1) - 1];
1409 char_type* __op; // pad here
1410 char_type* __oe; // end of output
1411 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1412 // [__o, __oe) contains thousands_sep'd wide number
1414 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1417 template <class _CharT, class _OutputIterator>
1419 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1420 char_type __fl, long long __v) const
1422 // Stage 1 - Get number in narrow char
1423 char __fmt[8] = {'%', 0};
1424 const char* __len = "ll";
1425 this->__format_int(__fmt+1, __len, true, __iob.flags());
1426 const unsigned __nbuf = (numeric_limits<long long>::digits / 3)
1427 + ((numeric_limits<long long>::digits % 3) != 0)
1430 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1431 char* __ne = __nar + __nc;
1432 char* __np = this->__identify_padding(__nar, __ne, __iob);
1433 // Stage 2 - Widen __nar while adding thousands separators
1434 char_type __o[2*(__nbuf-1) - 1];
1435 char_type* __op; // pad here
1436 char_type* __oe; // end of output
1437 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1438 // [__o, __oe) contains thousands_sep'd wide number
1440 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1443 template <class _CharT, class _OutputIterator>
1445 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1446 char_type __fl, unsigned long __v) const
1448 // Stage 1 - Get number in narrow char
1449 char __fmt[6] = {'%', 0};
1450 const char* __len = "l";
1451 this->__format_int(__fmt+1, __len, false, __iob.flags());
1452 const unsigned __nbuf = (numeric_limits<unsigned long>::digits / 3)
1453 + ((numeric_limits<unsigned long>::digits % 3) != 0)
1456 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1457 char* __ne = __nar + __nc;
1458 char* __np = this->__identify_padding(__nar, __ne, __iob);
1459 // Stage 2 - Widen __nar while adding thousands separators
1460 char_type __o[2*(__nbuf-1) - 1];
1461 char_type* __op; // pad here
1462 char_type* __oe; // end of output
1463 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1464 // [__o, __oe) contains thousands_sep'd wide number
1466 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1469 template <class _CharT, class _OutputIterator>
1471 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1472 char_type __fl, unsigned long long __v) const
1474 // Stage 1 - Get number in narrow char
1475 char __fmt[8] = {'%', 0};
1476 const char* __len = "ll";
1477 this->__format_int(__fmt+1, __len, false, __iob.flags());
1478 const unsigned __nbuf = (numeric_limits<unsigned long long>::digits / 3)
1479 + ((numeric_limits<unsigned long long>::digits % 3) != 0)
1482 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1483 char* __ne = __nar + __nc;
1484 char* __np = this->__identify_padding(__nar, __ne, __iob);
1485 // Stage 2 - Widen __nar while adding thousands separators
1486 char_type __o[2*(__nbuf-1) - 1];
1487 char_type* __op; // pad here
1488 char_type* __oe; // end of output
1489 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1490 // [__o, __oe) contains thousands_sep'd wide number
1492 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1495 template <class _CharT, class _OutputIterator>
1497 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1498 char_type __fl, double __v) const
1500 // Stage 1 - Get number in narrow char
1501 char __fmt[8] = {'%', 0};
1502 const char* __len = "";
1503 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1504 const unsigned __nbuf = 30;
1508 if (__specify_precision)
1509 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
1510 (int)__iob.precision(), __v);
1512 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1513 unique_ptr<char, void(*)(void*)> __nbh(0, free);
1514 if (__nc > static_cast<int>(__nbuf-1))
1516 if (__specify_precision)
1517 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
1519 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1521 __throw_bad_alloc();
1524 char* __ne = __nb + __nc;
1525 char* __np = this->__identify_padding(__nb, __ne, __iob);
1526 // Stage 2 - Widen __nar while adding thousands separators
1527 char_type __o[2*(__nbuf-1) - 1];
1528 char_type* __ob = __o;
1529 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1532 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
1534 __throw_bad_alloc();
1537 char_type* __op; // pad here
1538 char_type* __oe; // end of output
1539 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1540 // [__o, __oe) contains thousands_sep'd wide number
1542 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1546 template <class _CharT, class _OutputIterator>
1548 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1549 char_type __fl, long double __v) const
1551 // Stage 1 - Get number in narrow char
1552 char __fmt[8] = {'%', 0};
1553 const char* __len = "L";
1554 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1555 const unsigned __nbuf = 30;
1559 if (__specify_precision)
1560 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
1561 (int)__iob.precision(), __v);
1563 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1564 unique_ptr<char, void(*)(void*)> __nbh(0, free);
1565 if (__nc > static_cast<int>(__nbuf-1))
1567 if (__specify_precision)
1568 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
1570 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1572 __throw_bad_alloc();
1575 char* __ne = __nb + __nc;
1576 char* __np = this->__identify_padding(__nb, __ne, __iob);
1577 // Stage 2 - Widen __nar while adding thousands separators
1578 char_type __o[2*(__nbuf-1) - 1];
1579 char_type* __ob = __o;
1580 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1583 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
1585 __throw_bad_alloc();
1588 char_type* __op; // pad here
1589 char_type* __oe; // end of output
1590 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1591 // [__o, __oe) contains thousands_sep'd wide number
1593 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1597 template <class _CharT, class _OutputIterator>
1599 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1600 char_type __fl, const void* __v) const
1602 // Stage 1 - Get pointer in narrow char
1603 char __fmt[6] = "%p";
1604 const unsigned __nbuf = 20;
1606 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1607 char* __ne = __nar + __nc;
1608 char* __np = this->__identify_padding(__nar, __ne, __iob);
1609 // Stage 2 - Widen __nar
1610 char_type __o[2*(__nbuf-1) - 1];
1611 char_type* __op; // pad here
1612 char_type* __oe; // end of output
1613 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
1614 __ct.widen(__nar, __ne, __o);
1615 __oe = __o + (__ne - __nar);
1619 __op = __o + (__np - __nar);
1620 // [__o, __oe) contains wide number
1622 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1625 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<char>)
1626 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<wchar_t>)
1628 template <class _CharT, class _InputIterator>
1631 __get_up_to_n_digits(_InputIterator& __b, _InputIterator __e,
1632 ios_base::iostate& __err, const ctype<_CharT>& __ct, int __n)
1634 // Precondition: __n >= 1
1637 __err |= ios_base::eofbit | ios_base::failbit;
1642 if (!__ct.is(ctype_base::digit, __c))
1644 __err |= ios_base::failbit;
1647 int __r = __ct.narrow(__c, 0) - '0';
1648 for (++__b, (void) --__n; __b != __e && __n > 0; ++__b, (void) --__n)
1652 if (!__ct.is(ctype_base::digit, __c))
1654 __r = __r * 10 + __ct.narrow(__c, 0) - '0';
1657 __err |= ios_base::eofbit;
1661 class _LIBCPP_TYPE_VIS time_base
1664 enum dateorder {no_order, dmy, mdy, ymd, ydm};
1667 template <class _CharT>
1668 class _LIBCPP_TEMPLATE_VIS __time_get_c_storage
1671 typedef basic_string<_CharT> string_type;
1673 virtual const string_type* __weeks() const;
1674 virtual const string_type* __months() const;
1675 virtual const string_type* __am_pm() const;
1676 virtual const string_type& __c() const;
1677 virtual const string_type& __r() const;
1678 virtual const string_type& __x() const;
1679 virtual const string_type& __X() const;
1681 _LIBCPP_ALWAYS_INLINE
1682 ~__time_get_c_storage() {}
1685 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
1686 class _LIBCPP_TEMPLATE_VIS time_get
1687 : public locale::facet,
1689 private __time_get_c_storage<_CharT>
1692 typedef _CharT char_type;
1693 typedef _InputIterator iter_type;
1694 typedef time_base::dateorder dateorder;
1695 typedef basic_string<char_type> string_type;
1697 _LIBCPP_ALWAYS_INLINE
1698 explicit time_get(size_t __refs = 0)
1699 : locale::facet(__refs) {}
1701 _LIBCPP_ALWAYS_INLINE
1702 dateorder date_order() const
1704 return this->do_date_order();
1707 _LIBCPP_ALWAYS_INLINE
1708 iter_type get_time(iter_type __b, iter_type __e, ios_base& __iob,
1709 ios_base::iostate& __err, tm* __tm) const
1711 return do_get_time(__b, __e, __iob, __err, __tm);
1714 _LIBCPP_ALWAYS_INLINE
1715 iter_type get_date(iter_type __b, iter_type __e, ios_base& __iob,
1716 ios_base::iostate& __err, tm* __tm) const
1718 return do_get_date(__b, __e, __iob, __err, __tm);
1721 _LIBCPP_ALWAYS_INLINE
1722 iter_type get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
1723 ios_base::iostate& __err, tm* __tm) const
1725 return do_get_weekday(__b, __e, __iob, __err, __tm);
1728 _LIBCPP_ALWAYS_INLINE
1729 iter_type get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
1730 ios_base::iostate& __err, tm* __tm) const
1732 return do_get_monthname(__b, __e, __iob, __err, __tm);
1735 _LIBCPP_ALWAYS_INLINE
1736 iter_type get_year(iter_type __b, iter_type __e, ios_base& __iob,
1737 ios_base::iostate& __err, tm* __tm) const
1739 return do_get_year(__b, __e, __iob, __err, __tm);
1742 _LIBCPP_ALWAYS_INLINE
1743 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
1744 ios_base::iostate& __err, tm *__tm,
1745 char __fmt, char __mod = 0) const
1747 return do_get(__b, __e, __iob, __err, __tm, __fmt, __mod);
1750 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
1751 ios_base::iostate& __err, tm* __tm,
1752 const char_type* __fmtb, const char_type* __fmte) const;
1754 static locale::id id;
1757 _LIBCPP_ALWAYS_INLINE
1760 virtual dateorder do_date_order() const;
1761 virtual iter_type do_get_time(iter_type __b, iter_type __e, ios_base& __iob,
1762 ios_base::iostate& __err, tm* __tm) const;
1763 virtual iter_type do_get_date(iter_type __b, iter_type __e, ios_base& __iob,
1764 ios_base::iostate& __err, tm* __tm) const;
1765 virtual iter_type do_get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
1766 ios_base::iostate& __err, tm* __tm) const;
1767 virtual iter_type do_get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
1768 ios_base::iostate& __err, tm* __tm) const;
1769 virtual iter_type do_get_year(iter_type __b, iter_type __e, ios_base& __iob,
1770 ios_base::iostate& __err, tm* __tm) const;
1771 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
1772 ios_base::iostate& __err, tm* __tm,
1773 char __fmt, char __mod) const;
1775 void __get_white_space(iter_type& __b, iter_type __e,
1776 ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1777 void __get_percent(iter_type& __b, iter_type __e, ios_base::iostate& __err,
1778 const ctype<char_type>& __ct) const;
1780 void __get_weekdayname(int& __m,
1781 iter_type& __b, iter_type __e,
1782 ios_base::iostate& __err,
1783 const ctype<char_type>& __ct) const;
1784 void __get_monthname(int& __m,
1785 iter_type& __b, iter_type __e,
1786 ios_base::iostate& __err,
1787 const ctype<char_type>& __ct) const;
1788 void __get_day(int& __d,
1789 iter_type& __b, iter_type __e,
1790 ios_base::iostate& __err,
1791 const ctype<char_type>& __ct) const;
1792 void __get_month(int& __m,
1793 iter_type& __b, iter_type __e,
1794 ios_base::iostate& __err,
1795 const ctype<char_type>& __ct) const;
1796 void __get_year(int& __y,
1797 iter_type& __b, iter_type __e,
1798 ios_base::iostate& __err,
1799 const ctype<char_type>& __ct) const;
1800 void __get_year4(int& __y,
1801 iter_type& __b, iter_type __e,
1802 ios_base::iostate& __err,
1803 const ctype<char_type>& __ct) const;
1804 void __get_hour(int& __d,
1805 iter_type& __b, iter_type __e,
1806 ios_base::iostate& __err,
1807 const ctype<char_type>& __ct) const;
1808 void __get_12_hour(int& __h,
1809 iter_type& __b, iter_type __e,
1810 ios_base::iostate& __err,
1811 const ctype<char_type>& __ct) const;
1812 void __get_am_pm(int& __h,
1813 iter_type& __b, iter_type __e,
1814 ios_base::iostate& __err,
1815 const ctype<char_type>& __ct) const;
1816 void __get_minute(int& __m,
1817 iter_type& __b, iter_type __e,
1818 ios_base::iostate& __err,
1819 const ctype<char_type>& __ct) const;
1820 void __get_second(int& __s,
1821 iter_type& __b, iter_type __e,
1822 ios_base::iostate& __err,
1823 const ctype<char_type>& __ct) const;
1824 void __get_weekday(int& __w,
1825 iter_type& __b, iter_type __e,
1826 ios_base::iostate& __err,
1827 const ctype<char_type>& __ct) const;
1828 void __get_day_year_num(int& __w,
1829 iter_type& __b, iter_type __e,
1830 ios_base::iostate& __err,
1831 const ctype<char_type>& __ct) const;
1834 template <class _CharT, class _InputIterator>
1836 time_get<_CharT, _InputIterator>::id;
1838 // time_get primitives
1840 template <class _CharT, class _InputIterator>
1842 time_get<_CharT, _InputIterator>::__get_weekdayname(int& __w,
1843 iter_type& __b, iter_type __e,
1844 ios_base::iostate& __err,
1845 const ctype<char_type>& __ct) const
1847 // Note: ignoring case comes from the POSIX strptime spec
1848 const string_type* __wk = this->__weeks();
1849 ptrdiff_t __i = __scan_keyword(__b, __e, __wk, __wk+14, __ct, __err, false) - __wk;
1854 template <class _CharT, class _InputIterator>
1856 time_get<_CharT, _InputIterator>::__get_monthname(int& __m,
1857 iter_type& __b, iter_type __e,
1858 ios_base::iostate& __err,
1859 const ctype<char_type>& __ct) const
1861 // Note: ignoring case comes from the POSIX strptime spec
1862 const string_type* __month = this->__months();
1863 ptrdiff_t __i = __scan_keyword(__b, __e, __month, __month+24, __ct, __err, false) - __month;
1868 template <class _CharT, class _InputIterator>
1870 time_get<_CharT, _InputIterator>::__get_day(int& __d,
1871 iter_type& __b, iter_type __e,
1872 ios_base::iostate& __err,
1873 const ctype<char_type>& __ct) const
1875 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
1876 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 31)
1879 __err |= ios_base::failbit;
1882 template <class _CharT, class _InputIterator>
1884 time_get<_CharT, _InputIterator>::__get_month(int& __m,
1885 iter_type& __b, iter_type __e,
1886 ios_base::iostate& __err,
1887 const ctype<char_type>& __ct) const
1889 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2) - 1;
1890 if (!(__err & ios_base::failbit) && __t <= 11)
1893 __err |= ios_base::failbit;
1896 template <class _CharT, class _InputIterator>
1898 time_get<_CharT, _InputIterator>::__get_year(int& __y,
1899 iter_type& __b, iter_type __e,
1900 ios_base::iostate& __err,
1901 const ctype<char_type>& __ct) const
1903 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
1904 if (!(__err & ios_base::failbit))
1908 else if (69 <= __t && __t <= 99)
1914 template <class _CharT, class _InputIterator>
1916 time_get<_CharT, _InputIterator>::__get_year4(int& __y,
1917 iter_type& __b, iter_type __e,
1918 ios_base::iostate& __err,
1919 const ctype<char_type>& __ct) const
1921 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
1922 if (!(__err & ios_base::failbit))
1926 template <class _CharT, class _InputIterator>
1928 time_get<_CharT, _InputIterator>::__get_hour(int& __h,
1929 iter_type& __b, iter_type __e,
1930 ios_base::iostate& __err,
1931 const ctype<char_type>& __ct) const
1933 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
1934 if (!(__err & ios_base::failbit) && __t <= 23)
1937 __err |= ios_base::failbit;
1940 template <class _CharT, class _InputIterator>
1942 time_get<_CharT, _InputIterator>::__get_12_hour(int& __h,
1943 iter_type& __b, iter_type __e,
1944 ios_base::iostate& __err,
1945 const ctype<char_type>& __ct) const
1947 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
1948 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 12)
1951 __err |= ios_base::failbit;
1954 template <class _CharT, class _InputIterator>
1956 time_get<_CharT, _InputIterator>::__get_minute(int& __m,
1957 iter_type& __b, iter_type __e,
1958 ios_base::iostate& __err,
1959 const ctype<char_type>& __ct) const
1961 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
1962 if (!(__err & ios_base::failbit) && __t <= 59)
1965 __err |= ios_base::failbit;
1968 template <class _CharT, class _InputIterator>
1970 time_get<_CharT, _InputIterator>::__get_second(int& __s,
1971 iter_type& __b, iter_type __e,
1972 ios_base::iostate& __err,
1973 const ctype<char_type>& __ct) const
1975 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
1976 if (!(__err & ios_base::failbit) && __t <= 60)
1979 __err |= ios_base::failbit;
1982 template <class _CharT, class _InputIterator>
1984 time_get<_CharT, _InputIterator>::__get_weekday(int& __w,
1985 iter_type& __b, iter_type __e,
1986 ios_base::iostate& __err,
1987 const ctype<char_type>& __ct) const
1989 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 1);
1990 if (!(__err & ios_base::failbit) && __t <= 6)
1993 __err |= ios_base::failbit;
1996 template <class _CharT, class _InputIterator>
1998 time_get<_CharT, _InputIterator>::__get_day_year_num(int& __d,
1999 iter_type& __b, iter_type __e,
2000 ios_base::iostate& __err,
2001 const ctype<char_type>& __ct) const
2003 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 3);
2004 if (!(__err & ios_base::failbit) && __t <= 365)
2007 __err |= ios_base::failbit;
2010 template <class _CharT, class _InputIterator>
2012 time_get<_CharT, _InputIterator>::__get_white_space(iter_type& __b, iter_type __e,
2013 ios_base::iostate& __err,
2014 const ctype<char_type>& __ct) const
2016 for (; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2019 __err |= ios_base::eofbit;
2022 template <class _CharT, class _InputIterator>
2024 time_get<_CharT, _InputIterator>::__get_am_pm(int& __h,
2025 iter_type& __b, iter_type __e,
2026 ios_base::iostate& __err,
2027 const ctype<char_type>& __ct) const
2029 const string_type* __ap = this->__am_pm();
2030 if (__ap[0].size() + __ap[1].size() == 0)
2032 __err |= ios_base::failbit;
2035 ptrdiff_t __i = __scan_keyword(__b, __e, __ap, __ap+2, __ct, __err, false) - __ap;
2036 if (__i == 0 && __h == 12)
2038 else if (__i == 1 && __h < 12)
2042 template <class _CharT, class _InputIterator>
2044 time_get<_CharT, _InputIterator>::__get_percent(iter_type& __b, iter_type __e,
2045 ios_base::iostate& __err,
2046 const ctype<char_type>& __ct) const
2050 __err |= ios_base::eofbit | ios_base::failbit;
2053 if (__ct.narrow(*__b, 0) != '%')
2054 __err |= ios_base::failbit;
2055 else if(++__b == __e)
2056 __err |= ios_base::eofbit;
2059 // time_get end primitives
2061 template <class _CharT, class _InputIterator>
2063 time_get<_CharT, _InputIterator>::get(iter_type __b, iter_type __e,
2065 ios_base::iostate& __err, tm* __tm,
2066 const char_type* __fmtb, const char_type* __fmte) const
2068 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2069 __err = ios_base::goodbit;
2070 while (__fmtb != __fmte && __err == ios_base::goodbit)
2074 __err = ios_base::failbit;
2077 if (__ct.narrow(*__fmtb, 0) == '%')
2079 if (++__fmtb == __fmte)
2081 __err = ios_base::failbit;
2084 char __cmd = __ct.narrow(*__fmtb, 0);
2086 if (__cmd == 'E' || __cmd == '0')
2088 if (++__fmtb == __fmte)
2090 __err = ios_base::failbit;
2094 __cmd = __ct.narrow(*__fmtb, 0);
2096 __b = do_get(__b, __e, __iob, __err, __tm, __cmd, __opt);
2099 else if (__ct.is(ctype_base::space, *__fmtb))
2101 for (++__fmtb; __fmtb != __fmte && __ct.is(ctype_base::space, *__fmtb); ++__fmtb)
2103 for ( ; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2106 else if (__ct.toupper(*__b) == __ct.toupper(*__fmtb))
2112 __err = ios_base::failbit;
2115 __err |= ios_base::eofbit;
2119 template <class _CharT, class _InputIterator>
2120 typename time_get<_CharT, _InputIterator>::dateorder
2121 time_get<_CharT, _InputIterator>::do_date_order() const
2126 template <class _CharT, class _InputIterator>
2128 time_get<_CharT, _InputIterator>::do_get_time(iter_type __b, iter_type __e,
2130 ios_base::iostate& __err,
2133 const char_type __fmt[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2134 return get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt)/sizeof(__fmt[0]));
2137 template <class _CharT, class _InputIterator>
2139 time_get<_CharT, _InputIterator>::do_get_date(iter_type __b, iter_type __e,
2141 ios_base::iostate& __err,
2144 const string_type& __fmt = this->__x();
2145 return get(__b, __e, __iob, __err, __tm, __fmt.data(), __fmt.data() + __fmt.size());
2148 template <class _CharT, class _InputIterator>
2150 time_get<_CharT, _InputIterator>::do_get_weekday(iter_type __b, iter_type __e,
2152 ios_base::iostate& __err,
2155 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2156 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2160 template <class _CharT, class _InputIterator>
2162 time_get<_CharT, _InputIterator>::do_get_monthname(iter_type __b, iter_type __e,
2164 ios_base::iostate& __err,
2167 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2168 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2172 template <class _CharT, class _InputIterator>
2174 time_get<_CharT, _InputIterator>::do_get_year(iter_type __b, iter_type __e,
2176 ios_base::iostate& __err,
2179 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2180 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2184 template <class _CharT, class _InputIterator>
2186 time_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
2188 ios_base::iostate& __err, tm* __tm,
2189 char __fmt, char) const
2191 __err = ios_base::goodbit;
2192 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2197 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2202 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2206 const string_type& __fm = this->__c();
2207 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
2212 __get_day(__tm->tm_mday, __b, __e, __err, __ct);
2216 const char_type __fm[] = {'%', 'm', '/', '%', 'd', '/', '%', 'y'};
2217 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2222 const char_type __fm[] = {'%', 'Y', '-', '%', 'm', '-', '%', 'd'};
2223 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2227 __get_hour(__tm->tm_hour, __b, __e, __err, __ct);
2230 __get_12_hour(__tm->tm_hour, __b, __e, __err, __ct);
2233 __get_day_year_num(__tm->tm_yday, __b, __e, __err, __ct);
2236 __get_month(__tm->tm_mon, __b, __e, __err, __ct);
2239 __get_minute(__tm->tm_min, __b, __e, __err, __ct);
2243 __get_white_space(__b, __e, __err, __ct);
2246 __get_am_pm(__tm->tm_hour, __b, __e, __err, __ct);
2250 const char_type __fm[] = {'%', 'I', ':', '%', 'M', ':', '%', 'S', ' ', '%', 'p'};
2251 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2256 const char_type __fm[] = {'%', 'H', ':', '%', 'M'};
2257 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2261 __get_second(__tm->tm_sec, __b, __e, __err, __ct);
2265 const char_type __fm[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2266 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2270 __get_weekday(__tm->tm_wday, __b, __e, __err, __ct);
2273 return do_get_date(__b, __e, __iob, __err, __tm);
2276 const string_type& __fm = this->__X();
2277 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
2281 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2284 __get_year4(__tm->tm_year, __b, __e, __err, __ct);
2287 __get_percent(__b, __e, __err, __ct);
2290 __err |= ios_base::failbit;
2295 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get<char>)
2296 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get<wchar_t>)
2298 class _LIBCPP_TYPE_VIS __time_get
2303 __time_get(const char* __nm);
2304 __time_get(const string& __nm);
2308 template <class _CharT>
2309 class _LIBCPP_TEMPLATE_VIS __time_get_storage
2313 typedef basic_string<_CharT> string_type;
2315 string_type __weeks_[14];
2316 string_type __months_[24];
2317 string_type __am_pm_[2];
2323 explicit __time_get_storage(const char* __nm);
2324 explicit __time_get_storage(const string& __nm);
2326 _LIBCPP_ALWAYS_INLINE ~__time_get_storage() {}
2328 time_base::dateorder __do_date_order() const;
2331 void init(const ctype<_CharT>&);
2332 string_type __analyze(char __fmt, const ctype<_CharT>&);
2335 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2336 class _LIBCPP_TEMPLATE_VIS time_get_byname
2337 : public time_get<_CharT, _InputIterator>,
2338 private __time_get_storage<_CharT>
2341 typedef time_base::dateorder dateorder;
2342 typedef _InputIterator iter_type;
2343 typedef _CharT char_type;
2344 typedef basic_string<char_type> string_type;
2346 _LIBCPP_INLINE_VISIBILITY
2347 explicit time_get_byname(const char* __nm, size_t __refs = 0)
2348 : time_get<_CharT, _InputIterator>(__refs),
2349 __time_get_storage<_CharT>(__nm) {}
2350 _LIBCPP_INLINE_VISIBILITY
2351 explicit time_get_byname(const string& __nm, size_t __refs = 0)
2352 : time_get<_CharT, _InputIterator>(__refs),
2353 __time_get_storage<_CharT>(__nm) {}
2356 _LIBCPP_INLINE_VISIBILITY
2357 ~time_get_byname() {}
2359 _LIBCPP_INLINE_VISIBILITY
2360 virtual dateorder do_date_order() const {return this->__do_date_order();}
2362 _LIBCPP_INLINE_VISIBILITY
2363 virtual const string_type* __weeks() const {return this->__weeks_;}
2364 _LIBCPP_INLINE_VISIBILITY
2365 virtual const string_type* __months() const {return this->__months_;}
2366 _LIBCPP_INLINE_VISIBILITY
2367 virtual const string_type* __am_pm() const {return this->__am_pm_;}
2368 _LIBCPP_INLINE_VISIBILITY
2369 virtual const string_type& __c() const {return this->__c_;}
2370 _LIBCPP_INLINE_VISIBILITY
2371 virtual const string_type& __r() const {return this->__r_;}
2372 _LIBCPP_INLINE_VISIBILITY
2373 virtual const string_type& __x() const {return this->__x_;}
2374 _LIBCPP_INLINE_VISIBILITY
2375 virtual const string_type& __X() const {return this->__X_;}
2378 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<char>)
2379 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<wchar_t>)
2381 class _LIBCPP_TYPE_VIS __time_put
2385 _LIBCPP_ALWAYS_INLINE __time_put() : __loc_(_LIBCPP_GET_C_LOCALE) {}
2386 __time_put(const char* __nm);
2387 __time_put(const string& __nm);
2389 void __do_put(char* __nb, char*& __ne, const tm* __tm,
2390 char __fmt, char __mod) const;
2391 void __do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm,
2392 char __fmt, char __mod) const;
2395 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2396 class _LIBCPP_TEMPLATE_VIS time_put
2397 : public locale::facet,
2401 typedef _CharT char_type;
2402 typedef _OutputIterator iter_type;
2404 _LIBCPP_ALWAYS_INLINE
2405 explicit time_put(size_t __refs = 0)
2406 : locale::facet(__refs) {}
2408 iter_type put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm,
2409 const char_type* __pb, const char_type* __pe) const;
2411 _LIBCPP_ALWAYS_INLINE
2412 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
2413 const tm* __tm, char __fmt, char __mod = 0) const
2415 return do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2418 static locale::id id;
2421 _LIBCPP_ALWAYS_INLINE
2423 virtual iter_type do_put(iter_type __s, ios_base&, char_type, const tm* __tm,
2424 char __fmt, char __mod) const;
2426 _LIBCPP_ALWAYS_INLINE
2427 explicit time_put(const char* __nm, size_t __refs)
2428 : locale::facet(__refs),
2430 _LIBCPP_ALWAYS_INLINE
2431 explicit time_put(const string& __nm, size_t __refs)
2432 : locale::facet(__refs),
2436 template <class _CharT, class _OutputIterator>
2438 time_put<_CharT, _OutputIterator>::id;
2440 template <class _CharT, class _OutputIterator>
2442 time_put<_CharT, _OutputIterator>::put(iter_type __s, ios_base& __iob,
2443 char_type __fl, const tm* __tm,
2444 const char_type* __pb,
2445 const char_type* __pe) const
2447 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2448 for (; __pb != __pe; ++__pb)
2450 if (__ct.narrow(*__pb, 0) == '%')
2458 char __fmt = __ct.narrow(*__pb, 0);
2459 if (__fmt == 'E' || __fmt == 'O')
2468 __fmt = __ct.narrow(*__pb, 0);
2470 __s = do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2478 template <class _CharT, class _OutputIterator>
2480 time_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base&,
2481 char_type, const tm* __tm,
2482 char __fmt, char __mod) const
2484 char_type __nar[100];
2485 char_type* __nb = __nar;
2486 char_type* __ne = __nb + 100;
2487 __do_put(__nb, __ne, __tm, __fmt, __mod);
2488 return _VSTD::copy(__nb, __ne, __s);
2491 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<char>)
2492 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<wchar_t>)
2494 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2495 class _LIBCPP_TEMPLATE_VIS time_put_byname
2496 : public time_put<_CharT, _OutputIterator>
2499 _LIBCPP_ALWAYS_INLINE
2500 explicit time_put_byname(const char* __nm, size_t __refs = 0)
2501 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2503 _LIBCPP_ALWAYS_INLINE
2504 explicit time_put_byname(const string& __nm, size_t __refs = 0)
2505 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2508 _LIBCPP_ALWAYS_INLINE
2509 ~time_put_byname() {}
2512 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<char>)
2513 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<wchar_t>)
2517 class _LIBCPP_TYPE_VIS money_base
2520 enum part {none, space, symbol, sign, value};
2521 struct pattern {char field[4];};
2523 _LIBCPP_ALWAYS_INLINE money_base() {}
2528 template <class _CharT, bool _International = false>
2529 class _LIBCPP_TEMPLATE_VIS moneypunct
2530 : public locale::facet,
2534 typedef _CharT char_type;
2535 typedef basic_string<char_type> string_type;
2537 _LIBCPP_ALWAYS_INLINE
2538 explicit moneypunct(size_t __refs = 0)
2539 : locale::facet(__refs) {}
2541 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
2542 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
2543 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
2544 _LIBCPP_ALWAYS_INLINE string_type curr_symbol() const {return do_curr_symbol();}
2545 _LIBCPP_ALWAYS_INLINE string_type positive_sign() const {return do_positive_sign();}
2546 _LIBCPP_ALWAYS_INLINE string_type negative_sign() const {return do_negative_sign();}
2547 _LIBCPP_ALWAYS_INLINE int frac_digits() const {return do_frac_digits();}
2548 _LIBCPP_ALWAYS_INLINE pattern pos_format() const {return do_pos_format();}
2549 _LIBCPP_ALWAYS_INLINE pattern neg_format() const {return do_neg_format();}
2551 static locale::id id;
2552 static const bool intl = _International;
2555 _LIBCPP_ALWAYS_INLINE
2558 virtual char_type do_decimal_point() const {return numeric_limits<char_type>::max();}
2559 virtual char_type do_thousands_sep() const {return numeric_limits<char_type>::max();}
2560 virtual string do_grouping() const {return string();}
2561 virtual string_type do_curr_symbol() const {return string_type();}
2562 virtual string_type do_positive_sign() const {return string_type();}
2563 virtual string_type do_negative_sign() const {return string_type(1, '-');}
2564 virtual int do_frac_digits() const {return 0;}
2565 virtual pattern do_pos_format() const
2566 {pattern __p = {{symbol, sign, none, value}}; return __p;}
2567 virtual pattern do_neg_format() const
2568 {pattern __p = {{symbol, sign, none, value}}; return __p;}
2571 template <class _CharT, bool _International>
2573 moneypunct<_CharT, _International>::id;
2575 template <class _CharT, bool _International>
2577 moneypunct<_CharT, _International>::intl;
2579 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, false>)
2580 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, true>)
2581 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, false>)
2582 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, true>)
2584 // moneypunct_byname
2586 template <class _CharT, bool _International = false>
2587 class _LIBCPP_TEMPLATE_VIS moneypunct_byname
2588 : public moneypunct<_CharT, _International>
2591 typedef money_base::pattern pattern;
2592 typedef _CharT char_type;
2593 typedef basic_string<char_type> string_type;
2595 _LIBCPP_ALWAYS_INLINE
2596 explicit moneypunct_byname(const char* __nm, size_t __refs = 0)
2597 : moneypunct<_CharT, _International>(__refs) {init(__nm);}
2599 _LIBCPP_ALWAYS_INLINE
2600 explicit moneypunct_byname(const string& __nm, size_t __refs = 0)
2601 : moneypunct<_CharT, _International>(__refs) {init(__nm.c_str());}
2604 _LIBCPP_ALWAYS_INLINE
2605 ~moneypunct_byname() {}
2607 virtual char_type do_decimal_point() const {return __decimal_point_;}
2608 virtual char_type do_thousands_sep() const {return __thousands_sep_;}
2609 virtual string do_grouping() const {return __grouping_;}
2610 virtual string_type do_curr_symbol() const {return __curr_symbol_;}
2611 virtual string_type do_positive_sign() const {return __positive_sign_;}
2612 virtual string_type do_negative_sign() const {return __negative_sign_;}
2613 virtual int do_frac_digits() const {return __frac_digits_;}
2614 virtual pattern do_pos_format() const {return __pos_format_;}
2615 virtual pattern do_neg_format() const {return __neg_format_;}
2618 char_type __decimal_point_;
2619 char_type __thousands_sep_;
2621 string_type __curr_symbol_;
2622 string_type __positive_sign_;
2623 string_type __negative_sign_;
2625 pattern __pos_format_;
2626 pattern __neg_format_;
2628 void init(const char*);
2631 template<> void moneypunct_byname<char, false>::init(const char*);
2632 template<> void moneypunct_byname<char, true>::init(const char*);
2633 template<> void moneypunct_byname<wchar_t, false>::init(const char*);
2634 template<> void moneypunct_byname<wchar_t, true>::init(const char*);
2636 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, false>)
2637 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, true>)
2638 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, false>)
2639 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, true>)
2643 template <class _CharT>
2647 typedef _CharT char_type;
2648 typedef basic_string<char_type> string_type;
2650 _LIBCPP_ALWAYS_INLINE __money_get() {}
2652 static void __gather_info(bool __intl, const locale& __loc,
2653 money_base::pattern& __pat, char_type& __dp,
2654 char_type& __ts, string& __grp,
2655 string_type& __sym, string_type& __psn,
2656 string_type& __nsn, int& __fd);
2659 template <class _CharT>
2661 __money_get<_CharT>::__gather_info(bool __intl, const locale& __loc,
2662 money_base::pattern& __pat, char_type& __dp,
2663 char_type& __ts, string& __grp,
2664 string_type& __sym, string_type& __psn,
2665 string_type& __nsn, int& __fd)
2669 const moneypunct<char_type, true>& __mp =
2670 use_facet<moneypunct<char_type, true> >(__loc);
2671 __pat = __mp.neg_format();
2672 __nsn = __mp.negative_sign();
2673 __psn = __mp.positive_sign();
2674 __dp = __mp.decimal_point();
2675 __ts = __mp.thousands_sep();
2676 __grp = __mp.grouping();
2677 __sym = __mp.curr_symbol();
2678 __fd = __mp.frac_digits();
2682 const moneypunct<char_type, false>& __mp =
2683 use_facet<moneypunct<char_type, false> >(__loc);
2684 __pat = __mp.neg_format();
2685 __nsn = __mp.negative_sign();
2686 __psn = __mp.positive_sign();
2687 __dp = __mp.decimal_point();
2688 __ts = __mp.thousands_sep();
2689 __grp = __mp.grouping();
2690 __sym = __mp.curr_symbol();
2691 __fd = __mp.frac_digits();
2695 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<char>)
2696 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<wchar_t>)
2698 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2699 class _LIBCPP_TEMPLATE_VIS money_get
2700 : public locale::facet,
2701 private __money_get<_CharT>
2704 typedef _CharT char_type;
2705 typedef _InputIterator iter_type;
2706 typedef basic_string<char_type> string_type;
2708 _LIBCPP_ALWAYS_INLINE
2709 explicit money_get(size_t __refs = 0)
2710 : locale::facet(__refs) {}
2712 _LIBCPP_ALWAYS_INLINE
2713 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
2714 ios_base::iostate& __err, long double& __v) const
2716 return do_get(__b, __e, __intl, __iob, __err, __v);
2719 _LIBCPP_ALWAYS_INLINE
2720 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
2721 ios_base::iostate& __err, string_type& __v) const
2723 return do_get(__b, __e, __intl, __iob, __err, __v);
2726 static locale::id id;
2730 _LIBCPP_ALWAYS_INLINE
2733 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
2734 ios_base& __iob, ios_base::iostate& __err,
2735 long double& __v) const;
2736 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
2737 ios_base& __iob, ios_base::iostate& __err,
2738 string_type& __v) const;
2741 static bool __do_get(iter_type& __b, iter_type __e,
2742 bool __intl, const locale& __loc,
2743 ios_base::fmtflags __flags, ios_base::iostate& __err,
2744 bool& __neg, const ctype<char_type>& __ct,
2745 unique_ptr<char_type, void(*)(void*)>& __wb,
2746 char_type*& __wn, char_type* __we);
2749 template <class _CharT, class _InputIterator>
2751 money_get<_CharT, _InputIterator>::id;
2753 _LIBCPP_FUNC_VIS void __do_nothing(void*);
2755 template <class _Tp>
2758 __double_or_nothing(unique_ptr<_Tp, void(*)(void*)>& __b, _Tp*& __n, _Tp*& __e)
2760 bool __owns = __b.get_deleter() != __do_nothing;
2761 size_t __cur_cap = static_cast<size_t>(__e-__b.get()) * sizeof(_Tp);
2762 size_t __new_cap = __cur_cap < numeric_limits<size_t>::max() / 2 ?
2763 2 * __cur_cap : numeric_limits<size_t>::max();
2765 __new_cap = sizeof(_Tp);
2766 size_t __n_off = static_cast<size_t>(__n - __b.get());
2767 _Tp* __t = (_Tp*)realloc(__owns ? __b.get() : 0, __new_cap);
2769 __throw_bad_alloc();
2772 __b = unique_ptr<_Tp, void(*)(void*)>(__t, free);
2773 __new_cap /= sizeof(_Tp);
2774 __n = __b.get() + __n_off;
2775 __e = __b.get() + __new_cap;
2779 template <class _CharT, class _InputIterator>
2781 money_get<_CharT, _InputIterator>::__do_get(iter_type& __b, iter_type __e,
2782 bool __intl, const locale& __loc,
2783 ios_base::fmtflags __flags,
2784 ios_base::iostate& __err,
2786 const ctype<char_type>& __ct,
2787 unique_ptr<char_type, void(*)(void*)>& __wb,
2788 char_type*& __wn, char_type* __we)
2790 const unsigned __bz = 100;
2791 unsigned __gbuf[__bz];
2792 unique_ptr<unsigned, void(*)(void*)> __gb(__gbuf, __do_nothing);
2793 unsigned* __gn = __gb.get();
2794 unsigned* __ge = __gn + __bz;
2795 money_base::pattern __pat;
2802 // Capture the spaces read into money_base::{space,none} so they
2803 // can be compared to initial spaces in __sym.
2804 string_type __spaces;
2806 __money_get<_CharT>::__gather_info(__intl, __loc, __pat, __dp, __ts, __grp,
2807 __sym, __psn, __nsn, __fd);
2808 const string_type* __trailing_sign = 0;
2810 for (unsigned __p = 0; __p < 4 && __b != __e; ++__p)
2812 switch (__pat.field[__p])
2814 case money_base::space:
2817 if (__ct.is(ctype_base::space, *__b))
2818 __spaces.push_back(*__b++);
2821 __err |= ios_base::failbit;
2826 case money_base::none:
2829 while (__b != __e && __ct.is(ctype_base::space, *__b))
2830 __spaces.push_back(*__b++);
2833 case money_base::sign:
2834 if (__psn.size() + __nsn.size() > 0)
2836 if (__psn.size() == 0 || __nsn.size() == 0)
2837 { // sign is optional
2838 if (__psn.size() > 0)
2839 { // __nsn.size() == 0
2840 if (*__b == __psn[0])
2843 if (__psn.size() > 1)
2844 __trailing_sign = &__psn;
2849 else if (*__b == __nsn[0]) // __nsn.size() > 0 && __psn.size() == 0
2853 if (__nsn.size() > 1)
2854 __trailing_sign = &__nsn;
2857 else // sign is required
2859 if (*__b == __psn[0])
2862 if (__psn.size() > 1)
2863 __trailing_sign = &__psn;
2865 else if (*__b == __nsn[0])
2869 if (__nsn.size() > 1)
2870 __trailing_sign = &__nsn;
2874 __err |= ios_base::failbit;
2880 case money_base::symbol:
2882 bool __more_needed = __trailing_sign ||
2884 (__p == 2 && __pat.field[3] != static_cast<char>(money_base::none));
2885 bool __sb = (__flags & ios_base::showbase) != 0;
2886 if (__sb || __more_needed)
2888 typename string_type::const_iterator __sym_space_end = __sym.begin();
2889 if (__p > 0 && (__pat.field[__p - 1] == money_base::none ||
2890 __pat.field[__p - 1] == money_base::space)) {
2891 // Match spaces we've already read against spaces at
2892 // the beginning of __sym.
2893 while (__sym_space_end != __sym.end() &&
2894 __ct.is(ctype_base::space, *__sym_space_end))
2896 const size_t __num_spaces = __sym_space_end - __sym.begin();
2897 if (__num_spaces > __spaces.size() ||
2898 !equal(__spaces.end() - __num_spaces, __spaces.end(),
2900 // No match. Put __sym_space_end back at the
2901 // beginning of __sym, which will prevent a
2902 // match in the next loop.
2903 __sym_space_end = __sym.begin();
2906 typename string_type::const_iterator __sym_curr_char = __sym_space_end;
2907 while (__sym_curr_char != __sym.end() && __b != __e &&
2908 *__b == *__sym_curr_char) {
2912 if (__sb && __sym_curr_char != __sym.end())
2914 __err |= ios_base::failbit;
2920 case money_base::value:
2923 for (; __b != __e; ++__b)
2925 char_type __c = *__b;
2926 if (__ct.is(ctype_base::digit, __c))
2929 __double_or_nothing(__wb, __wn, __we);
2933 else if (__grp.size() > 0 && __ng > 0 && __c == __ts)
2936 __double_or_nothing(__gb, __gn, __ge);
2943 if (__gb.get() != __gn && __ng > 0)
2946 __double_or_nothing(__gb, __gn, __ge);
2951 if (__b == __e || *__b != __dp)
2953 __err |= ios_base::failbit;
2956 for (++__b; __fd > 0; --__fd, ++__b)
2958 if (__b == __e || !__ct.is(ctype_base::digit, *__b))
2960 __err |= ios_base::failbit;
2964 __double_or_nothing(__wb, __wn, __we);
2968 if (__wn == __wb.get())
2970 __err |= ios_base::failbit;
2977 if (__trailing_sign)
2979 for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, ++__b)
2981 if (__b == __e || *__b != (*__trailing_sign)[__i])
2983 __err |= ios_base::failbit;
2988 if (__gb.get() != __gn)
2990 ios_base::iostate __et = ios_base::goodbit;
2991 __check_grouping(__grp, __gb.get(), __gn, __et);
2994 __err |= ios_base::failbit;
3001 template <class _CharT, class _InputIterator>
3003 money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3004 bool __intl, ios_base& __iob,
3005 ios_base::iostate& __err,
3006 long double& __v) const
3008 const int __bz = 100;
3009 char_type __wbuf[__bz];
3010 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3012 char_type* __we = __wbuf + __bz;
3013 locale __loc = __iob.getloc();
3014 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3016 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3019 const char __src[] = "0123456789";
3020 char_type __atoms[sizeof(__src)-1];
3021 __ct.widen(__src, __src + (sizeof(__src)-1), __atoms);
3023 char* __nc = __nbuf;
3024 unique_ptr<char, void(*)(void*)> __h(0, free);
3025 if (__wn - __wb.get() > __bz-2)
3027 __h.reset((char*)malloc(static_cast<size_t>(__wn - __wb.get() + 2)));
3029 __throw_bad_alloc();
3034 for (const char_type* __w = __wb.get(); __w < __wn; ++__w, ++__nc)
3035 *__nc = __src[find(__atoms, _VSTD::end(__atoms), *__w) - __atoms];
3037 if (sscanf(__nbuf, "%Lf", &__v) != 1)
3038 __throw_runtime_error("money_get error");
3041 __err |= ios_base::eofbit;
3045 template <class _CharT, class _InputIterator>
3047 money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3048 bool __intl, ios_base& __iob,
3049 ios_base::iostate& __err,
3050 string_type& __v) const
3052 const int __bz = 100;
3053 char_type __wbuf[__bz];
3054 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3056 char_type* __we = __wbuf + __bz;
3057 locale __loc = __iob.getloc();
3058 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3060 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3065 __v.push_back(__ct.widen('-'));
3066 char_type __z = __ct.widen('0');
3068 for (__w = __wb.get(); __w < __wn-1; ++__w)
3071 __v.append(__w, __wn);
3074 __err |= ios_base::eofbit;
3078 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<char>)
3079 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<wchar_t>)
3083 template <class _CharT>
3087 typedef _CharT char_type;
3088 typedef basic_string<char_type> string_type;
3090 _LIBCPP_ALWAYS_INLINE __money_put() {}
3092 static void __gather_info(bool __intl, bool __neg, const locale& __loc,
3093 money_base::pattern& __pat, char_type& __dp,
3094 char_type& __ts, string& __grp,
3095 string_type& __sym, string_type& __sn,
3097 static void __format(char_type* __mb, char_type*& __mi, char_type*& __me,
3098 ios_base::fmtflags __flags,
3099 const char_type* __db, const char_type* __de,
3100 const ctype<char_type>& __ct, bool __neg,
3101 const money_base::pattern& __pat, char_type __dp,
3102 char_type __ts, const string& __grp,
3103 const string_type& __sym, const string_type& __sn,
3107 template <class _CharT>
3109 __money_put<_CharT>::__gather_info(bool __intl, bool __neg, const locale& __loc,
3110 money_base::pattern& __pat, char_type& __dp,
3111 char_type& __ts, string& __grp,
3112 string_type& __sym, string_type& __sn,
3117 const moneypunct<char_type, true>& __mp =
3118 use_facet<moneypunct<char_type, true> >(__loc);
3121 __pat = __mp.neg_format();
3122 __sn = __mp.negative_sign();
3126 __pat = __mp.pos_format();
3127 __sn = __mp.positive_sign();
3129 __dp = __mp.decimal_point();
3130 __ts = __mp.thousands_sep();
3131 __grp = __mp.grouping();
3132 __sym = __mp.curr_symbol();
3133 __fd = __mp.frac_digits();
3137 const moneypunct<char_type, false>& __mp =
3138 use_facet<moneypunct<char_type, false> >(__loc);
3141 __pat = __mp.neg_format();
3142 __sn = __mp.negative_sign();
3146 __pat = __mp.pos_format();
3147 __sn = __mp.positive_sign();
3149 __dp = __mp.decimal_point();
3150 __ts = __mp.thousands_sep();
3151 __grp = __mp.grouping();
3152 __sym = __mp.curr_symbol();
3153 __fd = __mp.frac_digits();
3157 template <class _CharT>
3159 __money_put<_CharT>::__format(char_type* __mb, char_type*& __mi, char_type*& __me,
3160 ios_base::fmtflags __flags,
3161 const char_type* __db, const char_type* __de,
3162 const ctype<char_type>& __ct, bool __neg,
3163 const money_base::pattern& __pat, char_type __dp,
3164 char_type __ts, const string& __grp,
3165 const string_type& __sym, const string_type& __sn,
3169 for (unsigned __p = 0; __p < 4; ++__p)
3171 switch (__pat.field[__p])
3173 case money_base::none:
3176 case money_base::space:
3178 *__me++ = __ct.widen(' ');
3180 case money_base::sign:
3184 case money_base::symbol:
3185 if (!__sym.empty() && (__flags & ios_base::showbase))
3186 __me = _VSTD::copy(__sym.begin(), __sym.end(), __me);
3188 case money_base::value:
3190 // remember start of value so we can reverse it
3191 char_type* __t = __me;
3192 // find beginning of digits
3195 // find end of digits
3196 const char_type* __d;
3197 for (__d = __db; __d < __de; ++__d)
3198 if (!__ct.is(ctype_base::digit, *__d))
3200 // print fractional part
3204 for (__f = __fd; __d > __db && __f > 0; --__f)
3206 char_type __z = __f > 0 ? __ct.widen('0') : char_type();
3207 for (; __f > 0; --__f)
3214 *__me++ = __ct.widen('0');
3220 unsigned __gl = __grp.empty() ? numeric_limits<unsigned>::max()
3221 : static_cast<unsigned>(__grp[__ig]);
3228 if (++__ig < __grp.size())
3229 __gl = __grp[__ig] == numeric_limits<char>::max() ?
3230 numeric_limits<unsigned>::max() :
3231 static_cast<unsigned>(__grp[__ig]);
3243 // print rest of sign, if any
3244 if (__sn.size() > 1)
3245 __me = _VSTD::copy(__sn.begin()+1, __sn.end(), __me);
3247 if ((__flags & ios_base::adjustfield) == ios_base::left)
3249 else if ((__flags & ios_base::adjustfield) != ios_base::internal)
3253 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<char>)
3254 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<wchar_t>)
3256 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
3257 class _LIBCPP_TEMPLATE_VIS money_put
3258 : public locale::facet,
3259 private __money_put<_CharT>
3262 typedef _CharT char_type;
3263 typedef _OutputIterator iter_type;
3264 typedef basic_string<char_type> string_type;
3266 _LIBCPP_ALWAYS_INLINE
3267 explicit money_put(size_t __refs = 0)
3268 : locale::facet(__refs) {}
3270 _LIBCPP_ALWAYS_INLINE
3271 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3272 long double __units) const
3274 return do_put(__s, __intl, __iob, __fl, __units);
3277 _LIBCPP_ALWAYS_INLINE
3278 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3279 const string_type& __digits) const
3281 return do_put(__s, __intl, __iob, __fl, __digits);
3284 static locale::id id;
3287 _LIBCPP_ALWAYS_INLINE
3290 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3291 char_type __fl, long double __units) const;
3292 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3293 char_type __fl, const string_type& __digits) const;
3296 template <class _CharT, class _OutputIterator>
3298 money_put<_CharT, _OutputIterator>::id;
3300 template <class _CharT, class _OutputIterator>
3302 money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3303 ios_base& __iob, char_type __fl,
3304 long double __units) const
3307 const size_t __bs = 100;
3310 char_type __digits[__bs];
3311 char_type* __db = __digits;
3312 size_t __n = static_cast<size_t>(snprintf(__bb, __bs, "%.0Lf", __units));
3313 unique_ptr<char, void(*)(void*)> __hn(0, free);
3314 unique_ptr<char_type, void(*)(void*)> __hd(0, free);
3315 // secure memory for digit storage
3318 __n = static_cast<size_t>(__libcpp_asprintf_l(&__bb, _LIBCPP_GET_C_LOCALE, "%.0Lf", __units));
3320 __throw_bad_alloc();
3322 __hd.reset((char_type*)malloc(__n * sizeof(char_type)));
3323 if (__hd == nullptr)
3324 __throw_bad_alloc();
3328 locale __loc = __iob.getloc();
3329 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3330 __ct.widen(__bb, __bb + __n, __db);
3331 bool __neg = __n > 0 && __bb[0] == '-';
3332 money_base::pattern __pat;
3339 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3340 // secure memory for formatting
3341 char_type __mbuf[__bs];
3342 char_type* __mb = __mbuf;
3343 unique_ptr<char_type, void(*)(void*)> __hw(0, free);
3344 size_t __exn = static_cast<int>(__n) > __fd ?
3345 (__n - static_cast<size_t>(__fd)) * 2 + __sn.size() +
3346 __sym.size() + static_cast<size_t>(__fd) + 1
3347 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
3350 __hw.reset((char_type*)malloc(__exn * sizeof(char_type)));
3353 __throw_bad_alloc();
3358 this->__format(__mb, __mi, __me, __iob.flags(),
3359 __db, __db + __n, __ct,
3360 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3361 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3364 template <class _CharT, class _OutputIterator>
3366 money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3367 ios_base& __iob, char_type __fl,
3368 const string_type& __digits) const
3371 locale __loc = __iob.getloc();
3372 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3373 bool __neg = __digits.size() > 0 && __digits[0] == __ct.widen('-');
3374 money_base::pattern __pat;
3381 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3382 // secure memory for formatting
3383 char_type __mbuf[100];
3384 char_type* __mb = __mbuf;
3385 unique_ptr<char_type, void(*)(void*)> __h(0, free);
3386 size_t __exn = static_cast<int>(__digits.size()) > __fd ?
3387 (__digits.size() - static_cast<size_t>(__fd)) * 2 +
3388 __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 1
3389 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
3392 __h.reset((char_type*)malloc(__exn * sizeof(char_type)));
3395 __throw_bad_alloc();
3400 this->__format(__mb, __mi, __me, __iob.flags(),
3401 __digits.data(), __digits.data() + __digits.size(), __ct,
3402 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3403 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3406 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<char>)
3407 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<wchar_t>)
3411 class _LIBCPP_TYPE_VIS messages_base
3414 typedef ptrdiff_t catalog;
3416 _LIBCPP_ALWAYS_INLINE messages_base() {}
3419 template <class _CharT>
3420 class _LIBCPP_TEMPLATE_VIS messages
3421 : public locale::facet,
3422 public messages_base
3425 typedef _CharT char_type;
3426 typedef basic_string<_CharT> string_type;
3428 _LIBCPP_ALWAYS_INLINE
3429 explicit messages(size_t __refs = 0)
3430 : locale::facet(__refs) {}
3432 _LIBCPP_ALWAYS_INLINE
3433 catalog open(const basic_string<char>& __nm, const locale& __loc) const
3435 return do_open(__nm, __loc);
3438 _LIBCPP_ALWAYS_INLINE
3439 string_type get(catalog __c, int __set, int __msgid,
3440 const string_type& __dflt) const
3442 return do_get(__c, __set, __msgid, __dflt);
3445 _LIBCPP_ALWAYS_INLINE
3446 void close(catalog __c) const
3451 static locale::id id;
3454 _LIBCPP_ALWAYS_INLINE
3457 virtual catalog do_open(const basic_string<char>&, const locale&) const;
3458 virtual string_type do_get(catalog, int __set, int __msgid,
3459 const string_type& __dflt) const;
3460 virtual void do_close(catalog) const;
3463 template <class _CharT>
3465 messages<_CharT>::id;
3467 template <class _CharT>
3468 typename messages<_CharT>::catalog
3469 messages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const
3471 #ifdef _LIBCPP_HAS_CATOPEN
3472 catalog __cat = (catalog)catopen(__nm.c_str(), NL_CAT_LOCALE);
3474 __cat = static_cast<catalog>((static_cast<size_t>(__cat) >> 1));
3476 #else // !_LIBCPP_HAS_CATOPEN
3478 #endif // _LIBCPP_HAS_CATOPEN
3481 template <class _CharT>
3482 typename messages<_CharT>::string_type
3483 messages<_CharT>::do_get(catalog __c, int __set, int __msgid,
3484 const string_type& __dflt) const
3486 #ifdef _LIBCPP_HAS_CATOPEN
3488 __narrow_to_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__ndflt),
3490 __dflt.c_str() + __dflt.size());
3493 nl_catd __cat = (nl_catd)__c;
3494 char* __n = catgets(__cat, __set, __msgid, __ndflt.c_str());
3496 __widen_from_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__w),
3497 __n, __n + strlen(__n));
3499 #else // !_LIBCPP_HAS_CATOPEN
3501 #endif // _LIBCPP_HAS_CATOPEN
3504 template <class _CharT>
3506 messages<_CharT>::do_close(catalog __c) const
3508 #ifdef _LIBCPP_HAS_CATOPEN
3511 nl_catd __cat = (nl_catd)__c;
3513 #endif // _LIBCPP_HAS_CATOPEN
3516 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<char>)
3517 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<wchar_t>)
3519 template <class _CharT>
3520 class _LIBCPP_TEMPLATE_VIS messages_byname
3521 : public messages<_CharT>
3524 typedef messages_base::catalog catalog;
3525 typedef basic_string<_CharT> string_type;
3527 _LIBCPP_ALWAYS_INLINE
3528 explicit messages_byname(const char*, size_t __refs = 0)
3529 : messages<_CharT>(__refs) {}
3531 _LIBCPP_ALWAYS_INLINE
3532 explicit messages_byname(const string&, size_t __refs = 0)
3533 : messages<_CharT>(__refs) {}
3536 _LIBCPP_ALWAYS_INLINE
3537 ~messages_byname() {}
3540 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<char>)
3541 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<wchar_t>)
3543 template<class _Codecvt, class _Elem = wchar_t,
3544 class _Wide_alloc = allocator<_Elem>,
3545 class _Byte_alloc = allocator<char> >
3546 class _LIBCPP_TEMPLATE_VIS wstring_convert
3549 typedef basic_string<char, char_traits<char>, _Byte_alloc> byte_string;
3550 typedef basic_string<_Elem, char_traits<_Elem>, _Wide_alloc> wide_string;
3551 typedef typename _Codecvt::state_type state_type;
3552 typedef typename wide_string::traits_type::int_type int_type;
3555 byte_string __byte_err_string_;
3556 wide_string __wide_err_string_;
3557 _Codecvt* __cvtptr_;
3558 state_type __cvtstate_;
3561 wstring_convert(const wstring_convert& __wc);
3562 wstring_convert& operator=(const wstring_convert& __wc);
3564 _LIBCPP_ALWAYS_INLINE
3565 _LIBCPP_EXPLICIT_AFTER_CXX11 wstring_convert(_Codecvt* __pcvt = new _Codecvt);
3566 _LIBCPP_ALWAYS_INLINE
3567 wstring_convert(_Codecvt* __pcvt, state_type __state);
3568 _LIBCPP_EXPLICIT_AFTER_CXX11 wstring_convert(const byte_string& __byte_err,
3569 const wide_string& __wide_err = wide_string());
3570 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3571 _LIBCPP_ALWAYS_INLINE
3572 wstring_convert(wstring_convert&& __wc);
3576 _LIBCPP_ALWAYS_INLINE
3577 wide_string from_bytes(char __byte)
3578 {return from_bytes(&__byte, &__byte+1);}
3579 _LIBCPP_ALWAYS_INLINE
3580 wide_string from_bytes(const char* __ptr)
3581 {return from_bytes(__ptr, __ptr + char_traits<char>::length(__ptr));}
3582 _LIBCPP_ALWAYS_INLINE
3583 wide_string from_bytes(const byte_string& __str)
3584 {return from_bytes(__str.data(), __str.data() + __str.size());}
3585 wide_string from_bytes(const char* __first, const char* __last);
3587 _LIBCPP_ALWAYS_INLINE
3588 byte_string to_bytes(_Elem __wchar)
3589 {return to_bytes(&__wchar, &__wchar+1);}
3590 _LIBCPP_ALWAYS_INLINE
3591 byte_string to_bytes(const _Elem* __wptr)
3592 {return to_bytes(__wptr, __wptr + char_traits<_Elem>::length(__wptr));}
3593 _LIBCPP_ALWAYS_INLINE
3594 byte_string to_bytes(const wide_string& __wstr)
3595 {return to_bytes(__wstr.data(), __wstr.data() + __wstr.size());}
3596 byte_string to_bytes(const _Elem* __first, const _Elem* __last);
3598 _LIBCPP_ALWAYS_INLINE
3599 size_t converted() const _NOEXCEPT {return __cvtcount_;}
3600 _LIBCPP_ALWAYS_INLINE
3601 state_type state() const {return __cvtstate_;}
3604 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3606 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3607 wstring_convert(_Codecvt* __pcvt)
3608 : __cvtptr_(__pcvt), __cvtstate_(), __cvtcount_(0)
3612 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3614 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3615 wstring_convert(_Codecvt* __pcvt, state_type __state)
3616 : __cvtptr_(__pcvt), __cvtstate_(__state), __cvtcount_(0)
3620 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3621 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3622 wstring_convert(const byte_string& __byte_err, const wide_string& __wide_err)
3623 : __byte_err_string_(__byte_err), __wide_err_string_(__wide_err),
3624 __cvtstate_(), __cvtcount_(0)
3626 __cvtptr_ = new _Codecvt;
3629 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3631 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3633 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3634 wstring_convert(wstring_convert&& __wc)
3635 : __byte_err_string_(_VSTD::move(__wc.__byte_err_string_)),
3636 __wide_err_string_(_VSTD::move(__wc.__wide_err_string_)),
3637 __cvtptr_(__wc.__cvtptr_),
3638 __cvtstate_(__wc.__cvtstate_), __cvtcount_(__wc.__cvtcount_)
3640 __wc.__cvtptr_ = nullptr;
3643 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
3645 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3646 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::~wstring_convert()
3651 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3652 typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::wide_string
3653 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3654 from_bytes(const char* __frm, const char* __frm_end)
3657 if (__cvtptr_ != nullptr)
3659 wide_string __ws(2*(__frm_end - __frm), _Elem());
3660 if (__frm != __frm_end)
3661 __ws.resize(__ws.capacity());
3662 codecvt_base::result __r = codecvt_base::ok;
3663 state_type __st = __cvtstate_;
3664 if (__frm != __frm_end)
3666 _Elem* __to = &__ws[0];
3667 _Elem* __to_end = __to + __ws.size();
3668 const char* __frm_nxt;
3672 __r = __cvtptr_->in(__st, __frm, __frm_end, __frm_nxt,
3673 __to, __to_end, __to_nxt);
3674 __cvtcount_ += __frm_nxt - __frm;
3675 if (__frm_nxt == __frm)
3677 __r = codecvt_base::error;
3679 else if (__r == codecvt_base::noconv)
3681 __ws.resize(__to - &__ws[0]);
3682 // This only gets executed if _Elem is char
3683 __ws.append((const _Elem*)__frm, (const _Elem*)__frm_end);
3685 __r = codecvt_base::ok;
3687 else if (__r == codecvt_base::ok)
3689 __ws.resize(__to_nxt - &__ws[0]);
3692 else if (__r == codecvt_base::partial)
3694 ptrdiff_t __s = __to_nxt - &__ws[0];
3695 __ws.resize(2 * __s);
3696 __to = &__ws[0] + __s;
3697 __to_end = &__ws[0] + __ws.size();
3700 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3702 if (__r == codecvt_base::ok)
3706 if (__wide_err_string_.empty())
3707 __throw_range_error("wstring_convert: from_bytes error");
3709 return __wide_err_string_;
3712 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3713 typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::byte_string
3714 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3715 to_bytes(const _Elem* __frm, const _Elem* __frm_end)
3718 if (__cvtptr_ != nullptr)
3720 byte_string __bs(2*(__frm_end - __frm), char());
3721 if (__frm != __frm_end)
3722 __bs.resize(__bs.capacity());
3723 codecvt_base::result __r = codecvt_base::ok;
3724 state_type __st = __cvtstate_;
3725 if (__frm != __frm_end)
3727 char* __to = &__bs[0];
3728 char* __to_end = __to + __bs.size();
3729 const _Elem* __frm_nxt;
3733 __r = __cvtptr_->out(__st, __frm, __frm_end, __frm_nxt,
3734 __to, __to_end, __to_nxt);
3735 __cvtcount_ += __frm_nxt - __frm;
3736 if (__frm_nxt == __frm)
3738 __r = codecvt_base::error;
3740 else if (__r == codecvt_base::noconv)
3742 __bs.resize(__to - &__bs[0]);
3743 // This only gets executed if _Elem is char
3744 __bs.append((const char*)__frm, (const char*)__frm_end);
3746 __r = codecvt_base::ok;
3748 else if (__r == codecvt_base::ok)
3750 __bs.resize(__to_nxt - &__bs[0]);
3753 else if (__r == codecvt_base::partial)
3755 ptrdiff_t __s = __to_nxt - &__bs[0];
3756 __bs.resize(2 * __s);
3757 __to = &__bs[0] + __s;
3758 __to_end = &__bs[0] + __bs.size();
3761 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3763 if (__r == codecvt_base::ok)
3765 size_t __s = __bs.size();
3766 __bs.resize(__bs.capacity());
3767 char* __to = &__bs[0] + __s;
3768 char* __to_end = __to + __bs.size();
3772 __r = __cvtptr_->unshift(__st, __to, __to_end, __to_nxt);
3773 if (__r == codecvt_base::noconv)
3775 __bs.resize(__to - &__bs[0]);
3776 __r = codecvt_base::ok;
3778 else if (__r == codecvt_base::ok)
3780 __bs.resize(__to_nxt - &__bs[0]);
3782 else if (__r == codecvt_base::partial)
3784 ptrdiff_t __sp = __to_nxt - &__bs[0];
3785 __bs.resize(2 * __sp);
3786 __to = &__bs[0] + __sp;
3787 __to_end = &__bs[0] + __bs.size();
3789 } while (__r == codecvt_base::partial);
3790 if (__r == codecvt_base::ok)
3795 if (__byte_err_string_.empty())
3796 __throw_range_error("wstring_convert: to_bytes error");
3798 return __byte_err_string_;
3801 template <class _Codecvt, class _Elem = wchar_t, class _Tr = char_traits<_Elem> >
3802 class _LIBCPP_TEMPLATE_VIS wbuffer_convert
3803 : public basic_streambuf<_Elem, _Tr>
3807 typedef _Elem char_type;
3808 typedef _Tr traits_type;
3809 typedef typename traits_type::int_type int_type;
3810 typedef typename traits_type::pos_type pos_type;
3811 typedef typename traits_type::off_type off_type;
3812 typedef typename _Codecvt::state_type state_type;
3816 const char* __extbufnext_;
3817 const char* __extbufend_;
3818 char __extbuf_min_[8];
3820 char_type* __intbuf_;
3822 streambuf* __bufptr_;
3825 ios_base::openmode __cm_;
3828 bool __always_noconv_;
3830 wbuffer_convert(const wbuffer_convert&);
3831 wbuffer_convert& operator=(const wbuffer_convert&);
3833 _LIBCPP_EXPLICIT_AFTER_CXX11 wbuffer_convert(streambuf* __bytebuf = 0,
3834 _Codecvt* __pcvt = new _Codecvt, state_type __state = state_type());
3837 _LIBCPP_INLINE_VISIBILITY
3838 streambuf* rdbuf() const {return __bufptr_;}
3839 _LIBCPP_INLINE_VISIBILITY
3840 streambuf* rdbuf(streambuf* __bytebuf)
3842 streambuf* __r = __bufptr_;
3843 __bufptr_ = __bytebuf;
3847 _LIBCPP_INLINE_VISIBILITY
3848 state_type state() const {return __st_;}
3851 virtual int_type underflow();
3852 virtual int_type pbackfail(int_type __c = traits_type::eof());
3853 virtual int_type overflow (int_type __c = traits_type::eof());
3854 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s,
3856 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
3857 ios_base::openmode __wch = ios_base::in | ios_base::out);
3858 virtual pos_type seekpos(pos_type __sp,
3859 ios_base::openmode __wch = ios_base::in | ios_base::out);
3864 void __write_mode();
3865 wbuffer_convert* __close();
3868 template <class _Codecvt, class _Elem, class _Tr>
3869 wbuffer_convert<_Codecvt, _Elem, _Tr>::
3870 wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt, state_type __state)
3877 __bufptr_(__bytebuf),
3883 __always_noconv_(__cv_ ? __cv_->always_noconv() : false)
3888 template <class _Codecvt, class _Elem, class _Tr>
3889 wbuffer_convert<_Codecvt, _Elem, _Tr>::~wbuffer_convert()
3894 delete [] __extbuf_;
3896 delete [] __intbuf_;
3899 template <class _Codecvt, class _Elem, class _Tr>
3900 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
3901 wbuffer_convert<_Codecvt, _Elem, _Tr>::underflow()
3903 if (__cv_ == 0 || __bufptr_ == 0)
3904 return traits_type::eof();
3905 bool __initial = __read_mode();
3907 if (this->gptr() == 0)
3908 this->setg(&__1buf, &__1buf+1, &__1buf+1);
3909 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
3910 int_type __c = traits_type::eof();
3911 if (this->gptr() == this->egptr())
3913 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
3914 if (__always_noconv_)
3916 streamsize __nmemb = static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz);
3917 __nmemb = __bufptr_->sgetn((char*)this->eback() + __unget_sz, __nmemb);
3920 this->setg(this->eback(),
3921 this->eback() + __unget_sz,
3922 this->eback() + __unget_sz + __nmemb);
3923 __c = *this->gptr();
3928 _LIBCPP_ASSERT(!(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
3929 if (__extbufend_ != __extbufnext_)
3930 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
3931 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
3932 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
3933 streamsize __nmemb = _VSTD::min(static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz),
3934 static_cast<streamsize>(__extbufend_ - __extbufnext_));
3935 codecvt_base::result __r;
3936 // FIXME: Do we ever need to restore the state here?
3937 //state_type __svs = __st_;
3938 streamsize __nr = __bufptr_->sgetn(const_cast<char*>(__extbufnext_), __nmemb);
3941 __extbufend_ = __extbufnext_ + __nr;
3943 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
3944 this->eback() + __unget_sz,
3945 this->egptr(), __inext);
3946 if (__r == codecvt_base::noconv)
3948 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)__extbufend_);
3949 __c = *this->gptr();
3951 else if (__inext != this->eback() + __unget_sz)
3953 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
3954 __c = *this->gptr();
3960 __c = *this->gptr();
3961 if (this->eback() == &__1buf)
3962 this->setg(0, 0, 0);
3966 template <class _Codecvt, class _Elem, class _Tr>
3967 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
3968 wbuffer_convert<_Codecvt, _Elem, _Tr>::pbackfail(int_type __c)
3970 if (__cv_ != 0 && __bufptr_ != 0 && this->eback() < this->gptr())
3972 if (traits_type::eq_int_type(__c, traits_type::eof()))
3975 return traits_type::not_eof(__c);
3977 if (traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
3980 *this->gptr() = traits_type::to_char_type(__c);
3984 return traits_type::eof();
3987 template <class _Codecvt, class _Elem, class _Tr>
3988 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
3989 wbuffer_convert<_Codecvt, _Elem, _Tr>::overflow(int_type __c)
3991 if (__cv_ == 0 || __bufptr_ == 0)
3992 return traits_type::eof();
3995 char_type* __pb_save = this->pbase();
3996 char_type* __epb_save = this->epptr();
3997 if (!traits_type::eq_int_type(__c, traits_type::eof()))
3999 if (this->pptr() == 0)
4000 this->setp(&__1buf, &__1buf+1);
4001 *this->pptr() = traits_type::to_char_type(__c);
4004 if (this->pptr() != this->pbase())
4006 if (__always_noconv_)
4008 streamsize __nmemb = static_cast<streamsize>(this->pptr() - this->pbase());
4009 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4010 return traits_type::eof();
4014 char* __extbe = __extbuf_;
4015 codecvt_base::result __r;
4018 const char_type* __e;
4019 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
4020 __extbuf_, __extbuf_ + __ebs_, __extbe);
4021 if (__e == this->pbase())
4022 return traits_type::eof();
4023 if (__r == codecvt_base::noconv)
4025 streamsize __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
4026 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4027 return traits_type::eof();
4029 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
4031 streamsize __nmemb = static_cast<size_t>(__extbe - __extbuf_);
4032 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4033 return traits_type::eof();
4034 if (__r == codecvt_base::partial)
4036 this->setp((char_type*)__e, this->pptr());
4037 this->pbump(this->epptr() - this->pbase());
4041 return traits_type::eof();
4042 } while (__r == codecvt_base::partial);
4044 this->setp(__pb_save, __epb_save);
4046 return traits_type::not_eof(__c);
4049 template <class _Codecvt, class _Elem, class _Tr>
4050 basic_streambuf<_Elem, _Tr>*
4051 wbuffer_convert<_Codecvt, _Elem, _Tr>::setbuf(char_type* __s, streamsize __n)
4053 this->setg(0, 0, 0);
4056 delete [] __extbuf_;
4058 delete [] __intbuf_;
4060 if (__ebs_ > sizeof(__extbuf_min_))
4062 if (__always_noconv_ && __s)
4064 __extbuf_ = (char*)__s;
4069 __extbuf_ = new char[__ebs_];
4075 __extbuf_ = __extbuf_min_;
4076 __ebs_ = sizeof(__extbuf_min_);
4079 if (!__always_noconv_)
4081 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
4082 if (__s && __ibs_ >= sizeof(__extbuf_min_))
4089 __intbuf_ = new char_type[__ibs_];
4102 template <class _Codecvt, class _Elem, class _Tr>
4103 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4104 wbuffer_convert<_Codecvt, _Elem, _Tr>::seekoff(off_type __off, ios_base::seekdir __way,
4105 ios_base::openmode __om)
4107 int __width = __cv_->encoding();
4108 if (__cv_ == 0 || __bufptr_ == 0 || (__width <= 0 && __off != 0) || sync())
4109 return pos_type(off_type(-1));
4110 // __width > 0 || __off == 0, now check __way
4111 if (__way != ios_base::beg && __way != ios_base::cur && __way != ios_base::end)
4112 return pos_type(off_type(-1));
4113 pos_type __r = __bufptr_->pubseekoff(__width * __off, __way, __om);
4118 template <class _Codecvt, class _Elem, class _Tr>
4119 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4120 wbuffer_convert<_Codecvt, _Elem, _Tr>::seekpos(pos_type __sp, ios_base::openmode __wch)
4122 if (__cv_ == 0 || __bufptr_ == 0 || sync())
4123 return pos_type(off_type(-1));
4124 if (__bufptr_->pubseekpos(__sp, __wch) == pos_type(off_type(-1)))
4125 return pos_type(off_type(-1));
4129 template <class _Codecvt, class _Elem, class _Tr>
4131 wbuffer_convert<_Codecvt, _Elem, _Tr>::sync()
4133 if (__cv_ == 0 || __bufptr_ == 0)
4135 if (__cm_ & ios_base::out)
4137 if (this->pptr() != this->pbase())
4138 if (overflow() == traits_type::eof())
4140 codecvt_base::result __r;
4144 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
4145 streamsize __nmemb = static_cast<streamsize>(__extbe - __extbuf_);
4146 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4148 } while (__r == codecvt_base::partial);
4149 if (__r == codecvt_base::error)
4151 if (__bufptr_->pubsync())
4154 else if (__cm_ & ios_base::in)
4157 if (__always_noconv_)
4158 __c = this->egptr() - this->gptr();
4161 int __width = __cv_->encoding();
4162 __c = __extbufend_ - __extbufnext_;
4164 __c += __width * (this->egptr() - this->gptr());
4167 if (this->gptr() != this->egptr())
4169 reverse(this->gptr(), this->egptr());
4170 codecvt_base::result __r;
4171 const char_type* __e = this->gptr();
4175 __r = __cv_->out(__st_, __e, this->egptr(), __e,
4176 __extbuf_, __extbuf_ + __ebs_, __extbe);
4179 case codecvt_base::noconv:
4180 __c += this->egptr() - this->gptr();
4182 case codecvt_base::ok:
4183 case codecvt_base::partial:
4184 __c += __extbe - __extbuf_;
4189 } while (__r == codecvt_base::partial);
4193 if (__bufptr_->pubseekoff(-__c, ios_base::cur, __cm_) == pos_type(off_type(-1)))
4195 this->setg(0, 0, 0);
4201 template <class _Codecvt, class _Elem, class _Tr>
4203 wbuffer_convert<_Codecvt, _Elem, _Tr>::__read_mode()
4205 if (!(__cm_ & ios_base::in))
4208 if (__always_noconv_)
4209 this->setg((char_type*)__extbuf_,
4210 (char_type*)__extbuf_ + __ebs_,
4211 (char_type*)__extbuf_ + __ebs_);
4213 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
4214 __cm_ = ios_base::in;
4220 template <class _Codecvt, class _Elem, class _Tr>
4222 wbuffer_convert<_Codecvt, _Elem, _Tr>::__write_mode()
4224 if (!(__cm_ & ios_base::out))
4226 this->setg(0, 0, 0);
4227 if (__ebs_ > sizeof(__extbuf_min_))
4229 if (__always_noconv_)
4230 this->setp((char_type*)__extbuf_,
4231 (char_type*)__extbuf_ + (__ebs_ - 1));
4233 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
4237 __cm_ = ios_base::out;
4241 template <class _Codecvt, class _Elem, class _Tr>
4242 wbuffer_convert<_Codecvt, _Elem, _Tr>*
4243 wbuffer_convert<_Codecvt, _Elem, _Tr>::__close()
4245 wbuffer_convert* __rt = 0;
4246 if (__cv_ != 0 && __bufptr_ != 0)
4249 if ((__cm_ & ios_base::out) && sync())
4255 _LIBCPP_END_NAMESPACE_STD
4257 #endif // _LIBCPP_LOCALE