2 //===-------------------------- locale ------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_LOCALE
11 #define _LIBCPP_LOCALE
27 static const category // values assigned here are for exposition only
35 all = collate | ctype | monetary | numeric | time | messages;
37 // construct/copy/destroy:
39 locale(const locale& other) noexcept;
40 explicit locale(const char* std_name);
41 explicit locale(const string& std_name);
42 locale(const locale& other, const char* std_name, category);
43 locale(const locale& other, const string& std_name, category);
44 template <class Facet> locale(const locale& other, Facet* f);
45 locale(const locale& other, const locale& one, category);
47 ~locale(); // not virtual
49 const locale& operator=(const locale& other) noexcept;
51 template <class Facet> locale combine(const locale& other) const;
54 basic_string<char> name() const;
55 bool operator==(const locale& other) const;
56 bool operator!=(const locale& other) const;
57 template <class charT, class Traits, class Allocator>
58 bool operator()(const basic_string<charT,Traits,Allocator>& s1,
59 const basic_string<charT,Traits,Allocator>& s2) const;
61 // global locale objects:
62 static locale global(const locale&);
63 static const locale& classic();
66 template <class Facet> const Facet& use_facet(const locale&);
67 template <class Facet> bool has_facet(const locale&) noexcept;
69 // 22.3.3, convenience interfaces:
70 template <class charT> bool isspace (charT c, const locale& loc);
71 template <class charT> bool isprint (charT c, const locale& loc);
72 template <class charT> bool iscntrl (charT c, const locale& loc);
73 template <class charT> bool isupper (charT c, const locale& loc);
74 template <class charT> bool islower (charT c, const locale& loc);
75 template <class charT> bool isalpha (charT c, const locale& loc);
76 template <class charT> bool isdigit (charT c, const locale& loc);
77 template <class charT> bool ispunct (charT c, const locale& loc);
78 template <class charT> bool isxdigit(charT c, const locale& loc);
79 template <class charT> bool isalnum (charT c, const locale& loc);
80 template <class charT> bool isgraph (charT c, const locale& loc);
81 template <class charT> charT toupper(charT c, const locale& loc);
82 template <class charT> charT tolower(charT c, const locale& loc);
84 template<class Codecvt, class Elem = wchar_t,
85 class Wide_alloc = allocator<Elem>,
86 class Byte_alloc = allocator<char>>
90 typedef basic_string<char, char_traits<char>, Byte_alloc> byte_string;
91 typedef basic_string<Elem, char_traits<Elem>, Wide_alloc> wide_string;
92 typedef typename Codecvt::state_type state_type;
93 typedef typename wide_string::traits_type::int_type int_type;
95 explicit wstring_convert(Codecvt* pcvt = new Codecvt); // explicit in C++14
96 wstring_convert(Codecvt* pcvt, state_type state);
97 explicit wstring_convert(const byte_string& byte_err, // explicit in C++14
98 const wide_string& wide_err = wide_string());
99 wstring_convert(const wstring_convert&) = delete; // C++14
100 wstring_convert & operator=(const wstring_convert &) = delete; // C++14
103 wide_string from_bytes(char byte);
104 wide_string from_bytes(const char* ptr);
105 wide_string from_bytes(const byte_string& str);
106 wide_string from_bytes(const char* first, const char* last);
108 byte_string to_bytes(Elem wchar);
109 byte_string to_bytes(const Elem* wptr);
110 byte_string to_bytes(const wide_string& wstr);
111 byte_string to_bytes(const Elem* first, const Elem* last);
113 size_t converted() const; // noexcept in C++14
114 state_type state() const;
117 template <class Codecvt, class Elem = wchar_t, class Tr = char_traits<Elem>>
118 class wbuffer_convert
119 : public basic_streambuf<Elem, Tr>
122 typedef typename Tr::state_type state_type;
124 explicit wbuffer_convert(streambuf* bytebuf = 0, Codecvt* pcvt = new Codecvt,
125 state_type state = state_type()); // explicit in C++14
126 wbuffer_convert(const wbuffer_convert&) = delete; // C++14
127 wbuffer_convert & operator=(const wbuffer_convert &) = delete; // C++14
128 ~wbuffer_convert(); // C++14
130 streambuf* rdbuf() const;
131 streambuf* rdbuf(streambuf* bytebuf);
133 state_type state() const;
136 // 22.4.1 and 22.4.1.3, ctype:
138 template <class charT> class ctype;
139 template <> class ctype<char>; // specialization
140 template <class charT> class ctype_byname;
141 template <> class ctype_byname<char>; // specialization
144 template <class internT, class externT, class stateT> class codecvt;
145 template <class internT, class externT, class stateT> class codecvt_byname;
147 // 22.4.2 and 22.4.3, numeric:
148 template <class charT, class InputIterator> class num_get;
149 template <class charT, class OutputIterator> class num_put;
150 template <class charT> class numpunct;
151 template <class charT> class numpunct_byname;
153 // 22.4.4, col lation:
154 template <class charT> class collate;
155 template <class charT> class collate_byname;
157 // 22.4.5, date and time:
159 template <class charT, class InputIterator> class time_get;
160 template <class charT, class InputIterator> class time_get_byname;
161 template <class charT, class OutputIterator> class time_put;
162 template <class charT, class OutputIterator> class time_put_byname;
166 template <class charT, class InputIterator> class money_get;
167 template <class charT, class OutputIterator> class money_put;
168 template <class charT, bool Intl> class moneypunct;
169 template <class charT, bool Intl> class moneypunct_byname;
171 // 22.4.7, message retrieval:
173 template <class charT> class messages;
174 template <class charT> class messages_byname;
196 #ifdef _LIBCPP_HAS_CATOPEN
197 #include <nl_types.h>
201 #include <Availability.h>
204 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
205 #include <__bsd_locale_defaults.h>
207 #include <__bsd_locale_fallbacks.h>
210 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
211 #pragma GCC system_header
215 #include <__undef_macros>
218 _LIBCPP_BEGIN_NAMESPACE_STD
220 #if defined(__APPLE__) || defined(__FreeBSD__)
221 # define _LIBCPP_GET_C_LOCALE 0
222 #elif defined(__CloudABI__) || defined(__NetBSD__)
223 # define _LIBCPP_GET_C_LOCALE LC_C_LOCALE
225 # define _LIBCPP_GET_C_LOCALE __cloc()
226 // Get the C locale object
227 _LIBCPP_FUNC_VIS locale_t __cloc();
228 #define __cloc_defined
232 // Scans [__b, __e) until a match is found in the basic_strings range
233 // [__kb, __ke) or until it can be shown that there is no match in [__kb, __ke).
234 // __b will be incremented (visibly), consuming CharT until a match is found
235 // or proved to not exist. A keyword may be "", in which will match anything.
236 // If one keyword is a prefix of another, and the next CharT in the input
237 // might match another keyword, the algorithm will attempt to find the longest
238 // matching keyword. If the longer matching keyword ends up not matching, then
239 // no keyword match is found. If no keyword match is found, __ke is returned
240 // and failbit is set in __err.
241 // Else an iterator pointing to the matching keyword is found. If more than
242 // one keyword matches, an iterator to the first matching keyword is returned.
243 // If on exit __b == __e, eofbit is set in __err. If __case_sensitive is false,
244 // __ct is used to force to lower case before comparing characters.
246 // Keywords: "a", "abb"
247 // If the input is "a", the first keyword matches and eofbit is set.
248 // If the input is "abc", no match is found and "ab" are consumed.
249 template <class _InputIterator, class _ForwardIterator, class _Ctype>
252 __scan_keyword(_InputIterator& __b, _InputIterator __e,
253 _ForwardIterator __kb, _ForwardIterator __ke,
254 const _Ctype& __ct, ios_base::iostate& __err,
255 bool __case_sensitive = true)
257 typedef typename iterator_traits<_InputIterator>::value_type _CharT;
258 size_t __nkw = static_cast<size_t>(_VSTD::distance(__kb, __ke));
259 const unsigned char __doesnt_match = '\0';
260 const unsigned char __might_match = '\1';
261 const unsigned char __does_match = '\2';
262 unsigned char __statbuf[100];
263 unsigned char* __status = __statbuf;
264 unique_ptr<unsigned char, void(*)(void*)> __stat_hold(0, free);
265 if (__nkw > sizeof(__statbuf))
267 __status = (unsigned char*)malloc(__nkw);
270 __stat_hold.reset(__status);
272 size_t __n_might_match = __nkw; // At this point, any keyword might match
273 size_t __n_does_match = 0; // but none of them definitely do
274 // Initialize all statuses to __might_match, except for "" keywords are __does_match
275 unsigned char* __st = __status;
276 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
279 *__st = __might_match;
282 *__st = __does_match;
287 // While there might be a match, test keywords against the next CharT
288 for (size_t __indx = 0; __b != __e && __n_might_match > 0; ++__indx)
290 // Peek at the next CharT but don't consume it
292 if (!__case_sensitive)
293 __c = __ct.toupper(__c);
294 bool __consume = false;
295 // For each keyword which might match, see if the __indx character is __c
296 // If a match if found, consume __c
297 // If a match is found, and that is the last character in the keyword,
298 // then that keyword matches.
299 // If the keyword doesn't match this character, then change the keyword
302 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
304 if (*__st == __might_match)
306 _CharT __kc = (*__ky)[__indx];
307 if (!__case_sensitive)
308 __kc = __ct.toupper(__kc);
312 if (__ky->size() == __indx+1)
314 *__st = __does_match;
321 *__st = __doesnt_match;
326 // consume if we matched a character
330 // If we consumed a character and there might be a matched keyword that
331 // was marked matched on a previous iteration, then such keywords
332 // which are now marked as not matching.
333 if (__n_might_match + __n_does_match > 1)
336 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
338 if (*__st == __does_match && __ky->size() != __indx+1)
340 *__st = __doesnt_match;
347 // We've exited the loop because we hit eof and/or we have no more "might matches".
349 __err |= ios_base::eofbit;
350 // Return the first matching result
351 for (__st = __status; __kb != __ke; ++__kb, (void) ++__st)
352 if (*__st == __does_match)
355 __err |= ios_base::failbit;
359 struct _LIBCPP_TYPE_VIS __num_get_base
361 static const int __num_get_buf_sz = 40;
363 static int __get_base(ios_base&);
364 static const char __src[33];
368 void __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end,
369 ios_base::iostate& __err);
371 template <class _CharT>
373 : protected __num_get_base
375 static string __stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
376 _CharT& __thousands_sep);
378 static int __stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp,
379 char* __a, char*& __a_end,
380 _CharT __decimal_point, _CharT __thousands_sep,
381 const string& __grouping, unsigned* __g,
382 unsigned*& __g_end, unsigned& __dc, _CharT* __atoms);
383 #ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
384 static string __stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep);
385 static int __stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
386 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
387 unsigned* __g, unsigned*& __g_end, _CharT* __atoms);
390 static string __stage2_int_prep(ios_base& __iob, _CharT& __thousands_sep)
392 locale __loc = __iob.getloc();
393 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
394 __thousands_sep = __np.thousands_sep();
395 return __np.grouping();
398 const _CharT* __do_widen(ios_base& __iob, _CharT* __atoms) const
400 return __do_widen_p(__iob, __atoms);
404 static int __stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
405 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
406 unsigned* __g, unsigned*& __g_end, const _CharT* __atoms);
409 const T* __do_widen_p(ios_base& __iob, T* __atoms) const
411 locale __loc = __iob.getloc();
412 use_facet<ctype<T> >(__loc).widen(__src, __src + 26, __atoms);
416 const char* __do_widen_p(ios_base& __iob, char* __atoms) const
425 #ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
426 template <class _CharT>
428 __num_get<_CharT>::__stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep)
430 locale __loc = __iob.getloc();
431 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 26, __atoms);
432 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
433 __thousands_sep = __np.thousands_sep();
434 return __np.grouping();
438 template <class _CharT>
440 __num_get<_CharT>::__stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
441 _CharT& __thousands_sep)
443 locale __loc = __iob.getloc();
444 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 32, __atoms);
445 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
446 __decimal_point = __np.decimal_point();
447 __thousands_sep = __np.thousands_sep();
448 return __np.grouping();
451 template <class _CharT>
453 #ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
454 __num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
455 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
456 unsigned* __g, unsigned*& __g_end, _CharT* __atoms)
458 __num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
459 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
460 unsigned* __g, unsigned*& __g_end, const _CharT* __atoms)
464 if (__a_end == __a && (__ct == __atoms[24] || __ct == __atoms[25]))
466 *__a_end++ = __ct == __atoms[24] ? '+' : '-';
470 if (__grouping.size() != 0 && __ct == __thousands_sep)
472 if (__g_end-__g < __num_get_buf_sz)
479 ptrdiff_t __f = find(__atoms, __atoms + 26, __ct) - __atoms;
492 if (__a_end != __a && __a_end - __a <= 2 && __a_end[-1] == '0')
495 *__a_end++ = __src[__f];
500 *__a_end++ = __src[__f];
505 template <class _CharT>
507 __num_get<_CharT>::__stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp, char* __a, char*& __a_end,
508 _CharT __decimal_point, _CharT __thousands_sep, const string& __grouping,
509 unsigned* __g, unsigned*& __g_end, unsigned& __dc, _CharT* __atoms)
511 if (__ct == __decimal_point)
517 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
521 if (__ct == __thousands_sep && __grouping.size() != 0)
525 if (__g_end-__g < __num_get_buf_sz)
532 ptrdiff_t __f = find(__atoms, __atoms + 32, __ct) - __atoms;
535 char __x = __src[__f];
536 if (__x == '-' || __x == '+')
538 if (__a_end == __a || (__a_end[-1] & 0x5F) == (__exp & 0x7F))
545 if (__x == 'x' || __x == 'X')
547 else if ((__x & 0x5F) == __exp)
549 __exp |= (char) 0x80;
553 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
564 _LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<char>)
565 _LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<wchar_t>)
567 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
568 class _LIBCPP_TEMPLATE_VIS num_get
569 : public locale::facet,
570 private __num_get<_CharT>
573 typedef _CharT char_type;
574 typedef _InputIterator iter_type;
576 _LIBCPP_INLINE_VISIBILITY
577 explicit num_get(size_t __refs = 0)
578 : locale::facet(__refs) {}
580 _LIBCPP_INLINE_VISIBILITY
581 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
582 ios_base::iostate& __err, bool& __v) const
584 return do_get(__b, __e, __iob, __err, __v);
587 _LIBCPP_INLINE_VISIBILITY
588 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
589 ios_base::iostate& __err, long& __v) const
591 return do_get(__b, __e, __iob, __err, __v);
594 _LIBCPP_INLINE_VISIBILITY
595 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
596 ios_base::iostate& __err, long long& __v) const
598 return do_get(__b, __e, __iob, __err, __v);
601 _LIBCPP_INLINE_VISIBILITY
602 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
603 ios_base::iostate& __err, unsigned short& __v) const
605 return do_get(__b, __e, __iob, __err, __v);
608 _LIBCPP_INLINE_VISIBILITY
609 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
610 ios_base::iostate& __err, unsigned int& __v) const
612 return do_get(__b, __e, __iob, __err, __v);
615 _LIBCPP_INLINE_VISIBILITY
616 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
617 ios_base::iostate& __err, unsigned long& __v) const
619 return do_get(__b, __e, __iob, __err, __v);
622 _LIBCPP_INLINE_VISIBILITY
623 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
624 ios_base::iostate& __err, unsigned long long& __v) const
626 return do_get(__b, __e, __iob, __err, __v);
629 _LIBCPP_INLINE_VISIBILITY
630 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
631 ios_base::iostate& __err, float& __v) const
633 return do_get(__b, __e, __iob, __err, __v);
636 _LIBCPP_INLINE_VISIBILITY
637 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
638 ios_base::iostate& __err, double& __v) const
640 return do_get(__b, __e, __iob, __err, __v);
643 _LIBCPP_INLINE_VISIBILITY
644 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
645 ios_base::iostate& __err, long double& __v) const
647 return do_get(__b, __e, __iob, __err, __v);
650 _LIBCPP_INLINE_VISIBILITY
651 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
652 ios_base::iostate& __err, void*& __v) const
654 return do_get(__b, __e, __iob, __err, __v);
657 static locale::id id;
660 _LIBCPP_INLINE_VISIBILITY
664 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
665 iter_type __do_get_floating_point
666 (iter_type __b, iter_type __e, ios_base& __iob,
667 ios_base::iostate& __err, _Fp& __v) const;
669 template <class _Signed>
670 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
671 iter_type __do_get_signed
672 (iter_type __b, iter_type __e, ios_base& __iob,
673 ios_base::iostate& __err, _Signed& __v) const;
675 template <class _Unsigned>
676 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
677 iter_type __do_get_unsigned
678 (iter_type __b, iter_type __e, ios_base& __iob,
679 ios_base::iostate& __err, _Unsigned& __v) const;
682 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
683 ios_base::iostate& __err, bool& __v) const;
685 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
686 ios_base::iostate& __err, long& __v) const
687 { return this->__do_get_signed ( __b, __e, __iob, __err, __v ); }
689 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
690 ios_base::iostate& __err, long long& __v) const
691 { return this->__do_get_signed ( __b, __e, __iob, __err, __v ); }
693 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
694 ios_base::iostate& __err, unsigned short& __v) const
695 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
697 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
698 ios_base::iostate& __err, unsigned int& __v) const
699 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
701 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
702 ios_base::iostate& __err, unsigned long& __v) const
703 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
705 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
706 ios_base::iostate& __err, unsigned long long& __v) const
707 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
709 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
710 ios_base::iostate& __err, float& __v) const
711 { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
713 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
714 ios_base::iostate& __err, double& __v) const
715 { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
717 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
718 ios_base::iostate& __err, long double& __v) const
719 { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
721 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
722 ios_base::iostate& __err, void*& __v) const;
725 template <class _CharT, class _InputIterator>
727 num_get<_CharT, _InputIterator>::id;
731 __num_get_signed_integral(const char* __a, const char* __a_end,
732 ios_base::iostate& __err, int __base)
736 typename remove_reference<decltype(errno)>::type __save_errno = errno;
739 long long __ll = strtoll_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
740 typename remove_reference<decltype(errno)>::type __current_errno = errno;
741 if (__current_errno == 0)
742 errno = __save_errno;
745 __err = ios_base::failbit;
748 else if (__current_errno == ERANGE ||
749 __ll < numeric_limits<_Tp>::min() ||
750 numeric_limits<_Tp>::max() < __ll)
752 __err = ios_base::failbit;
754 return numeric_limits<_Tp>::max();
756 return numeric_limits<_Tp>::min();
758 return static_cast<_Tp>(__ll);
760 __err = ios_base::failbit;
766 __num_get_unsigned_integral(const char* __a, const char* __a_end,
767 ios_base::iostate& __err, int __base)
771 const bool __negate = *__a == '-';
772 if (__negate && ++__a == __a_end) {
773 __err = ios_base::failbit;
776 typename remove_reference<decltype(errno)>::type __save_errno = errno;
779 unsigned long long __ll = strtoull_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
780 typename remove_reference<decltype(errno)>::type __current_errno = errno;
781 if (__current_errno == 0)
782 errno = __save_errno;
785 __err = ios_base::failbit;
788 else if (__current_errno == ERANGE || numeric_limits<_Tp>::max() < __ll)
790 __err = ios_base::failbit;
791 return numeric_limits<_Tp>::max();
793 _Tp __res = static_cast<_Tp>(__ll);
794 if (__negate) __res = -__res;
797 __err = ios_base::failbit;
802 _LIBCPP_INLINE_VISIBILITY
803 _Tp __do_strtod(const char* __a, char** __p2);
806 inline _LIBCPP_INLINE_VISIBILITY
807 float __do_strtod<float>(const char* __a, char** __p2) {
808 return strtof_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
812 inline _LIBCPP_INLINE_VISIBILITY
813 double __do_strtod<double>(const char* __a, char** __p2) {
814 return strtod_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
818 inline _LIBCPP_INLINE_VISIBILITY
819 long double __do_strtod<long double>(const char* __a, char** __p2) {
820 return strtold_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
826 __num_get_float(const char* __a, const char* __a_end, ios_base::iostate& __err)
830 typename remove_reference<decltype(errno)>::type __save_errno = errno;
833 _Tp __ld = __do_strtod<_Tp>(__a, &__p2);
834 typename remove_reference<decltype(errno)>::type __current_errno = errno;
835 if (__current_errno == 0)
836 errno = __save_errno;
839 __err = ios_base::failbit;
842 else if (__current_errno == ERANGE)
843 __err = ios_base::failbit;
846 __err = ios_base::failbit;
850 template <class _CharT, class _InputIterator>
852 num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
854 ios_base::iostate& __err,
857 if ((__iob.flags() & ios_base::boolalpha) == 0)
860 __b = do_get(__b, __e, __iob, __err, __lv);
871 __err = ios_base::failbit;
876 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__iob.getloc());
877 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__iob.getloc());
878 typedef typename numpunct<_CharT>::string_type string_type;
879 const string_type __names[2] = {__np.truename(), __np.falsename()};
880 const string_type* __i = __scan_keyword(__b, __e, __names, __names+2,
882 __v = __i == __names;
888 template <class _CharT, class _InputIterator>
889 template <class _Signed>
891 num_get<_CharT, _InputIterator>::__do_get_signed(iter_type __b, iter_type __e,
893 ios_base::iostate& __err,
897 int __base = this->__get_base(__iob);
899 char_type __thousands_sep;
900 const int __atoms_size = 26;
901 #ifdef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
902 char_type __atoms1[__atoms_size];
903 const char_type *__atoms = this->__do_widen(__iob, __atoms1);
904 string __grouping = this->__stage2_int_prep(__iob, __thousands_sep);
906 char_type __atoms[__atoms_size];
907 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
910 __buf.resize(__buf.capacity());
911 char* __a = &__buf[0];
913 unsigned __g[__num_get_base::__num_get_buf_sz];
914 unsigned* __g_end = __g;
916 for (; __b != __e; ++__b)
918 if (__a_end == __a + __buf.size())
920 size_t __tmp = __buf.size();
921 __buf.resize(2*__buf.size());
922 __buf.resize(__buf.capacity());
924 __a_end = __a + __tmp;
926 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
927 __thousands_sep, __grouping, __g, __g_end,
931 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
934 __v = __num_get_signed_integral<_Signed>(__a, __a_end, __err, __base);
935 // Digit grouping checked
936 __check_grouping(__grouping, __g, __g_end, __err);
939 __err |= ios_base::eofbit;
945 template <class _CharT, class _InputIterator>
946 template <class _Unsigned>
948 num_get<_CharT, _InputIterator>::__do_get_unsigned(iter_type __b, iter_type __e,
950 ios_base::iostate& __err,
951 _Unsigned& __v) const
954 int __base = this->__get_base(__iob);
956 char_type __thousands_sep;
957 const int __atoms_size = 26;
958 #ifdef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
959 char_type __atoms1[__atoms_size];
960 const char_type *__atoms = this->__do_widen(__iob, __atoms1);
961 string __grouping = this->__stage2_int_prep(__iob, __thousands_sep);
963 char_type __atoms[__atoms_size];
964 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
967 __buf.resize(__buf.capacity());
968 char* __a = &__buf[0];
970 unsigned __g[__num_get_base::__num_get_buf_sz];
971 unsigned* __g_end = __g;
973 for (; __b != __e; ++__b)
975 if (__a_end == __a + __buf.size())
977 size_t __tmp = __buf.size();
978 __buf.resize(2*__buf.size());
979 __buf.resize(__buf.capacity());
981 __a_end = __a + __tmp;
983 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
984 __thousands_sep, __grouping, __g, __g_end,
988 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
991 __v = __num_get_unsigned_integral<_Unsigned>(__a, __a_end, __err, __base);
992 // Digit grouping checked
993 __check_grouping(__grouping, __g, __g_end, __err);
996 __err |= ios_base::eofbit;
1002 template <class _CharT, class _InputIterator>
1003 template <class _Fp>
1005 num_get<_CharT, _InputIterator>::__do_get_floating_point(iter_type __b, iter_type __e,
1007 ios_base::iostate& __err,
1010 // Stage 1, nothing to do
1012 char_type __atoms[32];
1013 char_type __decimal_point;
1014 char_type __thousands_sep;
1015 string __grouping = this->__stage2_float_prep(__iob, __atoms,
1019 __buf.resize(__buf.capacity());
1020 char* __a = &__buf[0];
1021 char* __a_end = __a;
1022 unsigned __g[__num_get_base::__num_get_buf_sz];
1023 unsigned* __g_end = __g;
1025 bool __in_units = true;
1027 for (; __b != __e; ++__b)
1029 if (__a_end == __a + __buf.size())
1031 size_t __tmp = __buf.size();
1032 __buf.resize(2*__buf.size());
1033 __buf.resize(__buf.capacity());
1035 __a_end = __a + __tmp;
1037 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1038 __decimal_point, __thousands_sep,
1039 __grouping, __g, __g_end,
1043 if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1046 __v = __num_get_float<_Fp>(__a, __a_end, __err);
1047 // Digit grouping checked
1048 __check_grouping(__grouping, __g, __g_end, __err);
1051 __err |= ios_base::eofbit;
1055 template <class _CharT, class _InputIterator>
1057 num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1059 ios_base::iostate& __err,
1065 char_type __atoms[26];
1066 char_type __thousands_sep = 0;
1068 use_facet<ctype<_CharT> >(__iob.getloc()).widen(__num_get_base::__src,
1069 __num_get_base::__src + 26, __atoms);
1071 __buf.resize(__buf.capacity());
1072 char* __a = &__buf[0];
1073 char* __a_end = __a;
1074 unsigned __g[__num_get_base::__num_get_buf_sz];
1075 unsigned* __g_end = __g;
1077 for (; __b != __e; ++__b)
1079 if (__a_end == __a + __buf.size())
1081 size_t __tmp = __buf.size();
1082 __buf.resize(2*__buf.size());
1083 __buf.resize(__buf.capacity());
1085 __a_end = __a + __tmp;
1087 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1088 __thousands_sep, __grouping,
1089 __g, __g_end, __atoms))
1093 __buf.resize(__a_end - __a);
1094 if (__libcpp_sscanf_l(__buf.c_str(), _LIBCPP_GET_C_LOCALE, "%p", &__v) != 1)
1095 __err = ios_base::failbit;
1098 __err |= ios_base::eofbit;
1102 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<char>)
1103 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<wchar_t>)
1105 struct _LIBCPP_TYPE_VIS __num_put_base
1108 static void __format_int(char* __fmt, const char* __len, bool __signd,
1109 ios_base::fmtflags __flags);
1110 static bool __format_float(char* __fmt, const char* __len,
1111 ios_base::fmtflags __flags);
1112 static char* __identify_padding(char* __nb, char* __ne,
1113 const ios_base& __iob);
1116 template <class _CharT>
1118 : protected __num_put_base
1120 static void __widen_and_group_int(char* __nb, char* __np, char* __ne,
1121 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1122 const locale& __loc);
1123 static void __widen_and_group_float(char* __nb, char* __np, char* __ne,
1124 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1125 const locale& __loc);
1128 template <class _CharT>
1130 __num_put<_CharT>::__widen_and_group_int(char* __nb, char* __np, char* __ne,
1131 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1132 const locale& __loc)
1134 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1135 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1136 string __grouping = __npt.grouping();
1137 if (__grouping.empty())
1139 __ct.widen(__nb, __ne, __ob);
1140 __oe = __ob + (__ne - __nb);
1146 if (*__nf == '-' || *__nf == '+')
1147 *__oe++ = __ct.widen(*__nf++);
1148 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1151 *__oe++ = __ct.widen(*__nf++);
1152 *__oe++ = __ct.widen(*__nf++);
1154 reverse(__nf, __ne);
1155 _CharT __thousands_sep = __npt.thousands_sep();
1158 for (char* __p = __nf; __p < __ne; ++__p)
1160 if (static_cast<unsigned>(__grouping[__dg]) > 0 &&
1161 __dc == static_cast<unsigned>(__grouping[__dg]))
1163 *__oe++ = __thousands_sep;
1165 if (__dg < __grouping.size()-1)
1168 *__oe++ = __ct.widen(*__p);
1171 reverse(__ob + (__nf - __nb), __oe);
1176 __op = __ob + (__np - __nb);
1179 template <class _CharT>
1181 __num_put<_CharT>::__widen_and_group_float(char* __nb, char* __np, char* __ne,
1182 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1183 const locale& __loc)
1185 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1186 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1187 string __grouping = __npt.grouping();
1190 if (*__nf == '-' || *__nf == '+')
1191 *__oe++ = __ct.widen(*__nf++);
1193 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1196 *__oe++ = __ct.widen(*__nf++);
1197 *__oe++ = __ct.widen(*__nf++);
1198 for (__ns = __nf; __ns < __ne; ++__ns)
1199 if (!isxdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
1204 for (__ns = __nf; __ns < __ne; ++__ns)
1205 if (!isdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
1208 if (__grouping.empty())
1210 __ct.widen(__nf, __ns, __oe);
1211 __oe += __ns - __nf;
1215 reverse(__nf, __ns);
1216 _CharT __thousands_sep = __npt.thousands_sep();
1219 for (char* __p = __nf; __p < __ns; ++__p)
1221 if (__grouping[__dg] > 0 && __dc == static_cast<unsigned>(__grouping[__dg]))
1223 *__oe++ = __thousands_sep;
1225 if (__dg < __grouping.size()-1)
1228 *__oe++ = __ct.widen(*__p);
1231 reverse(__ob + (__nf - __nb), __oe);
1233 for (__nf = __ns; __nf < __ne; ++__nf)
1237 *__oe++ = __npt.decimal_point();
1242 *__oe++ = __ct.widen(*__nf);
1244 __ct.widen(__nf, __ne, __oe);
1245 __oe += __ne - __nf;
1249 __op = __ob + (__np - __nb);
1252 _LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<char>)
1253 _LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<wchar_t>)
1255 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
1256 class _LIBCPP_TEMPLATE_VIS num_put
1257 : public locale::facet,
1258 private __num_put<_CharT>
1261 typedef _CharT char_type;
1262 typedef _OutputIterator iter_type;
1264 _LIBCPP_INLINE_VISIBILITY
1265 explicit num_put(size_t __refs = 0)
1266 : locale::facet(__refs) {}
1268 _LIBCPP_INLINE_VISIBILITY
1269 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1272 return do_put(__s, __iob, __fl, __v);
1275 _LIBCPP_INLINE_VISIBILITY
1276 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1279 return do_put(__s, __iob, __fl, __v);
1282 _LIBCPP_INLINE_VISIBILITY
1283 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1284 long long __v) const
1286 return do_put(__s, __iob, __fl, __v);
1289 _LIBCPP_INLINE_VISIBILITY
1290 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1291 unsigned long __v) const
1293 return do_put(__s, __iob, __fl, __v);
1296 _LIBCPP_INLINE_VISIBILITY
1297 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1298 unsigned long long __v) const
1300 return do_put(__s, __iob, __fl, __v);
1303 _LIBCPP_INLINE_VISIBILITY
1304 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1307 return do_put(__s, __iob, __fl, __v);
1310 _LIBCPP_INLINE_VISIBILITY
1311 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1312 long double __v) const
1314 return do_put(__s, __iob, __fl, __v);
1317 _LIBCPP_INLINE_VISIBILITY
1318 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1319 const void* __v) const
1321 return do_put(__s, __iob, __fl, __v);
1324 static locale::id id;
1327 _LIBCPP_INLINE_VISIBILITY
1330 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1332 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1334 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1335 long long __v) const;
1336 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1337 unsigned long) const;
1338 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1339 unsigned long long) const;
1340 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1342 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1343 long double __v) const;
1344 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1345 const void* __v) const;
1348 template <class _CharT, class _OutputIterator>
1350 num_put<_CharT, _OutputIterator>::id;
1352 template <class _CharT, class _OutputIterator>
1355 __pad_and_output(_OutputIterator __s,
1356 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1357 ios_base& __iob, _CharT __fl)
1359 streamsize __sz = __oe - __ob;
1360 streamsize __ns = __iob.width();
1365 for (;__ob < __op; ++__ob, ++__s)
1367 for (; __ns; --__ns, ++__s)
1369 for (; __ob < __oe; ++__ob, ++__s)
1375 template <class _CharT, class _Traits>
1377 ostreambuf_iterator<_CharT, _Traits>
1378 __pad_and_output(ostreambuf_iterator<_CharT, _Traits> __s,
1379 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1380 ios_base& __iob, _CharT __fl)
1382 if (__s.__sbuf_ == nullptr)
1384 streamsize __sz = __oe - __ob;
1385 streamsize __ns = __iob.width();
1390 streamsize __np = __op - __ob;
1393 if (__s.__sbuf_->sputn(__ob, __np) != __np)
1395 __s.__sbuf_ = nullptr;
1401 basic_string<_CharT, _Traits> __sp(__ns, __fl);
1402 if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns)
1404 __s.__sbuf_ = nullptr;
1411 if (__s.__sbuf_->sputn(__op, __np) != __np)
1413 __s.__sbuf_ = nullptr;
1421 template <class _CharT, class _OutputIterator>
1423 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1424 char_type __fl, bool __v) const
1426 if ((__iob.flags() & ios_base::boolalpha) == 0)
1427 return do_put(__s, __iob, __fl, (unsigned long)__v);
1428 const numpunct<char_type>& __np = use_facet<numpunct<char_type> >(__iob.getloc());
1429 typedef typename numpunct<char_type>::string_type string_type;
1430 #if _LIBCPP_DEBUG_LEVEL >= 2
1431 string_type __tmp(__v ? __np.truename() : __np.falsename());
1432 string_type __nm = _VSTD::move(__tmp);
1434 string_type __nm = __v ? __np.truename() : __np.falsename();
1436 for (typename string_type::iterator __i = __nm.begin(); __i != __nm.end(); ++__i, ++__s)
1441 template <class _CharT, class _OutputIterator>
1443 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1444 char_type __fl, long __v) const
1446 // Stage 1 - Get number in narrow char
1447 char __fmt[6] = {'%', 0};
1448 const char* __len = "l";
1449 this->__format_int(__fmt+1, __len, true, __iob.flags());
1450 const unsigned __nbuf = (numeric_limits<long>::digits / 3)
1451 + ((numeric_limits<long>::digits % 3) != 0)
1452 + ((__iob.flags() & ios_base::showbase) != 0)
1455 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1456 char* __ne = __nar + __nc;
1457 char* __np = this->__identify_padding(__nar, __ne, __iob);
1458 // Stage 2 - Widen __nar while adding thousands separators
1459 char_type __o[2*(__nbuf-1) - 1];
1460 char_type* __op; // pad here
1461 char_type* __oe; // end of output
1462 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1463 // [__o, __oe) contains thousands_sep'd wide number
1465 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1468 template <class _CharT, class _OutputIterator>
1470 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1471 char_type __fl, long long __v) const
1473 // Stage 1 - Get number in narrow char
1474 char __fmt[8] = {'%', 0};
1475 const char* __len = "ll";
1476 this->__format_int(__fmt+1, __len, true, __iob.flags());
1477 const unsigned __nbuf = (numeric_limits<long long>::digits / 3)
1478 + ((numeric_limits<long long>::digits % 3) != 0)
1479 + ((__iob.flags() & ios_base::showbase) != 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, unsigned long __v) const
1500 // Stage 1 - Get number in narrow char
1501 char __fmt[6] = {'%', 0};
1502 const char* __len = "l";
1503 this->__format_int(__fmt+1, __len, false, __iob.flags());
1504 const unsigned __nbuf = (numeric_limits<unsigned long>::digits / 3)
1505 + ((numeric_limits<unsigned long>::digits % 3) != 0)
1506 + ((__iob.flags() & ios_base::showbase) != 0)
1509 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1510 char* __ne = __nar + __nc;
1511 char* __np = this->__identify_padding(__nar, __ne, __iob);
1512 // Stage 2 - Widen __nar while adding thousands separators
1513 char_type __o[2*(__nbuf-1) - 1];
1514 char_type* __op; // pad here
1515 char_type* __oe; // end of output
1516 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1517 // [__o, __oe) contains thousands_sep'd wide number
1519 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1522 template <class _CharT, class _OutputIterator>
1524 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1525 char_type __fl, unsigned long long __v) const
1527 // Stage 1 - Get number in narrow char
1528 char __fmt[8] = {'%', 0};
1529 const char* __len = "ll";
1530 this->__format_int(__fmt+1, __len, false, __iob.flags());
1531 const unsigned __nbuf = (numeric_limits<unsigned long long>::digits / 3)
1532 + ((numeric_limits<unsigned long long>::digits % 3) != 0)
1533 + ((__iob.flags() & ios_base::showbase) != 0)
1536 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1537 char* __ne = __nar + __nc;
1538 char* __np = this->__identify_padding(__nar, __ne, __iob);
1539 // Stage 2 - Widen __nar while adding thousands separators
1540 char_type __o[2*(__nbuf-1) - 1];
1541 char_type* __op; // pad here
1542 char_type* __oe; // end of output
1543 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1544 // [__o, __oe) contains thousands_sep'd wide number
1546 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1549 template <class _CharT, class _OutputIterator>
1551 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1552 char_type __fl, double __v) const
1554 // Stage 1 - Get number in narrow char
1555 char __fmt[8] = {'%', 0};
1556 const char* __len = "";
1557 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1558 const unsigned __nbuf = 30;
1562 if (__specify_precision)
1563 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
1564 (int)__iob.precision(), __v);
1566 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1567 unique_ptr<char, void(*)(void*)> __nbh(0, free);
1568 if (__nc > static_cast<int>(__nbuf-1))
1570 if (__specify_precision)
1571 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
1573 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1575 __throw_bad_alloc();
1578 char* __ne = __nb + __nc;
1579 char* __np = this->__identify_padding(__nb, __ne, __iob);
1580 // Stage 2 - Widen __nar while adding thousands separators
1581 char_type __o[2*(__nbuf-1) - 1];
1582 char_type* __ob = __o;
1583 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1586 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
1588 __throw_bad_alloc();
1591 char_type* __op; // pad here
1592 char_type* __oe; // end of output
1593 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1594 // [__o, __oe) contains thousands_sep'd wide number
1596 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1600 template <class _CharT, class _OutputIterator>
1602 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1603 char_type __fl, long double __v) const
1605 // Stage 1 - Get number in narrow char
1606 char __fmt[8] = {'%', 0};
1607 const char* __len = "L";
1608 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1609 const unsigned __nbuf = 30;
1613 if (__specify_precision)
1614 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
1615 (int)__iob.precision(), __v);
1617 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1618 unique_ptr<char, void(*)(void*)> __nbh(0, free);
1619 if (__nc > static_cast<int>(__nbuf-1))
1621 if (__specify_precision)
1622 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
1624 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1626 __throw_bad_alloc();
1629 char* __ne = __nb + __nc;
1630 char* __np = this->__identify_padding(__nb, __ne, __iob);
1631 // Stage 2 - Widen __nar while adding thousands separators
1632 char_type __o[2*(__nbuf-1) - 1];
1633 char_type* __ob = __o;
1634 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1637 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
1639 __throw_bad_alloc();
1642 char_type* __op; // pad here
1643 char_type* __oe; // end of output
1644 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1645 // [__o, __oe) contains thousands_sep'd wide number
1647 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1651 template <class _CharT, class _OutputIterator>
1653 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1654 char_type __fl, const void* __v) const
1656 // Stage 1 - Get pointer in narrow char
1657 char __fmt[6] = "%p";
1658 const unsigned __nbuf = 20;
1660 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1661 char* __ne = __nar + __nc;
1662 char* __np = this->__identify_padding(__nar, __ne, __iob);
1663 // Stage 2 - Widen __nar
1664 char_type __o[2*(__nbuf-1) - 1];
1665 char_type* __op; // pad here
1666 char_type* __oe; // end of output
1667 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
1668 __ct.widen(__nar, __ne, __o);
1669 __oe = __o + (__ne - __nar);
1673 __op = __o + (__np - __nar);
1674 // [__o, __oe) contains wide number
1676 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1679 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<char>)
1680 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<wchar_t>)
1682 template <class _CharT, class _InputIterator>
1685 __get_up_to_n_digits(_InputIterator& __b, _InputIterator __e,
1686 ios_base::iostate& __err, const ctype<_CharT>& __ct, int __n)
1688 // Precondition: __n >= 1
1691 __err |= ios_base::eofbit | ios_base::failbit;
1696 if (!__ct.is(ctype_base::digit, __c))
1698 __err |= ios_base::failbit;
1701 int __r = __ct.narrow(__c, 0) - '0';
1702 for (++__b, (void) --__n; __b != __e && __n > 0; ++__b, (void) --__n)
1706 if (!__ct.is(ctype_base::digit, __c))
1708 __r = __r * 10 + __ct.narrow(__c, 0) - '0';
1711 __err |= ios_base::eofbit;
1715 class _LIBCPP_TYPE_VIS time_base
1718 enum dateorder {no_order, dmy, mdy, ymd, ydm};
1721 template <class _CharT>
1722 class _LIBCPP_TEMPLATE_VIS __time_get_c_storage
1725 typedef basic_string<_CharT> string_type;
1727 virtual const string_type* __weeks() const;
1728 virtual const string_type* __months() const;
1729 virtual const string_type* __am_pm() const;
1730 virtual const string_type& __c() const;
1731 virtual const string_type& __r() const;
1732 virtual const string_type& __x() const;
1733 virtual const string_type& __X() const;
1735 _LIBCPP_INLINE_VISIBILITY
1736 ~__time_get_c_storage() {}
1739 template <> _LIBCPP_FUNC_VIS const string* __time_get_c_storage<char>::__weeks() const;
1740 template <> _LIBCPP_FUNC_VIS const string* __time_get_c_storage<char>::__months() const;
1741 template <> _LIBCPP_FUNC_VIS const string* __time_get_c_storage<char>::__am_pm() const;
1742 template <> _LIBCPP_FUNC_VIS const string& __time_get_c_storage<char>::__c() const;
1743 template <> _LIBCPP_FUNC_VIS const string& __time_get_c_storage<char>::__r() const;
1744 template <> _LIBCPP_FUNC_VIS const string& __time_get_c_storage<char>::__x() const;
1745 template <> _LIBCPP_FUNC_VIS const string& __time_get_c_storage<char>::__X() const;
1747 template <> _LIBCPP_FUNC_VIS const wstring* __time_get_c_storage<wchar_t>::__weeks() const;
1748 template <> _LIBCPP_FUNC_VIS const wstring* __time_get_c_storage<wchar_t>::__months() const;
1749 template <> _LIBCPP_FUNC_VIS const wstring* __time_get_c_storage<wchar_t>::__am_pm() const;
1750 template <> _LIBCPP_FUNC_VIS const wstring& __time_get_c_storage<wchar_t>::__c() const;
1751 template <> _LIBCPP_FUNC_VIS const wstring& __time_get_c_storage<wchar_t>::__r() const;
1752 template <> _LIBCPP_FUNC_VIS const wstring& __time_get_c_storage<wchar_t>::__x() const;
1753 template <> _LIBCPP_FUNC_VIS const wstring& __time_get_c_storage<wchar_t>::__X() const;
1755 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
1756 class _LIBCPP_TEMPLATE_VIS time_get
1757 : public locale::facet,
1759 private __time_get_c_storage<_CharT>
1762 typedef _CharT char_type;
1763 typedef _InputIterator iter_type;
1764 typedef time_base::dateorder dateorder;
1765 typedef basic_string<char_type> string_type;
1767 _LIBCPP_INLINE_VISIBILITY
1768 explicit time_get(size_t __refs = 0)
1769 : locale::facet(__refs) {}
1771 _LIBCPP_INLINE_VISIBILITY
1772 dateorder date_order() const
1774 return this->do_date_order();
1777 _LIBCPP_INLINE_VISIBILITY
1778 iter_type get_time(iter_type __b, iter_type __e, ios_base& __iob,
1779 ios_base::iostate& __err, tm* __tm) const
1781 return do_get_time(__b, __e, __iob, __err, __tm);
1784 _LIBCPP_INLINE_VISIBILITY
1785 iter_type get_date(iter_type __b, iter_type __e, ios_base& __iob,
1786 ios_base::iostate& __err, tm* __tm) const
1788 return do_get_date(__b, __e, __iob, __err, __tm);
1791 _LIBCPP_INLINE_VISIBILITY
1792 iter_type get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
1793 ios_base::iostate& __err, tm* __tm) const
1795 return do_get_weekday(__b, __e, __iob, __err, __tm);
1798 _LIBCPP_INLINE_VISIBILITY
1799 iter_type get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
1800 ios_base::iostate& __err, tm* __tm) const
1802 return do_get_monthname(__b, __e, __iob, __err, __tm);
1805 _LIBCPP_INLINE_VISIBILITY
1806 iter_type get_year(iter_type __b, iter_type __e, ios_base& __iob,
1807 ios_base::iostate& __err, tm* __tm) const
1809 return do_get_year(__b, __e, __iob, __err, __tm);
1812 _LIBCPP_INLINE_VISIBILITY
1813 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
1814 ios_base::iostate& __err, tm *__tm,
1815 char __fmt, char __mod = 0) const
1817 return do_get(__b, __e, __iob, __err, __tm, __fmt, __mod);
1820 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
1821 ios_base::iostate& __err, tm* __tm,
1822 const char_type* __fmtb, const char_type* __fmte) const;
1824 static locale::id id;
1827 _LIBCPP_INLINE_VISIBILITY
1830 virtual dateorder do_date_order() const;
1831 virtual iter_type do_get_time(iter_type __b, iter_type __e, ios_base& __iob,
1832 ios_base::iostate& __err, tm* __tm) const;
1833 virtual iter_type do_get_date(iter_type __b, iter_type __e, ios_base& __iob,
1834 ios_base::iostate& __err, tm* __tm) const;
1835 virtual iter_type do_get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
1836 ios_base::iostate& __err, tm* __tm) const;
1837 virtual iter_type do_get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
1838 ios_base::iostate& __err, tm* __tm) const;
1839 virtual iter_type do_get_year(iter_type __b, iter_type __e, ios_base& __iob,
1840 ios_base::iostate& __err, tm* __tm) const;
1841 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
1842 ios_base::iostate& __err, tm* __tm,
1843 char __fmt, char __mod) const;
1845 void __get_white_space(iter_type& __b, iter_type __e,
1846 ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1847 void __get_percent(iter_type& __b, iter_type __e, ios_base::iostate& __err,
1848 const ctype<char_type>& __ct) const;
1850 void __get_weekdayname(int& __m,
1851 iter_type& __b, iter_type __e,
1852 ios_base::iostate& __err,
1853 const ctype<char_type>& __ct) const;
1854 void __get_monthname(int& __m,
1855 iter_type& __b, iter_type __e,
1856 ios_base::iostate& __err,
1857 const ctype<char_type>& __ct) const;
1858 void __get_day(int& __d,
1859 iter_type& __b, iter_type __e,
1860 ios_base::iostate& __err,
1861 const ctype<char_type>& __ct) const;
1862 void __get_month(int& __m,
1863 iter_type& __b, iter_type __e,
1864 ios_base::iostate& __err,
1865 const ctype<char_type>& __ct) const;
1866 void __get_year(int& __y,
1867 iter_type& __b, iter_type __e,
1868 ios_base::iostate& __err,
1869 const ctype<char_type>& __ct) const;
1870 void __get_year4(int& __y,
1871 iter_type& __b, iter_type __e,
1872 ios_base::iostate& __err,
1873 const ctype<char_type>& __ct) const;
1874 void __get_hour(int& __d,
1875 iter_type& __b, iter_type __e,
1876 ios_base::iostate& __err,
1877 const ctype<char_type>& __ct) const;
1878 void __get_12_hour(int& __h,
1879 iter_type& __b, iter_type __e,
1880 ios_base::iostate& __err,
1881 const ctype<char_type>& __ct) const;
1882 void __get_am_pm(int& __h,
1883 iter_type& __b, iter_type __e,
1884 ios_base::iostate& __err,
1885 const ctype<char_type>& __ct) const;
1886 void __get_minute(int& __m,
1887 iter_type& __b, iter_type __e,
1888 ios_base::iostate& __err,
1889 const ctype<char_type>& __ct) const;
1890 void __get_second(int& __s,
1891 iter_type& __b, iter_type __e,
1892 ios_base::iostate& __err,
1893 const ctype<char_type>& __ct) const;
1894 void __get_weekday(int& __w,
1895 iter_type& __b, iter_type __e,
1896 ios_base::iostate& __err,
1897 const ctype<char_type>& __ct) const;
1898 void __get_day_year_num(int& __w,
1899 iter_type& __b, iter_type __e,
1900 ios_base::iostate& __err,
1901 const ctype<char_type>& __ct) const;
1904 template <class _CharT, class _InputIterator>
1906 time_get<_CharT, _InputIterator>::id;
1908 // time_get primitives
1910 template <class _CharT, class _InputIterator>
1912 time_get<_CharT, _InputIterator>::__get_weekdayname(int& __w,
1913 iter_type& __b, iter_type __e,
1914 ios_base::iostate& __err,
1915 const ctype<char_type>& __ct) const
1917 // Note: ignoring case comes from the POSIX strptime spec
1918 const string_type* __wk = this->__weeks();
1919 ptrdiff_t __i = __scan_keyword(__b, __e, __wk, __wk+14, __ct, __err, false) - __wk;
1924 template <class _CharT, class _InputIterator>
1926 time_get<_CharT, _InputIterator>::__get_monthname(int& __m,
1927 iter_type& __b, iter_type __e,
1928 ios_base::iostate& __err,
1929 const ctype<char_type>& __ct) const
1931 // Note: ignoring case comes from the POSIX strptime spec
1932 const string_type* __month = this->__months();
1933 ptrdiff_t __i = __scan_keyword(__b, __e, __month, __month+24, __ct, __err, false) - __month;
1938 template <class _CharT, class _InputIterator>
1940 time_get<_CharT, _InputIterator>::__get_day(int& __d,
1941 iter_type& __b, iter_type __e,
1942 ios_base::iostate& __err,
1943 const ctype<char_type>& __ct) const
1945 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
1946 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 31)
1949 __err |= ios_base::failbit;
1952 template <class _CharT, class _InputIterator>
1954 time_get<_CharT, _InputIterator>::__get_month(int& __m,
1955 iter_type& __b, iter_type __e,
1956 ios_base::iostate& __err,
1957 const ctype<char_type>& __ct) const
1959 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2) - 1;
1960 if (!(__err & ios_base::failbit) && __t <= 11)
1963 __err |= ios_base::failbit;
1966 template <class _CharT, class _InputIterator>
1968 time_get<_CharT, _InputIterator>::__get_year(int& __y,
1969 iter_type& __b, iter_type __e,
1970 ios_base::iostate& __err,
1971 const ctype<char_type>& __ct) const
1973 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
1974 if (!(__err & ios_base::failbit))
1978 else if (69 <= __t && __t <= 99)
1984 template <class _CharT, class _InputIterator>
1986 time_get<_CharT, _InputIterator>::__get_year4(int& __y,
1987 iter_type& __b, iter_type __e,
1988 ios_base::iostate& __err,
1989 const ctype<char_type>& __ct) const
1991 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
1992 if (!(__err & ios_base::failbit))
1996 template <class _CharT, class _InputIterator>
1998 time_get<_CharT, _InputIterator>::__get_hour(int& __h,
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, 2);
2004 if (!(__err & ios_base::failbit) && __t <= 23)
2007 __err |= ios_base::failbit;
2010 template <class _CharT, class _InputIterator>
2012 time_get<_CharT, _InputIterator>::__get_12_hour(int& __h,
2013 iter_type& __b, iter_type __e,
2014 ios_base::iostate& __err,
2015 const ctype<char_type>& __ct) const
2017 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2018 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 12)
2021 __err |= ios_base::failbit;
2024 template <class _CharT, class _InputIterator>
2026 time_get<_CharT, _InputIterator>::__get_minute(int& __m,
2027 iter_type& __b, iter_type __e,
2028 ios_base::iostate& __err,
2029 const ctype<char_type>& __ct) const
2031 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2032 if (!(__err & ios_base::failbit) && __t <= 59)
2035 __err |= ios_base::failbit;
2038 template <class _CharT, class _InputIterator>
2040 time_get<_CharT, _InputIterator>::__get_second(int& __s,
2041 iter_type& __b, iter_type __e,
2042 ios_base::iostate& __err,
2043 const ctype<char_type>& __ct) const
2045 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2046 if (!(__err & ios_base::failbit) && __t <= 60)
2049 __err |= ios_base::failbit;
2052 template <class _CharT, class _InputIterator>
2054 time_get<_CharT, _InputIterator>::__get_weekday(int& __w,
2055 iter_type& __b, iter_type __e,
2056 ios_base::iostate& __err,
2057 const ctype<char_type>& __ct) const
2059 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 1);
2060 if (!(__err & ios_base::failbit) && __t <= 6)
2063 __err |= ios_base::failbit;
2066 template <class _CharT, class _InputIterator>
2068 time_get<_CharT, _InputIterator>::__get_day_year_num(int& __d,
2069 iter_type& __b, iter_type __e,
2070 ios_base::iostate& __err,
2071 const ctype<char_type>& __ct) const
2073 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 3);
2074 if (!(__err & ios_base::failbit) && __t <= 365)
2077 __err |= ios_base::failbit;
2080 template <class _CharT, class _InputIterator>
2082 time_get<_CharT, _InputIterator>::__get_white_space(iter_type& __b, iter_type __e,
2083 ios_base::iostate& __err,
2084 const ctype<char_type>& __ct) const
2086 for (; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2089 __err |= ios_base::eofbit;
2092 template <class _CharT, class _InputIterator>
2094 time_get<_CharT, _InputIterator>::__get_am_pm(int& __h,
2095 iter_type& __b, iter_type __e,
2096 ios_base::iostate& __err,
2097 const ctype<char_type>& __ct) const
2099 const string_type* __ap = this->__am_pm();
2100 if (__ap[0].size() + __ap[1].size() == 0)
2102 __err |= ios_base::failbit;
2105 ptrdiff_t __i = __scan_keyword(__b, __e, __ap, __ap+2, __ct, __err, false) - __ap;
2106 if (__i == 0 && __h == 12)
2108 else if (__i == 1 && __h < 12)
2112 template <class _CharT, class _InputIterator>
2114 time_get<_CharT, _InputIterator>::__get_percent(iter_type& __b, iter_type __e,
2115 ios_base::iostate& __err,
2116 const ctype<char_type>& __ct) const
2120 __err |= ios_base::eofbit | ios_base::failbit;
2123 if (__ct.narrow(*__b, 0) != '%')
2124 __err |= ios_base::failbit;
2125 else if(++__b == __e)
2126 __err |= ios_base::eofbit;
2129 // time_get end primitives
2131 template <class _CharT, class _InputIterator>
2133 time_get<_CharT, _InputIterator>::get(iter_type __b, iter_type __e,
2135 ios_base::iostate& __err, tm* __tm,
2136 const char_type* __fmtb, const char_type* __fmte) const
2138 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2139 __err = ios_base::goodbit;
2140 while (__fmtb != __fmte && __err == ios_base::goodbit)
2144 __err = ios_base::failbit;
2147 if (__ct.narrow(*__fmtb, 0) == '%')
2149 if (++__fmtb == __fmte)
2151 __err = ios_base::failbit;
2154 char __cmd = __ct.narrow(*__fmtb, 0);
2156 if (__cmd == 'E' || __cmd == '0')
2158 if (++__fmtb == __fmte)
2160 __err = ios_base::failbit;
2164 __cmd = __ct.narrow(*__fmtb, 0);
2166 __b = do_get(__b, __e, __iob, __err, __tm, __cmd, __opt);
2169 else if (__ct.is(ctype_base::space, *__fmtb))
2171 for (++__fmtb; __fmtb != __fmte && __ct.is(ctype_base::space, *__fmtb); ++__fmtb)
2173 for ( ; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2176 else if (__ct.toupper(*__b) == __ct.toupper(*__fmtb))
2182 __err = ios_base::failbit;
2185 __err |= ios_base::eofbit;
2189 template <class _CharT, class _InputIterator>
2190 typename time_get<_CharT, _InputIterator>::dateorder
2191 time_get<_CharT, _InputIterator>::do_date_order() const
2196 template <class _CharT, class _InputIterator>
2198 time_get<_CharT, _InputIterator>::do_get_time(iter_type __b, iter_type __e,
2200 ios_base::iostate& __err,
2203 const char_type __fmt[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2204 return get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt)/sizeof(__fmt[0]));
2207 template <class _CharT, class _InputIterator>
2209 time_get<_CharT, _InputIterator>::do_get_date(iter_type __b, iter_type __e,
2211 ios_base::iostate& __err,
2214 const string_type& __fmt = this->__x();
2215 return get(__b, __e, __iob, __err, __tm, __fmt.data(), __fmt.data() + __fmt.size());
2218 template <class _CharT, class _InputIterator>
2220 time_get<_CharT, _InputIterator>::do_get_weekday(iter_type __b, iter_type __e,
2222 ios_base::iostate& __err,
2225 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2226 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2230 template <class _CharT, class _InputIterator>
2232 time_get<_CharT, _InputIterator>::do_get_monthname(iter_type __b, iter_type __e,
2234 ios_base::iostate& __err,
2237 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2238 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2242 template <class _CharT, class _InputIterator>
2244 time_get<_CharT, _InputIterator>::do_get_year(iter_type __b, iter_type __e,
2246 ios_base::iostate& __err,
2249 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2250 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2254 template <class _CharT, class _InputIterator>
2256 time_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
2258 ios_base::iostate& __err, tm* __tm,
2259 char __fmt, char) const
2261 __err = ios_base::goodbit;
2262 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2267 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2272 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2276 const string_type& __fm = this->__c();
2277 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
2282 __get_day(__tm->tm_mday, __b, __e, __err, __ct);
2286 const char_type __fm[] = {'%', 'm', '/', '%', 'd', '/', '%', 'y'};
2287 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2292 const char_type __fm[] = {'%', 'Y', '-', '%', 'm', '-', '%', 'd'};
2293 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2297 __get_hour(__tm->tm_hour, __b, __e, __err, __ct);
2300 __get_12_hour(__tm->tm_hour, __b, __e, __err, __ct);
2303 __get_day_year_num(__tm->tm_yday, __b, __e, __err, __ct);
2306 __get_month(__tm->tm_mon, __b, __e, __err, __ct);
2309 __get_minute(__tm->tm_min, __b, __e, __err, __ct);
2313 __get_white_space(__b, __e, __err, __ct);
2316 __get_am_pm(__tm->tm_hour, __b, __e, __err, __ct);
2320 const char_type __fm[] = {'%', 'I', ':', '%', 'M', ':', '%', 'S', ' ', '%', 'p'};
2321 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2326 const char_type __fm[] = {'%', 'H', ':', '%', 'M'};
2327 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2331 __get_second(__tm->tm_sec, __b, __e, __err, __ct);
2335 const char_type __fm[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2336 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2340 __get_weekday(__tm->tm_wday, __b, __e, __err, __ct);
2343 return do_get_date(__b, __e, __iob, __err, __tm);
2346 const string_type& __fm = this->__X();
2347 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
2351 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2354 __get_year4(__tm->tm_year, __b, __e, __err, __ct);
2357 __get_percent(__b, __e, __err, __ct);
2360 __err |= ios_base::failbit;
2365 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get<char>)
2366 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get<wchar_t>)
2368 class _LIBCPP_TYPE_VIS __time_get
2373 __time_get(const char* __nm);
2374 __time_get(const string& __nm);
2378 template <class _CharT>
2379 class _LIBCPP_TEMPLATE_VIS __time_get_storage
2383 typedef basic_string<_CharT> string_type;
2385 string_type __weeks_[14];
2386 string_type __months_[24];
2387 string_type __am_pm_[2];
2393 explicit __time_get_storage(const char* __nm);
2394 explicit __time_get_storage(const string& __nm);
2396 _LIBCPP_INLINE_VISIBILITY ~__time_get_storage() {}
2398 time_base::dateorder __do_date_order() const;
2401 void init(const ctype<_CharT>&);
2402 string_type __analyze(char __fmt, const ctype<_CharT>&);
2405 #define _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(_CharT) \
2406 template <> _LIBCPP_FUNC_VIS time_base::dateorder __time_get_storage<_CharT>::__do_date_order() const; \
2407 template <> _LIBCPP_FUNC_VIS __time_get_storage<_CharT>::__time_get_storage(const char*); \
2408 template <> _LIBCPP_FUNC_VIS __time_get_storage<_CharT>::__time_get_storage(const string&); \
2409 template <> _LIBCPP_FUNC_VIS void __time_get_storage<_CharT>::init(const ctype<_CharT>&); \
2410 template <> _LIBCPP_FUNC_VIS __time_get_storage<_CharT>::string_type __time_get_storage<_CharT>::__analyze(char, const ctype<_CharT>&); \
2411 extern template _LIBCPP_FUNC_VIS time_base::dateorder __time_get_storage<_CharT>::__do_date_order() const; \
2412 extern template _LIBCPP_FUNC_VIS __time_get_storage<_CharT>::__time_get_storage(const char*); \
2413 extern template _LIBCPP_FUNC_VIS __time_get_storage<_CharT>::__time_get_storage(const string&); \
2414 extern template _LIBCPP_FUNC_VIS void __time_get_storage<_CharT>::init(const ctype<_CharT>&); \
2415 extern template _LIBCPP_FUNC_VIS __time_get_storage<_CharT>::string_type __time_get_storage<_CharT>::__analyze(char, const ctype<_CharT>&); \
2418 _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(char)
2419 _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(wchar_t)
2420 #undef _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION
2422 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2423 class _LIBCPP_TEMPLATE_VIS time_get_byname
2424 : public time_get<_CharT, _InputIterator>,
2425 private __time_get_storage<_CharT>
2428 typedef time_base::dateorder dateorder;
2429 typedef _InputIterator iter_type;
2430 typedef _CharT char_type;
2431 typedef basic_string<char_type> string_type;
2433 _LIBCPP_INLINE_VISIBILITY
2434 explicit time_get_byname(const char* __nm, size_t __refs = 0)
2435 : time_get<_CharT, _InputIterator>(__refs),
2436 __time_get_storage<_CharT>(__nm) {}
2437 _LIBCPP_INLINE_VISIBILITY
2438 explicit time_get_byname(const string& __nm, size_t __refs = 0)
2439 : time_get<_CharT, _InputIterator>(__refs),
2440 __time_get_storage<_CharT>(__nm) {}
2443 _LIBCPP_INLINE_VISIBILITY
2444 ~time_get_byname() {}
2446 _LIBCPP_INLINE_VISIBILITY
2447 virtual dateorder do_date_order() const {return this->__do_date_order();}
2449 _LIBCPP_INLINE_VISIBILITY
2450 virtual const string_type* __weeks() const {return this->__weeks_;}
2451 _LIBCPP_INLINE_VISIBILITY
2452 virtual const string_type* __months() const {return this->__months_;}
2453 _LIBCPP_INLINE_VISIBILITY
2454 virtual const string_type* __am_pm() const {return this->__am_pm_;}
2455 _LIBCPP_INLINE_VISIBILITY
2456 virtual const string_type& __c() const {return this->__c_;}
2457 _LIBCPP_INLINE_VISIBILITY
2458 virtual const string_type& __r() const {return this->__r_;}
2459 _LIBCPP_INLINE_VISIBILITY
2460 virtual const string_type& __x() const {return this->__x_;}
2461 _LIBCPP_INLINE_VISIBILITY
2462 virtual const string_type& __X() const {return this->__X_;}
2465 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<char>)
2466 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<wchar_t>)
2468 class _LIBCPP_TYPE_VIS __time_put
2472 _LIBCPP_INLINE_VISIBILITY __time_put() : __loc_(_LIBCPP_GET_C_LOCALE) {}
2473 __time_put(const char* __nm);
2474 __time_put(const string& __nm);
2476 void __do_put(char* __nb, char*& __ne, const tm* __tm,
2477 char __fmt, char __mod) const;
2478 void __do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm,
2479 char __fmt, char __mod) const;
2482 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2483 class _LIBCPP_TEMPLATE_VIS time_put
2484 : public locale::facet,
2488 typedef _CharT char_type;
2489 typedef _OutputIterator iter_type;
2491 _LIBCPP_INLINE_VISIBILITY
2492 explicit time_put(size_t __refs = 0)
2493 : locale::facet(__refs) {}
2495 iter_type put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm,
2496 const char_type* __pb, const char_type* __pe) const;
2498 _LIBCPP_INLINE_VISIBILITY
2499 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
2500 const tm* __tm, char __fmt, char __mod = 0) const
2502 return do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2505 static locale::id id;
2508 _LIBCPP_INLINE_VISIBILITY
2510 virtual iter_type do_put(iter_type __s, ios_base&, char_type, const tm* __tm,
2511 char __fmt, char __mod) const;
2513 _LIBCPP_INLINE_VISIBILITY
2514 explicit time_put(const char* __nm, size_t __refs)
2515 : locale::facet(__refs),
2517 _LIBCPP_INLINE_VISIBILITY
2518 explicit time_put(const string& __nm, size_t __refs)
2519 : locale::facet(__refs),
2523 template <class _CharT, class _OutputIterator>
2525 time_put<_CharT, _OutputIterator>::id;
2527 template <class _CharT, class _OutputIterator>
2529 time_put<_CharT, _OutputIterator>::put(iter_type __s, ios_base& __iob,
2530 char_type __fl, const tm* __tm,
2531 const char_type* __pb,
2532 const char_type* __pe) const
2534 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2535 for (; __pb != __pe; ++__pb)
2537 if (__ct.narrow(*__pb, 0) == '%')
2545 char __fmt = __ct.narrow(*__pb, 0);
2546 if (__fmt == 'E' || __fmt == 'O')
2555 __fmt = __ct.narrow(*__pb, 0);
2557 __s = do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2565 template <class _CharT, class _OutputIterator>
2567 time_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base&,
2568 char_type, const tm* __tm,
2569 char __fmt, char __mod) const
2571 char_type __nar[100];
2572 char_type* __nb = __nar;
2573 char_type* __ne = __nb + 100;
2574 __do_put(__nb, __ne, __tm, __fmt, __mod);
2575 return _VSTD::copy(__nb, __ne, __s);
2578 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<char>)
2579 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<wchar_t>)
2581 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2582 class _LIBCPP_TEMPLATE_VIS time_put_byname
2583 : public time_put<_CharT, _OutputIterator>
2586 _LIBCPP_INLINE_VISIBILITY
2587 explicit time_put_byname(const char* __nm, size_t __refs = 0)
2588 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2590 _LIBCPP_INLINE_VISIBILITY
2591 explicit time_put_byname(const string& __nm, size_t __refs = 0)
2592 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2595 _LIBCPP_INLINE_VISIBILITY
2596 ~time_put_byname() {}
2599 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<char>)
2600 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<wchar_t>)
2604 class _LIBCPP_TYPE_VIS money_base
2607 enum part {none, space, symbol, sign, value};
2608 struct pattern {char field[4];};
2610 _LIBCPP_INLINE_VISIBILITY money_base() {}
2615 template <class _CharT, bool _International = false>
2616 class _LIBCPP_TEMPLATE_VIS moneypunct
2617 : public locale::facet,
2621 typedef _CharT char_type;
2622 typedef basic_string<char_type> string_type;
2624 _LIBCPP_INLINE_VISIBILITY
2625 explicit moneypunct(size_t __refs = 0)
2626 : locale::facet(__refs) {}
2628 _LIBCPP_INLINE_VISIBILITY char_type decimal_point() const {return do_decimal_point();}
2629 _LIBCPP_INLINE_VISIBILITY char_type thousands_sep() const {return do_thousands_sep();}
2630 _LIBCPP_INLINE_VISIBILITY string grouping() const {return do_grouping();}
2631 _LIBCPP_INLINE_VISIBILITY string_type curr_symbol() const {return do_curr_symbol();}
2632 _LIBCPP_INLINE_VISIBILITY string_type positive_sign() const {return do_positive_sign();}
2633 _LIBCPP_INLINE_VISIBILITY string_type negative_sign() const {return do_negative_sign();}
2634 _LIBCPP_INLINE_VISIBILITY int frac_digits() const {return do_frac_digits();}
2635 _LIBCPP_INLINE_VISIBILITY pattern pos_format() const {return do_pos_format();}
2636 _LIBCPP_INLINE_VISIBILITY pattern neg_format() const {return do_neg_format();}
2638 static locale::id id;
2639 static const bool intl = _International;
2642 _LIBCPP_INLINE_VISIBILITY
2645 virtual char_type do_decimal_point() const {return numeric_limits<char_type>::max();}
2646 virtual char_type do_thousands_sep() const {return numeric_limits<char_type>::max();}
2647 virtual string do_grouping() const {return string();}
2648 virtual string_type do_curr_symbol() const {return string_type();}
2649 virtual string_type do_positive_sign() const {return string_type();}
2650 virtual string_type do_negative_sign() const {return string_type(1, '-');}
2651 virtual int do_frac_digits() const {return 0;}
2652 virtual pattern do_pos_format() const
2653 {pattern __p = {{symbol, sign, none, value}}; return __p;}
2654 virtual pattern do_neg_format() const
2655 {pattern __p = {{symbol, sign, none, value}}; return __p;}
2658 template <class _CharT, bool _International>
2660 moneypunct<_CharT, _International>::id;
2662 template <class _CharT, bool _International>
2664 moneypunct<_CharT, _International>::intl;
2666 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, false>)
2667 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, true>)
2668 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, false>)
2669 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, true>)
2671 // moneypunct_byname
2673 template <class _CharT, bool _International = false>
2674 class _LIBCPP_TEMPLATE_VIS moneypunct_byname
2675 : public moneypunct<_CharT, _International>
2678 typedef money_base::pattern pattern;
2679 typedef _CharT char_type;
2680 typedef basic_string<char_type> string_type;
2682 _LIBCPP_INLINE_VISIBILITY
2683 explicit moneypunct_byname(const char* __nm, size_t __refs = 0)
2684 : moneypunct<_CharT, _International>(__refs) {init(__nm);}
2686 _LIBCPP_INLINE_VISIBILITY
2687 explicit moneypunct_byname(const string& __nm, size_t __refs = 0)
2688 : moneypunct<_CharT, _International>(__refs) {init(__nm.c_str());}
2691 _LIBCPP_INLINE_VISIBILITY
2692 ~moneypunct_byname() {}
2694 virtual char_type do_decimal_point() const {return __decimal_point_;}
2695 virtual char_type do_thousands_sep() const {return __thousands_sep_;}
2696 virtual string do_grouping() const {return __grouping_;}
2697 virtual string_type do_curr_symbol() const {return __curr_symbol_;}
2698 virtual string_type do_positive_sign() const {return __positive_sign_;}
2699 virtual string_type do_negative_sign() const {return __negative_sign_;}
2700 virtual int do_frac_digits() const {return __frac_digits_;}
2701 virtual pattern do_pos_format() const {return __pos_format_;}
2702 virtual pattern do_neg_format() const {return __neg_format_;}
2705 char_type __decimal_point_;
2706 char_type __thousands_sep_;
2708 string_type __curr_symbol_;
2709 string_type __positive_sign_;
2710 string_type __negative_sign_;
2712 pattern __pos_format_;
2713 pattern __neg_format_;
2715 void init(const char*);
2718 template<> _LIBCPP_FUNC_VIS void moneypunct_byname<char, false>::init(const char*);
2719 template<> _LIBCPP_FUNC_VIS void moneypunct_byname<char, true>::init(const char*);
2720 template<> _LIBCPP_FUNC_VIS void moneypunct_byname<wchar_t, false>::init(const char*);
2721 template<> _LIBCPP_FUNC_VIS void moneypunct_byname<wchar_t, true>::init(const char*);
2723 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, false>)
2724 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, true>)
2725 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, false>)
2726 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, true>)
2730 template <class _CharT>
2734 typedef _CharT char_type;
2735 typedef basic_string<char_type> string_type;
2737 _LIBCPP_INLINE_VISIBILITY __money_get() {}
2739 static void __gather_info(bool __intl, const locale& __loc,
2740 money_base::pattern& __pat, char_type& __dp,
2741 char_type& __ts, string& __grp,
2742 string_type& __sym, string_type& __psn,
2743 string_type& __nsn, int& __fd);
2746 template <class _CharT>
2748 __money_get<_CharT>::__gather_info(bool __intl, const locale& __loc,
2749 money_base::pattern& __pat, char_type& __dp,
2750 char_type& __ts, string& __grp,
2751 string_type& __sym, string_type& __psn,
2752 string_type& __nsn, int& __fd)
2756 const moneypunct<char_type, true>& __mp =
2757 use_facet<moneypunct<char_type, true> >(__loc);
2758 __pat = __mp.neg_format();
2759 __nsn = __mp.negative_sign();
2760 __psn = __mp.positive_sign();
2761 __dp = __mp.decimal_point();
2762 __ts = __mp.thousands_sep();
2763 __grp = __mp.grouping();
2764 __sym = __mp.curr_symbol();
2765 __fd = __mp.frac_digits();
2769 const moneypunct<char_type, false>& __mp =
2770 use_facet<moneypunct<char_type, false> >(__loc);
2771 __pat = __mp.neg_format();
2772 __nsn = __mp.negative_sign();
2773 __psn = __mp.positive_sign();
2774 __dp = __mp.decimal_point();
2775 __ts = __mp.thousands_sep();
2776 __grp = __mp.grouping();
2777 __sym = __mp.curr_symbol();
2778 __fd = __mp.frac_digits();
2782 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<char>)
2783 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<wchar_t>)
2785 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2786 class _LIBCPP_TEMPLATE_VIS money_get
2787 : public locale::facet,
2788 private __money_get<_CharT>
2791 typedef _CharT char_type;
2792 typedef _InputIterator iter_type;
2793 typedef basic_string<char_type> string_type;
2795 _LIBCPP_INLINE_VISIBILITY
2796 explicit money_get(size_t __refs = 0)
2797 : locale::facet(__refs) {}
2799 _LIBCPP_INLINE_VISIBILITY
2800 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
2801 ios_base::iostate& __err, long double& __v) const
2803 return do_get(__b, __e, __intl, __iob, __err, __v);
2806 _LIBCPP_INLINE_VISIBILITY
2807 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
2808 ios_base::iostate& __err, string_type& __v) const
2810 return do_get(__b, __e, __intl, __iob, __err, __v);
2813 static locale::id id;
2817 _LIBCPP_INLINE_VISIBILITY
2820 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
2821 ios_base& __iob, ios_base::iostate& __err,
2822 long double& __v) const;
2823 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
2824 ios_base& __iob, ios_base::iostate& __err,
2825 string_type& __v) const;
2828 static bool __do_get(iter_type& __b, iter_type __e,
2829 bool __intl, const locale& __loc,
2830 ios_base::fmtflags __flags, ios_base::iostate& __err,
2831 bool& __neg, const ctype<char_type>& __ct,
2832 unique_ptr<char_type, void(*)(void*)>& __wb,
2833 char_type*& __wn, char_type* __we);
2836 template <class _CharT, class _InputIterator>
2838 money_get<_CharT, _InputIterator>::id;
2840 _LIBCPP_FUNC_VIS void __do_nothing(void*);
2842 template <class _Tp>
2845 __double_or_nothing(unique_ptr<_Tp, void(*)(void*)>& __b, _Tp*& __n, _Tp*& __e)
2847 bool __owns = __b.get_deleter() != __do_nothing;
2848 size_t __cur_cap = static_cast<size_t>(__e-__b.get()) * sizeof(_Tp);
2849 size_t __new_cap = __cur_cap < numeric_limits<size_t>::max() / 2 ?
2850 2 * __cur_cap : numeric_limits<size_t>::max();
2852 __new_cap = sizeof(_Tp);
2853 size_t __n_off = static_cast<size_t>(__n - __b.get());
2854 _Tp* __t = (_Tp*)realloc(__owns ? __b.get() : 0, __new_cap);
2856 __throw_bad_alloc();
2859 __b = unique_ptr<_Tp, void(*)(void*)>(__t, free);
2860 __new_cap /= sizeof(_Tp);
2861 __n = __b.get() + __n_off;
2862 __e = __b.get() + __new_cap;
2866 template <class _CharT, class _InputIterator>
2868 money_get<_CharT, _InputIterator>::__do_get(iter_type& __b, iter_type __e,
2869 bool __intl, const locale& __loc,
2870 ios_base::fmtflags __flags,
2871 ios_base::iostate& __err,
2873 const ctype<char_type>& __ct,
2874 unique_ptr<char_type, void(*)(void*)>& __wb,
2875 char_type*& __wn, char_type* __we)
2877 const unsigned __bz = 100;
2878 unsigned __gbuf[__bz];
2879 unique_ptr<unsigned, void(*)(void*)> __gb(__gbuf, __do_nothing);
2880 unsigned* __gn = __gb.get();
2881 unsigned* __ge = __gn + __bz;
2882 money_base::pattern __pat;
2889 // Capture the spaces read into money_base::{space,none} so they
2890 // can be compared to initial spaces in __sym.
2891 string_type __spaces;
2893 __money_get<_CharT>::__gather_info(__intl, __loc, __pat, __dp, __ts, __grp,
2894 __sym, __psn, __nsn, __fd);
2895 const string_type* __trailing_sign = 0;
2897 for (unsigned __p = 0; __p < 4 && __b != __e; ++__p)
2899 switch (__pat.field[__p])
2901 case money_base::space:
2904 if (__ct.is(ctype_base::space, *__b))
2905 __spaces.push_back(*__b++);
2908 __err |= ios_base::failbit;
2912 _LIBCPP_FALLTHROUGH();
2913 case money_base::none:
2916 while (__b != __e && __ct.is(ctype_base::space, *__b))
2917 __spaces.push_back(*__b++);
2920 case money_base::sign:
2921 if (__psn.size() + __nsn.size() > 0)
2923 if (__psn.size() == 0 || __nsn.size() == 0)
2924 { // sign is optional
2925 if (__psn.size() > 0)
2926 { // __nsn.size() == 0
2927 if (*__b == __psn[0])
2930 if (__psn.size() > 1)
2931 __trailing_sign = &__psn;
2936 else if (*__b == __nsn[0]) // __nsn.size() > 0 && __psn.size() == 0
2940 if (__nsn.size() > 1)
2941 __trailing_sign = &__nsn;
2944 else // sign is required
2946 if (*__b == __psn[0])
2949 if (__psn.size() > 1)
2950 __trailing_sign = &__psn;
2952 else if (*__b == __nsn[0])
2956 if (__nsn.size() > 1)
2957 __trailing_sign = &__nsn;
2961 __err |= ios_base::failbit;
2967 case money_base::symbol:
2969 bool __more_needed = __trailing_sign ||
2971 (__p == 2 && __pat.field[3] != static_cast<char>(money_base::none));
2972 bool __sb = (__flags & ios_base::showbase) != 0;
2973 if (__sb || __more_needed)
2975 typename string_type::const_iterator __sym_space_end = __sym.begin();
2976 if (__p > 0 && (__pat.field[__p - 1] == money_base::none ||
2977 __pat.field[__p - 1] == money_base::space)) {
2978 // Match spaces we've already read against spaces at
2979 // the beginning of __sym.
2980 while (__sym_space_end != __sym.end() &&
2981 __ct.is(ctype_base::space, *__sym_space_end))
2983 const size_t __num_spaces = __sym_space_end - __sym.begin();
2984 if (__num_spaces > __spaces.size() ||
2985 !equal(__spaces.end() - __num_spaces, __spaces.end(),
2987 // No match. Put __sym_space_end back at the
2988 // beginning of __sym, which will prevent a
2989 // match in the next loop.
2990 __sym_space_end = __sym.begin();
2993 typename string_type::const_iterator __sym_curr_char = __sym_space_end;
2994 while (__sym_curr_char != __sym.end() && __b != __e &&
2995 *__b == *__sym_curr_char) {
2999 if (__sb && __sym_curr_char != __sym.end())
3001 __err |= ios_base::failbit;
3007 case money_base::value:
3010 for (; __b != __e; ++__b)
3012 char_type __c = *__b;
3013 if (__ct.is(ctype_base::digit, __c))
3016 __double_or_nothing(__wb, __wn, __we);
3020 else if (__grp.size() > 0 && __ng > 0 && __c == __ts)
3023 __double_or_nothing(__gb, __gn, __ge);
3030 if (__gb.get() != __gn && __ng > 0)
3033 __double_or_nothing(__gb, __gn, __ge);
3038 if (__b == __e || *__b != __dp)
3040 __err |= ios_base::failbit;
3043 for (++__b; __fd > 0; --__fd, ++__b)
3045 if (__b == __e || !__ct.is(ctype_base::digit, *__b))
3047 __err |= ios_base::failbit;
3051 __double_or_nothing(__wb, __wn, __we);
3055 if (__wn == __wb.get())
3057 __err |= ios_base::failbit;
3064 if (__trailing_sign)
3066 for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, ++__b)
3068 if (__b == __e || *__b != (*__trailing_sign)[__i])
3070 __err |= ios_base::failbit;
3075 if (__gb.get() != __gn)
3077 ios_base::iostate __et = ios_base::goodbit;
3078 __check_grouping(__grp, __gb.get(), __gn, __et);
3081 __err |= ios_base::failbit;
3088 template <class _CharT, class _InputIterator>
3090 money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3091 bool __intl, ios_base& __iob,
3092 ios_base::iostate& __err,
3093 long double& __v) const
3095 const int __bz = 100;
3096 char_type __wbuf[__bz];
3097 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3099 char_type* __we = __wbuf + __bz;
3100 locale __loc = __iob.getloc();
3101 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3103 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3106 const char __src[] = "0123456789";
3107 char_type __atoms[sizeof(__src)-1];
3108 __ct.widen(__src, __src + (sizeof(__src)-1), __atoms);
3110 char* __nc = __nbuf;
3111 unique_ptr<char, void(*)(void*)> __h(0, free);
3112 if (__wn - __wb.get() > __bz-2)
3114 __h.reset((char*)malloc(static_cast<size_t>(__wn - __wb.get() + 2)));
3116 __throw_bad_alloc();
3121 for (const char_type* __w = __wb.get(); __w < __wn; ++__w, ++__nc)
3122 *__nc = __src[find(__atoms, _VSTD::end(__atoms), *__w) - __atoms];
3124 if (sscanf(__nbuf, "%Lf", &__v) != 1)
3125 __throw_runtime_error("money_get error");
3128 __err |= ios_base::eofbit;
3132 template <class _CharT, class _InputIterator>
3134 money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3135 bool __intl, ios_base& __iob,
3136 ios_base::iostate& __err,
3137 string_type& __v) const
3139 const int __bz = 100;
3140 char_type __wbuf[__bz];
3141 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3143 char_type* __we = __wbuf + __bz;
3144 locale __loc = __iob.getloc();
3145 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3147 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3152 __v.push_back(__ct.widen('-'));
3153 char_type __z = __ct.widen('0');
3155 for (__w = __wb.get(); __w < __wn-1; ++__w)
3158 __v.append(__w, __wn);
3161 __err |= ios_base::eofbit;
3165 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<char>)
3166 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<wchar_t>)
3170 template <class _CharT>
3174 typedef _CharT char_type;
3175 typedef basic_string<char_type> string_type;
3177 _LIBCPP_INLINE_VISIBILITY __money_put() {}
3179 static void __gather_info(bool __intl, bool __neg, const locale& __loc,
3180 money_base::pattern& __pat, char_type& __dp,
3181 char_type& __ts, string& __grp,
3182 string_type& __sym, string_type& __sn,
3184 static void __format(char_type* __mb, char_type*& __mi, char_type*& __me,
3185 ios_base::fmtflags __flags,
3186 const char_type* __db, const char_type* __de,
3187 const ctype<char_type>& __ct, bool __neg,
3188 const money_base::pattern& __pat, char_type __dp,
3189 char_type __ts, const string& __grp,
3190 const string_type& __sym, const string_type& __sn,
3194 template <class _CharT>
3196 __money_put<_CharT>::__gather_info(bool __intl, bool __neg, const locale& __loc,
3197 money_base::pattern& __pat, char_type& __dp,
3198 char_type& __ts, string& __grp,
3199 string_type& __sym, string_type& __sn,
3204 const moneypunct<char_type, true>& __mp =
3205 use_facet<moneypunct<char_type, true> >(__loc);
3208 __pat = __mp.neg_format();
3209 __sn = __mp.negative_sign();
3213 __pat = __mp.pos_format();
3214 __sn = __mp.positive_sign();
3216 __dp = __mp.decimal_point();
3217 __ts = __mp.thousands_sep();
3218 __grp = __mp.grouping();
3219 __sym = __mp.curr_symbol();
3220 __fd = __mp.frac_digits();
3224 const moneypunct<char_type, false>& __mp =
3225 use_facet<moneypunct<char_type, false> >(__loc);
3228 __pat = __mp.neg_format();
3229 __sn = __mp.negative_sign();
3233 __pat = __mp.pos_format();
3234 __sn = __mp.positive_sign();
3236 __dp = __mp.decimal_point();
3237 __ts = __mp.thousands_sep();
3238 __grp = __mp.grouping();
3239 __sym = __mp.curr_symbol();
3240 __fd = __mp.frac_digits();
3244 template <class _CharT>
3246 __money_put<_CharT>::__format(char_type* __mb, char_type*& __mi, char_type*& __me,
3247 ios_base::fmtflags __flags,
3248 const char_type* __db, const char_type* __de,
3249 const ctype<char_type>& __ct, bool __neg,
3250 const money_base::pattern& __pat, char_type __dp,
3251 char_type __ts, const string& __grp,
3252 const string_type& __sym, const string_type& __sn,
3256 for (unsigned __p = 0; __p < 4; ++__p)
3258 switch (__pat.field[__p])
3260 case money_base::none:
3263 case money_base::space:
3265 *__me++ = __ct.widen(' ');
3267 case money_base::sign:
3271 case money_base::symbol:
3272 if (!__sym.empty() && (__flags & ios_base::showbase))
3273 __me = _VSTD::copy(__sym.begin(), __sym.end(), __me);
3275 case money_base::value:
3277 // remember start of value so we can reverse it
3278 char_type* __t = __me;
3279 // find beginning of digits
3282 // find end of digits
3283 const char_type* __d;
3284 for (__d = __db; __d < __de; ++__d)
3285 if (!__ct.is(ctype_base::digit, *__d))
3287 // print fractional part
3291 for (__f = __fd; __d > __db && __f > 0; --__f)
3293 char_type __z = __f > 0 ? __ct.widen('0') : char_type();
3294 for (; __f > 0; --__f)
3301 *__me++ = __ct.widen('0');
3307 unsigned __gl = __grp.empty() ? numeric_limits<unsigned>::max()
3308 : static_cast<unsigned>(__grp[__ig]);
3315 if (++__ig < __grp.size())
3316 __gl = __grp[__ig] == numeric_limits<char>::max() ?
3317 numeric_limits<unsigned>::max() :
3318 static_cast<unsigned>(__grp[__ig]);
3330 // print rest of sign, if any
3331 if (__sn.size() > 1)
3332 __me = _VSTD::copy(__sn.begin()+1, __sn.end(), __me);
3334 if ((__flags & ios_base::adjustfield) == ios_base::left)
3336 else if ((__flags & ios_base::adjustfield) != ios_base::internal)
3340 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<char>)
3341 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<wchar_t>)
3343 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
3344 class _LIBCPP_TEMPLATE_VIS money_put
3345 : public locale::facet,
3346 private __money_put<_CharT>
3349 typedef _CharT char_type;
3350 typedef _OutputIterator iter_type;
3351 typedef basic_string<char_type> string_type;
3353 _LIBCPP_INLINE_VISIBILITY
3354 explicit money_put(size_t __refs = 0)
3355 : locale::facet(__refs) {}
3357 _LIBCPP_INLINE_VISIBILITY
3358 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3359 long double __units) const
3361 return do_put(__s, __intl, __iob, __fl, __units);
3364 _LIBCPP_INLINE_VISIBILITY
3365 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3366 const string_type& __digits) const
3368 return do_put(__s, __intl, __iob, __fl, __digits);
3371 static locale::id id;
3374 _LIBCPP_INLINE_VISIBILITY
3377 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3378 char_type __fl, long double __units) const;
3379 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3380 char_type __fl, const string_type& __digits) const;
3383 template <class _CharT, class _OutputIterator>
3385 money_put<_CharT, _OutputIterator>::id;
3387 template <class _CharT, class _OutputIterator>
3389 money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3390 ios_base& __iob, char_type __fl,
3391 long double __units) const
3394 const size_t __bs = 100;
3397 char_type __digits[__bs];
3398 char_type* __db = __digits;
3399 size_t __n = static_cast<size_t>(snprintf(__bb, __bs, "%.0Lf", __units));
3400 unique_ptr<char, void(*)(void*)> __hn(0, free);
3401 unique_ptr<char_type, void(*)(void*)> __hd(0, free);
3402 // secure memory for digit storage
3405 __n = static_cast<size_t>(__libcpp_asprintf_l(&__bb, _LIBCPP_GET_C_LOCALE, "%.0Lf", __units));
3407 __throw_bad_alloc();
3409 __hd.reset((char_type*)malloc(__n * sizeof(char_type)));
3410 if (__hd == nullptr)
3411 __throw_bad_alloc();
3415 locale __loc = __iob.getloc();
3416 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3417 __ct.widen(__bb, __bb + __n, __db);
3418 bool __neg = __n > 0 && __bb[0] == '-';
3419 money_base::pattern __pat;
3426 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3427 // secure memory for formatting
3428 char_type __mbuf[__bs];
3429 char_type* __mb = __mbuf;
3430 unique_ptr<char_type, void(*)(void*)> __hw(0, free);
3431 size_t __exn = static_cast<int>(__n) > __fd ?
3432 (__n - static_cast<size_t>(__fd)) * 2 + __sn.size() +
3433 __sym.size() + static_cast<size_t>(__fd) + 1
3434 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
3437 __hw.reset((char_type*)malloc(__exn * sizeof(char_type)));
3440 __throw_bad_alloc();
3445 this->__format(__mb, __mi, __me, __iob.flags(),
3446 __db, __db + __n, __ct,
3447 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3448 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3451 template <class _CharT, class _OutputIterator>
3453 money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3454 ios_base& __iob, char_type __fl,
3455 const string_type& __digits) const
3458 locale __loc = __iob.getloc();
3459 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3460 bool __neg = __digits.size() > 0 && __digits[0] == __ct.widen('-');
3461 money_base::pattern __pat;
3468 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3469 // secure memory for formatting
3470 char_type __mbuf[100];
3471 char_type* __mb = __mbuf;
3472 unique_ptr<char_type, void(*)(void*)> __h(0, free);
3473 size_t __exn = static_cast<int>(__digits.size()) > __fd ?
3474 (__digits.size() - static_cast<size_t>(__fd)) * 2 +
3475 __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 1
3476 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
3479 __h.reset((char_type*)malloc(__exn * sizeof(char_type)));
3482 __throw_bad_alloc();
3487 this->__format(__mb, __mi, __me, __iob.flags(),
3488 __digits.data(), __digits.data() + __digits.size(), __ct,
3489 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3490 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3493 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<char>)
3494 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<wchar_t>)
3498 class _LIBCPP_TYPE_VIS messages_base
3501 typedef ptrdiff_t catalog;
3503 _LIBCPP_INLINE_VISIBILITY messages_base() {}
3506 template <class _CharT>
3507 class _LIBCPP_TEMPLATE_VIS messages
3508 : public locale::facet,
3509 public messages_base
3512 typedef _CharT char_type;
3513 typedef basic_string<_CharT> string_type;
3515 _LIBCPP_INLINE_VISIBILITY
3516 explicit messages(size_t __refs = 0)
3517 : locale::facet(__refs) {}
3519 _LIBCPP_INLINE_VISIBILITY
3520 catalog open(const basic_string<char>& __nm, const locale& __loc) const
3522 return do_open(__nm, __loc);
3525 _LIBCPP_INLINE_VISIBILITY
3526 string_type get(catalog __c, int __set, int __msgid,
3527 const string_type& __dflt) const
3529 return do_get(__c, __set, __msgid, __dflt);
3532 _LIBCPP_INLINE_VISIBILITY
3533 void close(catalog __c) const
3538 static locale::id id;
3541 _LIBCPP_INLINE_VISIBILITY
3544 virtual catalog do_open(const basic_string<char>&, const locale&) const;
3545 virtual string_type do_get(catalog, int __set, int __msgid,
3546 const string_type& __dflt) const;
3547 virtual void do_close(catalog) const;
3550 template <class _CharT>
3552 messages<_CharT>::id;
3554 template <class _CharT>
3555 typename messages<_CharT>::catalog
3556 messages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const
3558 #ifdef _LIBCPP_HAS_CATOPEN
3559 catalog __cat = (catalog)catopen(__nm.c_str(), NL_CAT_LOCALE);
3561 __cat = static_cast<catalog>((static_cast<size_t>(__cat) >> 1));
3563 #else // !_LIBCPP_HAS_CATOPEN
3564 _LIBCPP_UNUSED_VAR(__nm);
3566 #endif // _LIBCPP_HAS_CATOPEN
3569 template <class _CharT>
3570 typename messages<_CharT>::string_type
3571 messages<_CharT>::do_get(catalog __c, int __set, int __msgid,
3572 const string_type& __dflt) const
3574 #ifdef _LIBCPP_HAS_CATOPEN
3576 __narrow_to_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__ndflt),
3578 __dflt.c_str() + __dflt.size());
3581 nl_catd __cat = (nl_catd)__c;
3582 char* __n = catgets(__cat, __set, __msgid, __ndflt.c_str());
3584 __widen_from_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__w),
3585 __n, __n + strlen(__n));
3587 #else // !_LIBCPP_HAS_CATOPEN
3588 _LIBCPP_UNUSED_VAR(__c);
3589 _LIBCPP_UNUSED_VAR(__set);
3590 _LIBCPP_UNUSED_VAR(__msgid);
3592 #endif // _LIBCPP_HAS_CATOPEN
3595 template <class _CharT>
3597 messages<_CharT>::do_close(catalog __c) const
3599 #ifdef _LIBCPP_HAS_CATOPEN
3602 nl_catd __cat = (nl_catd)__c;
3604 #else // !_LIBCPP_HAS_CATOPEN
3605 _LIBCPP_UNUSED_VAR(__c);
3606 #endif // _LIBCPP_HAS_CATOPEN
3609 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<char>)
3610 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<wchar_t>)
3612 template <class _CharT>
3613 class _LIBCPP_TEMPLATE_VIS messages_byname
3614 : public messages<_CharT>
3617 typedef messages_base::catalog catalog;
3618 typedef basic_string<_CharT> string_type;
3620 _LIBCPP_INLINE_VISIBILITY
3621 explicit messages_byname(const char*, size_t __refs = 0)
3622 : messages<_CharT>(__refs) {}
3624 _LIBCPP_INLINE_VISIBILITY
3625 explicit messages_byname(const string&, size_t __refs = 0)
3626 : messages<_CharT>(__refs) {}
3629 _LIBCPP_INLINE_VISIBILITY
3630 ~messages_byname() {}
3633 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<char>)
3634 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<wchar_t>)
3636 template<class _Codecvt, class _Elem = wchar_t,
3637 class _Wide_alloc = allocator<_Elem>,
3638 class _Byte_alloc = allocator<char> >
3639 class _LIBCPP_TEMPLATE_VIS wstring_convert
3642 typedef basic_string<char, char_traits<char>, _Byte_alloc> byte_string;
3643 typedef basic_string<_Elem, char_traits<_Elem>, _Wide_alloc> wide_string;
3644 typedef typename _Codecvt::state_type state_type;
3645 typedef typename wide_string::traits_type::int_type int_type;
3648 byte_string __byte_err_string_;
3649 wide_string __wide_err_string_;
3650 _Codecvt* __cvtptr_;
3651 state_type __cvtstate_;
3654 wstring_convert(const wstring_convert& __wc);
3655 wstring_convert& operator=(const wstring_convert& __wc);
3657 _LIBCPP_INLINE_VISIBILITY
3658 _LIBCPP_EXPLICIT_AFTER_CXX11 wstring_convert(_Codecvt* __pcvt = new _Codecvt);
3659 _LIBCPP_INLINE_VISIBILITY
3660 wstring_convert(_Codecvt* __pcvt, state_type __state);
3661 _LIBCPP_EXPLICIT_AFTER_CXX11 wstring_convert(const byte_string& __byte_err,
3662 const wide_string& __wide_err = wide_string());
3663 #ifndef _LIBCPP_CXX03_LANG
3664 _LIBCPP_INLINE_VISIBILITY
3665 wstring_convert(wstring_convert&& __wc);
3669 _LIBCPP_INLINE_VISIBILITY
3670 wide_string from_bytes(char __byte)
3671 {return from_bytes(&__byte, &__byte+1);}
3672 _LIBCPP_INLINE_VISIBILITY
3673 wide_string from_bytes(const char* __ptr)
3674 {return from_bytes(__ptr, __ptr + char_traits<char>::length(__ptr));}
3675 _LIBCPP_INLINE_VISIBILITY
3676 wide_string from_bytes(const byte_string& __str)
3677 {return from_bytes(__str.data(), __str.data() + __str.size());}
3678 wide_string from_bytes(const char* __first, const char* __last);
3680 _LIBCPP_INLINE_VISIBILITY
3681 byte_string to_bytes(_Elem __wchar)
3682 {return to_bytes(&__wchar, &__wchar+1);}
3683 _LIBCPP_INLINE_VISIBILITY
3684 byte_string to_bytes(const _Elem* __wptr)
3685 {return to_bytes(__wptr, __wptr + char_traits<_Elem>::length(__wptr));}
3686 _LIBCPP_INLINE_VISIBILITY
3687 byte_string to_bytes(const wide_string& __wstr)
3688 {return to_bytes(__wstr.data(), __wstr.data() + __wstr.size());}
3689 byte_string to_bytes(const _Elem* __first, const _Elem* __last);
3691 _LIBCPP_INLINE_VISIBILITY
3692 size_t converted() const _NOEXCEPT {return __cvtcount_;}
3693 _LIBCPP_INLINE_VISIBILITY
3694 state_type state() const {return __cvtstate_;}
3697 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3699 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3700 wstring_convert(_Codecvt* __pcvt)
3701 : __cvtptr_(__pcvt), __cvtstate_(), __cvtcount_(0)
3705 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3707 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3708 wstring_convert(_Codecvt* __pcvt, state_type __state)
3709 : __cvtptr_(__pcvt), __cvtstate_(__state), __cvtcount_(0)
3713 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3714 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3715 wstring_convert(const byte_string& __byte_err, const wide_string& __wide_err)
3716 : __byte_err_string_(__byte_err), __wide_err_string_(__wide_err),
3717 __cvtstate_(), __cvtcount_(0)
3719 __cvtptr_ = new _Codecvt;
3722 #ifndef _LIBCPP_CXX03_LANG
3724 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3726 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3727 wstring_convert(wstring_convert&& __wc)
3728 : __byte_err_string_(_VSTD::move(__wc.__byte_err_string_)),
3729 __wide_err_string_(_VSTD::move(__wc.__wide_err_string_)),
3730 __cvtptr_(__wc.__cvtptr_),
3731 __cvtstate_(__wc.__cvtstate_), __cvtcount_(__wc.__cvtcount_)
3733 __wc.__cvtptr_ = nullptr;
3736 #endif // _LIBCPP_CXX03_LANG
3738 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3739 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::~wstring_convert()
3744 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3745 typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::wide_string
3746 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3747 from_bytes(const char* __frm, const char* __frm_end)
3750 if (__cvtptr_ != nullptr)
3752 wide_string __ws(2*(__frm_end - __frm), _Elem());
3753 if (__frm != __frm_end)
3754 __ws.resize(__ws.capacity());
3755 codecvt_base::result __r = codecvt_base::ok;
3756 state_type __st = __cvtstate_;
3757 if (__frm != __frm_end)
3759 _Elem* __to = &__ws[0];
3760 _Elem* __to_end = __to + __ws.size();
3761 const char* __frm_nxt;
3765 __r = __cvtptr_->in(__st, __frm, __frm_end, __frm_nxt,
3766 __to, __to_end, __to_nxt);
3767 __cvtcount_ += __frm_nxt - __frm;
3768 if (__frm_nxt == __frm)
3770 __r = codecvt_base::error;
3772 else if (__r == codecvt_base::noconv)
3774 __ws.resize(__to - &__ws[0]);
3775 // This only gets executed if _Elem is char
3776 __ws.append((const _Elem*)__frm, (const _Elem*)__frm_end);
3778 __r = codecvt_base::ok;
3780 else if (__r == codecvt_base::ok)
3782 __ws.resize(__to_nxt - &__ws[0]);
3785 else if (__r == codecvt_base::partial)
3787 ptrdiff_t __s = __to_nxt - &__ws[0];
3788 __ws.resize(2 * __s);
3789 __to = &__ws[0] + __s;
3790 __to_end = &__ws[0] + __ws.size();
3793 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3795 if (__r == codecvt_base::ok)
3799 if (__wide_err_string_.empty())
3800 __throw_range_error("wstring_convert: from_bytes error");
3802 return __wide_err_string_;
3805 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3806 typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::byte_string
3807 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3808 to_bytes(const _Elem* __frm, const _Elem* __frm_end)
3811 if (__cvtptr_ != nullptr)
3813 byte_string __bs(2*(__frm_end - __frm), char());
3814 if (__frm != __frm_end)
3815 __bs.resize(__bs.capacity());
3816 codecvt_base::result __r = codecvt_base::ok;
3817 state_type __st = __cvtstate_;
3818 if (__frm != __frm_end)
3820 char* __to = &__bs[0];
3821 char* __to_end = __to + __bs.size();
3822 const _Elem* __frm_nxt;
3826 __r = __cvtptr_->out(__st, __frm, __frm_end, __frm_nxt,
3827 __to, __to_end, __to_nxt);
3828 __cvtcount_ += __frm_nxt - __frm;
3829 if (__frm_nxt == __frm)
3831 __r = codecvt_base::error;
3833 else if (__r == codecvt_base::noconv)
3835 __bs.resize(__to - &__bs[0]);
3836 // This only gets executed if _Elem is char
3837 __bs.append((const char*)__frm, (const char*)__frm_end);
3839 __r = codecvt_base::ok;
3841 else if (__r == codecvt_base::ok)
3843 __bs.resize(__to_nxt - &__bs[0]);
3846 else if (__r == codecvt_base::partial)
3848 ptrdiff_t __s = __to_nxt - &__bs[0];
3849 __bs.resize(2 * __s);
3850 __to = &__bs[0] + __s;
3851 __to_end = &__bs[0] + __bs.size();
3854 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3856 if (__r == codecvt_base::ok)
3858 size_t __s = __bs.size();
3859 __bs.resize(__bs.capacity());
3860 char* __to = &__bs[0] + __s;
3861 char* __to_end = __to + __bs.size();
3865 __r = __cvtptr_->unshift(__st, __to, __to_end, __to_nxt);
3866 if (__r == codecvt_base::noconv)
3868 __bs.resize(__to - &__bs[0]);
3869 __r = codecvt_base::ok;
3871 else if (__r == codecvt_base::ok)
3873 __bs.resize(__to_nxt - &__bs[0]);
3875 else if (__r == codecvt_base::partial)
3877 ptrdiff_t __sp = __to_nxt - &__bs[0];
3878 __bs.resize(2 * __sp);
3879 __to = &__bs[0] + __sp;
3880 __to_end = &__bs[0] + __bs.size();
3882 } while (__r == codecvt_base::partial);
3883 if (__r == codecvt_base::ok)
3888 if (__byte_err_string_.empty())
3889 __throw_range_error("wstring_convert: to_bytes error");
3891 return __byte_err_string_;
3894 template <class _Codecvt, class _Elem = wchar_t, class _Tr = char_traits<_Elem> >
3895 class _LIBCPP_TEMPLATE_VIS wbuffer_convert
3896 : public basic_streambuf<_Elem, _Tr>
3900 typedef _Elem char_type;
3901 typedef _Tr traits_type;
3902 typedef typename traits_type::int_type int_type;
3903 typedef typename traits_type::pos_type pos_type;
3904 typedef typename traits_type::off_type off_type;
3905 typedef typename _Codecvt::state_type state_type;
3909 const char* __extbufnext_;
3910 const char* __extbufend_;
3911 char __extbuf_min_[8];
3913 char_type* __intbuf_;
3915 streambuf* __bufptr_;
3918 ios_base::openmode __cm_;
3921 bool __always_noconv_;
3923 wbuffer_convert(const wbuffer_convert&);
3924 wbuffer_convert& operator=(const wbuffer_convert&);
3926 _LIBCPP_EXPLICIT_AFTER_CXX11 wbuffer_convert(streambuf* __bytebuf = 0,
3927 _Codecvt* __pcvt = new _Codecvt, state_type __state = state_type());
3930 _LIBCPP_INLINE_VISIBILITY
3931 streambuf* rdbuf() const {return __bufptr_;}
3932 _LIBCPP_INLINE_VISIBILITY
3933 streambuf* rdbuf(streambuf* __bytebuf)
3935 streambuf* __r = __bufptr_;
3936 __bufptr_ = __bytebuf;
3940 _LIBCPP_INLINE_VISIBILITY
3941 state_type state() const {return __st_;}
3944 virtual int_type underflow();
3945 virtual int_type pbackfail(int_type __c = traits_type::eof());
3946 virtual int_type overflow (int_type __c = traits_type::eof());
3947 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s,
3949 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
3950 ios_base::openmode __wch = ios_base::in | ios_base::out);
3951 virtual pos_type seekpos(pos_type __sp,
3952 ios_base::openmode __wch = ios_base::in | ios_base::out);
3957 void __write_mode();
3958 wbuffer_convert* __close();
3961 template <class _Codecvt, class _Elem, class _Tr>
3962 wbuffer_convert<_Codecvt, _Elem, _Tr>::
3963 wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt, state_type __state)
3970 __bufptr_(__bytebuf),
3976 __always_noconv_(__cv_ ? __cv_->always_noconv() : false)
3981 template <class _Codecvt, class _Elem, class _Tr>
3982 wbuffer_convert<_Codecvt, _Elem, _Tr>::~wbuffer_convert()
3987 delete [] __extbuf_;
3989 delete [] __intbuf_;
3992 template <class _Codecvt, class _Elem, class _Tr>
3993 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
3994 wbuffer_convert<_Codecvt, _Elem, _Tr>::underflow()
3996 if (__cv_ == 0 || __bufptr_ == 0)
3997 return traits_type::eof();
3998 bool __initial = __read_mode();
4000 if (this->gptr() == 0)
4001 this->setg(&__1buf, &__1buf+1, &__1buf+1);
4002 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
4003 int_type __c = traits_type::eof();
4004 if (this->gptr() == this->egptr())
4006 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
4007 if (__always_noconv_)
4009 streamsize __nmemb = static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz);
4010 __nmemb = __bufptr_->sgetn((char*)this->eback() + __unget_sz, __nmemb);
4013 this->setg(this->eback(),
4014 this->eback() + __unget_sz,
4015 this->eback() + __unget_sz + __nmemb);
4016 __c = *this->gptr();
4021 _LIBCPP_ASSERT(!(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
4022 if (__extbufend_ != __extbufnext_)
4023 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
4024 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
4025 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
4026 streamsize __nmemb = _VSTD::min(static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz),
4027 static_cast<streamsize>(__extbufend_ - __extbufnext_));
4028 codecvt_base::result __r;
4029 // FIXME: Do we ever need to restore the state here?
4030 //state_type __svs = __st_;
4031 streamsize __nr = __bufptr_->sgetn(const_cast<char*>(__extbufnext_), __nmemb);
4034 __extbufend_ = __extbufnext_ + __nr;
4036 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
4037 this->eback() + __unget_sz,
4038 this->egptr(), __inext);
4039 if (__r == codecvt_base::noconv)
4041 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
4042 (char_type*) const_cast<char *>(__extbufend_));
4043 __c = *this->gptr();
4045 else if (__inext != this->eback() + __unget_sz)
4047 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
4048 __c = *this->gptr();
4054 __c = *this->gptr();
4055 if (this->eback() == &__1buf)
4056 this->setg(0, 0, 0);
4060 template <class _Codecvt, class _Elem, class _Tr>
4061 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4062 wbuffer_convert<_Codecvt, _Elem, _Tr>::pbackfail(int_type __c)
4064 if (__cv_ != 0 && __bufptr_ != 0 && this->eback() < this->gptr())
4066 if (traits_type::eq_int_type(__c, traits_type::eof()))
4069 return traits_type::not_eof(__c);
4071 if (traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
4074 *this->gptr() = traits_type::to_char_type(__c);
4078 return traits_type::eof();
4081 template <class _Codecvt, class _Elem, class _Tr>
4082 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4083 wbuffer_convert<_Codecvt, _Elem, _Tr>::overflow(int_type __c)
4085 if (__cv_ == 0 || __bufptr_ == 0)
4086 return traits_type::eof();
4089 char_type* __pb_save = this->pbase();
4090 char_type* __epb_save = this->epptr();
4091 if (!traits_type::eq_int_type(__c, traits_type::eof()))
4093 if (this->pptr() == 0)
4094 this->setp(&__1buf, &__1buf+1);
4095 *this->pptr() = traits_type::to_char_type(__c);
4098 if (this->pptr() != this->pbase())
4100 if (__always_noconv_)
4102 streamsize __nmemb = static_cast<streamsize>(this->pptr() - this->pbase());
4103 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4104 return traits_type::eof();
4108 char* __extbe = __extbuf_;
4109 codecvt_base::result __r;
4112 const char_type* __e;
4113 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
4114 __extbuf_, __extbuf_ + __ebs_, __extbe);
4115 if (__e == this->pbase())
4116 return traits_type::eof();
4117 if (__r == codecvt_base::noconv)
4119 streamsize __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
4120 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4121 return traits_type::eof();
4123 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
4125 streamsize __nmemb = static_cast<size_t>(__extbe - __extbuf_);
4126 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4127 return traits_type::eof();
4128 if (__r == codecvt_base::partial)
4130 this->setp(const_cast<char_type *>(__e), this->pptr());
4131 this->__pbump(this->epptr() - this->pbase());
4135 return traits_type::eof();
4136 } while (__r == codecvt_base::partial);
4138 this->setp(__pb_save, __epb_save);
4140 return traits_type::not_eof(__c);
4143 template <class _Codecvt, class _Elem, class _Tr>
4144 basic_streambuf<_Elem, _Tr>*
4145 wbuffer_convert<_Codecvt, _Elem, _Tr>::setbuf(char_type* __s, streamsize __n)
4147 this->setg(0, 0, 0);
4150 delete [] __extbuf_;
4152 delete [] __intbuf_;
4154 if (__ebs_ > sizeof(__extbuf_min_))
4156 if (__always_noconv_ && __s)
4158 __extbuf_ = (char*)__s;
4163 __extbuf_ = new char[__ebs_];
4169 __extbuf_ = __extbuf_min_;
4170 __ebs_ = sizeof(__extbuf_min_);
4173 if (!__always_noconv_)
4175 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
4176 if (__s && __ibs_ >= sizeof(__extbuf_min_))
4183 __intbuf_ = new char_type[__ibs_];
4196 template <class _Codecvt, class _Elem, class _Tr>
4197 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4198 wbuffer_convert<_Codecvt, _Elem, _Tr>::seekoff(off_type __off, ios_base::seekdir __way,
4199 ios_base::openmode __om)
4201 int __width = __cv_->encoding();
4202 if (__cv_ == 0 || __bufptr_ == 0 || (__width <= 0 && __off != 0) || sync())
4203 return pos_type(off_type(-1));
4204 // __width > 0 || __off == 0, now check __way
4205 if (__way != ios_base::beg && __way != ios_base::cur && __way != ios_base::end)
4206 return pos_type(off_type(-1));
4207 pos_type __r = __bufptr_->pubseekoff(__width * __off, __way, __om);
4212 template <class _Codecvt, class _Elem, class _Tr>
4213 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4214 wbuffer_convert<_Codecvt, _Elem, _Tr>::seekpos(pos_type __sp, ios_base::openmode __wch)
4216 if (__cv_ == 0 || __bufptr_ == 0 || sync())
4217 return pos_type(off_type(-1));
4218 if (__bufptr_->pubseekpos(__sp, __wch) == pos_type(off_type(-1)))
4219 return pos_type(off_type(-1));
4223 template <class _Codecvt, class _Elem, class _Tr>
4225 wbuffer_convert<_Codecvt, _Elem, _Tr>::sync()
4227 if (__cv_ == 0 || __bufptr_ == 0)
4229 if (__cm_ & ios_base::out)
4231 if (this->pptr() != this->pbase())
4232 if (overflow() == traits_type::eof())
4234 codecvt_base::result __r;
4238 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
4239 streamsize __nmemb = static_cast<streamsize>(__extbe - __extbuf_);
4240 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4242 } while (__r == codecvt_base::partial);
4243 if (__r == codecvt_base::error)
4245 if (__bufptr_->pubsync())
4248 else if (__cm_ & ios_base::in)
4251 if (__always_noconv_)
4252 __c = this->egptr() - this->gptr();
4255 int __width = __cv_->encoding();
4256 __c = __extbufend_ - __extbufnext_;
4258 __c += __width * (this->egptr() - this->gptr());
4261 if (this->gptr() != this->egptr())
4263 reverse(this->gptr(), this->egptr());
4264 codecvt_base::result __r;
4265 const char_type* __e = this->gptr();
4269 __r = __cv_->out(__st_, __e, this->egptr(), __e,
4270 __extbuf_, __extbuf_ + __ebs_, __extbe);
4273 case codecvt_base::noconv:
4274 __c += this->egptr() - this->gptr();
4276 case codecvt_base::ok:
4277 case codecvt_base::partial:
4278 __c += __extbe - __extbuf_;
4283 } while (__r == codecvt_base::partial);
4287 if (__bufptr_->pubseekoff(-__c, ios_base::cur, __cm_) == pos_type(off_type(-1)))
4289 this->setg(0, 0, 0);
4295 template <class _Codecvt, class _Elem, class _Tr>
4297 wbuffer_convert<_Codecvt, _Elem, _Tr>::__read_mode()
4299 if (!(__cm_ & ios_base::in))
4302 if (__always_noconv_)
4303 this->setg((char_type*)__extbuf_,
4304 (char_type*)__extbuf_ + __ebs_,
4305 (char_type*)__extbuf_ + __ebs_);
4307 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
4308 __cm_ = ios_base::in;
4314 template <class _Codecvt, class _Elem, class _Tr>
4316 wbuffer_convert<_Codecvt, _Elem, _Tr>::__write_mode()
4318 if (!(__cm_ & ios_base::out))
4320 this->setg(0, 0, 0);
4321 if (__ebs_ > sizeof(__extbuf_min_))
4323 if (__always_noconv_)
4324 this->setp((char_type*)__extbuf_,
4325 (char_type*)__extbuf_ + (__ebs_ - 1));
4327 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
4331 __cm_ = ios_base::out;
4335 template <class _Codecvt, class _Elem, class _Tr>
4336 wbuffer_convert<_Codecvt, _Elem, _Tr>*
4337 wbuffer_convert<_Codecvt, _Elem, _Tr>::__close()
4339 wbuffer_convert* __rt = 0;
4340 if (__cv_ != 0 && __bufptr_ != 0)
4343 if ((__cm_ & ios_base::out) && sync())
4349 _LIBCPP_END_NAMESPACE_STD
4353 #endif // _LIBCPP_LOCALE