2 //===-------------------------- locale ------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_LOCALE
12 #define _LIBCPP_LOCALE
28 static const category // values assigned here are for exposition only
36 all = collate | ctype | monetary | numeric | time | messages;
38 // construct/copy/destroy:
40 locale(const locale& other) noexcept;
41 explicit locale(const char* std_name);
42 explicit locale(const string& std_name);
43 locale(const locale& other, const char* std_name, category);
44 locale(const locale& other, const string& std_name, category);
45 template <class Facet> locale(const locale& other, Facet* f);
46 locale(const locale& other, const locale& one, category);
48 ~locale(); // not virtual
50 const locale& operator=(const locale& other) noexcept;
52 template <class Facet> locale combine(const locale& other) const;
55 basic_string<char> name() const;
56 bool operator==(const locale& other) const;
57 bool operator!=(const locale& other) const;
58 template <class charT, class Traits, class Allocator>
59 bool operator()(const basic_string<charT,Traits,Allocator>& s1,
60 const basic_string<charT,Traits,Allocator>& s2) const;
62 // global locale objects:
63 static locale global(const locale&);
64 static const locale& classic();
67 template <class Facet> const Facet& use_facet(const locale&);
68 template <class Facet> bool has_facet(const locale&) noexcept;
70 // 22.3.3, convenience interfaces:
71 template <class charT> bool isspace (charT c, const locale& loc);
72 template <class charT> bool isprint (charT c, const locale& loc);
73 template <class charT> bool iscntrl (charT c, const locale& loc);
74 template <class charT> bool isupper (charT c, const locale& loc);
75 template <class charT> bool islower (charT c, const locale& loc);
76 template <class charT> bool isalpha (charT c, const locale& loc);
77 template <class charT> bool isdigit (charT c, const locale& loc);
78 template <class charT> bool ispunct (charT c, const locale& loc);
79 template <class charT> bool isxdigit(charT c, const locale& loc);
80 template <class charT> bool isalnum (charT c, const locale& loc);
81 template <class charT> bool isgraph (charT c, const locale& loc);
82 template <class charT> charT toupper(charT c, const locale& loc);
83 template <class charT> charT tolower(charT c, const locale& loc);
85 template<class Codecvt, class Elem = wchar_t,
86 class Wide_alloc = allocator<Elem>,
87 class Byte_alloc = allocator<char>>
91 typedef basic_string<char, char_traits<char>, Byte_alloc> byte_string;
92 typedef basic_string<Elem, char_traits<Elem>, Wide_alloc> wide_string;
93 typedef typename Codecvt::state_type state_type;
94 typedef typename wide_string::traits_type::int_type int_type;
96 explicit wstring_convert(Codecvt* pcvt = new Codecvt); // explicit in C++14
97 wstring_convert(Codecvt* pcvt, state_type state);
98 explicit wstring_convert(const byte_string& byte_err, // explicit in C++14
99 const wide_string& wide_err = wide_string());
100 wstring_convert(const wstring_convert&) = delete; // C++14
101 wstring_convert & operator=(const wstring_convert &) = delete; // C++14
104 wide_string from_bytes(char byte);
105 wide_string from_bytes(const char* ptr);
106 wide_string from_bytes(const byte_string& str);
107 wide_string from_bytes(const char* first, const char* last);
109 byte_string to_bytes(Elem wchar);
110 byte_string to_bytes(const Elem* wptr);
111 byte_string to_bytes(const wide_string& wstr);
112 byte_string to_bytes(const Elem* first, const Elem* last);
114 size_t converted() const; // noexcept in C++14
115 state_type state() const;
118 template <class Codecvt, class Elem = wchar_t, class Tr = char_traits<Elem>>
119 class wbuffer_convert
120 : public basic_streambuf<Elem, Tr>
123 typedef typename Tr::state_type state_type;
125 explicit wbuffer_convert(streambuf* bytebuf = 0, Codecvt* pcvt = new Codecvt,
126 state_type state = state_type()); // explicit in C++14
127 wbuffer_convert(const wbuffer_convert&) = delete; // C++14
128 wbuffer_convert & operator=(const wbuffer_convert &) = delete; // C++14
129 ~wbuffer_convert(); // C++14
131 streambuf* rdbuf() const;
132 streambuf* rdbuf(streambuf* bytebuf);
134 state_type state() const;
137 // 22.4.1 and 22.4.1.3, ctype:
139 template <class charT> class ctype;
140 template <> class ctype<char>; // specialization
141 template <class charT> class ctype_byname;
142 template <> class ctype_byname<char>; // specialization
145 template <class internT, class externT, class stateT> class codecvt;
146 template <class internT, class externT, class stateT> class codecvt_byname;
148 // 22.4.2 and 22.4.3, numeric:
149 template <class charT, class InputIterator> class num_get;
150 template <class charT, class OutputIterator> class num_put;
151 template <class charT> class numpunct;
152 template <class charT> class numpunct_byname;
154 // 22.4.4, col lation:
155 template <class charT> class collate;
156 template <class charT> class collate_byname;
158 // 22.4.5, date and time:
160 template <class charT, class InputIterator> class time_get;
161 template <class charT, class InputIterator> class time_get_byname;
162 template <class charT, class OutputIterator> class time_put;
163 template <class charT, class OutputIterator> class time_put_byname;
167 template <class charT, class InputIterator> class money_get;
168 template <class charT, class OutputIterator> class money_put;
169 template <class charT, bool Intl> class moneypunct;
170 template <class charT, bool Intl> class moneypunct_byname;
172 // 22.4.7, message retrieval:
174 template <class charT> class messages;
175 template <class charT> class messages_byname;
197 #ifdef _LIBCPP_HAS_CATOPEN
198 #include <nl_types.h>
202 #include <Availability.h>
205 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
206 #include <__bsd_locale_defaults.h>
208 #include <__bsd_locale_fallbacks.h>
211 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
212 #pragma GCC system_header
216 #include <__undef_macros>
219 _LIBCPP_BEGIN_NAMESPACE_STD
221 #if defined(__APPLE__) || defined(__FreeBSD__)
222 # define _LIBCPP_GET_C_LOCALE 0
223 #elif defined(__CloudABI__) || defined(__NetBSD__)
224 # define _LIBCPP_GET_C_LOCALE LC_C_LOCALE
226 # define _LIBCPP_GET_C_LOCALE __cloc()
227 // Get the C locale object
228 _LIBCPP_FUNC_VIS locale_t __cloc();
229 #define __cloc_defined
233 // Scans [__b, __e) until a match is found in the basic_strings range
234 // [__kb, __ke) or until it can be shown that there is no match in [__kb, __ke).
235 // __b will be incremented (visibly), consuming CharT until a match is found
236 // or proved to not exist. A keyword may be "", in which will match anything.
237 // If one keyword is a prefix of another, and the next CharT in the input
238 // might match another keyword, the algorithm will attempt to find the longest
239 // matching keyword. If the longer matching keyword ends up not matching, then
240 // no keyword match is found. If no keyword match is found, __ke is returned
241 // and failbit is set in __err.
242 // Else an iterator pointing to the matching keyword is found. If more than
243 // one keyword matches, an iterator to the first matching keyword is returned.
244 // If on exit __b == __e, eofbit is set in __err. If __case_sensitive is false,
245 // __ct is used to force to lower case before comparing characters.
247 // Keywords: "a", "abb"
248 // If the input is "a", the first keyword matches and eofbit is set.
249 // If the input is "abc", no match is found and "ab" are consumed.
250 template <class _InputIterator, class _ForwardIterator, class _Ctype>
253 __scan_keyword(_InputIterator& __b, _InputIterator __e,
254 _ForwardIterator __kb, _ForwardIterator __ke,
255 const _Ctype& __ct, ios_base::iostate& __err,
256 bool __case_sensitive = true)
258 typedef typename iterator_traits<_InputIterator>::value_type _CharT;
259 size_t __nkw = static_cast<size_t>(_VSTD::distance(__kb, __ke));
260 const unsigned char __doesnt_match = '\0';
261 const unsigned char __might_match = '\1';
262 const unsigned char __does_match = '\2';
263 unsigned char __statbuf[100];
264 unsigned char* __status = __statbuf;
265 unique_ptr<unsigned char, void(*)(void*)> __stat_hold(0, free);
266 if (__nkw > sizeof(__statbuf))
268 __status = (unsigned char*)malloc(__nkw);
271 __stat_hold.reset(__status);
273 size_t __n_might_match = __nkw; // At this point, any keyword might match
274 size_t __n_does_match = 0; // but none of them definitely do
275 // Initialize all statuses to __might_match, except for "" keywords are __does_match
276 unsigned char* __st = __status;
277 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
280 *__st = __might_match;
283 *__st = __does_match;
288 // While there might be a match, test keywords against the next CharT
289 for (size_t __indx = 0; __b != __e && __n_might_match > 0; ++__indx)
291 // Peek at the next CharT but don't consume it
293 if (!__case_sensitive)
294 __c = __ct.toupper(__c);
295 bool __consume = false;
296 // For each keyword which might match, see if the __indx character is __c
297 // If a match if found, consume __c
298 // If a match is found, and that is the last character in the keyword,
299 // then that keyword matches.
300 // If the keyword doesn't match this character, then change the keyword
303 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
305 if (*__st == __might_match)
307 _CharT __kc = (*__ky)[__indx];
308 if (!__case_sensitive)
309 __kc = __ct.toupper(__kc);
313 if (__ky->size() == __indx+1)
315 *__st = __does_match;
322 *__st = __doesnt_match;
327 // consume if we matched a character
331 // If we consumed a character and there might be a matched keyword that
332 // was marked matched on a previous iteration, then such keywords
333 // which are now marked as not matching.
334 if (__n_might_match + __n_does_match > 1)
337 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
339 if (*__st == __does_match && __ky->size() != __indx+1)
341 *__st = __doesnt_match;
348 // We've exited the loop because we hit eof and/or we have no more "might matches".
350 __err |= ios_base::eofbit;
351 // Return the first matching result
352 for (__st = __status; __kb != __ke; ++__kb, (void) ++__st)
353 if (*__st == __does_match)
356 __err |= ios_base::failbit;
360 struct _LIBCPP_TYPE_VIS __num_get_base
362 static const int __num_get_buf_sz = 40;
364 static int __get_base(ios_base&);
365 static const char __src[33];
369 void __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end,
370 ios_base::iostate& __err);
372 template <class _CharT>
374 : protected __num_get_base
376 static string __stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
377 _CharT& __thousands_sep);
379 static int __stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp,
380 char* __a, char*& __a_end,
381 _CharT __decimal_point, _CharT __thousands_sep,
382 const string& __grouping, unsigned* __g,
383 unsigned*& __g_end, unsigned& __dc, _CharT* __atoms);
384 #ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
385 static string __stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep);
386 static int __stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
387 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
388 unsigned* __g, unsigned*& __g_end, _CharT* __atoms);
391 static string __stage2_int_prep(ios_base& __iob, _CharT& __thousands_sep)
393 locale __loc = __iob.getloc();
394 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
395 __thousands_sep = __np.thousands_sep();
396 return __np.grouping();
399 const _CharT* __do_widen(ios_base& __iob, _CharT* __atoms) const
401 return __do_widen_p(__iob, __atoms);
405 static int __stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
406 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
407 unsigned* __g, unsigned*& __g_end, const _CharT* __atoms);
410 const T* __do_widen_p(ios_base& __iob, T* __atoms) const
412 locale __loc = __iob.getloc();
413 use_facet<ctype<T> >(__loc).widen(__src, __src + 26, __atoms);
417 const char* __do_widen_p(ios_base& __iob, char* __atoms) const
426 #ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
427 template <class _CharT>
429 __num_get<_CharT>::__stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep)
431 locale __loc = __iob.getloc();
432 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 26, __atoms);
433 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
434 __thousands_sep = __np.thousands_sep();
435 return __np.grouping();
439 template <class _CharT>
441 __num_get<_CharT>::__stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
442 _CharT& __thousands_sep)
444 locale __loc = __iob.getloc();
445 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 32, __atoms);
446 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
447 __decimal_point = __np.decimal_point();
448 __thousands_sep = __np.thousands_sep();
449 return __np.grouping();
452 template <class _CharT>
454 #ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
455 __num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
456 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
457 unsigned* __g, unsigned*& __g_end, _CharT* __atoms)
459 __num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
460 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
461 unsigned* __g, unsigned*& __g_end, const _CharT* __atoms)
465 if (__a_end == __a && (__ct == __atoms[24] || __ct == __atoms[25]))
467 *__a_end++ = __ct == __atoms[24] ? '+' : '-';
471 if (__grouping.size() != 0 && __ct == __thousands_sep)
473 if (__g_end-__g < __num_get_buf_sz)
480 ptrdiff_t __f = find(__atoms, __atoms + 26, __ct) - __atoms;
493 if (__a_end != __a && __a_end - __a <= 2 && __a_end[-1] == '0')
496 *__a_end++ = __src[__f];
501 *__a_end++ = __src[__f];
506 template <class _CharT>
508 __num_get<_CharT>::__stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp, char* __a, char*& __a_end,
509 _CharT __decimal_point, _CharT __thousands_sep, const string& __grouping,
510 unsigned* __g, unsigned*& __g_end, unsigned& __dc, _CharT* __atoms)
512 if (__ct == __decimal_point)
518 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
522 if (__ct == __thousands_sep && __grouping.size() != 0)
526 if (__g_end-__g < __num_get_buf_sz)
533 ptrdiff_t __f = find(__atoms, __atoms + 32, __ct) - __atoms;
536 char __x = __src[__f];
537 if (__x == '-' || __x == '+')
539 if (__a_end == __a || (__a_end[-1] & 0x5F) == (__exp & 0x7F))
546 if (__x == 'x' || __x == 'X')
548 else if ((__x & 0x5F) == __exp)
554 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
565 _LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<char>)
566 _LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<wchar_t>)
568 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
569 class _LIBCPP_TEMPLATE_VIS num_get
570 : public locale::facet,
571 private __num_get<_CharT>
574 typedef _CharT char_type;
575 typedef _InputIterator iter_type;
577 _LIBCPP_INLINE_VISIBILITY
578 explicit num_get(size_t __refs = 0)
579 : locale::facet(__refs) {}
581 _LIBCPP_INLINE_VISIBILITY
582 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
583 ios_base::iostate& __err, bool& __v) const
585 return do_get(__b, __e, __iob, __err, __v);
588 _LIBCPP_INLINE_VISIBILITY
589 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
590 ios_base::iostate& __err, long& __v) const
592 return do_get(__b, __e, __iob, __err, __v);
595 _LIBCPP_INLINE_VISIBILITY
596 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
597 ios_base::iostate& __err, long long& __v) const
599 return do_get(__b, __e, __iob, __err, __v);
602 _LIBCPP_INLINE_VISIBILITY
603 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
604 ios_base::iostate& __err, unsigned short& __v) const
606 return do_get(__b, __e, __iob, __err, __v);
609 _LIBCPP_INLINE_VISIBILITY
610 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
611 ios_base::iostate& __err, unsigned int& __v) const
613 return do_get(__b, __e, __iob, __err, __v);
616 _LIBCPP_INLINE_VISIBILITY
617 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
618 ios_base::iostate& __err, unsigned long& __v) const
620 return do_get(__b, __e, __iob, __err, __v);
623 _LIBCPP_INLINE_VISIBILITY
624 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
625 ios_base::iostate& __err, unsigned long long& __v) const
627 return do_get(__b, __e, __iob, __err, __v);
630 _LIBCPP_INLINE_VISIBILITY
631 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
632 ios_base::iostate& __err, float& __v) const
634 return do_get(__b, __e, __iob, __err, __v);
637 _LIBCPP_INLINE_VISIBILITY
638 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
639 ios_base::iostate& __err, double& __v) const
641 return do_get(__b, __e, __iob, __err, __v);
644 _LIBCPP_INLINE_VISIBILITY
645 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
646 ios_base::iostate& __err, long double& __v) const
648 return do_get(__b, __e, __iob, __err, __v);
651 _LIBCPP_INLINE_VISIBILITY
652 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
653 ios_base::iostate& __err, void*& __v) const
655 return do_get(__b, __e, __iob, __err, __v);
658 static locale::id id;
661 _LIBCPP_INLINE_VISIBILITY
665 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
666 iter_type __do_get_floating_point
667 (iter_type __b, iter_type __e, ios_base& __iob,
668 ios_base::iostate& __err, _Fp& __v) const;
670 template <class _Signed>
671 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
672 iter_type __do_get_signed
673 (iter_type __b, iter_type __e, ios_base& __iob,
674 ios_base::iostate& __err, _Signed& __v) const;
676 template <class _Unsigned>
677 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
678 iter_type __do_get_unsigned
679 (iter_type __b, iter_type __e, ios_base& __iob,
680 ios_base::iostate& __err, _Unsigned& __v) const;
683 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
684 ios_base::iostate& __err, bool& __v) const;
686 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
687 ios_base::iostate& __err, long& __v) const
688 { return this->__do_get_signed ( __b, __e, __iob, __err, __v ); }
690 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
691 ios_base::iostate& __err, long long& __v) const
692 { return this->__do_get_signed ( __b, __e, __iob, __err, __v ); }
694 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
695 ios_base::iostate& __err, unsigned short& __v) const
696 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
698 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
699 ios_base::iostate& __err, unsigned int& __v) const
700 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
702 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
703 ios_base::iostate& __err, unsigned long& __v) const
704 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
706 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
707 ios_base::iostate& __err, unsigned long long& __v) const
708 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
710 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
711 ios_base::iostate& __err, float& __v) const
712 { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
714 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
715 ios_base::iostate& __err, double& __v) const
716 { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
718 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
719 ios_base::iostate& __err, long double& __v) const
720 { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
722 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
723 ios_base::iostate& __err, void*& __v) const;
726 template <class _CharT, class _InputIterator>
728 num_get<_CharT, _InputIterator>::id;
732 __num_get_signed_integral(const char* __a, const char* __a_end,
733 ios_base::iostate& __err, int __base)
737 typename remove_reference<decltype(errno)>::type __save_errno = errno;
740 long long __ll = strtoll_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
741 typename remove_reference<decltype(errno)>::type __current_errno = errno;
742 if (__current_errno == 0)
743 errno = __save_errno;
746 __err = ios_base::failbit;
749 else if (__current_errno == ERANGE ||
750 __ll < numeric_limits<_Tp>::min() ||
751 numeric_limits<_Tp>::max() < __ll)
753 __err = ios_base::failbit;
755 return numeric_limits<_Tp>::max();
757 return numeric_limits<_Tp>::min();
759 return static_cast<_Tp>(__ll);
761 __err = ios_base::failbit;
767 __num_get_unsigned_integral(const char* __a, const char* __a_end,
768 ios_base::iostate& __err, int __base)
772 const bool __negate = *__a == '-';
773 if (__negate && ++__a == __a_end) {
774 __err = ios_base::failbit;
777 typename remove_reference<decltype(errno)>::type __save_errno = errno;
780 unsigned long long __ll = strtoull_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
781 typename remove_reference<decltype(errno)>::type __current_errno = errno;
782 if (__current_errno == 0)
783 errno = __save_errno;
786 __err = ios_base::failbit;
789 else if (__current_errno == ERANGE || numeric_limits<_Tp>::max() < __ll)
791 __err = ios_base::failbit;
792 return numeric_limits<_Tp>::max();
794 _Tp __res = static_cast<_Tp>(__ll);
795 if (__negate) __res = -__res;
798 __err = ios_base::failbit;
803 _LIBCPP_INLINE_VISIBILITY
804 _Tp __do_strtod(const char* __a, char** __p2);
807 inline _LIBCPP_INLINE_VISIBILITY
808 float __do_strtod<float>(const char* __a, char** __p2) {
809 return strtof_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
813 inline _LIBCPP_INLINE_VISIBILITY
814 double __do_strtod<double>(const char* __a, char** __p2) {
815 return strtod_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
819 inline _LIBCPP_INLINE_VISIBILITY
820 long double __do_strtod<long double>(const char* __a, char** __p2) {
821 return strtold_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
827 __num_get_float(const char* __a, const char* __a_end, ios_base::iostate& __err)
831 typename remove_reference<decltype(errno)>::type __save_errno = errno;
834 _Tp __ld = __do_strtod<_Tp>(__a, &__p2);
835 typename remove_reference<decltype(errno)>::type __current_errno = errno;
836 if (__current_errno == 0)
837 errno = __save_errno;
840 __err = ios_base::failbit;
843 else if (__current_errno == ERANGE)
844 __err = ios_base::failbit;
847 __err = ios_base::failbit;
851 template <class _CharT, class _InputIterator>
853 num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
855 ios_base::iostate& __err,
858 if ((__iob.flags() & ios_base::boolalpha) == 0)
861 __b = do_get(__b, __e, __iob, __err, __lv);
872 __err = ios_base::failbit;
877 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__iob.getloc());
878 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__iob.getloc());
879 typedef typename numpunct<_CharT>::string_type string_type;
880 const string_type __names[2] = {__np.truename(), __np.falsename()};
881 const string_type* __i = __scan_keyword(__b, __e, __names, __names+2,
883 __v = __i == __names;
889 template <class _CharT, class _InputIterator>
890 template <class _Signed>
892 num_get<_CharT, _InputIterator>::__do_get_signed(iter_type __b, iter_type __e,
894 ios_base::iostate& __err,
898 int __base = this->__get_base(__iob);
900 char_type __thousands_sep;
901 const int __atoms_size = 26;
902 #ifdef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
903 char_type __atoms1[__atoms_size];
904 const char_type *__atoms = this->__do_widen(__iob, __atoms1);
905 string __grouping = this->__stage2_int_prep(__iob, __thousands_sep);
907 char_type __atoms[__atoms_size];
908 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
911 __buf.resize(__buf.capacity());
912 char* __a = &__buf[0];
914 unsigned __g[__num_get_base::__num_get_buf_sz];
915 unsigned* __g_end = __g;
917 for (; __b != __e; ++__b)
919 if (__a_end == __a + __buf.size())
921 size_t __tmp = __buf.size();
922 __buf.resize(2*__buf.size());
923 __buf.resize(__buf.capacity());
925 __a_end = __a + __tmp;
927 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
928 __thousands_sep, __grouping, __g, __g_end,
932 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
935 __v = __num_get_signed_integral<_Signed>(__a, __a_end, __err, __base);
936 // Digit grouping checked
937 __check_grouping(__grouping, __g, __g_end, __err);
940 __err |= ios_base::eofbit;
946 template <class _CharT, class _InputIterator>
947 template <class _Unsigned>
949 num_get<_CharT, _InputIterator>::__do_get_unsigned(iter_type __b, iter_type __e,
951 ios_base::iostate& __err,
952 _Unsigned& __v) const
955 int __base = this->__get_base(__iob);
957 char_type __thousands_sep;
958 const int __atoms_size = 26;
959 #ifdef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
960 char_type __atoms1[__atoms_size];
961 const char_type *__atoms = this->__do_widen(__iob, __atoms1);
962 string __grouping = this->__stage2_int_prep(__iob, __thousands_sep);
964 char_type __atoms[__atoms_size];
965 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
968 __buf.resize(__buf.capacity());
969 char* __a = &__buf[0];
971 unsigned __g[__num_get_base::__num_get_buf_sz];
972 unsigned* __g_end = __g;
974 for (; __b != __e; ++__b)
976 if (__a_end == __a + __buf.size())
978 size_t __tmp = __buf.size();
979 __buf.resize(2*__buf.size());
980 __buf.resize(__buf.capacity());
982 __a_end = __a + __tmp;
984 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
985 __thousands_sep, __grouping, __g, __g_end,
989 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
992 __v = __num_get_unsigned_integral<_Unsigned>(__a, __a_end, __err, __base);
993 // Digit grouping checked
994 __check_grouping(__grouping, __g, __g_end, __err);
997 __err |= ios_base::eofbit;
1003 template <class _CharT, class _InputIterator>
1004 template <class _Fp>
1006 num_get<_CharT, _InputIterator>::__do_get_floating_point(iter_type __b, iter_type __e,
1008 ios_base::iostate& __err,
1011 // Stage 1, nothing to do
1013 char_type __atoms[32];
1014 char_type __decimal_point;
1015 char_type __thousands_sep;
1016 string __grouping = this->__stage2_float_prep(__iob, __atoms,
1020 __buf.resize(__buf.capacity());
1021 char* __a = &__buf[0];
1022 char* __a_end = __a;
1023 unsigned __g[__num_get_base::__num_get_buf_sz];
1024 unsigned* __g_end = __g;
1026 bool __in_units = true;
1028 for (; __b != __e; ++__b)
1030 if (__a_end == __a + __buf.size())
1032 size_t __tmp = __buf.size();
1033 __buf.resize(2*__buf.size());
1034 __buf.resize(__buf.capacity());
1036 __a_end = __a + __tmp;
1038 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1039 __decimal_point, __thousands_sep,
1040 __grouping, __g, __g_end,
1044 if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1047 __v = __num_get_float<_Fp>(__a, __a_end, __err);
1048 // Digit grouping checked
1049 __check_grouping(__grouping, __g, __g_end, __err);
1052 __err |= ios_base::eofbit;
1056 template <class _CharT, class _InputIterator>
1058 num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1060 ios_base::iostate& __err,
1066 char_type __atoms[26];
1067 char_type __thousands_sep = 0;
1069 use_facet<ctype<_CharT> >(__iob.getloc()).widen(__num_get_base::__src,
1070 __num_get_base::__src + 26, __atoms);
1072 __buf.resize(__buf.capacity());
1073 char* __a = &__buf[0];
1074 char* __a_end = __a;
1075 unsigned __g[__num_get_base::__num_get_buf_sz];
1076 unsigned* __g_end = __g;
1078 for (; __b != __e; ++__b)
1080 if (__a_end == __a + __buf.size())
1082 size_t __tmp = __buf.size();
1083 __buf.resize(2*__buf.size());
1084 __buf.resize(__buf.capacity());
1086 __a_end = __a + __tmp;
1088 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1089 __thousands_sep, __grouping,
1090 __g, __g_end, __atoms))
1094 __buf.resize(__a_end - __a);
1095 if (__libcpp_sscanf_l(__buf.c_str(), _LIBCPP_GET_C_LOCALE, "%p", &__v) != 1)
1096 __err = ios_base::failbit;
1099 __err |= ios_base::eofbit;
1103 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<char>)
1104 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<wchar_t>)
1106 struct _LIBCPP_TYPE_VIS __num_put_base
1109 static void __format_int(char* __fmt, const char* __len, bool __signd,
1110 ios_base::fmtflags __flags);
1111 static bool __format_float(char* __fmt, const char* __len,
1112 ios_base::fmtflags __flags);
1113 static char* __identify_padding(char* __nb, char* __ne,
1114 const ios_base& __iob);
1117 template <class _CharT>
1119 : protected __num_put_base
1121 static void __widen_and_group_int(char* __nb, char* __np, char* __ne,
1122 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1123 const locale& __loc);
1124 static void __widen_and_group_float(char* __nb, char* __np, char* __ne,
1125 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1126 const locale& __loc);
1129 template <class _CharT>
1131 __num_put<_CharT>::__widen_and_group_int(char* __nb, char* __np, char* __ne,
1132 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1133 const locale& __loc)
1135 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1136 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1137 string __grouping = __npt.grouping();
1138 if (__grouping.empty())
1140 __ct.widen(__nb, __ne, __ob);
1141 __oe = __ob + (__ne - __nb);
1147 if (*__nf == '-' || *__nf == '+')
1148 *__oe++ = __ct.widen(*__nf++);
1149 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1152 *__oe++ = __ct.widen(*__nf++);
1153 *__oe++ = __ct.widen(*__nf++);
1155 reverse(__nf, __ne);
1156 _CharT __thousands_sep = __npt.thousands_sep();
1159 for (char* __p = __nf; __p < __ne; ++__p)
1161 if (static_cast<unsigned>(__grouping[__dg]) > 0 &&
1162 __dc == static_cast<unsigned>(__grouping[__dg]))
1164 *__oe++ = __thousands_sep;
1166 if (__dg < __grouping.size()-1)
1169 *__oe++ = __ct.widen(*__p);
1172 reverse(__ob + (__nf - __nb), __oe);
1177 __op = __ob + (__np - __nb);
1180 template <class _CharT>
1182 __num_put<_CharT>::__widen_and_group_float(char* __nb, char* __np, char* __ne,
1183 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1184 const locale& __loc)
1186 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1187 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1188 string __grouping = __npt.grouping();
1191 if (*__nf == '-' || *__nf == '+')
1192 *__oe++ = __ct.widen(*__nf++);
1194 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1197 *__oe++ = __ct.widen(*__nf++);
1198 *__oe++ = __ct.widen(*__nf++);
1199 for (__ns = __nf; __ns < __ne; ++__ns)
1200 if (!isxdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
1205 for (__ns = __nf; __ns < __ne; ++__ns)
1206 if (!isdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
1209 if (__grouping.empty())
1211 __ct.widen(__nf, __ns, __oe);
1212 __oe += __ns - __nf;
1216 reverse(__nf, __ns);
1217 _CharT __thousands_sep = __npt.thousands_sep();
1220 for (char* __p = __nf; __p < __ns; ++__p)
1222 if (__grouping[__dg] > 0 && __dc == static_cast<unsigned>(__grouping[__dg]))
1224 *__oe++ = __thousands_sep;
1226 if (__dg < __grouping.size()-1)
1229 *__oe++ = __ct.widen(*__p);
1232 reverse(__ob + (__nf - __nb), __oe);
1234 for (__nf = __ns; __nf < __ne; ++__nf)
1238 *__oe++ = __npt.decimal_point();
1243 *__oe++ = __ct.widen(*__nf);
1245 __ct.widen(__nf, __ne, __oe);
1246 __oe += __ne - __nf;
1250 __op = __ob + (__np - __nb);
1253 _LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<char>)
1254 _LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<wchar_t>)
1256 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
1257 class _LIBCPP_TEMPLATE_VIS num_put
1258 : public locale::facet,
1259 private __num_put<_CharT>
1262 typedef _CharT char_type;
1263 typedef _OutputIterator iter_type;
1265 _LIBCPP_INLINE_VISIBILITY
1266 explicit num_put(size_t __refs = 0)
1267 : locale::facet(__refs) {}
1269 _LIBCPP_INLINE_VISIBILITY
1270 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1273 return do_put(__s, __iob, __fl, __v);
1276 _LIBCPP_INLINE_VISIBILITY
1277 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1280 return do_put(__s, __iob, __fl, __v);
1283 _LIBCPP_INLINE_VISIBILITY
1284 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1285 long long __v) const
1287 return do_put(__s, __iob, __fl, __v);
1290 _LIBCPP_INLINE_VISIBILITY
1291 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1292 unsigned long __v) const
1294 return do_put(__s, __iob, __fl, __v);
1297 _LIBCPP_INLINE_VISIBILITY
1298 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1299 unsigned long long __v) const
1301 return do_put(__s, __iob, __fl, __v);
1304 _LIBCPP_INLINE_VISIBILITY
1305 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1308 return do_put(__s, __iob, __fl, __v);
1311 _LIBCPP_INLINE_VISIBILITY
1312 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1313 long double __v) const
1315 return do_put(__s, __iob, __fl, __v);
1318 _LIBCPP_INLINE_VISIBILITY
1319 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1320 const void* __v) const
1322 return do_put(__s, __iob, __fl, __v);
1325 static locale::id id;
1328 _LIBCPP_INLINE_VISIBILITY
1331 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1333 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1335 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1336 long long __v) const;
1337 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1338 unsigned long) const;
1339 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1340 unsigned long long) const;
1341 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1343 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1344 long double __v) const;
1345 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1346 const void* __v) const;
1349 template <class _CharT, class _OutputIterator>
1351 num_put<_CharT, _OutputIterator>::id;
1353 template <class _CharT, class _OutputIterator>
1356 __pad_and_output(_OutputIterator __s,
1357 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1358 ios_base& __iob, _CharT __fl)
1360 streamsize __sz = __oe - __ob;
1361 streamsize __ns = __iob.width();
1366 for (;__ob < __op; ++__ob, ++__s)
1368 for (; __ns; --__ns, ++__s)
1370 for (; __ob < __oe; ++__ob, ++__s)
1376 #if !defined(__APPLE__) || \
1377 (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED > __MAC_10_8) || \
1378 (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_6_0)
1380 template <class _CharT, class _Traits>
1382 ostreambuf_iterator<_CharT, _Traits>
1383 __pad_and_output(ostreambuf_iterator<_CharT, _Traits> __s,
1384 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1385 ios_base& __iob, _CharT __fl)
1387 if (__s.__sbuf_ == nullptr)
1389 streamsize __sz = __oe - __ob;
1390 streamsize __ns = __iob.width();
1395 streamsize __np = __op - __ob;
1398 if (__s.__sbuf_->sputn(__ob, __np) != __np)
1400 __s.__sbuf_ = nullptr;
1406 basic_string<_CharT, _Traits> __sp(__ns, __fl);
1407 if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns)
1409 __s.__sbuf_ = nullptr;
1416 if (__s.__sbuf_->sputn(__op, __np) != __np)
1418 __s.__sbuf_ = nullptr;
1428 template <class _CharT, class _OutputIterator>
1430 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1431 char_type __fl, bool __v) const
1433 if ((__iob.flags() & ios_base::boolalpha) == 0)
1434 return do_put(__s, __iob, __fl, (unsigned long)__v);
1435 const numpunct<char_type>& __np = use_facet<numpunct<char_type> >(__iob.getloc());
1436 typedef typename numpunct<char_type>::string_type string_type;
1437 #if _LIBCPP_DEBUG_LEVEL >= 2
1438 string_type __tmp(__v ? __np.truename() : __np.falsename());
1439 string_type __nm = _VSTD::move(__tmp);
1441 string_type __nm = __v ? __np.truename() : __np.falsename();
1443 for (typename string_type::iterator __i = __nm.begin(); __i != __nm.end(); ++__i, ++__s)
1448 template <class _CharT, class _OutputIterator>
1450 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1451 char_type __fl, long __v) const
1453 // Stage 1 - Get number in narrow char
1454 char __fmt[6] = {'%', 0};
1455 const char* __len = "l";
1456 this->__format_int(__fmt+1, __len, true, __iob.flags());
1457 const unsigned __nbuf = (numeric_limits<long>::digits / 3)
1458 + ((numeric_limits<long>::digits % 3) != 0)
1459 + ((__iob.flags() & ios_base::showbase) != 0)
1462 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1463 char* __ne = __nar + __nc;
1464 char* __np = this->__identify_padding(__nar, __ne, __iob);
1465 // Stage 2 - Widen __nar while adding thousands separators
1466 char_type __o[2*(__nbuf-1) - 1];
1467 char_type* __op; // pad here
1468 char_type* __oe; // end of output
1469 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1470 // [__o, __oe) contains thousands_sep'd wide number
1472 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1475 template <class _CharT, class _OutputIterator>
1477 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1478 char_type __fl, long long __v) const
1480 // Stage 1 - Get number in narrow char
1481 char __fmt[8] = {'%', 0};
1482 const char* __len = "ll";
1483 this->__format_int(__fmt+1, __len, true, __iob.flags());
1484 const unsigned __nbuf = (numeric_limits<long long>::digits / 3)
1485 + ((numeric_limits<long long>::digits % 3) != 0)
1486 + ((__iob.flags() & ios_base::showbase) != 0)
1489 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1490 char* __ne = __nar + __nc;
1491 char* __np = this->__identify_padding(__nar, __ne, __iob);
1492 // Stage 2 - Widen __nar while adding thousands separators
1493 char_type __o[2*(__nbuf-1) - 1];
1494 char_type* __op; // pad here
1495 char_type* __oe; // end of output
1496 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1497 // [__o, __oe) contains thousands_sep'd wide number
1499 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1502 template <class _CharT, class _OutputIterator>
1504 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1505 char_type __fl, unsigned long __v) const
1507 // Stage 1 - Get number in narrow char
1508 char __fmt[6] = {'%', 0};
1509 const char* __len = "l";
1510 this->__format_int(__fmt+1, __len, false, __iob.flags());
1511 const unsigned __nbuf = (numeric_limits<unsigned long>::digits / 3)
1512 + ((numeric_limits<unsigned long>::digits % 3) != 0)
1513 + ((__iob.flags() & ios_base::showbase) != 0)
1516 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1517 char* __ne = __nar + __nc;
1518 char* __np = this->__identify_padding(__nar, __ne, __iob);
1519 // Stage 2 - Widen __nar while adding thousands separators
1520 char_type __o[2*(__nbuf-1) - 1];
1521 char_type* __op; // pad here
1522 char_type* __oe; // end of output
1523 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1524 // [__o, __oe) contains thousands_sep'd wide number
1526 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1529 template <class _CharT, class _OutputIterator>
1531 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1532 char_type __fl, unsigned long long __v) const
1534 // Stage 1 - Get number in narrow char
1535 char __fmt[8] = {'%', 0};
1536 const char* __len = "ll";
1537 this->__format_int(__fmt+1, __len, false, __iob.flags());
1538 const unsigned __nbuf = (numeric_limits<unsigned long long>::digits / 3)
1539 + ((numeric_limits<unsigned long long>::digits % 3) != 0)
1540 + ((__iob.flags() & ios_base::showbase) != 0)
1543 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1544 char* __ne = __nar + __nc;
1545 char* __np = this->__identify_padding(__nar, __ne, __iob);
1546 // Stage 2 - Widen __nar while adding thousands separators
1547 char_type __o[2*(__nbuf-1) - 1];
1548 char_type* __op; // pad here
1549 char_type* __oe; // end of output
1550 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1551 // [__o, __oe) contains thousands_sep'd wide number
1553 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1556 template <class _CharT, class _OutputIterator>
1558 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1559 char_type __fl, double __v) const
1561 // Stage 1 - Get number in narrow char
1562 char __fmt[8] = {'%', 0};
1563 const char* __len = "";
1564 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1565 const unsigned __nbuf = 30;
1569 if (__specify_precision)
1570 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
1571 (int)__iob.precision(), __v);
1573 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1574 unique_ptr<char, void(*)(void*)> __nbh(0, free);
1575 if (__nc > static_cast<int>(__nbuf-1))
1577 if (__specify_precision)
1578 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
1580 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1582 __throw_bad_alloc();
1585 char* __ne = __nb + __nc;
1586 char* __np = this->__identify_padding(__nb, __ne, __iob);
1587 // Stage 2 - Widen __nar while adding thousands separators
1588 char_type __o[2*(__nbuf-1) - 1];
1589 char_type* __ob = __o;
1590 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1593 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
1595 __throw_bad_alloc();
1598 char_type* __op; // pad here
1599 char_type* __oe; // end of output
1600 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1601 // [__o, __oe) contains thousands_sep'd wide number
1603 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1607 template <class _CharT, class _OutputIterator>
1609 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1610 char_type __fl, long double __v) const
1612 // Stage 1 - Get number in narrow char
1613 char __fmt[8] = {'%', 0};
1614 const char* __len = "L";
1615 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1616 const unsigned __nbuf = 30;
1620 if (__specify_precision)
1621 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
1622 (int)__iob.precision(), __v);
1624 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1625 unique_ptr<char, void(*)(void*)> __nbh(0, free);
1626 if (__nc > static_cast<int>(__nbuf-1))
1628 if (__specify_precision)
1629 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
1631 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1633 __throw_bad_alloc();
1636 char* __ne = __nb + __nc;
1637 char* __np = this->__identify_padding(__nb, __ne, __iob);
1638 // Stage 2 - Widen __nar while adding thousands separators
1639 char_type __o[2*(__nbuf-1) - 1];
1640 char_type* __ob = __o;
1641 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1644 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
1646 __throw_bad_alloc();
1649 char_type* __op; // pad here
1650 char_type* __oe; // end of output
1651 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1652 // [__o, __oe) contains thousands_sep'd wide number
1654 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1658 template <class _CharT, class _OutputIterator>
1660 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1661 char_type __fl, const void* __v) const
1663 // Stage 1 - Get pointer in narrow char
1664 char __fmt[6] = "%p";
1665 const unsigned __nbuf = 20;
1667 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1668 char* __ne = __nar + __nc;
1669 char* __np = this->__identify_padding(__nar, __ne, __iob);
1670 // Stage 2 - Widen __nar
1671 char_type __o[2*(__nbuf-1) - 1];
1672 char_type* __op; // pad here
1673 char_type* __oe; // end of output
1674 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
1675 __ct.widen(__nar, __ne, __o);
1676 __oe = __o + (__ne - __nar);
1680 __op = __o + (__np - __nar);
1681 // [__o, __oe) contains wide number
1683 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1686 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<char>)
1687 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<wchar_t>)
1689 template <class _CharT, class _InputIterator>
1692 __get_up_to_n_digits(_InputIterator& __b, _InputIterator __e,
1693 ios_base::iostate& __err, const ctype<_CharT>& __ct, int __n)
1695 // Precondition: __n >= 1
1698 __err |= ios_base::eofbit | ios_base::failbit;
1703 if (!__ct.is(ctype_base::digit, __c))
1705 __err |= ios_base::failbit;
1708 int __r = __ct.narrow(__c, 0) - '0';
1709 for (++__b, (void) --__n; __b != __e && __n > 0; ++__b, (void) --__n)
1713 if (!__ct.is(ctype_base::digit, __c))
1715 __r = __r * 10 + __ct.narrow(__c, 0) - '0';
1718 __err |= ios_base::eofbit;
1722 class _LIBCPP_TYPE_VIS time_base
1725 enum dateorder {no_order, dmy, mdy, ymd, ydm};
1728 template <class _CharT>
1729 class _LIBCPP_TEMPLATE_VIS __time_get_c_storage
1732 typedef basic_string<_CharT> string_type;
1734 virtual const string_type* __weeks() const;
1735 virtual const string_type* __months() const;
1736 virtual const string_type* __am_pm() const;
1737 virtual const string_type& __c() const;
1738 virtual const string_type& __r() const;
1739 virtual const string_type& __x() const;
1740 virtual const string_type& __X() const;
1742 _LIBCPP_INLINE_VISIBILITY
1743 ~__time_get_c_storage() {}
1746 template <> _LIBCPP_FUNC_VIS const string* __time_get_c_storage<char>::__weeks() const;
1747 template <> _LIBCPP_FUNC_VIS const string* __time_get_c_storage<char>::__months() const;
1748 template <> _LIBCPP_FUNC_VIS const string* __time_get_c_storage<char>::__am_pm() const;
1749 template <> _LIBCPP_FUNC_VIS const string& __time_get_c_storage<char>::__c() const;
1750 template <> _LIBCPP_FUNC_VIS const string& __time_get_c_storage<char>::__r() const;
1751 template <> _LIBCPP_FUNC_VIS const string& __time_get_c_storage<char>::__x() const;
1752 template <> _LIBCPP_FUNC_VIS const string& __time_get_c_storage<char>::__X() const;
1754 template <> _LIBCPP_FUNC_VIS const wstring* __time_get_c_storage<wchar_t>::__weeks() const;
1755 template <> _LIBCPP_FUNC_VIS const wstring* __time_get_c_storage<wchar_t>::__months() const;
1756 template <> _LIBCPP_FUNC_VIS const wstring* __time_get_c_storage<wchar_t>::__am_pm() const;
1757 template <> _LIBCPP_FUNC_VIS const wstring& __time_get_c_storage<wchar_t>::__c() const;
1758 template <> _LIBCPP_FUNC_VIS const wstring& __time_get_c_storage<wchar_t>::__r() const;
1759 template <> _LIBCPP_FUNC_VIS const wstring& __time_get_c_storage<wchar_t>::__x() const;
1760 template <> _LIBCPP_FUNC_VIS const wstring& __time_get_c_storage<wchar_t>::__X() const;
1762 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
1763 class _LIBCPP_TEMPLATE_VIS time_get
1764 : public locale::facet,
1766 private __time_get_c_storage<_CharT>
1769 typedef _CharT char_type;
1770 typedef _InputIterator iter_type;
1771 typedef time_base::dateorder dateorder;
1772 typedef basic_string<char_type> string_type;
1774 _LIBCPP_INLINE_VISIBILITY
1775 explicit time_get(size_t __refs = 0)
1776 : locale::facet(__refs) {}
1778 _LIBCPP_INLINE_VISIBILITY
1779 dateorder date_order() const
1781 return this->do_date_order();
1784 _LIBCPP_INLINE_VISIBILITY
1785 iter_type get_time(iter_type __b, iter_type __e, ios_base& __iob,
1786 ios_base::iostate& __err, tm* __tm) const
1788 return do_get_time(__b, __e, __iob, __err, __tm);
1791 _LIBCPP_INLINE_VISIBILITY
1792 iter_type get_date(iter_type __b, iter_type __e, ios_base& __iob,
1793 ios_base::iostate& __err, tm* __tm) const
1795 return do_get_date(__b, __e, __iob, __err, __tm);
1798 _LIBCPP_INLINE_VISIBILITY
1799 iter_type get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
1800 ios_base::iostate& __err, tm* __tm) const
1802 return do_get_weekday(__b, __e, __iob, __err, __tm);
1805 _LIBCPP_INLINE_VISIBILITY
1806 iter_type get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
1807 ios_base::iostate& __err, tm* __tm) const
1809 return do_get_monthname(__b, __e, __iob, __err, __tm);
1812 _LIBCPP_INLINE_VISIBILITY
1813 iter_type get_year(iter_type __b, iter_type __e, ios_base& __iob,
1814 ios_base::iostate& __err, tm* __tm) const
1816 return do_get_year(__b, __e, __iob, __err, __tm);
1819 _LIBCPP_INLINE_VISIBILITY
1820 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
1821 ios_base::iostate& __err, tm *__tm,
1822 char __fmt, char __mod = 0) const
1824 return do_get(__b, __e, __iob, __err, __tm, __fmt, __mod);
1827 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
1828 ios_base::iostate& __err, tm* __tm,
1829 const char_type* __fmtb, const char_type* __fmte) const;
1831 static locale::id id;
1834 _LIBCPP_INLINE_VISIBILITY
1837 virtual dateorder do_date_order() const;
1838 virtual iter_type do_get_time(iter_type __b, iter_type __e, ios_base& __iob,
1839 ios_base::iostate& __err, tm* __tm) const;
1840 virtual iter_type do_get_date(iter_type __b, iter_type __e, ios_base& __iob,
1841 ios_base::iostate& __err, tm* __tm) const;
1842 virtual iter_type do_get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
1843 ios_base::iostate& __err, tm* __tm) const;
1844 virtual iter_type do_get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
1845 ios_base::iostate& __err, tm* __tm) const;
1846 virtual iter_type do_get_year(iter_type __b, iter_type __e, ios_base& __iob,
1847 ios_base::iostate& __err, tm* __tm) const;
1848 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
1849 ios_base::iostate& __err, tm* __tm,
1850 char __fmt, char __mod) const;
1852 void __get_white_space(iter_type& __b, iter_type __e,
1853 ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1854 void __get_percent(iter_type& __b, iter_type __e, ios_base::iostate& __err,
1855 const ctype<char_type>& __ct) const;
1857 void __get_weekdayname(int& __m,
1858 iter_type& __b, iter_type __e,
1859 ios_base::iostate& __err,
1860 const ctype<char_type>& __ct) const;
1861 void __get_monthname(int& __m,
1862 iter_type& __b, iter_type __e,
1863 ios_base::iostate& __err,
1864 const ctype<char_type>& __ct) const;
1865 void __get_day(int& __d,
1866 iter_type& __b, iter_type __e,
1867 ios_base::iostate& __err,
1868 const ctype<char_type>& __ct) const;
1869 void __get_month(int& __m,
1870 iter_type& __b, iter_type __e,
1871 ios_base::iostate& __err,
1872 const ctype<char_type>& __ct) const;
1873 void __get_year(int& __y,
1874 iter_type& __b, iter_type __e,
1875 ios_base::iostate& __err,
1876 const ctype<char_type>& __ct) const;
1877 void __get_year4(int& __y,
1878 iter_type& __b, iter_type __e,
1879 ios_base::iostate& __err,
1880 const ctype<char_type>& __ct) const;
1881 void __get_hour(int& __d,
1882 iter_type& __b, iter_type __e,
1883 ios_base::iostate& __err,
1884 const ctype<char_type>& __ct) const;
1885 void __get_12_hour(int& __h,
1886 iter_type& __b, iter_type __e,
1887 ios_base::iostate& __err,
1888 const ctype<char_type>& __ct) const;
1889 void __get_am_pm(int& __h,
1890 iter_type& __b, iter_type __e,
1891 ios_base::iostate& __err,
1892 const ctype<char_type>& __ct) const;
1893 void __get_minute(int& __m,
1894 iter_type& __b, iter_type __e,
1895 ios_base::iostate& __err,
1896 const ctype<char_type>& __ct) const;
1897 void __get_second(int& __s,
1898 iter_type& __b, iter_type __e,
1899 ios_base::iostate& __err,
1900 const ctype<char_type>& __ct) const;
1901 void __get_weekday(int& __w,
1902 iter_type& __b, iter_type __e,
1903 ios_base::iostate& __err,
1904 const ctype<char_type>& __ct) const;
1905 void __get_day_year_num(int& __w,
1906 iter_type& __b, iter_type __e,
1907 ios_base::iostate& __err,
1908 const ctype<char_type>& __ct) const;
1911 template <class _CharT, class _InputIterator>
1913 time_get<_CharT, _InputIterator>::id;
1915 // time_get primitives
1917 template <class _CharT, class _InputIterator>
1919 time_get<_CharT, _InputIterator>::__get_weekdayname(int& __w,
1920 iter_type& __b, iter_type __e,
1921 ios_base::iostate& __err,
1922 const ctype<char_type>& __ct) const
1924 // Note: ignoring case comes from the POSIX strptime spec
1925 const string_type* __wk = this->__weeks();
1926 ptrdiff_t __i = __scan_keyword(__b, __e, __wk, __wk+14, __ct, __err, false) - __wk;
1931 template <class _CharT, class _InputIterator>
1933 time_get<_CharT, _InputIterator>::__get_monthname(int& __m,
1934 iter_type& __b, iter_type __e,
1935 ios_base::iostate& __err,
1936 const ctype<char_type>& __ct) const
1938 // Note: ignoring case comes from the POSIX strptime spec
1939 const string_type* __month = this->__months();
1940 ptrdiff_t __i = __scan_keyword(__b, __e, __month, __month+24, __ct, __err, false) - __month;
1945 template <class _CharT, class _InputIterator>
1947 time_get<_CharT, _InputIterator>::__get_day(int& __d,
1948 iter_type& __b, iter_type __e,
1949 ios_base::iostate& __err,
1950 const ctype<char_type>& __ct) const
1952 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
1953 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 31)
1956 __err |= ios_base::failbit;
1959 template <class _CharT, class _InputIterator>
1961 time_get<_CharT, _InputIterator>::__get_month(int& __m,
1962 iter_type& __b, iter_type __e,
1963 ios_base::iostate& __err,
1964 const ctype<char_type>& __ct) const
1966 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2) - 1;
1967 if (!(__err & ios_base::failbit) && __t <= 11)
1970 __err |= ios_base::failbit;
1973 template <class _CharT, class _InputIterator>
1975 time_get<_CharT, _InputIterator>::__get_year(int& __y,
1976 iter_type& __b, iter_type __e,
1977 ios_base::iostate& __err,
1978 const ctype<char_type>& __ct) const
1980 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
1981 if (!(__err & ios_base::failbit))
1985 else if (69 <= __t && __t <= 99)
1991 template <class _CharT, class _InputIterator>
1993 time_get<_CharT, _InputIterator>::__get_year4(int& __y,
1994 iter_type& __b, iter_type __e,
1995 ios_base::iostate& __err,
1996 const ctype<char_type>& __ct) const
1998 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
1999 if (!(__err & ios_base::failbit))
2003 template <class _CharT, class _InputIterator>
2005 time_get<_CharT, _InputIterator>::__get_hour(int& __h,
2006 iter_type& __b, iter_type __e,
2007 ios_base::iostate& __err,
2008 const ctype<char_type>& __ct) const
2010 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2011 if (!(__err & ios_base::failbit) && __t <= 23)
2014 __err |= ios_base::failbit;
2017 template <class _CharT, class _InputIterator>
2019 time_get<_CharT, _InputIterator>::__get_12_hour(int& __h,
2020 iter_type& __b, iter_type __e,
2021 ios_base::iostate& __err,
2022 const ctype<char_type>& __ct) const
2024 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2025 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 12)
2028 __err |= ios_base::failbit;
2031 template <class _CharT, class _InputIterator>
2033 time_get<_CharT, _InputIterator>::__get_minute(int& __m,
2034 iter_type& __b, iter_type __e,
2035 ios_base::iostate& __err,
2036 const ctype<char_type>& __ct) const
2038 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2039 if (!(__err & ios_base::failbit) && __t <= 59)
2042 __err |= ios_base::failbit;
2045 template <class _CharT, class _InputIterator>
2047 time_get<_CharT, _InputIterator>::__get_second(int& __s,
2048 iter_type& __b, iter_type __e,
2049 ios_base::iostate& __err,
2050 const ctype<char_type>& __ct) const
2052 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2053 if (!(__err & ios_base::failbit) && __t <= 60)
2056 __err |= ios_base::failbit;
2059 template <class _CharT, class _InputIterator>
2061 time_get<_CharT, _InputIterator>::__get_weekday(int& __w,
2062 iter_type& __b, iter_type __e,
2063 ios_base::iostate& __err,
2064 const ctype<char_type>& __ct) const
2066 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 1);
2067 if (!(__err & ios_base::failbit) && __t <= 6)
2070 __err |= ios_base::failbit;
2073 template <class _CharT, class _InputIterator>
2075 time_get<_CharT, _InputIterator>::__get_day_year_num(int& __d,
2076 iter_type& __b, iter_type __e,
2077 ios_base::iostate& __err,
2078 const ctype<char_type>& __ct) const
2080 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 3);
2081 if (!(__err & ios_base::failbit) && __t <= 365)
2084 __err |= ios_base::failbit;
2087 template <class _CharT, class _InputIterator>
2089 time_get<_CharT, _InputIterator>::__get_white_space(iter_type& __b, iter_type __e,
2090 ios_base::iostate& __err,
2091 const ctype<char_type>& __ct) const
2093 for (; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2096 __err |= ios_base::eofbit;
2099 template <class _CharT, class _InputIterator>
2101 time_get<_CharT, _InputIterator>::__get_am_pm(int& __h,
2102 iter_type& __b, iter_type __e,
2103 ios_base::iostate& __err,
2104 const ctype<char_type>& __ct) const
2106 const string_type* __ap = this->__am_pm();
2107 if (__ap[0].size() + __ap[1].size() == 0)
2109 __err |= ios_base::failbit;
2112 ptrdiff_t __i = __scan_keyword(__b, __e, __ap, __ap+2, __ct, __err, false) - __ap;
2113 if (__i == 0 && __h == 12)
2115 else if (__i == 1 && __h < 12)
2119 template <class _CharT, class _InputIterator>
2121 time_get<_CharT, _InputIterator>::__get_percent(iter_type& __b, iter_type __e,
2122 ios_base::iostate& __err,
2123 const ctype<char_type>& __ct) const
2127 __err |= ios_base::eofbit | ios_base::failbit;
2130 if (__ct.narrow(*__b, 0) != '%')
2131 __err |= ios_base::failbit;
2132 else if(++__b == __e)
2133 __err |= ios_base::eofbit;
2136 // time_get end primitives
2138 template <class _CharT, class _InputIterator>
2140 time_get<_CharT, _InputIterator>::get(iter_type __b, iter_type __e,
2142 ios_base::iostate& __err, tm* __tm,
2143 const char_type* __fmtb, const char_type* __fmte) const
2145 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2146 __err = ios_base::goodbit;
2147 while (__fmtb != __fmte && __err == ios_base::goodbit)
2151 __err = ios_base::failbit;
2154 if (__ct.narrow(*__fmtb, 0) == '%')
2156 if (++__fmtb == __fmte)
2158 __err = ios_base::failbit;
2161 char __cmd = __ct.narrow(*__fmtb, 0);
2163 if (__cmd == 'E' || __cmd == '0')
2165 if (++__fmtb == __fmte)
2167 __err = ios_base::failbit;
2171 __cmd = __ct.narrow(*__fmtb, 0);
2173 __b = do_get(__b, __e, __iob, __err, __tm, __cmd, __opt);
2176 else if (__ct.is(ctype_base::space, *__fmtb))
2178 for (++__fmtb; __fmtb != __fmte && __ct.is(ctype_base::space, *__fmtb); ++__fmtb)
2180 for ( ; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2183 else if (__ct.toupper(*__b) == __ct.toupper(*__fmtb))
2189 __err = ios_base::failbit;
2192 __err |= ios_base::eofbit;
2196 template <class _CharT, class _InputIterator>
2197 typename time_get<_CharT, _InputIterator>::dateorder
2198 time_get<_CharT, _InputIterator>::do_date_order() const
2203 template <class _CharT, class _InputIterator>
2205 time_get<_CharT, _InputIterator>::do_get_time(iter_type __b, iter_type __e,
2207 ios_base::iostate& __err,
2210 const char_type __fmt[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2211 return get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt)/sizeof(__fmt[0]));
2214 template <class _CharT, class _InputIterator>
2216 time_get<_CharT, _InputIterator>::do_get_date(iter_type __b, iter_type __e,
2218 ios_base::iostate& __err,
2221 const string_type& __fmt = this->__x();
2222 return get(__b, __e, __iob, __err, __tm, __fmt.data(), __fmt.data() + __fmt.size());
2225 template <class _CharT, class _InputIterator>
2227 time_get<_CharT, _InputIterator>::do_get_weekday(iter_type __b, iter_type __e,
2229 ios_base::iostate& __err,
2232 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2233 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2237 template <class _CharT, class _InputIterator>
2239 time_get<_CharT, _InputIterator>::do_get_monthname(iter_type __b, iter_type __e,
2241 ios_base::iostate& __err,
2244 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2245 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2249 template <class _CharT, class _InputIterator>
2251 time_get<_CharT, _InputIterator>::do_get_year(iter_type __b, iter_type __e,
2253 ios_base::iostate& __err,
2256 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2257 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2261 template <class _CharT, class _InputIterator>
2263 time_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
2265 ios_base::iostate& __err, tm* __tm,
2266 char __fmt, char) const
2268 __err = ios_base::goodbit;
2269 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2274 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2279 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2283 const string_type& __fm = this->__c();
2284 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
2289 __get_day(__tm->tm_mday, __b, __e, __err, __ct);
2293 const char_type __fm[] = {'%', 'm', '/', '%', 'd', '/', '%', 'y'};
2294 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2299 const char_type __fm[] = {'%', 'Y', '-', '%', 'm', '-', '%', 'd'};
2300 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2304 __get_hour(__tm->tm_hour, __b, __e, __err, __ct);
2307 __get_12_hour(__tm->tm_hour, __b, __e, __err, __ct);
2310 __get_day_year_num(__tm->tm_yday, __b, __e, __err, __ct);
2313 __get_month(__tm->tm_mon, __b, __e, __err, __ct);
2316 __get_minute(__tm->tm_min, __b, __e, __err, __ct);
2320 __get_white_space(__b, __e, __err, __ct);
2323 __get_am_pm(__tm->tm_hour, __b, __e, __err, __ct);
2327 const char_type __fm[] = {'%', 'I', ':', '%', 'M', ':', '%', 'S', ' ', '%', 'p'};
2328 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2333 const char_type __fm[] = {'%', 'H', ':', '%', 'M'};
2334 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2338 __get_second(__tm->tm_sec, __b, __e, __err, __ct);
2342 const char_type __fm[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2343 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2347 __get_weekday(__tm->tm_wday, __b, __e, __err, __ct);
2350 return do_get_date(__b, __e, __iob, __err, __tm);
2353 const string_type& __fm = this->__X();
2354 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
2358 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2361 __get_year4(__tm->tm_year, __b, __e, __err, __ct);
2364 __get_percent(__b, __e, __err, __ct);
2367 __err |= ios_base::failbit;
2372 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get<char>)
2373 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get<wchar_t>)
2375 class _LIBCPP_TYPE_VIS __time_get
2380 __time_get(const char* __nm);
2381 __time_get(const string& __nm);
2385 template <class _CharT>
2386 class _LIBCPP_TEMPLATE_VIS __time_get_storage
2390 typedef basic_string<_CharT> string_type;
2392 string_type __weeks_[14];
2393 string_type __months_[24];
2394 string_type __am_pm_[2];
2400 explicit __time_get_storage(const char* __nm);
2401 explicit __time_get_storage(const string& __nm);
2403 _LIBCPP_INLINE_VISIBILITY ~__time_get_storage() {}
2405 time_base::dateorder __do_date_order() const;
2408 void init(const ctype<_CharT>&);
2409 string_type __analyze(char __fmt, const ctype<_CharT>&);
2412 #define _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(_CharT) \
2413 template <> _LIBCPP_FUNC_VIS time_base::dateorder __time_get_storage<_CharT>::__do_date_order() const; \
2414 template <> _LIBCPP_FUNC_VIS __time_get_storage<_CharT>::__time_get_storage(const char*); \
2415 template <> _LIBCPP_FUNC_VIS __time_get_storage<_CharT>::__time_get_storage(const string&); \
2416 template <> _LIBCPP_FUNC_VIS void __time_get_storage<_CharT>::init(const ctype<_CharT>&); \
2417 template <> _LIBCPP_FUNC_VIS __time_get_storage<_CharT>::string_type __time_get_storage<_CharT>::__analyze(char, const ctype<_CharT>&); \
2418 extern template _LIBCPP_FUNC_VIS time_base::dateorder __time_get_storage<_CharT>::__do_date_order() const; \
2419 extern template _LIBCPP_FUNC_VIS __time_get_storage<_CharT>::__time_get_storage(const char*); \
2420 extern template _LIBCPP_FUNC_VIS __time_get_storage<_CharT>::__time_get_storage(const string&); \
2421 extern template _LIBCPP_FUNC_VIS void __time_get_storage<_CharT>::init(const ctype<_CharT>&); \
2422 extern template _LIBCPP_FUNC_VIS __time_get_storage<_CharT>::string_type __time_get_storage<_CharT>::__analyze(char, const ctype<_CharT>&); \
2425 _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(char)
2426 _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(wchar_t)
2427 #undef _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION
2429 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2430 class _LIBCPP_TEMPLATE_VIS time_get_byname
2431 : public time_get<_CharT, _InputIterator>,
2432 private __time_get_storage<_CharT>
2435 typedef time_base::dateorder dateorder;
2436 typedef _InputIterator iter_type;
2437 typedef _CharT char_type;
2438 typedef basic_string<char_type> string_type;
2440 _LIBCPP_INLINE_VISIBILITY
2441 explicit time_get_byname(const char* __nm, size_t __refs = 0)
2442 : time_get<_CharT, _InputIterator>(__refs),
2443 __time_get_storage<_CharT>(__nm) {}
2444 _LIBCPP_INLINE_VISIBILITY
2445 explicit time_get_byname(const string& __nm, size_t __refs = 0)
2446 : time_get<_CharT, _InputIterator>(__refs),
2447 __time_get_storage<_CharT>(__nm) {}
2450 _LIBCPP_INLINE_VISIBILITY
2451 ~time_get_byname() {}
2453 _LIBCPP_INLINE_VISIBILITY
2454 virtual dateorder do_date_order() const {return this->__do_date_order();}
2456 _LIBCPP_INLINE_VISIBILITY
2457 virtual const string_type* __weeks() const {return this->__weeks_;}
2458 _LIBCPP_INLINE_VISIBILITY
2459 virtual const string_type* __months() const {return this->__months_;}
2460 _LIBCPP_INLINE_VISIBILITY
2461 virtual const string_type* __am_pm() const {return this->__am_pm_;}
2462 _LIBCPP_INLINE_VISIBILITY
2463 virtual const string_type& __c() const {return this->__c_;}
2464 _LIBCPP_INLINE_VISIBILITY
2465 virtual const string_type& __r() const {return this->__r_;}
2466 _LIBCPP_INLINE_VISIBILITY
2467 virtual const string_type& __x() const {return this->__x_;}
2468 _LIBCPP_INLINE_VISIBILITY
2469 virtual const string_type& __X() const {return this->__X_;}
2472 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<char>)
2473 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<wchar_t>)
2475 class _LIBCPP_TYPE_VIS __time_put
2479 _LIBCPP_INLINE_VISIBILITY __time_put() : __loc_(_LIBCPP_GET_C_LOCALE) {}
2480 __time_put(const char* __nm);
2481 __time_put(const string& __nm);
2483 void __do_put(char* __nb, char*& __ne, const tm* __tm,
2484 char __fmt, char __mod) const;
2485 void __do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm,
2486 char __fmt, char __mod) const;
2489 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2490 class _LIBCPP_TEMPLATE_VIS time_put
2491 : public locale::facet,
2495 typedef _CharT char_type;
2496 typedef _OutputIterator iter_type;
2498 _LIBCPP_INLINE_VISIBILITY
2499 explicit time_put(size_t __refs = 0)
2500 : locale::facet(__refs) {}
2502 iter_type put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm,
2503 const char_type* __pb, const char_type* __pe) const;
2505 _LIBCPP_INLINE_VISIBILITY
2506 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
2507 const tm* __tm, char __fmt, char __mod = 0) const
2509 return do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2512 static locale::id id;
2515 _LIBCPP_INLINE_VISIBILITY
2517 virtual iter_type do_put(iter_type __s, ios_base&, char_type, const tm* __tm,
2518 char __fmt, char __mod) const;
2520 _LIBCPP_INLINE_VISIBILITY
2521 explicit time_put(const char* __nm, size_t __refs)
2522 : locale::facet(__refs),
2524 _LIBCPP_INLINE_VISIBILITY
2525 explicit time_put(const string& __nm, size_t __refs)
2526 : locale::facet(__refs),
2530 template <class _CharT, class _OutputIterator>
2532 time_put<_CharT, _OutputIterator>::id;
2534 template <class _CharT, class _OutputIterator>
2536 time_put<_CharT, _OutputIterator>::put(iter_type __s, ios_base& __iob,
2537 char_type __fl, const tm* __tm,
2538 const char_type* __pb,
2539 const char_type* __pe) const
2541 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2542 for (; __pb != __pe; ++__pb)
2544 if (__ct.narrow(*__pb, 0) == '%')
2552 char __fmt = __ct.narrow(*__pb, 0);
2553 if (__fmt == 'E' || __fmt == 'O')
2562 __fmt = __ct.narrow(*__pb, 0);
2564 __s = do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2572 template <class _CharT, class _OutputIterator>
2574 time_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base&,
2575 char_type, const tm* __tm,
2576 char __fmt, char __mod) const
2578 char_type __nar[100];
2579 char_type* __nb = __nar;
2580 char_type* __ne = __nb + 100;
2581 __do_put(__nb, __ne, __tm, __fmt, __mod);
2582 return _VSTD::copy(__nb, __ne, __s);
2585 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<char>)
2586 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<wchar_t>)
2588 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2589 class _LIBCPP_TEMPLATE_VIS time_put_byname
2590 : public time_put<_CharT, _OutputIterator>
2593 _LIBCPP_INLINE_VISIBILITY
2594 explicit time_put_byname(const char* __nm, size_t __refs = 0)
2595 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2597 _LIBCPP_INLINE_VISIBILITY
2598 explicit time_put_byname(const string& __nm, size_t __refs = 0)
2599 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2602 _LIBCPP_INLINE_VISIBILITY
2603 ~time_put_byname() {}
2606 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<char>)
2607 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<wchar_t>)
2611 class _LIBCPP_TYPE_VIS money_base
2614 enum part {none, space, symbol, sign, value};
2615 struct pattern {char field[4];};
2617 _LIBCPP_INLINE_VISIBILITY money_base() {}
2622 template <class _CharT, bool _International = false>
2623 class _LIBCPP_TEMPLATE_VIS moneypunct
2624 : public locale::facet,
2628 typedef _CharT char_type;
2629 typedef basic_string<char_type> string_type;
2631 _LIBCPP_INLINE_VISIBILITY
2632 explicit moneypunct(size_t __refs = 0)
2633 : locale::facet(__refs) {}
2635 _LIBCPP_INLINE_VISIBILITY char_type decimal_point() const {return do_decimal_point();}
2636 _LIBCPP_INLINE_VISIBILITY char_type thousands_sep() const {return do_thousands_sep();}
2637 _LIBCPP_INLINE_VISIBILITY string grouping() const {return do_grouping();}
2638 _LIBCPP_INLINE_VISIBILITY string_type curr_symbol() const {return do_curr_symbol();}
2639 _LIBCPP_INLINE_VISIBILITY string_type positive_sign() const {return do_positive_sign();}
2640 _LIBCPP_INLINE_VISIBILITY string_type negative_sign() const {return do_negative_sign();}
2641 _LIBCPP_INLINE_VISIBILITY int frac_digits() const {return do_frac_digits();}
2642 _LIBCPP_INLINE_VISIBILITY pattern pos_format() const {return do_pos_format();}
2643 _LIBCPP_INLINE_VISIBILITY pattern neg_format() const {return do_neg_format();}
2645 static locale::id id;
2646 static const bool intl = _International;
2649 _LIBCPP_INLINE_VISIBILITY
2652 virtual char_type do_decimal_point() const {return numeric_limits<char_type>::max();}
2653 virtual char_type do_thousands_sep() const {return numeric_limits<char_type>::max();}
2654 virtual string do_grouping() const {return string();}
2655 virtual string_type do_curr_symbol() const {return string_type();}
2656 virtual string_type do_positive_sign() const {return string_type();}
2657 virtual string_type do_negative_sign() const {return string_type(1, '-');}
2658 virtual int do_frac_digits() const {return 0;}
2659 virtual pattern do_pos_format() const
2660 {pattern __p = {{symbol, sign, none, value}}; return __p;}
2661 virtual pattern do_neg_format() const
2662 {pattern __p = {{symbol, sign, none, value}}; return __p;}
2665 template <class _CharT, bool _International>
2667 moneypunct<_CharT, _International>::id;
2669 template <class _CharT, bool _International>
2671 moneypunct<_CharT, _International>::intl;
2673 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, false>)
2674 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, true>)
2675 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, false>)
2676 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, true>)
2678 // moneypunct_byname
2680 template <class _CharT, bool _International = false>
2681 class _LIBCPP_TEMPLATE_VIS moneypunct_byname
2682 : public moneypunct<_CharT, _International>
2685 typedef money_base::pattern pattern;
2686 typedef _CharT char_type;
2687 typedef basic_string<char_type> string_type;
2689 _LIBCPP_INLINE_VISIBILITY
2690 explicit moneypunct_byname(const char* __nm, size_t __refs = 0)
2691 : moneypunct<_CharT, _International>(__refs) {init(__nm);}
2693 _LIBCPP_INLINE_VISIBILITY
2694 explicit moneypunct_byname(const string& __nm, size_t __refs = 0)
2695 : moneypunct<_CharT, _International>(__refs) {init(__nm.c_str());}
2698 _LIBCPP_INLINE_VISIBILITY
2699 ~moneypunct_byname() {}
2701 virtual char_type do_decimal_point() const {return __decimal_point_;}
2702 virtual char_type do_thousands_sep() const {return __thousands_sep_;}
2703 virtual string do_grouping() const {return __grouping_;}
2704 virtual string_type do_curr_symbol() const {return __curr_symbol_;}
2705 virtual string_type do_positive_sign() const {return __positive_sign_;}
2706 virtual string_type do_negative_sign() const {return __negative_sign_;}
2707 virtual int do_frac_digits() const {return __frac_digits_;}
2708 virtual pattern do_pos_format() const {return __pos_format_;}
2709 virtual pattern do_neg_format() const {return __neg_format_;}
2712 char_type __decimal_point_;
2713 char_type __thousands_sep_;
2715 string_type __curr_symbol_;
2716 string_type __positive_sign_;
2717 string_type __negative_sign_;
2719 pattern __pos_format_;
2720 pattern __neg_format_;
2722 void init(const char*);
2725 template<> _LIBCPP_FUNC_VIS void moneypunct_byname<char, false>::init(const char*);
2726 template<> _LIBCPP_FUNC_VIS void moneypunct_byname<char, true>::init(const char*);
2727 template<> _LIBCPP_FUNC_VIS void moneypunct_byname<wchar_t, false>::init(const char*);
2728 template<> _LIBCPP_FUNC_VIS void moneypunct_byname<wchar_t, true>::init(const char*);
2730 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, false>)
2731 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, true>)
2732 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, false>)
2733 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, true>)
2737 template <class _CharT>
2741 typedef _CharT char_type;
2742 typedef basic_string<char_type> string_type;
2744 _LIBCPP_INLINE_VISIBILITY __money_get() {}
2746 static void __gather_info(bool __intl, const locale& __loc,
2747 money_base::pattern& __pat, char_type& __dp,
2748 char_type& __ts, string& __grp,
2749 string_type& __sym, string_type& __psn,
2750 string_type& __nsn, int& __fd);
2753 template <class _CharT>
2755 __money_get<_CharT>::__gather_info(bool __intl, const locale& __loc,
2756 money_base::pattern& __pat, char_type& __dp,
2757 char_type& __ts, string& __grp,
2758 string_type& __sym, string_type& __psn,
2759 string_type& __nsn, int& __fd)
2763 const moneypunct<char_type, true>& __mp =
2764 use_facet<moneypunct<char_type, true> >(__loc);
2765 __pat = __mp.neg_format();
2766 __nsn = __mp.negative_sign();
2767 __psn = __mp.positive_sign();
2768 __dp = __mp.decimal_point();
2769 __ts = __mp.thousands_sep();
2770 __grp = __mp.grouping();
2771 __sym = __mp.curr_symbol();
2772 __fd = __mp.frac_digits();
2776 const moneypunct<char_type, false>& __mp =
2777 use_facet<moneypunct<char_type, false> >(__loc);
2778 __pat = __mp.neg_format();
2779 __nsn = __mp.negative_sign();
2780 __psn = __mp.positive_sign();
2781 __dp = __mp.decimal_point();
2782 __ts = __mp.thousands_sep();
2783 __grp = __mp.grouping();
2784 __sym = __mp.curr_symbol();
2785 __fd = __mp.frac_digits();
2789 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<char>)
2790 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<wchar_t>)
2792 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2793 class _LIBCPP_TEMPLATE_VIS money_get
2794 : public locale::facet,
2795 private __money_get<_CharT>
2798 typedef _CharT char_type;
2799 typedef _InputIterator iter_type;
2800 typedef basic_string<char_type> string_type;
2802 _LIBCPP_INLINE_VISIBILITY
2803 explicit money_get(size_t __refs = 0)
2804 : locale::facet(__refs) {}
2806 _LIBCPP_INLINE_VISIBILITY
2807 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
2808 ios_base::iostate& __err, long double& __v) const
2810 return do_get(__b, __e, __intl, __iob, __err, __v);
2813 _LIBCPP_INLINE_VISIBILITY
2814 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
2815 ios_base::iostate& __err, string_type& __v) const
2817 return do_get(__b, __e, __intl, __iob, __err, __v);
2820 static locale::id id;
2824 _LIBCPP_INLINE_VISIBILITY
2827 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
2828 ios_base& __iob, ios_base::iostate& __err,
2829 long double& __v) const;
2830 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
2831 ios_base& __iob, ios_base::iostate& __err,
2832 string_type& __v) const;
2835 static bool __do_get(iter_type& __b, iter_type __e,
2836 bool __intl, const locale& __loc,
2837 ios_base::fmtflags __flags, ios_base::iostate& __err,
2838 bool& __neg, const ctype<char_type>& __ct,
2839 unique_ptr<char_type, void(*)(void*)>& __wb,
2840 char_type*& __wn, char_type* __we);
2843 template <class _CharT, class _InputIterator>
2845 money_get<_CharT, _InputIterator>::id;
2847 _LIBCPP_FUNC_VIS void __do_nothing(void*);
2849 template <class _Tp>
2852 __double_or_nothing(unique_ptr<_Tp, void(*)(void*)>& __b, _Tp*& __n, _Tp*& __e)
2854 bool __owns = __b.get_deleter() != __do_nothing;
2855 size_t __cur_cap = static_cast<size_t>(__e-__b.get()) * sizeof(_Tp);
2856 size_t __new_cap = __cur_cap < numeric_limits<size_t>::max() / 2 ?
2857 2 * __cur_cap : numeric_limits<size_t>::max();
2859 __new_cap = sizeof(_Tp);
2860 size_t __n_off = static_cast<size_t>(__n - __b.get());
2861 _Tp* __t = (_Tp*)realloc(__owns ? __b.get() : 0, __new_cap);
2863 __throw_bad_alloc();
2866 __b = unique_ptr<_Tp, void(*)(void*)>(__t, free);
2867 __new_cap /= sizeof(_Tp);
2868 __n = __b.get() + __n_off;
2869 __e = __b.get() + __new_cap;
2873 template <class _CharT, class _InputIterator>
2875 money_get<_CharT, _InputIterator>::__do_get(iter_type& __b, iter_type __e,
2876 bool __intl, const locale& __loc,
2877 ios_base::fmtflags __flags,
2878 ios_base::iostate& __err,
2880 const ctype<char_type>& __ct,
2881 unique_ptr<char_type, void(*)(void*)>& __wb,
2882 char_type*& __wn, char_type* __we)
2884 const unsigned __bz = 100;
2885 unsigned __gbuf[__bz];
2886 unique_ptr<unsigned, void(*)(void*)> __gb(__gbuf, __do_nothing);
2887 unsigned* __gn = __gb.get();
2888 unsigned* __ge = __gn + __bz;
2889 money_base::pattern __pat;
2896 // Capture the spaces read into money_base::{space,none} so they
2897 // can be compared to initial spaces in __sym.
2898 string_type __spaces;
2900 __money_get<_CharT>::__gather_info(__intl, __loc, __pat, __dp, __ts, __grp,
2901 __sym, __psn, __nsn, __fd);
2902 const string_type* __trailing_sign = 0;
2904 for (unsigned __p = 0; __p < 4 && __b != __e; ++__p)
2906 switch (__pat.field[__p])
2908 case money_base::space:
2911 if (__ct.is(ctype_base::space, *__b))
2912 __spaces.push_back(*__b++);
2915 __err |= ios_base::failbit;
2919 _LIBCPP_FALLTHROUGH();
2920 case money_base::none:
2923 while (__b != __e && __ct.is(ctype_base::space, *__b))
2924 __spaces.push_back(*__b++);
2927 case money_base::sign:
2928 if (__psn.size() + __nsn.size() > 0)
2930 if (__psn.size() == 0 || __nsn.size() == 0)
2931 { // sign is optional
2932 if (__psn.size() > 0)
2933 { // __nsn.size() == 0
2934 if (*__b == __psn[0])
2937 if (__psn.size() > 1)
2938 __trailing_sign = &__psn;
2943 else if (*__b == __nsn[0]) // __nsn.size() > 0 && __psn.size() == 0
2947 if (__nsn.size() > 1)
2948 __trailing_sign = &__nsn;
2951 else // sign is required
2953 if (*__b == __psn[0])
2956 if (__psn.size() > 1)
2957 __trailing_sign = &__psn;
2959 else if (*__b == __nsn[0])
2963 if (__nsn.size() > 1)
2964 __trailing_sign = &__nsn;
2968 __err |= ios_base::failbit;
2974 case money_base::symbol:
2976 bool __more_needed = __trailing_sign ||
2978 (__p == 2 && __pat.field[3] != static_cast<char>(money_base::none));
2979 bool __sb = (__flags & ios_base::showbase) != 0;
2980 if (__sb || __more_needed)
2982 typename string_type::const_iterator __sym_space_end = __sym.begin();
2983 if (__p > 0 && (__pat.field[__p - 1] == money_base::none ||
2984 __pat.field[__p - 1] == money_base::space)) {
2985 // Match spaces we've already read against spaces at
2986 // the beginning of __sym.
2987 while (__sym_space_end != __sym.end() &&
2988 __ct.is(ctype_base::space, *__sym_space_end))
2990 const size_t __num_spaces = __sym_space_end - __sym.begin();
2991 if (__num_spaces > __spaces.size() ||
2992 !equal(__spaces.end() - __num_spaces, __spaces.end(),
2994 // No match. Put __sym_space_end back at the
2995 // beginning of __sym, which will prevent a
2996 // match in the next loop.
2997 __sym_space_end = __sym.begin();
3000 typename string_type::const_iterator __sym_curr_char = __sym_space_end;
3001 while (__sym_curr_char != __sym.end() && __b != __e &&
3002 *__b == *__sym_curr_char) {
3006 if (__sb && __sym_curr_char != __sym.end())
3008 __err |= ios_base::failbit;
3014 case money_base::value:
3017 for (; __b != __e; ++__b)
3019 char_type __c = *__b;
3020 if (__ct.is(ctype_base::digit, __c))
3023 __double_or_nothing(__wb, __wn, __we);
3027 else if (__grp.size() > 0 && __ng > 0 && __c == __ts)
3030 __double_or_nothing(__gb, __gn, __ge);
3037 if (__gb.get() != __gn && __ng > 0)
3040 __double_or_nothing(__gb, __gn, __ge);
3045 if (__b == __e || *__b != __dp)
3047 __err |= ios_base::failbit;
3050 for (++__b; __fd > 0; --__fd, ++__b)
3052 if (__b == __e || !__ct.is(ctype_base::digit, *__b))
3054 __err |= ios_base::failbit;
3058 __double_or_nothing(__wb, __wn, __we);
3062 if (__wn == __wb.get())
3064 __err |= ios_base::failbit;
3071 if (__trailing_sign)
3073 for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, ++__b)
3075 if (__b == __e || *__b != (*__trailing_sign)[__i])
3077 __err |= ios_base::failbit;
3082 if (__gb.get() != __gn)
3084 ios_base::iostate __et = ios_base::goodbit;
3085 __check_grouping(__grp, __gb.get(), __gn, __et);
3088 __err |= ios_base::failbit;
3095 template <class _CharT, class _InputIterator>
3097 money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3098 bool __intl, ios_base& __iob,
3099 ios_base::iostate& __err,
3100 long double& __v) const
3102 const int __bz = 100;
3103 char_type __wbuf[__bz];
3104 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3106 char_type* __we = __wbuf + __bz;
3107 locale __loc = __iob.getloc();
3108 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3110 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3113 const char __src[] = "0123456789";
3114 char_type __atoms[sizeof(__src)-1];
3115 __ct.widen(__src, __src + (sizeof(__src)-1), __atoms);
3117 char* __nc = __nbuf;
3118 unique_ptr<char, void(*)(void*)> __h(0, free);
3119 if (__wn - __wb.get() > __bz-2)
3121 __h.reset((char*)malloc(static_cast<size_t>(__wn - __wb.get() + 2)));
3123 __throw_bad_alloc();
3128 for (const char_type* __w = __wb.get(); __w < __wn; ++__w, ++__nc)
3129 *__nc = __src[find(__atoms, _VSTD::end(__atoms), *__w) - __atoms];
3131 if (sscanf(__nbuf, "%Lf", &__v) != 1)
3132 __throw_runtime_error("money_get error");
3135 __err |= ios_base::eofbit;
3139 template <class _CharT, class _InputIterator>
3141 money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3142 bool __intl, ios_base& __iob,
3143 ios_base::iostate& __err,
3144 string_type& __v) const
3146 const int __bz = 100;
3147 char_type __wbuf[__bz];
3148 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3150 char_type* __we = __wbuf + __bz;
3151 locale __loc = __iob.getloc();
3152 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3154 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3159 __v.push_back(__ct.widen('-'));
3160 char_type __z = __ct.widen('0');
3162 for (__w = __wb.get(); __w < __wn-1; ++__w)
3165 __v.append(__w, __wn);
3168 __err |= ios_base::eofbit;
3172 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<char>)
3173 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<wchar_t>)
3177 template <class _CharT>
3181 typedef _CharT char_type;
3182 typedef basic_string<char_type> string_type;
3184 _LIBCPP_INLINE_VISIBILITY __money_put() {}
3186 static void __gather_info(bool __intl, bool __neg, const locale& __loc,
3187 money_base::pattern& __pat, char_type& __dp,
3188 char_type& __ts, string& __grp,
3189 string_type& __sym, string_type& __sn,
3191 static void __format(char_type* __mb, char_type*& __mi, char_type*& __me,
3192 ios_base::fmtflags __flags,
3193 const char_type* __db, const char_type* __de,
3194 const ctype<char_type>& __ct, bool __neg,
3195 const money_base::pattern& __pat, char_type __dp,
3196 char_type __ts, const string& __grp,
3197 const string_type& __sym, const string_type& __sn,
3201 template <class _CharT>
3203 __money_put<_CharT>::__gather_info(bool __intl, bool __neg, const locale& __loc,
3204 money_base::pattern& __pat, char_type& __dp,
3205 char_type& __ts, string& __grp,
3206 string_type& __sym, string_type& __sn,
3211 const moneypunct<char_type, true>& __mp =
3212 use_facet<moneypunct<char_type, true> >(__loc);
3215 __pat = __mp.neg_format();
3216 __sn = __mp.negative_sign();
3220 __pat = __mp.pos_format();
3221 __sn = __mp.positive_sign();
3223 __dp = __mp.decimal_point();
3224 __ts = __mp.thousands_sep();
3225 __grp = __mp.grouping();
3226 __sym = __mp.curr_symbol();
3227 __fd = __mp.frac_digits();
3231 const moneypunct<char_type, false>& __mp =
3232 use_facet<moneypunct<char_type, false> >(__loc);
3235 __pat = __mp.neg_format();
3236 __sn = __mp.negative_sign();
3240 __pat = __mp.pos_format();
3241 __sn = __mp.positive_sign();
3243 __dp = __mp.decimal_point();
3244 __ts = __mp.thousands_sep();
3245 __grp = __mp.grouping();
3246 __sym = __mp.curr_symbol();
3247 __fd = __mp.frac_digits();
3251 template <class _CharT>
3253 __money_put<_CharT>::__format(char_type* __mb, char_type*& __mi, char_type*& __me,
3254 ios_base::fmtflags __flags,
3255 const char_type* __db, const char_type* __de,
3256 const ctype<char_type>& __ct, bool __neg,
3257 const money_base::pattern& __pat, char_type __dp,
3258 char_type __ts, const string& __grp,
3259 const string_type& __sym, const string_type& __sn,
3263 for (unsigned __p = 0; __p < 4; ++__p)
3265 switch (__pat.field[__p])
3267 case money_base::none:
3270 case money_base::space:
3272 *__me++ = __ct.widen(' ');
3274 case money_base::sign:
3278 case money_base::symbol:
3279 if (!__sym.empty() && (__flags & ios_base::showbase))
3280 __me = _VSTD::copy(__sym.begin(), __sym.end(), __me);
3282 case money_base::value:
3284 // remember start of value so we can reverse it
3285 char_type* __t = __me;
3286 // find beginning of digits
3289 // find end of digits
3290 const char_type* __d;
3291 for (__d = __db; __d < __de; ++__d)
3292 if (!__ct.is(ctype_base::digit, *__d))
3294 // print fractional part
3298 for (__f = __fd; __d > __db && __f > 0; --__f)
3300 char_type __z = __f > 0 ? __ct.widen('0') : char_type();
3301 for (; __f > 0; --__f)
3308 *__me++ = __ct.widen('0');
3314 unsigned __gl = __grp.empty() ? numeric_limits<unsigned>::max()
3315 : static_cast<unsigned>(__grp[__ig]);
3322 if (++__ig < __grp.size())
3323 __gl = __grp[__ig] == numeric_limits<char>::max() ?
3324 numeric_limits<unsigned>::max() :
3325 static_cast<unsigned>(__grp[__ig]);
3337 // print rest of sign, if any
3338 if (__sn.size() > 1)
3339 __me = _VSTD::copy(__sn.begin()+1, __sn.end(), __me);
3341 if ((__flags & ios_base::adjustfield) == ios_base::left)
3343 else if ((__flags & ios_base::adjustfield) != ios_base::internal)
3347 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<char>)
3348 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<wchar_t>)
3350 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
3351 class _LIBCPP_TEMPLATE_VIS money_put
3352 : public locale::facet,
3353 private __money_put<_CharT>
3356 typedef _CharT char_type;
3357 typedef _OutputIterator iter_type;
3358 typedef basic_string<char_type> string_type;
3360 _LIBCPP_INLINE_VISIBILITY
3361 explicit money_put(size_t __refs = 0)
3362 : locale::facet(__refs) {}
3364 _LIBCPP_INLINE_VISIBILITY
3365 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3366 long double __units) const
3368 return do_put(__s, __intl, __iob, __fl, __units);
3371 _LIBCPP_INLINE_VISIBILITY
3372 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3373 const string_type& __digits) const
3375 return do_put(__s, __intl, __iob, __fl, __digits);
3378 static locale::id id;
3381 _LIBCPP_INLINE_VISIBILITY
3384 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3385 char_type __fl, long double __units) const;
3386 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3387 char_type __fl, const string_type& __digits) const;
3390 template <class _CharT, class _OutputIterator>
3392 money_put<_CharT, _OutputIterator>::id;
3394 template <class _CharT, class _OutputIterator>
3396 money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3397 ios_base& __iob, char_type __fl,
3398 long double __units) const
3401 const size_t __bs = 100;
3404 char_type __digits[__bs];
3405 char_type* __db = __digits;
3406 size_t __n = static_cast<size_t>(snprintf(__bb, __bs, "%.0Lf", __units));
3407 unique_ptr<char, void(*)(void*)> __hn(0, free);
3408 unique_ptr<char_type, void(*)(void*)> __hd(0, free);
3409 // secure memory for digit storage
3412 __n = static_cast<size_t>(__libcpp_asprintf_l(&__bb, _LIBCPP_GET_C_LOCALE, "%.0Lf", __units));
3414 __throw_bad_alloc();
3416 __hd.reset((char_type*)malloc(__n * sizeof(char_type)));
3417 if (__hd == nullptr)
3418 __throw_bad_alloc();
3422 locale __loc = __iob.getloc();
3423 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3424 __ct.widen(__bb, __bb + __n, __db);
3425 bool __neg = __n > 0 && __bb[0] == '-';
3426 money_base::pattern __pat;
3433 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3434 // secure memory for formatting
3435 char_type __mbuf[__bs];
3436 char_type* __mb = __mbuf;
3437 unique_ptr<char_type, void(*)(void*)> __hw(0, free);
3438 size_t __exn = static_cast<int>(__n) > __fd ?
3439 (__n - static_cast<size_t>(__fd)) * 2 + __sn.size() +
3440 __sym.size() + static_cast<size_t>(__fd) + 1
3441 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
3444 __hw.reset((char_type*)malloc(__exn * sizeof(char_type)));
3447 __throw_bad_alloc();
3452 this->__format(__mb, __mi, __me, __iob.flags(),
3453 __db, __db + __n, __ct,
3454 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3455 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3458 template <class _CharT, class _OutputIterator>
3460 money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3461 ios_base& __iob, char_type __fl,
3462 const string_type& __digits) const
3465 locale __loc = __iob.getloc();
3466 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3467 bool __neg = __digits.size() > 0 && __digits[0] == __ct.widen('-');
3468 money_base::pattern __pat;
3475 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3476 // secure memory for formatting
3477 char_type __mbuf[100];
3478 char_type* __mb = __mbuf;
3479 unique_ptr<char_type, void(*)(void*)> __h(0, free);
3480 size_t __exn = static_cast<int>(__digits.size()) > __fd ?
3481 (__digits.size() - static_cast<size_t>(__fd)) * 2 +
3482 __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 1
3483 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
3486 __h.reset((char_type*)malloc(__exn * sizeof(char_type)));
3489 __throw_bad_alloc();
3494 this->__format(__mb, __mi, __me, __iob.flags(),
3495 __digits.data(), __digits.data() + __digits.size(), __ct,
3496 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3497 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3500 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<char>)
3501 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<wchar_t>)
3505 class _LIBCPP_TYPE_VIS messages_base
3508 typedef ptrdiff_t catalog;
3510 _LIBCPP_INLINE_VISIBILITY messages_base() {}
3513 template <class _CharT>
3514 class _LIBCPP_TEMPLATE_VIS messages
3515 : public locale::facet,
3516 public messages_base
3519 typedef _CharT char_type;
3520 typedef basic_string<_CharT> string_type;
3522 _LIBCPP_INLINE_VISIBILITY
3523 explicit messages(size_t __refs = 0)
3524 : locale::facet(__refs) {}
3526 _LIBCPP_INLINE_VISIBILITY
3527 catalog open(const basic_string<char>& __nm, const locale& __loc) const
3529 return do_open(__nm, __loc);
3532 _LIBCPP_INLINE_VISIBILITY
3533 string_type get(catalog __c, int __set, int __msgid,
3534 const string_type& __dflt) const
3536 return do_get(__c, __set, __msgid, __dflt);
3539 _LIBCPP_INLINE_VISIBILITY
3540 void close(catalog __c) const
3545 static locale::id id;
3548 _LIBCPP_INLINE_VISIBILITY
3551 virtual catalog do_open(const basic_string<char>&, const locale&) const;
3552 virtual string_type do_get(catalog, int __set, int __msgid,
3553 const string_type& __dflt) const;
3554 virtual void do_close(catalog) const;
3557 template <class _CharT>
3559 messages<_CharT>::id;
3561 template <class _CharT>
3562 typename messages<_CharT>::catalog
3563 messages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const
3565 #ifdef _LIBCPP_HAS_CATOPEN
3566 catalog __cat = (catalog)catopen(__nm.c_str(), NL_CAT_LOCALE);
3568 __cat = static_cast<catalog>((static_cast<size_t>(__cat) >> 1));
3570 #else // !_LIBCPP_HAS_CATOPEN
3571 _LIBCPP_UNUSED_VAR(__nm);
3573 #endif // _LIBCPP_HAS_CATOPEN
3576 template <class _CharT>
3577 typename messages<_CharT>::string_type
3578 messages<_CharT>::do_get(catalog __c, int __set, int __msgid,
3579 const string_type& __dflt) const
3581 #ifdef _LIBCPP_HAS_CATOPEN
3583 __narrow_to_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__ndflt),
3585 __dflt.c_str() + __dflt.size());
3588 nl_catd __cat = (nl_catd)__c;
3589 char* __n = catgets(__cat, __set, __msgid, __ndflt.c_str());
3591 __widen_from_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__w),
3592 __n, __n + strlen(__n));
3594 #else // !_LIBCPP_HAS_CATOPEN
3595 _LIBCPP_UNUSED_VAR(__c);
3596 _LIBCPP_UNUSED_VAR(__set);
3597 _LIBCPP_UNUSED_VAR(__msgid);
3599 #endif // _LIBCPP_HAS_CATOPEN
3602 template <class _CharT>
3604 messages<_CharT>::do_close(catalog __c) const
3606 #ifdef _LIBCPP_HAS_CATOPEN
3609 nl_catd __cat = (nl_catd)__c;
3611 #else // !_LIBCPP_HAS_CATOPEN
3612 _LIBCPP_UNUSED_VAR(__c);
3613 #endif // _LIBCPP_HAS_CATOPEN
3616 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<char>)
3617 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<wchar_t>)
3619 template <class _CharT>
3620 class _LIBCPP_TEMPLATE_VIS messages_byname
3621 : public messages<_CharT>
3624 typedef messages_base::catalog catalog;
3625 typedef basic_string<_CharT> string_type;
3627 _LIBCPP_INLINE_VISIBILITY
3628 explicit messages_byname(const char*, size_t __refs = 0)
3629 : messages<_CharT>(__refs) {}
3631 _LIBCPP_INLINE_VISIBILITY
3632 explicit messages_byname(const string&, size_t __refs = 0)
3633 : messages<_CharT>(__refs) {}
3636 _LIBCPP_INLINE_VISIBILITY
3637 ~messages_byname() {}
3640 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<char>)
3641 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<wchar_t>)
3643 template<class _Codecvt, class _Elem = wchar_t,
3644 class _Wide_alloc = allocator<_Elem>,
3645 class _Byte_alloc = allocator<char> >
3646 class _LIBCPP_TEMPLATE_VIS wstring_convert
3649 typedef basic_string<char, char_traits<char>, _Byte_alloc> byte_string;
3650 typedef basic_string<_Elem, char_traits<_Elem>, _Wide_alloc> wide_string;
3651 typedef typename _Codecvt::state_type state_type;
3652 typedef typename wide_string::traits_type::int_type int_type;
3655 byte_string __byte_err_string_;
3656 wide_string __wide_err_string_;
3657 _Codecvt* __cvtptr_;
3658 state_type __cvtstate_;
3661 wstring_convert(const wstring_convert& __wc);
3662 wstring_convert& operator=(const wstring_convert& __wc);
3664 _LIBCPP_INLINE_VISIBILITY
3665 _LIBCPP_EXPLICIT_AFTER_CXX11 wstring_convert(_Codecvt* __pcvt = new _Codecvt);
3666 _LIBCPP_INLINE_VISIBILITY
3667 wstring_convert(_Codecvt* __pcvt, state_type __state);
3668 _LIBCPP_EXPLICIT_AFTER_CXX11 wstring_convert(const byte_string& __byte_err,
3669 const wide_string& __wide_err = wide_string());
3670 #ifndef _LIBCPP_CXX03_LANG
3671 _LIBCPP_INLINE_VISIBILITY
3672 wstring_convert(wstring_convert&& __wc);
3676 _LIBCPP_INLINE_VISIBILITY
3677 wide_string from_bytes(char __byte)
3678 {return from_bytes(&__byte, &__byte+1);}
3679 _LIBCPP_INLINE_VISIBILITY
3680 wide_string from_bytes(const char* __ptr)
3681 {return from_bytes(__ptr, __ptr + char_traits<char>::length(__ptr));}
3682 _LIBCPP_INLINE_VISIBILITY
3683 wide_string from_bytes(const byte_string& __str)
3684 {return from_bytes(__str.data(), __str.data() + __str.size());}
3685 wide_string from_bytes(const char* __first, const char* __last);
3687 _LIBCPP_INLINE_VISIBILITY
3688 byte_string to_bytes(_Elem __wchar)
3689 {return to_bytes(&__wchar, &__wchar+1);}
3690 _LIBCPP_INLINE_VISIBILITY
3691 byte_string to_bytes(const _Elem* __wptr)
3692 {return to_bytes(__wptr, __wptr + char_traits<_Elem>::length(__wptr));}
3693 _LIBCPP_INLINE_VISIBILITY
3694 byte_string to_bytes(const wide_string& __wstr)
3695 {return to_bytes(__wstr.data(), __wstr.data() + __wstr.size());}
3696 byte_string to_bytes(const _Elem* __first, const _Elem* __last);
3698 _LIBCPP_INLINE_VISIBILITY
3699 size_t converted() const _NOEXCEPT {return __cvtcount_;}
3700 _LIBCPP_INLINE_VISIBILITY
3701 state_type state() const {return __cvtstate_;}
3704 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3706 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3707 wstring_convert(_Codecvt* __pcvt)
3708 : __cvtptr_(__pcvt), __cvtstate_(), __cvtcount_(0)
3712 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3714 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3715 wstring_convert(_Codecvt* __pcvt, state_type __state)
3716 : __cvtptr_(__pcvt), __cvtstate_(__state), __cvtcount_(0)
3720 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3721 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3722 wstring_convert(const byte_string& __byte_err, const wide_string& __wide_err)
3723 : __byte_err_string_(__byte_err), __wide_err_string_(__wide_err),
3724 __cvtstate_(), __cvtcount_(0)
3726 __cvtptr_ = new _Codecvt;
3729 #ifndef _LIBCPP_CXX03_LANG
3731 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3733 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3734 wstring_convert(wstring_convert&& __wc)
3735 : __byte_err_string_(_VSTD::move(__wc.__byte_err_string_)),
3736 __wide_err_string_(_VSTD::move(__wc.__wide_err_string_)),
3737 __cvtptr_(__wc.__cvtptr_),
3738 __cvtstate_(__wc.__cvtstate_), __cvtcount_(__wc.__cvtcount_)
3740 __wc.__cvtptr_ = nullptr;
3743 #endif // _LIBCPP_CXX03_LANG
3745 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3746 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::~wstring_convert()
3751 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3752 typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::wide_string
3753 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3754 from_bytes(const char* __frm, const char* __frm_end)
3757 if (__cvtptr_ != nullptr)
3759 wide_string __ws(2*(__frm_end - __frm), _Elem());
3760 if (__frm != __frm_end)
3761 __ws.resize(__ws.capacity());
3762 codecvt_base::result __r = codecvt_base::ok;
3763 state_type __st = __cvtstate_;
3764 if (__frm != __frm_end)
3766 _Elem* __to = &__ws[0];
3767 _Elem* __to_end = __to + __ws.size();
3768 const char* __frm_nxt;
3772 __r = __cvtptr_->in(__st, __frm, __frm_end, __frm_nxt,
3773 __to, __to_end, __to_nxt);
3774 __cvtcount_ += __frm_nxt - __frm;
3775 if (__frm_nxt == __frm)
3777 __r = codecvt_base::error;
3779 else if (__r == codecvt_base::noconv)
3781 __ws.resize(__to - &__ws[0]);
3782 // This only gets executed if _Elem is char
3783 __ws.append((const _Elem*)__frm, (const _Elem*)__frm_end);
3785 __r = codecvt_base::ok;
3787 else if (__r == codecvt_base::ok)
3789 __ws.resize(__to_nxt - &__ws[0]);
3792 else if (__r == codecvt_base::partial)
3794 ptrdiff_t __s = __to_nxt - &__ws[0];
3795 __ws.resize(2 * __s);
3796 __to = &__ws[0] + __s;
3797 __to_end = &__ws[0] + __ws.size();
3800 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3802 if (__r == codecvt_base::ok)
3806 if (__wide_err_string_.empty())
3807 __throw_range_error("wstring_convert: from_bytes error");
3809 return __wide_err_string_;
3812 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3813 typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::byte_string
3814 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3815 to_bytes(const _Elem* __frm, const _Elem* __frm_end)
3818 if (__cvtptr_ != nullptr)
3820 byte_string __bs(2*(__frm_end - __frm), char());
3821 if (__frm != __frm_end)
3822 __bs.resize(__bs.capacity());
3823 codecvt_base::result __r = codecvt_base::ok;
3824 state_type __st = __cvtstate_;
3825 if (__frm != __frm_end)
3827 char* __to = &__bs[0];
3828 char* __to_end = __to + __bs.size();
3829 const _Elem* __frm_nxt;
3833 __r = __cvtptr_->out(__st, __frm, __frm_end, __frm_nxt,
3834 __to, __to_end, __to_nxt);
3835 __cvtcount_ += __frm_nxt - __frm;
3836 if (__frm_nxt == __frm)
3838 __r = codecvt_base::error;
3840 else if (__r == codecvt_base::noconv)
3842 __bs.resize(__to - &__bs[0]);
3843 // This only gets executed if _Elem is char
3844 __bs.append((const char*)__frm, (const char*)__frm_end);
3846 __r = codecvt_base::ok;
3848 else if (__r == codecvt_base::ok)
3850 __bs.resize(__to_nxt - &__bs[0]);
3853 else if (__r == codecvt_base::partial)
3855 ptrdiff_t __s = __to_nxt - &__bs[0];
3856 __bs.resize(2 * __s);
3857 __to = &__bs[0] + __s;
3858 __to_end = &__bs[0] + __bs.size();
3861 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3863 if (__r == codecvt_base::ok)
3865 size_t __s = __bs.size();
3866 __bs.resize(__bs.capacity());
3867 char* __to = &__bs[0] + __s;
3868 char* __to_end = __to + __bs.size();
3872 __r = __cvtptr_->unshift(__st, __to, __to_end, __to_nxt);
3873 if (__r == codecvt_base::noconv)
3875 __bs.resize(__to - &__bs[0]);
3876 __r = codecvt_base::ok;
3878 else if (__r == codecvt_base::ok)
3880 __bs.resize(__to_nxt - &__bs[0]);
3882 else if (__r == codecvt_base::partial)
3884 ptrdiff_t __sp = __to_nxt - &__bs[0];
3885 __bs.resize(2 * __sp);
3886 __to = &__bs[0] + __sp;
3887 __to_end = &__bs[0] + __bs.size();
3889 } while (__r == codecvt_base::partial);
3890 if (__r == codecvt_base::ok)
3895 if (__byte_err_string_.empty())
3896 __throw_range_error("wstring_convert: to_bytes error");
3898 return __byte_err_string_;
3901 template <class _Codecvt, class _Elem = wchar_t, class _Tr = char_traits<_Elem> >
3902 class _LIBCPP_TEMPLATE_VIS wbuffer_convert
3903 : public basic_streambuf<_Elem, _Tr>
3907 typedef _Elem char_type;
3908 typedef _Tr traits_type;
3909 typedef typename traits_type::int_type int_type;
3910 typedef typename traits_type::pos_type pos_type;
3911 typedef typename traits_type::off_type off_type;
3912 typedef typename _Codecvt::state_type state_type;
3916 const char* __extbufnext_;
3917 const char* __extbufend_;
3918 char __extbuf_min_[8];
3920 char_type* __intbuf_;
3922 streambuf* __bufptr_;
3925 ios_base::openmode __cm_;
3928 bool __always_noconv_;
3930 wbuffer_convert(const wbuffer_convert&);
3931 wbuffer_convert& operator=(const wbuffer_convert&);
3933 _LIBCPP_EXPLICIT_AFTER_CXX11 wbuffer_convert(streambuf* __bytebuf = 0,
3934 _Codecvt* __pcvt = new _Codecvt, state_type __state = state_type());
3937 _LIBCPP_INLINE_VISIBILITY
3938 streambuf* rdbuf() const {return __bufptr_;}
3939 _LIBCPP_INLINE_VISIBILITY
3940 streambuf* rdbuf(streambuf* __bytebuf)
3942 streambuf* __r = __bufptr_;
3943 __bufptr_ = __bytebuf;
3947 _LIBCPP_INLINE_VISIBILITY
3948 state_type state() const {return __st_;}
3951 virtual int_type underflow();
3952 virtual int_type pbackfail(int_type __c = traits_type::eof());
3953 virtual int_type overflow (int_type __c = traits_type::eof());
3954 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s,
3956 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
3957 ios_base::openmode __wch = ios_base::in | ios_base::out);
3958 virtual pos_type seekpos(pos_type __sp,
3959 ios_base::openmode __wch = ios_base::in | ios_base::out);
3964 void __write_mode();
3965 wbuffer_convert* __close();
3968 template <class _Codecvt, class _Elem, class _Tr>
3969 wbuffer_convert<_Codecvt, _Elem, _Tr>::
3970 wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt, state_type __state)
3977 __bufptr_(__bytebuf),
3983 __always_noconv_(__cv_ ? __cv_->always_noconv() : false)
3988 template <class _Codecvt, class _Elem, class _Tr>
3989 wbuffer_convert<_Codecvt, _Elem, _Tr>::~wbuffer_convert()
3994 delete [] __extbuf_;
3996 delete [] __intbuf_;
3999 template <class _Codecvt, class _Elem, class _Tr>
4000 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4001 wbuffer_convert<_Codecvt, _Elem, _Tr>::underflow()
4003 if (__cv_ == 0 || __bufptr_ == 0)
4004 return traits_type::eof();
4005 bool __initial = __read_mode();
4007 if (this->gptr() == 0)
4008 this->setg(&__1buf, &__1buf+1, &__1buf+1);
4009 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
4010 int_type __c = traits_type::eof();
4011 if (this->gptr() == this->egptr())
4013 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
4014 if (__always_noconv_)
4016 streamsize __nmemb = static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz);
4017 __nmemb = __bufptr_->sgetn((char*)this->eback() + __unget_sz, __nmemb);
4020 this->setg(this->eback(),
4021 this->eback() + __unget_sz,
4022 this->eback() + __unget_sz + __nmemb);
4023 __c = *this->gptr();
4028 _LIBCPP_ASSERT(!(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
4029 if (__extbufend_ != __extbufnext_)
4030 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
4031 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
4032 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
4033 streamsize __nmemb = _VSTD::min(static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz),
4034 static_cast<streamsize>(__extbufend_ - __extbufnext_));
4035 codecvt_base::result __r;
4036 // FIXME: Do we ever need to restore the state here?
4037 //state_type __svs = __st_;
4038 streamsize __nr = __bufptr_->sgetn(const_cast<char*>(__extbufnext_), __nmemb);
4041 __extbufend_ = __extbufnext_ + __nr;
4043 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
4044 this->eback() + __unget_sz,
4045 this->egptr(), __inext);
4046 if (__r == codecvt_base::noconv)
4048 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
4049 (char_type*) const_cast<char *>(__extbufend_));
4050 __c = *this->gptr();
4052 else if (__inext != this->eback() + __unget_sz)
4054 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
4055 __c = *this->gptr();
4061 __c = *this->gptr();
4062 if (this->eback() == &__1buf)
4063 this->setg(0, 0, 0);
4067 template <class _Codecvt, class _Elem, class _Tr>
4068 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4069 wbuffer_convert<_Codecvt, _Elem, _Tr>::pbackfail(int_type __c)
4071 if (__cv_ != 0 && __bufptr_ != 0 && this->eback() < this->gptr())
4073 if (traits_type::eq_int_type(__c, traits_type::eof()))
4076 return traits_type::not_eof(__c);
4078 if (traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
4081 *this->gptr() = traits_type::to_char_type(__c);
4085 return traits_type::eof();
4088 template <class _Codecvt, class _Elem, class _Tr>
4089 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4090 wbuffer_convert<_Codecvt, _Elem, _Tr>::overflow(int_type __c)
4092 if (__cv_ == 0 || __bufptr_ == 0)
4093 return traits_type::eof();
4096 char_type* __pb_save = this->pbase();
4097 char_type* __epb_save = this->epptr();
4098 if (!traits_type::eq_int_type(__c, traits_type::eof()))
4100 if (this->pptr() == 0)
4101 this->setp(&__1buf, &__1buf+1);
4102 *this->pptr() = traits_type::to_char_type(__c);
4105 if (this->pptr() != this->pbase())
4107 if (__always_noconv_)
4109 streamsize __nmemb = static_cast<streamsize>(this->pptr() - this->pbase());
4110 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4111 return traits_type::eof();
4115 char* __extbe = __extbuf_;
4116 codecvt_base::result __r;
4119 const char_type* __e;
4120 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
4121 __extbuf_, __extbuf_ + __ebs_, __extbe);
4122 if (__e == this->pbase())
4123 return traits_type::eof();
4124 if (__r == codecvt_base::noconv)
4126 streamsize __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
4127 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4128 return traits_type::eof();
4130 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
4132 streamsize __nmemb = static_cast<size_t>(__extbe - __extbuf_);
4133 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4134 return traits_type::eof();
4135 if (__r == codecvt_base::partial)
4137 this->setp(const_cast<char_type *>(__e), this->pptr());
4138 this->__pbump(this->epptr() - this->pbase());
4142 return traits_type::eof();
4143 } while (__r == codecvt_base::partial);
4145 this->setp(__pb_save, __epb_save);
4147 return traits_type::not_eof(__c);
4150 template <class _Codecvt, class _Elem, class _Tr>
4151 basic_streambuf<_Elem, _Tr>*
4152 wbuffer_convert<_Codecvt, _Elem, _Tr>::setbuf(char_type* __s, streamsize __n)
4154 this->setg(0, 0, 0);
4157 delete [] __extbuf_;
4159 delete [] __intbuf_;
4161 if (__ebs_ > sizeof(__extbuf_min_))
4163 if (__always_noconv_ && __s)
4165 __extbuf_ = (char*)__s;
4170 __extbuf_ = new char[__ebs_];
4176 __extbuf_ = __extbuf_min_;
4177 __ebs_ = sizeof(__extbuf_min_);
4180 if (!__always_noconv_)
4182 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
4183 if (__s && __ibs_ >= sizeof(__extbuf_min_))
4190 __intbuf_ = new char_type[__ibs_];
4203 template <class _Codecvt, class _Elem, class _Tr>
4204 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4205 wbuffer_convert<_Codecvt, _Elem, _Tr>::seekoff(off_type __off, ios_base::seekdir __way,
4206 ios_base::openmode __om)
4208 int __width = __cv_->encoding();
4209 if (__cv_ == 0 || __bufptr_ == 0 || (__width <= 0 && __off != 0) || sync())
4210 return pos_type(off_type(-1));
4211 // __width > 0 || __off == 0, now check __way
4212 if (__way != ios_base::beg && __way != ios_base::cur && __way != ios_base::end)
4213 return pos_type(off_type(-1));
4214 pos_type __r = __bufptr_->pubseekoff(__width * __off, __way, __om);
4219 template <class _Codecvt, class _Elem, class _Tr>
4220 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4221 wbuffer_convert<_Codecvt, _Elem, _Tr>::seekpos(pos_type __sp, ios_base::openmode __wch)
4223 if (__cv_ == 0 || __bufptr_ == 0 || sync())
4224 return pos_type(off_type(-1));
4225 if (__bufptr_->pubseekpos(__sp, __wch) == pos_type(off_type(-1)))
4226 return pos_type(off_type(-1));
4230 template <class _Codecvt, class _Elem, class _Tr>
4232 wbuffer_convert<_Codecvt, _Elem, _Tr>::sync()
4234 if (__cv_ == 0 || __bufptr_ == 0)
4236 if (__cm_ & ios_base::out)
4238 if (this->pptr() != this->pbase())
4239 if (overflow() == traits_type::eof())
4241 codecvt_base::result __r;
4245 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
4246 streamsize __nmemb = static_cast<streamsize>(__extbe - __extbuf_);
4247 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4249 } while (__r == codecvt_base::partial);
4250 if (__r == codecvt_base::error)
4252 if (__bufptr_->pubsync())
4255 else if (__cm_ & ios_base::in)
4258 if (__always_noconv_)
4259 __c = this->egptr() - this->gptr();
4262 int __width = __cv_->encoding();
4263 __c = __extbufend_ - __extbufnext_;
4265 __c += __width * (this->egptr() - this->gptr());
4268 if (this->gptr() != this->egptr())
4270 reverse(this->gptr(), this->egptr());
4271 codecvt_base::result __r;
4272 const char_type* __e = this->gptr();
4276 __r = __cv_->out(__st_, __e, this->egptr(), __e,
4277 __extbuf_, __extbuf_ + __ebs_, __extbe);
4280 case codecvt_base::noconv:
4281 __c += this->egptr() - this->gptr();
4283 case codecvt_base::ok:
4284 case codecvt_base::partial:
4285 __c += __extbe - __extbuf_;
4290 } while (__r == codecvt_base::partial);
4294 if (__bufptr_->pubseekoff(-__c, ios_base::cur, __cm_) == pos_type(off_type(-1)))
4296 this->setg(0, 0, 0);
4302 template <class _Codecvt, class _Elem, class _Tr>
4304 wbuffer_convert<_Codecvt, _Elem, _Tr>::__read_mode()
4306 if (!(__cm_ & ios_base::in))
4309 if (__always_noconv_)
4310 this->setg((char_type*)__extbuf_,
4311 (char_type*)__extbuf_ + __ebs_,
4312 (char_type*)__extbuf_ + __ebs_);
4314 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
4315 __cm_ = ios_base::in;
4321 template <class _Codecvt, class _Elem, class _Tr>
4323 wbuffer_convert<_Codecvt, _Elem, _Tr>::__write_mode()
4325 if (!(__cm_ & ios_base::out))
4327 this->setg(0, 0, 0);
4328 if (__ebs_ > sizeof(__extbuf_min_))
4330 if (__always_noconv_)
4331 this->setp((char_type*)__extbuf_,
4332 (char_type*)__extbuf_ + (__ebs_ - 1));
4334 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
4338 __cm_ = ios_base::out;
4342 template <class _Codecvt, class _Elem, class _Tr>
4343 wbuffer_convert<_Codecvt, _Elem, _Tr>*
4344 wbuffer_convert<_Codecvt, _Elem, _Tr>::__close()
4346 wbuffer_convert* __rt = 0;
4347 if (__cv_ != 0 && __bufptr_ != 0)
4350 if ((__cm_ & ios_base::out) && sync())
4356 _LIBCPP_END_NAMESPACE_STD
4360 #endif // _LIBCPP_LOCALE