2 //===-------------------------- locale ------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_LOCALE
12 #define _LIBCPP_LOCALE
28 static const category // values assigned here are for exposition only
36 all = collate | ctype | monetary | numeric | time | messages;
38 // construct/copy/destroy:
40 locale(const locale& other) noexcept;
41 explicit locale(const char* std_name);
42 explicit locale(const string& std_name);
43 locale(const locale& other, const char* std_name, category);
44 locale(const locale& other, const string& std_name, category);
45 template <class Facet> locale(const locale& other, Facet* f);
46 locale(const locale& other, const locale& one, category);
48 ~locale(); // not virtual
50 const locale& operator=(const locale& other) noexcept;
52 template <class Facet> locale combine(const locale& other) const;
55 basic_string<char> name() const;
56 bool operator==(const locale& other) const;
57 bool operator!=(const locale& other) const;
58 template <class charT, class Traits, class Allocator>
59 bool operator()(const basic_string<charT,Traits,Allocator>& s1,
60 const basic_string<charT,Traits,Allocator>& s2) const;
62 // global locale objects:
63 static locale global(const locale&);
64 static const locale& classic();
67 template <class Facet> const Facet& use_facet(const locale&);
68 template <class Facet> bool has_facet(const locale&) noexcept;
70 // 22.3.3, convenience interfaces:
71 template <class charT> bool isspace (charT c, const locale& loc);
72 template <class charT> bool isprint (charT c, const locale& loc);
73 template <class charT> bool iscntrl (charT c, const locale& loc);
74 template <class charT> bool isupper (charT c, const locale& loc);
75 template <class charT> bool islower (charT c, const locale& loc);
76 template <class charT> bool isalpha (charT c, const locale& loc);
77 template <class charT> bool isdigit (charT c, const locale& loc);
78 template <class charT> bool ispunct (charT c, const locale& loc);
79 template <class charT> bool isxdigit(charT c, const locale& loc);
80 template <class charT> bool isalnum (charT c, const locale& loc);
81 template <class charT> bool isgraph (charT c, const locale& loc);
82 template <class charT> charT toupper(charT c, const locale& loc);
83 template <class charT> charT tolower(charT c, const locale& loc);
85 template<class Codecvt, class Elem = wchar_t,
86 class Wide_alloc = allocator<Elem>,
87 class Byte_alloc = allocator<char>>
91 typedef basic_string<char, char_traits<char>, Byte_alloc> byte_string;
92 typedef basic_string<Elem, char_traits<Elem>, Wide_alloc> wide_string;
93 typedef typename Codecvt::state_type state_type;
94 typedef typename wide_string::traits_type::int_type int_type;
96 explicit wstring_convert(Codecvt* pcvt = new Codecvt); // explicit in C++14
97 wstring_convert(Codecvt* pcvt, state_type state);
98 explicit wstring_convert(const byte_string& byte_err, // explicit in C++14
99 const wide_string& wide_err = wide_string());
100 wstring_convert(const wstring_convert&) = delete; // C++14
101 wstring_convert & operator=(const wstring_convert &) = delete; // C++14
104 wide_string from_bytes(char byte);
105 wide_string from_bytes(const char* ptr);
106 wide_string from_bytes(const byte_string& str);
107 wide_string from_bytes(const char* first, const char* last);
109 byte_string to_bytes(Elem wchar);
110 byte_string to_bytes(const Elem* wptr);
111 byte_string to_bytes(const wide_string& wstr);
112 byte_string to_bytes(const Elem* first, const Elem* last);
114 size_t converted() const; // noexcept in C++14
115 state_type state() const;
118 template <class Codecvt, class Elem = wchar_t, class Tr = char_traits<Elem>>
119 class wbuffer_convert
120 : public basic_streambuf<Elem, Tr>
123 typedef typename Tr::state_type state_type;
125 explicit wbuffer_convert(streambuf* bytebuf = 0, Codecvt* pcvt = new Codecvt,
126 state_type state = state_type()); // explicit in C++14
127 wbuffer_convert(const wbuffer_convert&) = delete; // C++14
128 wbuffer_convert & operator=(const wbuffer_convert &) = delete; // C++14
129 ~wbuffer_convert(); // C++14
131 streambuf* rdbuf() const;
132 streambuf* rdbuf(streambuf* bytebuf);
134 state_type state() const;
137 // 22.4.1 and 22.4.1.3, ctype:
139 template <class charT> class ctype;
140 template <> class ctype<char>; // specialization
141 template <class charT> class ctype_byname;
142 template <> class ctype_byname<char>; // specialization
145 template <class internT, class externT, class stateT> class codecvt;
146 template <class internT, class externT, class stateT> class codecvt_byname;
148 // 22.4.2 and 22.4.3, numeric:
149 template <class charT, class InputIterator> class num_get;
150 template <class charT, class OutputIterator> class num_put;
151 template <class charT> class numpunct;
152 template <class charT> class numpunct_byname;
154 // 22.4.4, col lation:
155 template <class charT> class collate;
156 template <class charT> class collate_byname;
158 // 22.4.5, date and time:
160 template <class charT, class InputIterator> class time_get;
161 template <class charT, class InputIterator> class time_get_byname;
162 template <class charT, class OutputIterator> class time_put;
163 template <class charT, class OutputIterator> class time_put_byname;
167 template <class charT, class InputIterator> class money_get;
168 template <class charT, class OutputIterator> class money_put;
169 template <class charT, bool Intl> class moneypunct;
170 template <class charT, bool Intl> class moneypunct_byname;
172 // 22.4.7, message retrieval:
174 template <class charT> class messages;
175 template <class charT> class messages_byname;
195 #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
196 #include <support/win32/locale_win32.h>
197 #elif defined(_NEWLIB_VERSION)
198 // FIXME: replace all the uses of _NEWLIB_VERSION with __NEWLIB__ preceded by an
199 // include of <sys/cdefs.h> once https://sourceware.org/ml/newlib-cvs/2014-q3/msg00038.html
200 // has had a chance to bake for a bit
201 #include <support/newlib/xlocale.h>
203 #ifdef _LIBCPP_HAS_CATOPEN
204 #include <nl_types.h>
208 #include <Availability.h>
211 #include <__undef_min_max>
213 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
214 #pragma GCC system_header
217 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
218 #include <__bsd_locale_defaults.h>
220 #include <__bsd_locale_fallbacks.h>
223 _LIBCPP_BEGIN_NAMESPACE_STD
225 #if defined(__APPLE__) || defined(__FreeBSD__)
226 # define _LIBCPP_GET_C_LOCALE 0
227 #elif defined(__CloudABI__) || defined(__NetBSD__)
228 # define _LIBCPP_GET_C_LOCALE LC_C_LOCALE
230 # define _LIBCPP_GET_C_LOCALE __cloc()
231 // Get the C locale object
232 _LIBCPP_FUNC_VIS locale_t __cloc();
233 #define __cloc_defined
237 // Scans [__b, __e) until a match is found in the basic_strings range
238 // [__kb, __ke) or until it can be shown that there is no match in [__kb, __ke).
239 // __b will be incremented (visibly), consuming CharT until a match is found
240 // or proved to not exist. A keyword may be "", in which will match anything.
241 // If one keyword is a prefix of another, and the next CharT in the input
242 // might match another keyword, the algorithm will attempt to find the longest
243 // matching keyword. If the longer matching keyword ends up not matching, then
244 // no keyword match is found. If no keyword match is found, __ke is returned
245 // and failbit is set in __err.
246 // Else an iterator pointing to the matching keyword is found. If more than
247 // one keyword matches, an iterator to the first matching keyword is returned.
248 // If on exit __b == __e, eofbit is set in __err. If __case_sensitive is false,
249 // __ct is used to force to lower case before comparing characters.
251 // Keywords: "a", "abb"
252 // If the input is "a", the first keyword matches and eofbit is set.
253 // If the input is "abc", no match is found and "ab" are consumed.
254 template <class _InputIterator, class _ForwardIterator, class _Ctype>
257 __scan_keyword(_InputIterator& __b, _InputIterator __e,
258 _ForwardIterator __kb, _ForwardIterator __ke,
259 const _Ctype& __ct, ios_base::iostate& __err,
260 bool __case_sensitive = true)
262 typedef typename iterator_traits<_InputIterator>::value_type _CharT;
263 size_t __nkw = static_cast<size_t>(_VSTD::distance(__kb, __ke));
264 const unsigned char __doesnt_match = '\0';
265 const unsigned char __might_match = '\1';
266 const unsigned char __does_match = '\2';
267 unsigned char __statbuf[100];
268 unsigned char* __status = __statbuf;
269 unique_ptr<unsigned char, void(*)(void*)> __stat_hold(0, free);
270 if (__nkw > sizeof(__statbuf))
272 __status = (unsigned char*)malloc(__nkw);
275 __stat_hold.reset(__status);
277 size_t __n_might_match = __nkw; // At this point, any keyword might match
278 size_t __n_does_match = 0; // but none of them definitely do
279 // Initialize all statuses to __might_match, except for "" keywords are __does_match
280 unsigned char* __st = __status;
281 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
284 *__st = __might_match;
287 *__st = __does_match;
292 // While there might be a match, test keywords against the next CharT
293 for (size_t __indx = 0; __b != __e && __n_might_match > 0; ++__indx)
295 // Peek at the next CharT but don't consume it
297 if (!__case_sensitive)
298 __c = __ct.toupper(__c);
299 bool __consume = false;
300 // For each keyword which might match, see if the __indx character is __c
301 // If a match if found, consume __c
302 // If a match is found, and that is the last character in the keyword,
303 // then that keyword matches.
304 // If the keyword doesn't match this character, then change the keyword
307 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
309 if (*__st == __might_match)
311 _CharT __kc = (*__ky)[__indx];
312 if (!__case_sensitive)
313 __kc = __ct.toupper(__kc);
317 if (__ky->size() == __indx+1)
319 *__st = __does_match;
326 *__st = __doesnt_match;
331 // consume if we matched a character
335 // If we consumed a character and there might be a matched keyword that
336 // was marked matched on a previous iteration, then such keywords
337 // which are now marked as not matching.
338 if (__n_might_match + __n_does_match > 1)
341 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
343 if (*__st == __does_match && __ky->size() != __indx+1)
345 *__st = __doesnt_match;
352 // We've exited the loop because we hit eof and/or we have no more "might matches".
354 __err |= ios_base::eofbit;
355 // Return the first matching result
356 for (__st = __status; __kb != __ke; ++__kb, (void) ++__st)
357 if (*__st == __does_match)
360 __err |= ios_base::failbit;
364 struct _LIBCPP_TYPE_VIS __num_get_base
366 static const int __num_get_buf_sz = 40;
368 static int __get_base(ios_base&);
369 static const char __src[33];
373 void __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end,
374 ios_base::iostate& __err);
376 template <class _CharT>
378 : protected __num_get_base
380 static string __stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep);
381 static string __stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
382 _CharT& __thousands_sep);
383 static int __stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
384 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
385 unsigned* __g, unsigned*& __g_end, _CharT* __atoms);
386 static int __stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp,
387 char* __a, char*& __a_end,
388 _CharT __decimal_point, _CharT __thousands_sep,
389 const string& __grouping, unsigned* __g,
390 unsigned*& __g_end, unsigned& __dc, _CharT* __atoms);
393 template <class _CharT>
395 __num_get<_CharT>::__stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep)
397 locale __loc = __iob.getloc();
398 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 26, __atoms);
399 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
400 __thousands_sep = __np.thousands_sep();
401 return __np.grouping();
404 template <class _CharT>
406 __num_get<_CharT>::__stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
407 _CharT& __thousands_sep)
409 locale __loc = __iob.getloc();
410 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 32, __atoms);
411 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
412 __decimal_point = __np.decimal_point();
413 __thousands_sep = __np.thousands_sep();
414 return __np.grouping();
417 template <class _CharT>
419 __num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
420 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
421 unsigned* __g, unsigned*& __g_end, _CharT* __atoms)
423 if (__a_end == __a && (__ct == __atoms[24] || __ct == __atoms[25]))
425 *__a_end++ = __ct == __atoms[24] ? '+' : '-';
429 if (__grouping.size() != 0 && __ct == __thousands_sep)
431 if (__g_end-__g < __num_get_buf_sz)
438 ptrdiff_t __f = find(__atoms, __atoms + 26, __ct) - __atoms;
451 if (__a_end != __a && __a_end - __a <= 2 && __a_end[-1] == '0')
454 *__a_end++ = __src[__f];
459 *__a_end++ = __src[__f];
464 template <class _CharT>
466 __num_get<_CharT>::__stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp, char* __a, char*& __a_end,
467 _CharT __decimal_point, _CharT __thousands_sep, const string& __grouping,
468 unsigned* __g, unsigned*& __g_end, unsigned& __dc, _CharT* __atoms)
470 if (__ct == __decimal_point)
476 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
480 if (__ct == __thousands_sep && __grouping.size() != 0)
484 if (__g_end-__g < __num_get_buf_sz)
491 ptrdiff_t __f = find(__atoms, __atoms + 32, __ct) - __atoms;
494 char __x = __src[__f];
495 if (__x == '-' || __x == '+')
497 if (__a_end == __a || (__a_end[-1] & 0x5F) == (__exp & 0x7F))
504 if (__x == 'x' || __x == 'X')
506 else if ((__x & 0x5F) == __exp)
512 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
523 _LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<char>)
524 _LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<wchar_t>)
526 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
527 class _LIBCPP_TEMPLATE_VIS num_get
528 : public locale::facet,
529 private __num_get<_CharT>
532 typedef _CharT char_type;
533 typedef _InputIterator iter_type;
535 _LIBCPP_ALWAYS_INLINE
536 explicit num_get(size_t __refs = 0)
537 : locale::facet(__refs) {}
539 _LIBCPP_ALWAYS_INLINE
540 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
541 ios_base::iostate& __err, bool& __v) const
543 return do_get(__b, __e, __iob, __err, __v);
546 _LIBCPP_ALWAYS_INLINE
547 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
548 ios_base::iostate& __err, long& __v) const
550 return do_get(__b, __e, __iob, __err, __v);
553 _LIBCPP_ALWAYS_INLINE
554 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
555 ios_base::iostate& __err, long long& __v) const
557 return do_get(__b, __e, __iob, __err, __v);
560 _LIBCPP_ALWAYS_INLINE
561 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
562 ios_base::iostate& __err, unsigned short& __v) const
564 return do_get(__b, __e, __iob, __err, __v);
567 _LIBCPP_ALWAYS_INLINE
568 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
569 ios_base::iostate& __err, unsigned int& __v) const
571 return do_get(__b, __e, __iob, __err, __v);
574 _LIBCPP_ALWAYS_INLINE
575 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
576 ios_base::iostate& __err, unsigned long& __v) const
578 return do_get(__b, __e, __iob, __err, __v);
581 _LIBCPP_ALWAYS_INLINE
582 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
583 ios_base::iostate& __err, unsigned long long& __v) const
585 return do_get(__b, __e, __iob, __err, __v);
588 _LIBCPP_ALWAYS_INLINE
589 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
590 ios_base::iostate& __err, float& __v) const
592 return do_get(__b, __e, __iob, __err, __v);
595 _LIBCPP_ALWAYS_INLINE
596 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
597 ios_base::iostate& __err, double& __v) const
599 return do_get(__b, __e, __iob, __err, __v);
602 _LIBCPP_ALWAYS_INLINE
603 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
604 ios_base::iostate& __err, long double& __v) const
606 return do_get(__b, __e, __iob, __err, __v);
609 _LIBCPP_ALWAYS_INLINE
610 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
611 ios_base::iostate& __err, void*& __v) const
613 return do_get(__b, __e, __iob, __err, __v);
616 static locale::id id;
619 _LIBCPP_ALWAYS_INLINE
623 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
624 iter_type __do_get_floating_point
625 (iter_type __b, iter_type __e, ios_base& __iob,
626 ios_base::iostate& __err, _Fp& __v) const;
628 template <class _Signed>
629 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
630 iter_type __do_get_signed
631 (iter_type __b, iter_type __e, ios_base& __iob,
632 ios_base::iostate& __err, _Signed& __v) const;
634 template <class _Unsigned>
635 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
636 iter_type __do_get_unsigned
637 (iter_type __b, iter_type __e, ios_base& __iob,
638 ios_base::iostate& __err, _Unsigned& __v) const;
641 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
642 ios_base::iostate& __err, bool& __v) const;
644 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
645 ios_base::iostate& __err, long& __v) const
646 { return this->__do_get_signed ( __b, __e, __iob, __err, __v ); }
648 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
649 ios_base::iostate& __err, long long& __v) const
650 { return this->__do_get_signed ( __b, __e, __iob, __err, __v ); }
652 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
653 ios_base::iostate& __err, unsigned short& __v) const
654 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
656 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
657 ios_base::iostate& __err, unsigned int& __v) const
658 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
660 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
661 ios_base::iostate& __err, unsigned long& __v) const
662 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
664 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
665 ios_base::iostate& __err, unsigned long long& __v) const
666 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
668 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
669 ios_base::iostate& __err, float& __v) const
670 { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
672 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
673 ios_base::iostate& __err, double& __v) const
674 { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
676 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
677 ios_base::iostate& __err, long double& __v) const
678 { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
680 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
681 ios_base::iostate& __err, void*& __v) const;
684 template <class _CharT, class _InputIterator>
686 num_get<_CharT, _InputIterator>::id;
690 __num_get_signed_integral(const char* __a, const char* __a_end,
691 ios_base::iostate& __err, int __base)
695 typename remove_reference<decltype(errno)>::type __save_errno = errno;
698 long long __ll = strtoll_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
699 typename remove_reference<decltype(errno)>::type __current_errno = errno;
700 if (__current_errno == 0)
701 errno = __save_errno;
704 __err = ios_base::failbit;
707 else if (__current_errno == ERANGE ||
708 __ll < numeric_limits<_Tp>::min() ||
709 numeric_limits<_Tp>::max() < __ll)
711 __err = ios_base::failbit;
713 return numeric_limits<_Tp>::max();
715 return numeric_limits<_Tp>::min();
717 return static_cast<_Tp>(__ll);
719 __err = ios_base::failbit;
725 __num_get_unsigned_integral(const char* __a, const char* __a_end,
726 ios_base::iostate& __err, int __base)
732 __err = ios_base::failbit;
735 typename remove_reference<decltype(errno)>::type __save_errno = errno;
738 unsigned long long __ll = strtoull_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
739 typename remove_reference<decltype(errno)>::type __current_errno = errno;
740 if (__current_errno == 0)
741 errno = __save_errno;
744 __err = ios_base::failbit;
747 else if (__current_errno == ERANGE ||
748 numeric_limits<_Tp>::max() < __ll)
750 __err = ios_base::failbit;
751 return numeric_limits<_Tp>::max();
753 return static_cast<_Tp>(__ll);
755 __err = ios_base::failbit;
760 _LIBCPP_INLINE_VISIBILITY
761 _Tp __do_strtod(const char* __a, char** __p2);
764 inline _LIBCPP_INLINE_VISIBILITY
765 float __do_strtod<float>(const char* __a, char** __p2) {
766 return strtof_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
770 inline _LIBCPP_INLINE_VISIBILITY
771 double __do_strtod<double>(const char* __a, char** __p2) {
772 return strtod_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
776 inline _LIBCPP_INLINE_VISIBILITY
777 long double __do_strtod<long double>(const char* __a, char** __p2) {
778 return strtold_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
784 __num_get_float(const char* __a, const char* __a_end, ios_base::iostate& __err)
788 typename remove_reference<decltype(errno)>::type __save_errno = errno;
791 _Tp __ld = __do_strtod<_Tp>(__a, &__p2);
792 typename remove_reference<decltype(errno)>::type __current_errno = errno;
793 if (__current_errno == 0)
794 errno = __save_errno;
797 __err = ios_base::failbit;
800 else if (__current_errno == ERANGE)
801 __err = ios_base::failbit;
804 __err = ios_base::failbit;
808 template <class _CharT, class _InputIterator>
810 num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
812 ios_base::iostate& __err,
815 if ((__iob.flags() & ios_base::boolalpha) == 0)
818 __b = do_get(__b, __e, __iob, __err, __lv);
829 __err = ios_base::failbit;
834 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__iob.getloc());
835 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__iob.getloc());
836 typedef typename numpunct<_CharT>::string_type string_type;
837 const string_type __names[2] = {__np.truename(), __np.falsename()};
838 const string_type* __i = __scan_keyword(__b, __e, __names, __names+2,
840 __v = __i == __names;
846 template <class _CharT, class _InputIterator>
847 template <class _Signed>
849 num_get<_CharT, _InputIterator>::__do_get_signed(iter_type __b, iter_type __e,
851 ios_base::iostate& __err,
855 int __base = this->__get_base(__iob);
857 char_type __atoms[26];
858 char_type __thousands_sep;
859 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
861 __buf.resize(__buf.capacity());
862 char* __a = &__buf[0];
864 unsigned __g[__num_get_base::__num_get_buf_sz];
865 unsigned* __g_end = __g;
867 for (; __b != __e; ++__b)
869 if (__a_end == __a + __buf.size())
871 size_t __tmp = __buf.size();
872 __buf.resize(2*__buf.size());
873 __buf.resize(__buf.capacity());
875 __a_end = __a + __tmp;
877 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
878 __thousands_sep, __grouping, __g, __g_end,
882 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
885 __v = __num_get_signed_integral<_Signed>(__a, __a_end, __err, __base);
886 // Digit grouping checked
887 __check_grouping(__grouping, __g, __g_end, __err);
890 __err |= ios_base::eofbit;
896 template <class _CharT, class _InputIterator>
897 template <class _Unsigned>
899 num_get<_CharT, _InputIterator>::__do_get_unsigned(iter_type __b, iter_type __e,
901 ios_base::iostate& __err,
902 _Unsigned& __v) const
905 int __base = this->__get_base(__iob);
907 char_type __atoms[26];
908 char_type __thousands_sep;
909 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
911 __buf.resize(__buf.capacity());
912 char* __a = &__buf[0];
914 unsigned __g[__num_get_base::__num_get_buf_sz];
915 unsigned* __g_end = __g;
917 for (; __b != __e; ++__b)
919 if (__a_end == __a + __buf.size())
921 size_t __tmp = __buf.size();
922 __buf.resize(2*__buf.size());
923 __buf.resize(__buf.capacity());
925 __a_end = __a + __tmp;
927 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
928 __thousands_sep, __grouping, __g, __g_end,
932 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
935 __v = __num_get_unsigned_integral<_Unsigned>(__a, __a_end, __err, __base);
936 // Digit grouping checked
937 __check_grouping(__grouping, __g, __g_end, __err);
940 __err |= ios_base::eofbit;
946 template <class _CharT, class _InputIterator>
949 num_get<_CharT, _InputIterator>::__do_get_floating_point(iter_type __b, iter_type __e,
951 ios_base::iostate& __err,
954 // Stage 1, nothing to do
956 char_type __atoms[32];
957 char_type __decimal_point;
958 char_type __thousands_sep;
959 string __grouping = this->__stage2_float_prep(__iob, __atoms,
963 __buf.resize(__buf.capacity());
964 char* __a = &__buf[0];
966 unsigned __g[__num_get_base::__num_get_buf_sz];
967 unsigned* __g_end = __g;
969 bool __in_units = true;
971 for (; __b != __e; ++__b)
973 if (__a_end == __a + __buf.size())
975 size_t __tmp = __buf.size();
976 __buf.resize(2*__buf.size());
977 __buf.resize(__buf.capacity());
979 __a_end = __a + __tmp;
981 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
982 __decimal_point, __thousands_sep,
983 __grouping, __g, __g_end,
987 if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
990 __v = __num_get_float<_Fp>(__a, __a_end, __err);
991 // Digit grouping checked
992 __check_grouping(__grouping, __g, __g_end, __err);
995 __err |= ios_base::eofbit;
999 template <class _CharT, class _InputIterator>
1001 num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1003 ios_base::iostate& __err,
1009 char_type __atoms[26];
1010 char_type __thousands_sep = 0;
1012 use_facet<ctype<_CharT> >(__iob.getloc()).widen(__num_get_base::__src,
1013 __num_get_base::__src + 26, __atoms);
1015 __buf.resize(__buf.capacity());
1016 char* __a = &__buf[0];
1017 char* __a_end = __a;
1018 unsigned __g[__num_get_base::__num_get_buf_sz];
1019 unsigned* __g_end = __g;
1021 for (; __b != __e; ++__b)
1023 if (__a_end == __a + __buf.size())
1025 size_t __tmp = __buf.size();
1026 __buf.resize(2*__buf.size());
1027 __buf.resize(__buf.capacity());
1029 __a_end = __a + __tmp;
1031 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1032 __thousands_sep, __grouping,
1033 __g, __g_end, __atoms))
1037 __buf.resize(__a_end - __a);
1038 if (__libcpp_sscanf_l(__buf.c_str(), _LIBCPP_GET_C_LOCALE, "%p", &__v) != 1)
1039 __err = ios_base::failbit;
1042 __err |= ios_base::eofbit;
1046 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<char>)
1047 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<wchar_t>)
1049 struct _LIBCPP_TYPE_VIS __num_put_base
1052 static void __format_int(char* __fmt, const char* __len, bool __signd,
1053 ios_base::fmtflags __flags);
1054 static bool __format_float(char* __fmt, const char* __len,
1055 ios_base::fmtflags __flags);
1056 static char* __identify_padding(char* __nb, char* __ne,
1057 const ios_base& __iob);
1060 template <class _CharT>
1062 : protected __num_put_base
1064 static void __widen_and_group_int(char* __nb, char* __np, char* __ne,
1065 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1066 const locale& __loc);
1067 static void __widen_and_group_float(char* __nb, char* __np, char* __ne,
1068 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1069 const locale& __loc);
1072 template <class _CharT>
1074 __num_put<_CharT>::__widen_and_group_int(char* __nb, char* __np, char* __ne,
1075 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1076 const locale& __loc)
1078 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1079 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1080 string __grouping = __npt.grouping();
1081 if (__grouping.empty())
1083 __ct.widen(__nb, __ne, __ob);
1084 __oe = __ob + (__ne - __nb);
1090 if (*__nf == '-' || *__nf == '+')
1091 *__oe++ = __ct.widen(*__nf++);
1092 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1095 *__oe++ = __ct.widen(*__nf++);
1096 *__oe++ = __ct.widen(*__nf++);
1098 reverse(__nf, __ne);
1099 _CharT __thousands_sep = __npt.thousands_sep();
1102 for (char* __p = __nf; __p < __ne; ++__p)
1104 if (static_cast<unsigned>(__grouping[__dg]) > 0 &&
1105 __dc == static_cast<unsigned>(__grouping[__dg]))
1107 *__oe++ = __thousands_sep;
1109 if (__dg < __grouping.size()-1)
1112 *__oe++ = __ct.widen(*__p);
1115 reverse(__ob + (__nf - __nb), __oe);
1120 __op = __ob + (__np - __nb);
1123 template <class _CharT>
1125 __num_put<_CharT>::__widen_and_group_float(char* __nb, char* __np, char* __ne,
1126 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1127 const locale& __loc)
1129 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1130 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1131 string __grouping = __npt.grouping();
1134 if (*__nf == '-' || *__nf == '+')
1135 *__oe++ = __ct.widen(*__nf++);
1137 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1140 *__oe++ = __ct.widen(*__nf++);
1141 *__oe++ = __ct.widen(*__nf++);
1142 for (__ns = __nf; __ns < __ne; ++__ns)
1143 if (!isxdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
1148 for (__ns = __nf; __ns < __ne; ++__ns)
1149 if (!isdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
1152 if (__grouping.empty())
1154 __ct.widen(__nf, __ns, __oe);
1155 __oe += __ns - __nf;
1159 reverse(__nf, __ns);
1160 _CharT __thousands_sep = __npt.thousands_sep();
1163 for (char* __p = __nf; __p < __ns; ++__p)
1165 if (__grouping[__dg] > 0 && __dc == static_cast<unsigned>(__grouping[__dg]))
1167 *__oe++ = __thousands_sep;
1169 if (__dg < __grouping.size()-1)
1172 *__oe++ = __ct.widen(*__p);
1175 reverse(__ob + (__nf - __nb), __oe);
1177 for (__nf = __ns; __nf < __ne; ++__nf)
1181 *__oe++ = __npt.decimal_point();
1186 *__oe++ = __ct.widen(*__nf);
1188 __ct.widen(__nf, __ne, __oe);
1189 __oe += __ne - __nf;
1193 __op = __ob + (__np - __nb);
1196 _LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<char>)
1197 _LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<wchar_t>)
1199 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
1200 class _LIBCPP_TEMPLATE_VIS num_put
1201 : public locale::facet,
1202 private __num_put<_CharT>
1205 typedef _CharT char_type;
1206 typedef _OutputIterator iter_type;
1208 _LIBCPP_ALWAYS_INLINE
1209 explicit num_put(size_t __refs = 0)
1210 : locale::facet(__refs) {}
1212 _LIBCPP_ALWAYS_INLINE
1213 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1216 return do_put(__s, __iob, __fl, __v);
1219 _LIBCPP_ALWAYS_INLINE
1220 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1223 return do_put(__s, __iob, __fl, __v);
1226 _LIBCPP_ALWAYS_INLINE
1227 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1228 long long __v) const
1230 return do_put(__s, __iob, __fl, __v);
1233 _LIBCPP_ALWAYS_INLINE
1234 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1235 unsigned long __v) const
1237 return do_put(__s, __iob, __fl, __v);
1240 _LIBCPP_ALWAYS_INLINE
1241 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1242 unsigned long long __v) const
1244 return do_put(__s, __iob, __fl, __v);
1247 _LIBCPP_ALWAYS_INLINE
1248 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1251 return do_put(__s, __iob, __fl, __v);
1254 _LIBCPP_ALWAYS_INLINE
1255 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1256 long double __v) const
1258 return do_put(__s, __iob, __fl, __v);
1261 _LIBCPP_ALWAYS_INLINE
1262 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1263 const void* __v) const
1265 return do_put(__s, __iob, __fl, __v);
1268 static locale::id id;
1271 _LIBCPP_ALWAYS_INLINE
1274 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1276 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1278 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1279 long long __v) const;
1280 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1281 unsigned long) const;
1282 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1283 unsigned long long) const;
1284 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1286 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1287 long double __v) const;
1288 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1289 const void* __v) const;
1292 template <class _CharT, class _OutputIterator>
1294 num_put<_CharT, _OutputIterator>::id;
1296 template <class _CharT, class _OutputIterator>
1299 __pad_and_output(_OutputIterator __s,
1300 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1301 ios_base& __iob, _CharT __fl)
1303 streamsize __sz = __oe - __ob;
1304 streamsize __ns = __iob.width();
1309 for (;__ob < __op; ++__ob, ++__s)
1311 for (; __ns; --__ns, ++__s)
1313 for (; __ob < __oe; ++__ob, ++__s)
1319 #if !defined(__APPLE__) || \
1320 (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED > __MAC_10_8) || \
1321 (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_6_0)
1323 template <class _CharT, class _Traits>
1325 ostreambuf_iterator<_CharT, _Traits>
1326 __pad_and_output(ostreambuf_iterator<_CharT, _Traits> __s,
1327 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1328 ios_base& __iob, _CharT __fl)
1330 if (__s.__sbuf_ == nullptr)
1332 streamsize __sz = __oe - __ob;
1333 streamsize __ns = __iob.width();
1338 streamsize __np = __op - __ob;
1341 if (__s.__sbuf_->sputn(__ob, __np) != __np)
1343 __s.__sbuf_ = nullptr;
1349 basic_string<_CharT, _Traits> __sp(__ns, __fl);
1350 if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns)
1352 __s.__sbuf_ = nullptr;
1359 if (__s.__sbuf_->sputn(__op, __np) != __np)
1361 __s.__sbuf_ = nullptr;
1371 template <class _CharT, class _OutputIterator>
1373 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1374 char_type __fl, bool __v) const
1376 if ((__iob.flags() & ios_base::boolalpha) == 0)
1377 return do_put(__s, __iob, __fl, (unsigned long)__v);
1378 const numpunct<char_type>& __np = use_facet<numpunct<char_type> >(__iob.getloc());
1379 typedef typename numpunct<char_type>::string_type string_type;
1380 #if _LIBCPP_DEBUG_LEVEL >= 2
1381 string_type __tmp(__v ? __np.truename() : __np.falsename());
1382 string_type __nm = _VSTD::move(__tmp);
1384 string_type __nm = __v ? __np.truename() : __np.falsename();
1386 for (typename string_type::iterator __i = __nm.begin(); __i != __nm.end(); ++__i, ++__s)
1391 template <class _CharT, class _OutputIterator>
1393 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1394 char_type __fl, long __v) const
1396 // Stage 1 - Get number in narrow char
1397 char __fmt[6] = {'%', 0};
1398 const char* __len = "l";
1399 this->__format_int(__fmt+1, __len, true, __iob.flags());
1400 const unsigned __nbuf = (numeric_limits<long>::digits / 3)
1401 + ((numeric_limits<long>::digits % 3) != 0)
1402 + ((__iob.flags() & ios_base::showbase) != 0)
1405 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1406 char* __ne = __nar + __nc;
1407 char* __np = this->__identify_padding(__nar, __ne, __iob);
1408 // Stage 2 - Widen __nar while adding thousands separators
1409 char_type __o[2*(__nbuf-1) - 1];
1410 char_type* __op; // pad here
1411 char_type* __oe; // end of output
1412 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1413 // [__o, __oe) contains thousands_sep'd wide number
1415 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1418 template <class _CharT, class _OutputIterator>
1420 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1421 char_type __fl, long long __v) const
1423 // Stage 1 - Get number in narrow char
1424 char __fmt[8] = {'%', 0};
1425 const char* __len = "ll";
1426 this->__format_int(__fmt+1, __len, true, __iob.flags());
1427 const unsigned __nbuf = (numeric_limits<long long>::digits / 3)
1428 + ((numeric_limits<long long>::digits % 3) != 0)
1429 + ((__iob.flags() & ios_base::showbase) != 0)
1432 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1433 char* __ne = __nar + __nc;
1434 char* __np = this->__identify_padding(__nar, __ne, __iob);
1435 // Stage 2 - Widen __nar while adding thousands separators
1436 char_type __o[2*(__nbuf-1) - 1];
1437 char_type* __op; // pad here
1438 char_type* __oe; // end of output
1439 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1440 // [__o, __oe) contains thousands_sep'd wide number
1442 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1445 template <class _CharT, class _OutputIterator>
1447 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1448 char_type __fl, unsigned long __v) const
1450 // Stage 1 - Get number in narrow char
1451 char __fmt[6] = {'%', 0};
1452 const char* __len = "l";
1453 this->__format_int(__fmt+1, __len, false, __iob.flags());
1454 const unsigned __nbuf = (numeric_limits<unsigned long>::digits / 3)
1455 + ((numeric_limits<unsigned long>::digits % 3) != 0)
1456 + ((__iob.flags() & ios_base::showbase) != 0)
1459 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1460 char* __ne = __nar + __nc;
1461 char* __np = this->__identify_padding(__nar, __ne, __iob);
1462 // Stage 2 - Widen __nar while adding thousands separators
1463 char_type __o[2*(__nbuf-1) - 1];
1464 char_type* __op; // pad here
1465 char_type* __oe; // end of output
1466 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1467 // [__o, __oe) contains thousands_sep'd wide number
1469 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1472 template <class _CharT, class _OutputIterator>
1474 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1475 char_type __fl, unsigned long long __v) const
1477 // Stage 1 - Get number in narrow char
1478 char __fmt[8] = {'%', 0};
1479 const char* __len = "ll";
1480 this->__format_int(__fmt+1, __len, false, __iob.flags());
1481 const unsigned __nbuf = (numeric_limits<unsigned long long>::digits / 3)
1482 + ((numeric_limits<unsigned long long>::digits % 3) != 0)
1483 + ((__iob.flags() & ios_base::showbase) != 0)
1486 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1487 char* __ne = __nar + __nc;
1488 char* __np = this->__identify_padding(__nar, __ne, __iob);
1489 // Stage 2 - Widen __nar while adding thousands separators
1490 char_type __o[2*(__nbuf-1) - 1];
1491 char_type* __op; // pad here
1492 char_type* __oe; // end of output
1493 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1494 // [__o, __oe) contains thousands_sep'd wide number
1496 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1499 template <class _CharT, class _OutputIterator>
1501 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1502 char_type __fl, double __v) const
1504 // Stage 1 - Get number in narrow char
1505 char __fmt[8] = {'%', 0};
1506 const char* __len = "";
1507 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1508 const unsigned __nbuf = 30;
1512 if (__specify_precision)
1513 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
1514 (int)__iob.precision(), __v);
1516 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1517 unique_ptr<char, void(*)(void*)> __nbh(0, free);
1518 if (__nc > static_cast<int>(__nbuf-1))
1520 if (__specify_precision)
1521 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
1523 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1525 __throw_bad_alloc();
1528 char* __ne = __nb + __nc;
1529 char* __np = this->__identify_padding(__nb, __ne, __iob);
1530 // Stage 2 - Widen __nar while adding thousands separators
1531 char_type __o[2*(__nbuf-1) - 1];
1532 char_type* __ob = __o;
1533 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1536 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
1538 __throw_bad_alloc();
1541 char_type* __op; // pad here
1542 char_type* __oe; // end of output
1543 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1544 // [__o, __oe) contains thousands_sep'd wide number
1546 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1550 template <class _CharT, class _OutputIterator>
1552 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1553 char_type __fl, long double __v) const
1555 // Stage 1 - Get number in narrow char
1556 char __fmt[8] = {'%', 0};
1557 const char* __len = "L";
1558 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1559 const unsigned __nbuf = 30;
1563 if (__specify_precision)
1564 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
1565 (int)__iob.precision(), __v);
1567 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1568 unique_ptr<char, void(*)(void*)> __nbh(0, free);
1569 if (__nc > static_cast<int>(__nbuf-1))
1571 if (__specify_precision)
1572 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
1574 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1576 __throw_bad_alloc();
1579 char* __ne = __nb + __nc;
1580 char* __np = this->__identify_padding(__nb, __ne, __iob);
1581 // Stage 2 - Widen __nar while adding thousands separators
1582 char_type __o[2*(__nbuf-1) - 1];
1583 char_type* __ob = __o;
1584 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1587 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
1589 __throw_bad_alloc();
1592 char_type* __op; // pad here
1593 char_type* __oe; // end of output
1594 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1595 // [__o, __oe) contains thousands_sep'd wide number
1597 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1601 template <class _CharT, class _OutputIterator>
1603 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1604 char_type __fl, const void* __v) const
1606 // Stage 1 - Get pointer in narrow char
1607 char __fmt[6] = "%p";
1608 const unsigned __nbuf = 20;
1610 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1611 char* __ne = __nar + __nc;
1612 char* __np = this->__identify_padding(__nar, __ne, __iob);
1613 // Stage 2 - Widen __nar
1614 char_type __o[2*(__nbuf-1) - 1];
1615 char_type* __op; // pad here
1616 char_type* __oe; // end of output
1617 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
1618 __ct.widen(__nar, __ne, __o);
1619 __oe = __o + (__ne - __nar);
1623 __op = __o + (__np - __nar);
1624 // [__o, __oe) contains wide number
1626 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1629 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<char>)
1630 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<wchar_t>)
1632 template <class _CharT, class _InputIterator>
1635 __get_up_to_n_digits(_InputIterator& __b, _InputIterator __e,
1636 ios_base::iostate& __err, const ctype<_CharT>& __ct, int __n)
1638 // Precondition: __n >= 1
1641 __err |= ios_base::eofbit | ios_base::failbit;
1646 if (!__ct.is(ctype_base::digit, __c))
1648 __err |= ios_base::failbit;
1651 int __r = __ct.narrow(__c, 0) - '0';
1652 for (++__b, (void) --__n; __b != __e && __n > 0; ++__b, (void) --__n)
1656 if (!__ct.is(ctype_base::digit, __c))
1658 __r = __r * 10 + __ct.narrow(__c, 0) - '0';
1661 __err |= ios_base::eofbit;
1665 class _LIBCPP_TYPE_VIS time_base
1668 enum dateorder {no_order, dmy, mdy, ymd, ydm};
1671 template <class _CharT>
1672 class _LIBCPP_TEMPLATE_VIS __time_get_c_storage
1675 typedef basic_string<_CharT> string_type;
1677 virtual const string_type* __weeks() const;
1678 virtual const string_type* __months() const;
1679 virtual const string_type* __am_pm() const;
1680 virtual const string_type& __c() const;
1681 virtual const string_type& __r() const;
1682 virtual const string_type& __x() const;
1683 virtual const string_type& __X() const;
1685 _LIBCPP_ALWAYS_INLINE
1686 ~__time_get_c_storage() {}
1689 template <> _LIBCPP_FUNC_VIS const string* __time_get_c_storage<char>::__weeks() const;
1690 template <> _LIBCPP_FUNC_VIS const string* __time_get_c_storage<char>::__months() const;
1691 template <> _LIBCPP_FUNC_VIS const string* __time_get_c_storage<char>::__am_pm() const;
1692 template <> _LIBCPP_FUNC_VIS const string& __time_get_c_storage<char>::__c() const;
1693 template <> _LIBCPP_FUNC_VIS const string& __time_get_c_storage<char>::__r() const;
1694 template <> _LIBCPP_FUNC_VIS const string& __time_get_c_storage<char>::__x() const;
1695 template <> _LIBCPP_FUNC_VIS const string& __time_get_c_storage<char>::__X() const;
1697 template <> _LIBCPP_FUNC_VIS const wstring* __time_get_c_storage<wchar_t>::__weeks() const;
1698 template <> _LIBCPP_FUNC_VIS const wstring* __time_get_c_storage<wchar_t>::__months() const;
1699 template <> _LIBCPP_FUNC_VIS const wstring* __time_get_c_storage<wchar_t>::__am_pm() const;
1700 template <> _LIBCPP_FUNC_VIS const wstring& __time_get_c_storage<wchar_t>::__c() const;
1701 template <> _LIBCPP_FUNC_VIS const wstring& __time_get_c_storage<wchar_t>::__r() const;
1702 template <> _LIBCPP_FUNC_VIS const wstring& __time_get_c_storage<wchar_t>::__x() const;
1703 template <> _LIBCPP_FUNC_VIS const wstring& __time_get_c_storage<wchar_t>::__X() const;
1705 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
1706 class _LIBCPP_TEMPLATE_VIS time_get
1707 : public locale::facet,
1709 private __time_get_c_storage<_CharT>
1712 typedef _CharT char_type;
1713 typedef _InputIterator iter_type;
1714 typedef time_base::dateorder dateorder;
1715 typedef basic_string<char_type> string_type;
1717 _LIBCPP_ALWAYS_INLINE
1718 explicit time_get(size_t __refs = 0)
1719 : locale::facet(__refs) {}
1721 _LIBCPP_ALWAYS_INLINE
1722 dateorder date_order() const
1724 return this->do_date_order();
1727 _LIBCPP_ALWAYS_INLINE
1728 iter_type get_time(iter_type __b, iter_type __e, ios_base& __iob,
1729 ios_base::iostate& __err, tm* __tm) const
1731 return do_get_time(__b, __e, __iob, __err, __tm);
1734 _LIBCPP_ALWAYS_INLINE
1735 iter_type get_date(iter_type __b, iter_type __e, ios_base& __iob,
1736 ios_base::iostate& __err, tm* __tm) const
1738 return do_get_date(__b, __e, __iob, __err, __tm);
1741 _LIBCPP_ALWAYS_INLINE
1742 iter_type get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
1743 ios_base::iostate& __err, tm* __tm) const
1745 return do_get_weekday(__b, __e, __iob, __err, __tm);
1748 _LIBCPP_ALWAYS_INLINE
1749 iter_type get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
1750 ios_base::iostate& __err, tm* __tm) const
1752 return do_get_monthname(__b, __e, __iob, __err, __tm);
1755 _LIBCPP_ALWAYS_INLINE
1756 iter_type get_year(iter_type __b, iter_type __e, ios_base& __iob,
1757 ios_base::iostate& __err, tm* __tm) const
1759 return do_get_year(__b, __e, __iob, __err, __tm);
1762 _LIBCPP_ALWAYS_INLINE
1763 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
1764 ios_base::iostate& __err, tm *__tm,
1765 char __fmt, char __mod = 0) const
1767 return do_get(__b, __e, __iob, __err, __tm, __fmt, __mod);
1770 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
1771 ios_base::iostate& __err, tm* __tm,
1772 const char_type* __fmtb, const char_type* __fmte) const;
1774 static locale::id id;
1777 _LIBCPP_ALWAYS_INLINE
1780 virtual dateorder do_date_order() const;
1781 virtual iter_type do_get_time(iter_type __b, iter_type __e, ios_base& __iob,
1782 ios_base::iostate& __err, tm* __tm) const;
1783 virtual iter_type do_get_date(iter_type __b, iter_type __e, ios_base& __iob,
1784 ios_base::iostate& __err, tm* __tm) const;
1785 virtual iter_type do_get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
1786 ios_base::iostate& __err, tm* __tm) const;
1787 virtual iter_type do_get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
1788 ios_base::iostate& __err, tm* __tm) const;
1789 virtual iter_type do_get_year(iter_type __b, iter_type __e, ios_base& __iob,
1790 ios_base::iostate& __err, tm* __tm) const;
1791 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
1792 ios_base::iostate& __err, tm* __tm,
1793 char __fmt, char __mod) const;
1795 void __get_white_space(iter_type& __b, iter_type __e,
1796 ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1797 void __get_percent(iter_type& __b, iter_type __e, ios_base::iostate& __err,
1798 const ctype<char_type>& __ct) const;
1800 void __get_weekdayname(int& __m,
1801 iter_type& __b, iter_type __e,
1802 ios_base::iostate& __err,
1803 const ctype<char_type>& __ct) const;
1804 void __get_monthname(int& __m,
1805 iter_type& __b, iter_type __e,
1806 ios_base::iostate& __err,
1807 const ctype<char_type>& __ct) const;
1808 void __get_day(int& __d,
1809 iter_type& __b, iter_type __e,
1810 ios_base::iostate& __err,
1811 const ctype<char_type>& __ct) const;
1812 void __get_month(int& __m,
1813 iter_type& __b, iter_type __e,
1814 ios_base::iostate& __err,
1815 const ctype<char_type>& __ct) const;
1816 void __get_year(int& __y,
1817 iter_type& __b, iter_type __e,
1818 ios_base::iostate& __err,
1819 const ctype<char_type>& __ct) const;
1820 void __get_year4(int& __y,
1821 iter_type& __b, iter_type __e,
1822 ios_base::iostate& __err,
1823 const ctype<char_type>& __ct) const;
1824 void __get_hour(int& __d,
1825 iter_type& __b, iter_type __e,
1826 ios_base::iostate& __err,
1827 const ctype<char_type>& __ct) const;
1828 void __get_12_hour(int& __h,
1829 iter_type& __b, iter_type __e,
1830 ios_base::iostate& __err,
1831 const ctype<char_type>& __ct) const;
1832 void __get_am_pm(int& __h,
1833 iter_type& __b, iter_type __e,
1834 ios_base::iostate& __err,
1835 const ctype<char_type>& __ct) const;
1836 void __get_minute(int& __m,
1837 iter_type& __b, iter_type __e,
1838 ios_base::iostate& __err,
1839 const ctype<char_type>& __ct) const;
1840 void __get_second(int& __s,
1841 iter_type& __b, iter_type __e,
1842 ios_base::iostate& __err,
1843 const ctype<char_type>& __ct) const;
1844 void __get_weekday(int& __w,
1845 iter_type& __b, iter_type __e,
1846 ios_base::iostate& __err,
1847 const ctype<char_type>& __ct) const;
1848 void __get_day_year_num(int& __w,
1849 iter_type& __b, iter_type __e,
1850 ios_base::iostate& __err,
1851 const ctype<char_type>& __ct) const;
1854 template <class _CharT, class _InputIterator>
1856 time_get<_CharT, _InputIterator>::id;
1858 // time_get primitives
1860 template <class _CharT, class _InputIterator>
1862 time_get<_CharT, _InputIterator>::__get_weekdayname(int& __w,
1863 iter_type& __b, iter_type __e,
1864 ios_base::iostate& __err,
1865 const ctype<char_type>& __ct) const
1867 // Note: ignoring case comes from the POSIX strptime spec
1868 const string_type* __wk = this->__weeks();
1869 ptrdiff_t __i = __scan_keyword(__b, __e, __wk, __wk+14, __ct, __err, false) - __wk;
1874 template <class _CharT, class _InputIterator>
1876 time_get<_CharT, _InputIterator>::__get_monthname(int& __m,
1877 iter_type& __b, iter_type __e,
1878 ios_base::iostate& __err,
1879 const ctype<char_type>& __ct) const
1881 // Note: ignoring case comes from the POSIX strptime spec
1882 const string_type* __month = this->__months();
1883 ptrdiff_t __i = __scan_keyword(__b, __e, __month, __month+24, __ct, __err, false) - __month;
1888 template <class _CharT, class _InputIterator>
1890 time_get<_CharT, _InputIterator>::__get_day(int& __d,
1891 iter_type& __b, iter_type __e,
1892 ios_base::iostate& __err,
1893 const ctype<char_type>& __ct) const
1895 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
1896 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 31)
1899 __err |= ios_base::failbit;
1902 template <class _CharT, class _InputIterator>
1904 time_get<_CharT, _InputIterator>::__get_month(int& __m,
1905 iter_type& __b, iter_type __e,
1906 ios_base::iostate& __err,
1907 const ctype<char_type>& __ct) const
1909 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2) - 1;
1910 if (!(__err & ios_base::failbit) && __t <= 11)
1913 __err |= ios_base::failbit;
1916 template <class _CharT, class _InputIterator>
1918 time_get<_CharT, _InputIterator>::__get_year(int& __y,
1919 iter_type& __b, iter_type __e,
1920 ios_base::iostate& __err,
1921 const ctype<char_type>& __ct) const
1923 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
1924 if (!(__err & ios_base::failbit))
1928 else if (69 <= __t && __t <= 99)
1934 template <class _CharT, class _InputIterator>
1936 time_get<_CharT, _InputIterator>::__get_year4(int& __y,
1937 iter_type& __b, iter_type __e,
1938 ios_base::iostate& __err,
1939 const ctype<char_type>& __ct) const
1941 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
1942 if (!(__err & ios_base::failbit))
1946 template <class _CharT, class _InputIterator>
1948 time_get<_CharT, _InputIterator>::__get_hour(int& __h,
1949 iter_type& __b, iter_type __e,
1950 ios_base::iostate& __err,
1951 const ctype<char_type>& __ct) const
1953 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
1954 if (!(__err & ios_base::failbit) && __t <= 23)
1957 __err |= ios_base::failbit;
1960 template <class _CharT, class _InputIterator>
1962 time_get<_CharT, _InputIterator>::__get_12_hour(int& __h,
1963 iter_type& __b, iter_type __e,
1964 ios_base::iostate& __err,
1965 const ctype<char_type>& __ct) const
1967 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
1968 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 12)
1971 __err |= ios_base::failbit;
1974 template <class _CharT, class _InputIterator>
1976 time_get<_CharT, _InputIterator>::__get_minute(int& __m,
1977 iter_type& __b, iter_type __e,
1978 ios_base::iostate& __err,
1979 const ctype<char_type>& __ct) const
1981 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
1982 if (!(__err & ios_base::failbit) && __t <= 59)
1985 __err |= ios_base::failbit;
1988 template <class _CharT, class _InputIterator>
1990 time_get<_CharT, _InputIterator>::__get_second(int& __s,
1991 iter_type& __b, iter_type __e,
1992 ios_base::iostate& __err,
1993 const ctype<char_type>& __ct) const
1995 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
1996 if (!(__err & ios_base::failbit) && __t <= 60)
1999 __err |= ios_base::failbit;
2002 template <class _CharT, class _InputIterator>
2004 time_get<_CharT, _InputIterator>::__get_weekday(int& __w,
2005 iter_type& __b, iter_type __e,
2006 ios_base::iostate& __err,
2007 const ctype<char_type>& __ct) const
2009 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 1);
2010 if (!(__err & ios_base::failbit) && __t <= 6)
2013 __err |= ios_base::failbit;
2016 template <class _CharT, class _InputIterator>
2018 time_get<_CharT, _InputIterator>::__get_day_year_num(int& __d,
2019 iter_type& __b, iter_type __e,
2020 ios_base::iostate& __err,
2021 const ctype<char_type>& __ct) const
2023 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 3);
2024 if (!(__err & ios_base::failbit) && __t <= 365)
2027 __err |= ios_base::failbit;
2030 template <class _CharT, class _InputIterator>
2032 time_get<_CharT, _InputIterator>::__get_white_space(iter_type& __b, iter_type __e,
2033 ios_base::iostate& __err,
2034 const ctype<char_type>& __ct) const
2036 for (; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2039 __err |= ios_base::eofbit;
2042 template <class _CharT, class _InputIterator>
2044 time_get<_CharT, _InputIterator>::__get_am_pm(int& __h,
2045 iter_type& __b, iter_type __e,
2046 ios_base::iostate& __err,
2047 const ctype<char_type>& __ct) const
2049 const string_type* __ap = this->__am_pm();
2050 if (__ap[0].size() + __ap[1].size() == 0)
2052 __err |= ios_base::failbit;
2055 ptrdiff_t __i = __scan_keyword(__b, __e, __ap, __ap+2, __ct, __err, false) - __ap;
2056 if (__i == 0 && __h == 12)
2058 else if (__i == 1 && __h < 12)
2062 template <class _CharT, class _InputIterator>
2064 time_get<_CharT, _InputIterator>::__get_percent(iter_type& __b, iter_type __e,
2065 ios_base::iostate& __err,
2066 const ctype<char_type>& __ct) const
2070 __err |= ios_base::eofbit | ios_base::failbit;
2073 if (__ct.narrow(*__b, 0) != '%')
2074 __err |= ios_base::failbit;
2075 else if(++__b == __e)
2076 __err |= ios_base::eofbit;
2079 // time_get end primitives
2081 template <class _CharT, class _InputIterator>
2083 time_get<_CharT, _InputIterator>::get(iter_type __b, iter_type __e,
2085 ios_base::iostate& __err, tm* __tm,
2086 const char_type* __fmtb, const char_type* __fmte) const
2088 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2089 __err = ios_base::goodbit;
2090 while (__fmtb != __fmte && __err == ios_base::goodbit)
2094 __err = ios_base::failbit;
2097 if (__ct.narrow(*__fmtb, 0) == '%')
2099 if (++__fmtb == __fmte)
2101 __err = ios_base::failbit;
2104 char __cmd = __ct.narrow(*__fmtb, 0);
2106 if (__cmd == 'E' || __cmd == '0')
2108 if (++__fmtb == __fmte)
2110 __err = ios_base::failbit;
2114 __cmd = __ct.narrow(*__fmtb, 0);
2116 __b = do_get(__b, __e, __iob, __err, __tm, __cmd, __opt);
2119 else if (__ct.is(ctype_base::space, *__fmtb))
2121 for (++__fmtb; __fmtb != __fmte && __ct.is(ctype_base::space, *__fmtb); ++__fmtb)
2123 for ( ; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2126 else if (__ct.toupper(*__b) == __ct.toupper(*__fmtb))
2132 __err = ios_base::failbit;
2135 __err |= ios_base::eofbit;
2139 template <class _CharT, class _InputIterator>
2140 typename time_get<_CharT, _InputIterator>::dateorder
2141 time_get<_CharT, _InputIterator>::do_date_order() const
2146 template <class _CharT, class _InputIterator>
2148 time_get<_CharT, _InputIterator>::do_get_time(iter_type __b, iter_type __e,
2150 ios_base::iostate& __err,
2153 const char_type __fmt[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2154 return get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt)/sizeof(__fmt[0]));
2157 template <class _CharT, class _InputIterator>
2159 time_get<_CharT, _InputIterator>::do_get_date(iter_type __b, iter_type __e,
2161 ios_base::iostate& __err,
2164 const string_type& __fmt = this->__x();
2165 return get(__b, __e, __iob, __err, __tm, __fmt.data(), __fmt.data() + __fmt.size());
2168 template <class _CharT, class _InputIterator>
2170 time_get<_CharT, _InputIterator>::do_get_weekday(iter_type __b, iter_type __e,
2172 ios_base::iostate& __err,
2175 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2176 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2180 template <class _CharT, class _InputIterator>
2182 time_get<_CharT, _InputIterator>::do_get_monthname(iter_type __b, iter_type __e,
2184 ios_base::iostate& __err,
2187 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2188 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2192 template <class _CharT, class _InputIterator>
2194 time_get<_CharT, _InputIterator>::do_get_year(iter_type __b, iter_type __e,
2196 ios_base::iostate& __err,
2199 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2200 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2204 template <class _CharT, class _InputIterator>
2206 time_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
2208 ios_base::iostate& __err, tm* __tm,
2209 char __fmt, char) const
2211 __err = ios_base::goodbit;
2212 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2217 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2222 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2226 const string_type& __fm = this->__c();
2227 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
2232 __get_day(__tm->tm_mday, __b, __e, __err, __ct);
2236 const char_type __fm[] = {'%', 'm', '/', '%', 'd', '/', '%', 'y'};
2237 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2242 const char_type __fm[] = {'%', 'Y', '-', '%', 'm', '-', '%', 'd'};
2243 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2247 __get_hour(__tm->tm_hour, __b, __e, __err, __ct);
2250 __get_12_hour(__tm->tm_hour, __b, __e, __err, __ct);
2253 __get_day_year_num(__tm->tm_yday, __b, __e, __err, __ct);
2256 __get_month(__tm->tm_mon, __b, __e, __err, __ct);
2259 __get_minute(__tm->tm_min, __b, __e, __err, __ct);
2263 __get_white_space(__b, __e, __err, __ct);
2266 __get_am_pm(__tm->tm_hour, __b, __e, __err, __ct);
2270 const char_type __fm[] = {'%', 'I', ':', '%', 'M', ':', '%', 'S', ' ', '%', 'p'};
2271 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2276 const char_type __fm[] = {'%', 'H', ':', '%', 'M'};
2277 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2281 __get_second(__tm->tm_sec, __b, __e, __err, __ct);
2285 const char_type __fm[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2286 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2290 __get_weekday(__tm->tm_wday, __b, __e, __err, __ct);
2293 return do_get_date(__b, __e, __iob, __err, __tm);
2296 const string_type& __fm = this->__X();
2297 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
2301 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2304 __get_year4(__tm->tm_year, __b, __e, __err, __ct);
2307 __get_percent(__b, __e, __err, __ct);
2310 __err |= ios_base::failbit;
2315 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get<char>)
2316 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get<wchar_t>)
2318 class _LIBCPP_TYPE_VIS __time_get
2323 __time_get(const char* __nm);
2324 __time_get(const string& __nm);
2328 template <class _CharT>
2329 class _LIBCPP_TEMPLATE_VIS __time_get_storage
2333 typedef basic_string<_CharT> string_type;
2335 string_type __weeks_[14];
2336 string_type __months_[24];
2337 string_type __am_pm_[2];
2343 explicit __time_get_storage(const char* __nm);
2344 explicit __time_get_storage(const string& __nm);
2346 _LIBCPP_ALWAYS_INLINE ~__time_get_storage() {}
2348 time_base::dateorder __do_date_order() const;
2351 void init(const ctype<_CharT>&);
2352 string_type __analyze(char __fmt, const ctype<_CharT>&);
2355 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2356 class _LIBCPP_TEMPLATE_VIS time_get_byname
2357 : public time_get<_CharT, _InputIterator>,
2358 private __time_get_storage<_CharT>
2361 typedef time_base::dateorder dateorder;
2362 typedef _InputIterator iter_type;
2363 typedef _CharT char_type;
2364 typedef basic_string<char_type> string_type;
2366 _LIBCPP_INLINE_VISIBILITY
2367 explicit time_get_byname(const char* __nm, size_t __refs = 0)
2368 : time_get<_CharT, _InputIterator>(__refs),
2369 __time_get_storage<_CharT>(__nm) {}
2370 _LIBCPP_INLINE_VISIBILITY
2371 explicit time_get_byname(const string& __nm, size_t __refs = 0)
2372 : time_get<_CharT, _InputIterator>(__refs),
2373 __time_get_storage<_CharT>(__nm) {}
2376 _LIBCPP_INLINE_VISIBILITY
2377 ~time_get_byname() {}
2379 _LIBCPP_INLINE_VISIBILITY
2380 virtual dateorder do_date_order() const {return this->__do_date_order();}
2382 _LIBCPP_INLINE_VISIBILITY
2383 virtual const string_type* __weeks() const {return this->__weeks_;}
2384 _LIBCPP_INLINE_VISIBILITY
2385 virtual const string_type* __months() const {return this->__months_;}
2386 _LIBCPP_INLINE_VISIBILITY
2387 virtual const string_type* __am_pm() const {return this->__am_pm_;}
2388 _LIBCPP_INLINE_VISIBILITY
2389 virtual const string_type& __c() const {return this->__c_;}
2390 _LIBCPP_INLINE_VISIBILITY
2391 virtual const string_type& __r() const {return this->__r_;}
2392 _LIBCPP_INLINE_VISIBILITY
2393 virtual const string_type& __x() const {return this->__x_;}
2394 _LIBCPP_INLINE_VISIBILITY
2395 virtual const string_type& __X() const {return this->__X_;}
2398 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<char>)
2399 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<wchar_t>)
2401 class _LIBCPP_TYPE_VIS __time_put
2405 _LIBCPP_ALWAYS_INLINE __time_put() : __loc_(_LIBCPP_GET_C_LOCALE) {}
2406 __time_put(const char* __nm);
2407 __time_put(const string& __nm);
2409 void __do_put(char* __nb, char*& __ne, const tm* __tm,
2410 char __fmt, char __mod) const;
2411 void __do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm,
2412 char __fmt, char __mod) const;
2415 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2416 class _LIBCPP_TEMPLATE_VIS time_put
2417 : public locale::facet,
2421 typedef _CharT char_type;
2422 typedef _OutputIterator iter_type;
2424 _LIBCPP_ALWAYS_INLINE
2425 explicit time_put(size_t __refs = 0)
2426 : locale::facet(__refs) {}
2428 iter_type put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm,
2429 const char_type* __pb, const char_type* __pe) const;
2431 _LIBCPP_ALWAYS_INLINE
2432 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
2433 const tm* __tm, char __fmt, char __mod = 0) const
2435 return do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2438 static locale::id id;
2441 _LIBCPP_ALWAYS_INLINE
2443 virtual iter_type do_put(iter_type __s, ios_base&, char_type, const tm* __tm,
2444 char __fmt, char __mod) const;
2446 _LIBCPP_ALWAYS_INLINE
2447 explicit time_put(const char* __nm, size_t __refs)
2448 : locale::facet(__refs),
2450 _LIBCPP_ALWAYS_INLINE
2451 explicit time_put(const string& __nm, size_t __refs)
2452 : locale::facet(__refs),
2456 template <class _CharT, class _OutputIterator>
2458 time_put<_CharT, _OutputIterator>::id;
2460 template <class _CharT, class _OutputIterator>
2462 time_put<_CharT, _OutputIterator>::put(iter_type __s, ios_base& __iob,
2463 char_type __fl, const tm* __tm,
2464 const char_type* __pb,
2465 const char_type* __pe) const
2467 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2468 for (; __pb != __pe; ++__pb)
2470 if (__ct.narrow(*__pb, 0) == '%')
2478 char __fmt = __ct.narrow(*__pb, 0);
2479 if (__fmt == 'E' || __fmt == 'O')
2488 __fmt = __ct.narrow(*__pb, 0);
2490 __s = do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2498 template <class _CharT, class _OutputIterator>
2500 time_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base&,
2501 char_type, const tm* __tm,
2502 char __fmt, char __mod) const
2504 char_type __nar[100];
2505 char_type* __nb = __nar;
2506 char_type* __ne = __nb + 100;
2507 __do_put(__nb, __ne, __tm, __fmt, __mod);
2508 return _VSTD::copy(__nb, __ne, __s);
2511 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<char>)
2512 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<wchar_t>)
2514 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2515 class _LIBCPP_TEMPLATE_VIS time_put_byname
2516 : public time_put<_CharT, _OutputIterator>
2519 _LIBCPP_ALWAYS_INLINE
2520 explicit time_put_byname(const char* __nm, size_t __refs = 0)
2521 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2523 _LIBCPP_ALWAYS_INLINE
2524 explicit time_put_byname(const string& __nm, size_t __refs = 0)
2525 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2528 _LIBCPP_ALWAYS_INLINE
2529 ~time_put_byname() {}
2532 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<char>)
2533 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<wchar_t>)
2537 class _LIBCPP_TYPE_VIS money_base
2540 enum part {none, space, symbol, sign, value};
2541 struct pattern {char field[4];};
2543 _LIBCPP_ALWAYS_INLINE money_base() {}
2548 template <class _CharT, bool _International = false>
2549 class _LIBCPP_TEMPLATE_VIS moneypunct
2550 : public locale::facet,
2554 typedef _CharT char_type;
2555 typedef basic_string<char_type> string_type;
2557 _LIBCPP_ALWAYS_INLINE
2558 explicit moneypunct(size_t __refs = 0)
2559 : locale::facet(__refs) {}
2561 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
2562 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
2563 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
2564 _LIBCPP_ALWAYS_INLINE string_type curr_symbol() const {return do_curr_symbol();}
2565 _LIBCPP_ALWAYS_INLINE string_type positive_sign() const {return do_positive_sign();}
2566 _LIBCPP_ALWAYS_INLINE string_type negative_sign() const {return do_negative_sign();}
2567 _LIBCPP_ALWAYS_INLINE int frac_digits() const {return do_frac_digits();}
2568 _LIBCPP_ALWAYS_INLINE pattern pos_format() const {return do_pos_format();}
2569 _LIBCPP_ALWAYS_INLINE pattern neg_format() const {return do_neg_format();}
2571 static locale::id id;
2572 static const bool intl = _International;
2575 _LIBCPP_ALWAYS_INLINE
2578 virtual char_type do_decimal_point() const {return numeric_limits<char_type>::max();}
2579 virtual char_type do_thousands_sep() const {return numeric_limits<char_type>::max();}
2580 virtual string do_grouping() const {return string();}
2581 virtual string_type do_curr_symbol() const {return string_type();}
2582 virtual string_type do_positive_sign() const {return string_type();}
2583 virtual string_type do_negative_sign() const {return string_type(1, '-');}
2584 virtual int do_frac_digits() const {return 0;}
2585 virtual pattern do_pos_format() const
2586 {pattern __p = {{symbol, sign, none, value}}; return __p;}
2587 virtual pattern do_neg_format() const
2588 {pattern __p = {{symbol, sign, none, value}}; return __p;}
2591 template <class _CharT, bool _International>
2593 moneypunct<_CharT, _International>::id;
2595 template <class _CharT, bool _International>
2597 moneypunct<_CharT, _International>::intl;
2599 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, false>)
2600 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, true>)
2601 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, false>)
2602 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, true>)
2604 // moneypunct_byname
2606 template <class _CharT, bool _International = false>
2607 class _LIBCPP_TEMPLATE_VIS moneypunct_byname
2608 : public moneypunct<_CharT, _International>
2611 typedef money_base::pattern pattern;
2612 typedef _CharT char_type;
2613 typedef basic_string<char_type> string_type;
2615 _LIBCPP_ALWAYS_INLINE
2616 explicit moneypunct_byname(const char* __nm, size_t __refs = 0)
2617 : moneypunct<_CharT, _International>(__refs) {init(__nm);}
2619 _LIBCPP_ALWAYS_INLINE
2620 explicit moneypunct_byname(const string& __nm, size_t __refs = 0)
2621 : moneypunct<_CharT, _International>(__refs) {init(__nm.c_str());}
2624 _LIBCPP_ALWAYS_INLINE
2625 ~moneypunct_byname() {}
2627 virtual char_type do_decimal_point() const {return __decimal_point_;}
2628 virtual char_type do_thousands_sep() const {return __thousands_sep_;}
2629 virtual string do_grouping() const {return __grouping_;}
2630 virtual string_type do_curr_symbol() const {return __curr_symbol_;}
2631 virtual string_type do_positive_sign() const {return __positive_sign_;}
2632 virtual string_type do_negative_sign() const {return __negative_sign_;}
2633 virtual int do_frac_digits() const {return __frac_digits_;}
2634 virtual pattern do_pos_format() const {return __pos_format_;}
2635 virtual pattern do_neg_format() const {return __neg_format_;}
2638 char_type __decimal_point_;
2639 char_type __thousands_sep_;
2641 string_type __curr_symbol_;
2642 string_type __positive_sign_;
2643 string_type __negative_sign_;
2645 pattern __pos_format_;
2646 pattern __neg_format_;
2648 void init(const char*);
2651 template<> _LIBCPP_FUNC_VIS void moneypunct_byname<char, false>::init(const char*);
2652 template<> _LIBCPP_FUNC_VIS void moneypunct_byname<char, true>::init(const char*);
2653 template<> _LIBCPP_FUNC_VIS void moneypunct_byname<wchar_t, false>::init(const char*);
2654 template<> _LIBCPP_FUNC_VIS void moneypunct_byname<wchar_t, true>::init(const char*);
2656 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, false>)
2657 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, true>)
2658 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, false>)
2659 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, true>)
2663 template <class _CharT>
2667 typedef _CharT char_type;
2668 typedef basic_string<char_type> string_type;
2670 _LIBCPP_ALWAYS_INLINE __money_get() {}
2672 static void __gather_info(bool __intl, const locale& __loc,
2673 money_base::pattern& __pat, char_type& __dp,
2674 char_type& __ts, string& __grp,
2675 string_type& __sym, string_type& __psn,
2676 string_type& __nsn, int& __fd);
2679 template <class _CharT>
2681 __money_get<_CharT>::__gather_info(bool __intl, const locale& __loc,
2682 money_base::pattern& __pat, char_type& __dp,
2683 char_type& __ts, string& __grp,
2684 string_type& __sym, string_type& __psn,
2685 string_type& __nsn, int& __fd)
2689 const moneypunct<char_type, true>& __mp =
2690 use_facet<moneypunct<char_type, true> >(__loc);
2691 __pat = __mp.neg_format();
2692 __nsn = __mp.negative_sign();
2693 __psn = __mp.positive_sign();
2694 __dp = __mp.decimal_point();
2695 __ts = __mp.thousands_sep();
2696 __grp = __mp.grouping();
2697 __sym = __mp.curr_symbol();
2698 __fd = __mp.frac_digits();
2702 const moneypunct<char_type, false>& __mp =
2703 use_facet<moneypunct<char_type, false> >(__loc);
2704 __pat = __mp.neg_format();
2705 __nsn = __mp.negative_sign();
2706 __psn = __mp.positive_sign();
2707 __dp = __mp.decimal_point();
2708 __ts = __mp.thousands_sep();
2709 __grp = __mp.grouping();
2710 __sym = __mp.curr_symbol();
2711 __fd = __mp.frac_digits();
2715 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<char>)
2716 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<wchar_t>)
2718 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2719 class _LIBCPP_TEMPLATE_VIS money_get
2720 : public locale::facet,
2721 private __money_get<_CharT>
2724 typedef _CharT char_type;
2725 typedef _InputIterator iter_type;
2726 typedef basic_string<char_type> string_type;
2728 _LIBCPP_ALWAYS_INLINE
2729 explicit money_get(size_t __refs = 0)
2730 : locale::facet(__refs) {}
2732 _LIBCPP_ALWAYS_INLINE
2733 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
2734 ios_base::iostate& __err, long double& __v) const
2736 return do_get(__b, __e, __intl, __iob, __err, __v);
2739 _LIBCPP_ALWAYS_INLINE
2740 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
2741 ios_base::iostate& __err, string_type& __v) const
2743 return do_get(__b, __e, __intl, __iob, __err, __v);
2746 static locale::id id;
2750 _LIBCPP_ALWAYS_INLINE
2753 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
2754 ios_base& __iob, ios_base::iostate& __err,
2755 long double& __v) const;
2756 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
2757 ios_base& __iob, ios_base::iostate& __err,
2758 string_type& __v) const;
2761 static bool __do_get(iter_type& __b, iter_type __e,
2762 bool __intl, const locale& __loc,
2763 ios_base::fmtflags __flags, ios_base::iostate& __err,
2764 bool& __neg, const ctype<char_type>& __ct,
2765 unique_ptr<char_type, void(*)(void*)>& __wb,
2766 char_type*& __wn, char_type* __we);
2769 template <class _CharT, class _InputIterator>
2771 money_get<_CharT, _InputIterator>::id;
2773 _LIBCPP_FUNC_VIS void __do_nothing(void*);
2775 template <class _Tp>
2778 __double_or_nothing(unique_ptr<_Tp, void(*)(void*)>& __b, _Tp*& __n, _Tp*& __e)
2780 bool __owns = __b.get_deleter() != __do_nothing;
2781 size_t __cur_cap = static_cast<size_t>(__e-__b.get()) * sizeof(_Tp);
2782 size_t __new_cap = __cur_cap < numeric_limits<size_t>::max() / 2 ?
2783 2 * __cur_cap : numeric_limits<size_t>::max();
2785 __new_cap = sizeof(_Tp);
2786 size_t __n_off = static_cast<size_t>(__n - __b.get());
2787 _Tp* __t = (_Tp*)realloc(__owns ? __b.get() : 0, __new_cap);
2789 __throw_bad_alloc();
2792 __b = unique_ptr<_Tp, void(*)(void*)>(__t, free);
2793 __new_cap /= sizeof(_Tp);
2794 __n = __b.get() + __n_off;
2795 __e = __b.get() + __new_cap;
2799 template <class _CharT, class _InputIterator>
2801 money_get<_CharT, _InputIterator>::__do_get(iter_type& __b, iter_type __e,
2802 bool __intl, const locale& __loc,
2803 ios_base::fmtflags __flags,
2804 ios_base::iostate& __err,
2806 const ctype<char_type>& __ct,
2807 unique_ptr<char_type, void(*)(void*)>& __wb,
2808 char_type*& __wn, char_type* __we)
2810 const unsigned __bz = 100;
2811 unsigned __gbuf[__bz];
2812 unique_ptr<unsigned, void(*)(void*)> __gb(__gbuf, __do_nothing);
2813 unsigned* __gn = __gb.get();
2814 unsigned* __ge = __gn + __bz;
2815 money_base::pattern __pat;
2822 // Capture the spaces read into money_base::{space,none} so they
2823 // can be compared to initial spaces in __sym.
2824 string_type __spaces;
2826 __money_get<_CharT>::__gather_info(__intl, __loc, __pat, __dp, __ts, __grp,
2827 __sym, __psn, __nsn, __fd);
2828 const string_type* __trailing_sign = 0;
2830 for (unsigned __p = 0; __p < 4 && __b != __e; ++__p)
2832 switch (__pat.field[__p])
2834 case money_base::space:
2837 if (__ct.is(ctype_base::space, *__b))
2838 __spaces.push_back(*__b++);
2841 __err |= ios_base::failbit;
2845 _LIBCPP_FALLTHROUGH();
2846 case money_base::none:
2849 while (__b != __e && __ct.is(ctype_base::space, *__b))
2850 __spaces.push_back(*__b++);
2853 case money_base::sign:
2854 if (__psn.size() + __nsn.size() > 0)
2856 if (__psn.size() == 0 || __nsn.size() == 0)
2857 { // sign is optional
2858 if (__psn.size() > 0)
2859 { // __nsn.size() == 0
2860 if (*__b == __psn[0])
2863 if (__psn.size() > 1)
2864 __trailing_sign = &__psn;
2869 else if (*__b == __nsn[0]) // __nsn.size() > 0 && __psn.size() == 0
2873 if (__nsn.size() > 1)
2874 __trailing_sign = &__nsn;
2877 else // sign is required
2879 if (*__b == __psn[0])
2882 if (__psn.size() > 1)
2883 __trailing_sign = &__psn;
2885 else if (*__b == __nsn[0])
2889 if (__nsn.size() > 1)
2890 __trailing_sign = &__nsn;
2894 __err |= ios_base::failbit;
2900 case money_base::symbol:
2902 bool __more_needed = __trailing_sign ||
2904 (__p == 2 && __pat.field[3] != static_cast<char>(money_base::none));
2905 bool __sb = (__flags & ios_base::showbase) != 0;
2906 if (__sb || __more_needed)
2908 typename string_type::const_iterator __sym_space_end = __sym.begin();
2909 if (__p > 0 && (__pat.field[__p - 1] == money_base::none ||
2910 __pat.field[__p - 1] == money_base::space)) {
2911 // Match spaces we've already read against spaces at
2912 // the beginning of __sym.
2913 while (__sym_space_end != __sym.end() &&
2914 __ct.is(ctype_base::space, *__sym_space_end))
2916 const size_t __num_spaces = __sym_space_end - __sym.begin();
2917 if (__num_spaces > __spaces.size() ||
2918 !equal(__spaces.end() - __num_spaces, __spaces.end(),
2920 // No match. Put __sym_space_end back at the
2921 // beginning of __sym, which will prevent a
2922 // match in the next loop.
2923 __sym_space_end = __sym.begin();
2926 typename string_type::const_iterator __sym_curr_char = __sym_space_end;
2927 while (__sym_curr_char != __sym.end() && __b != __e &&
2928 *__b == *__sym_curr_char) {
2932 if (__sb && __sym_curr_char != __sym.end())
2934 __err |= ios_base::failbit;
2940 case money_base::value:
2943 for (; __b != __e; ++__b)
2945 char_type __c = *__b;
2946 if (__ct.is(ctype_base::digit, __c))
2949 __double_or_nothing(__wb, __wn, __we);
2953 else if (__grp.size() > 0 && __ng > 0 && __c == __ts)
2956 __double_or_nothing(__gb, __gn, __ge);
2963 if (__gb.get() != __gn && __ng > 0)
2966 __double_or_nothing(__gb, __gn, __ge);
2971 if (__b == __e || *__b != __dp)
2973 __err |= ios_base::failbit;
2976 for (++__b; __fd > 0; --__fd, ++__b)
2978 if (__b == __e || !__ct.is(ctype_base::digit, *__b))
2980 __err |= ios_base::failbit;
2984 __double_or_nothing(__wb, __wn, __we);
2988 if (__wn == __wb.get())
2990 __err |= ios_base::failbit;
2997 if (__trailing_sign)
2999 for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, ++__b)
3001 if (__b == __e || *__b != (*__trailing_sign)[__i])
3003 __err |= ios_base::failbit;
3008 if (__gb.get() != __gn)
3010 ios_base::iostate __et = ios_base::goodbit;
3011 __check_grouping(__grp, __gb.get(), __gn, __et);
3014 __err |= ios_base::failbit;
3021 template <class _CharT, class _InputIterator>
3023 money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3024 bool __intl, ios_base& __iob,
3025 ios_base::iostate& __err,
3026 long double& __v) const
3028 const int __bz = 100;
3029 char_type __wbuf[__bz];
3030 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3032 char_type* __we = __wbuf + __bz;
3033 locale __loc = __iob.getloc();
3034 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3036 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3039 const char __src[] = "0123456789";
3040 char_type __atoms[sizeof(__src)-1];
3041 __ct.widen(__src, __src + (sizeof(__src)-1), __atoms);
3043 char* __nc = __nbuf;
3044 unique_ptr<char, void(*)(void*)> __h(0, free);
3045 if (__wn - __wb.get() > __bz-2)
3047 __h.reset((char*)malloc(static_cast<size_t>(__wn - __wb.get() + 2)));
3049 __throw_bad_alloc();
3054 for (const char_type* __w = __wb.get(); __w < __wn; ++__w, ++__nc)
3055 *__nc = __src[find(__atoms, _VSTD::end(__atoms), *__w) - __atoms];
3057 if (sscanf(__nbuf, "%Lf", &__v) != 1)
3058 __throw_runtime_error("money_get error");
3061 __err |= ios_base::eofbit;
3065 template <class _CharT, class _InputIterator>
3067 money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3068 bool __intl, ios_base& __iob,
3069 ios_base::iostate& __err,
3070 string_type& __v) const
3072 const int __bz = 100;
3073 char_type __wbuf[__bz];
3074 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3076 char_type* __we = __wbuf + __bz;
3077 locale __loc = __iob.getloc();
3078 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3080 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3085 __v.push_back(__ct.widen('-'));
3086 char_type __z = __ct.widen('0');
3088 for (__w = __wb.get(); __w < __wn-1; ++__w)
3091 __v.append(__w, __wn);
3094 __err |= ios_base::eofbit;
3098 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<char>)
3099 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<wchar_t>)
3103 template <class _CharT>
3107 typedef _CharT char_type;
3108 typedef basic_string<char_type> string_type;
3110 _LIBCPP_ALWAYS_INLINE __money_put() {}
3112 static void __gather_info(bool __intl, bool __neg, const locale& __loc,
3113 money_base::pattern& __pat, char_type& __dp,
3114 char_type& __ts, string& __grp,
3115 string_type& __sym, string_type& __sn,
3117 static void __format(char_type* __mb, char_type*& __mi, char_type*& __me,
3118 ios_base::fmtflags __flags,
3119 const char_type* __db, const char_type* __de,
3120 const ctype<char_type>& __ct, bool __neg,
3121 const money_base::pattern& __pat, char_type __dp,
3122 char_type __ts, const string& __grp,
3123 const string_type& __sym, const string_type& __sn,
3127 template <class _CharT>
3129 __money_put<_CharT>::__gather_info(bool __intl, bool __neg, const locale& __loc,
3130 money_base::pattern& __pat, char_type& __dp,
3131 char_type& __ts, string& __grp,
3132 string_type& __sym, string_type& __sn,
3137 const moneypunct<char_type, true>& __mp =
3138 use_facet<moneypunct<char_type, true> >(__loc);
3141 __pat = __mp.neg_format();
3142 __sn = __mp.negative_sign();
3146 __pat = __mp.pos_format();
3147 __sn = __mp.positive_sign();
3149 __dp = __mp.decimal_point();
3150 __ts = __mp.thousands_sep();
3151 __grp = __mp.grouping();
3152 __sym = __mp.curr_symbol();
3153 __fd = __mp.frac_digits();
3157 const moneypunct<char_type, false>& __mp =
3158 use_facet<moneypunct<char_type, false> >(__loc);
3161 __pat = __mp.neg_format();
3162 __sn = __mp.negative_sign();
3166 __pat = __mp.pos_format();
3167 __sn = __mp.positive_sign();
3169 __dp = __mp.decimal_point();
3170 __ts = __mp.thousands_sep();
3171 __grp = __mp.grouping();
3172 __sym = __mp.curr_symbol();
3173 __fd = __mp.frac_digits();
3177 template <class _CharT>
3179 __money_put<_CharT>::__format(char_type* __mb, char_type*& __mi, char_type*& __me,
3180 ios_base::fmtflags __flags,
3181 const char_type* __db, const char_type* __de,
3182 const ctype<char_type>& __ct, bool __neg,
3183 const money_base::pattern& __pat, char_type __dp,
3184 char_type __ts, const string& __grp,
3185 const string_type& __sym, const string_type& __sn,
3189 for (unsigned __p = 0; __p < 4; ++__p)
3191 switch (__pat.field[__p])
3193 case money_base::none:
3196 case money_base::space:
3198 *__me++ = __ct.widen(' ');
3200 case money_base::sign:
3204 case money_base::symbol:
3205 if (!__sym.empty() && (__flags & ios_base::showbase))
3206 __me = _VSTD::copy(__sym.begin(), __sym.end(), __me);
3208 case money_base::value:
3210 // remember start of value so we can reverse it
3211 char_type* __t = __me;
3212 // find beginning of digits
3215 // find end of digits
3216 const char_type* __d;
3217 for (__d = __db; __d < __de; ++__d)
3218 if (!__ct.is(ctype_base::digit, *__d))
3220 // print fractional part
3224 for (__f = __fd; __d > __db && __f > 0; --__f)
3226 char_type __z = __f > 0 ? __ct.widen('0') : char_type();
3227 for (; __f > 0; --__f)
3234 *__me++ = __ct.widen('0');
3240 unsigned __gl = __grp.empty() ? numeric_limits<unsigned>::max()
3241 : static_cast<unsigned>(__grp[__ig]);
3248 if (++__ig < __grp.size())
3249 __gl = __grp[__ig] == numeric_limits<char>::max() ?
3250 numeric_limits<unsigned>::max() :
3251 static_cast<unsigned>(__grp[__ig]);
3263 // print rest of sign, if any
3264 if (__sn.size() > 1)
3265 __me = _VSTD::copy(__sn.begin()+1, __sn.end(), __me);
3267 if ((__flags & ios_base::adjustfield) == ios_base::left)
3269 else if ((__flags & ios_base::adjustfield) != ios_base::internal)
3273 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<char>)
3274 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<wchar_t>)
3276 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
3277 class _LIBCPP_TEMPLATE_VIS money_put
3278 : public locale::facet,
3279 private __money_put<_CharT>
3282 typedef _CharT char_type;
3283 typedef _OutputIterator iter_type;
3284 typedef basic_string<char_type> string_type;
3286 _LIBCPP_ALWAYS_INLINE
3287 explicit money_put(size_t __refs = 0)
3288 : locale::facet(__refs) {}
3290 _LIBCPP_ALWAYS_INLINE
3291 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3292 long double __units) const
3294 return do_put(__s, __intl, __iob, __fl, __units);
3297 _LIBCPP_ALWAYS_INLINE
3298 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3299 const string_type& __digits) const
3301 return do_put(__s, __intl, __iob, __fl, __digits);
3304 static locale::id id;
3307 _LIBCPP_ALWAYS_INLINE
3310 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3311 char_type __fl, long double __units) const;
3312 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3313 char_type __fl, const string_type& __digits) const;
3316 template <class _CharT, class _OutputIterator>
3318 money_put<_CharT, _OutputIterator>::id;
3320 template <class _CharT, class _OutputIterator>
3322 money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3323 ios_base& __iob, char_type __fl,
3324 long double __units) const
3327 const size_t __bs = 100;
3330 char_type __digits[__bs];
3331 char_type* __db = __digits;
3332 size_t __n = static_cast<size_t>(snprintf(__bb, __bs, "%.0Lf", __units));
3333 unique_ptr<char, void(*)(void*)> __hn(0, free);
3334 unique_ptr<char_type, void(*)(void*)> __hd(0, free);
3335 // secure memory for digit storage
3338 __n = static_cast<size_t>(__libcpp_asprintf_l(&__bb, _LIBCPP_GET_C_LOCALE, "%.0Lf", __units));
3340 __throw_bad_alloc();
3342 __hd.reset((char_type*)malloc(__n * sizeof(char_type)));
3343 if (__hd == nullptr)
3344 __throw_bad_alloc();
3348 locale __loc = __iob.getloc();
3349 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3350 __ct.widen(__bb, __bb + __n, __db);
3351 bool __neg = __n > 0 && __bb[0] == '-';
3352 money_base::pattern __pat;
3359 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3360 // secure memory for formatting
3361 char_type __mbuf[__bs];
3362 char_type* __mb = __mbuf;
3363 unique_ptr<char_type, void(*)(void*)> __hw(0, free);
3364 size_t __exn = static_cast<int>(__n) > __fd ?
3365 (__n - static_cast<size_t>(__fd)) * 2 + __sn.size() +
3366 __sym.size() + static_cast<size_t>(__fd) + 1
3367 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
3370 __hw.reset((char_type*)malloc(__exn * sizeof(char_type)));
3373 __throw_bad_alloc();
3378 this->__format(__mb, __mi, __me, __iob.flags(),
3379 __db, __db + __n, __ct,
3380 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3381 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3384 template <class _CharT, class _OutputIterator>
3386 money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3387 ios_base& __iob, char_type __fl,
3388 const string_type& __digits) const
3391 locale __loc = __iob.getloc();
3392 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3393 bool __neg = __digits.size() > 0 && __digits[0] == __ct.widen('-');
3394 money_base::pattern __pat;
3401 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3402 // secure memory for formatting
3403 char_type __mbuf[100];
3404 char_type* __mb = __mbuf;
3405 unique_ptr<char_type, void(*)(void*)> __h(0, free);
3406 size_t __exn = static_cast<int>(__digits.size()) > __fd ?
3407 (__digits.size() - static_cast<size_t>(__fd)) * 2 +
3408 __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 1
3409 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
3412 __h.reset((char_type*)malloc(__exn * sizeof(char_type)));
3415 __throw_bad_alloc();
3420 this->__format(__mb, __mi, __me, __iob.flags(),
3421 __digits.data(), __digits.data() + __digits.size(), __ct,
3422 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3423 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3426 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<char>)
3427 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<wchar_t>)
3431 class _LIBCPP_TYPE_VIS messages_base
3434 typedef ptrdiff_t catalog;
3436 _LIBCPP_ALWAYS_INLINE messages_base() {}
3439 template <class _CharT>
3440 class _LIBCPP_TEMPLATE_VIS messages
3441 : public locale::facet,
3442 public messages_base
3445 typedef _CharT char_type;
3446 typedef basic_string<_CharT> string_type;
3448 _LIBCPP_ALWAYS_INLINE
3449 explicit messages(size_t __refs = 0)
3450 : locale::facet(__refs) {}
3452 _LIBCPP_ALWAYS_INLINE
3453 catalog open(const basic_string<char>& __nm, const locale& __loc) const
3455 return do_open(__nm, __loc);
3458 _LIBCPP_ALWAYS_INLINE
3459 string_type get(catalog __c, int __set, int __msgid,
3460 const string_type& __dflt) const
3462 return do_get(__c, __set, __msgid, __dflt);
3465 _LIBCPP_ALWAYS_INLINE
3466 void close(catalog __c) const
3471 static locale::id id;
3474 _LIBCPP_ALWAYS_INLINE
3477 virtual catalog do_open(const basic_string<char>&, const locale&) const;
3478 virtual string_type do_get(catalog, int __set, int __msgid,
3479 const string_type& __dflt) const;
3480 virtual void do_close(catalog) const;
3483 template <class _CharT>
3485 messages<_CharT>::id;
3487 template <class _CharT>
3488 typename messages<_CharT>::catalog
3489 messages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const
3491 #ifdef _LIBCPP_HAS_CATOPEN
3492 catalog __cat = (catalog)catopen(__nm.c_str(), NL_CAT_LOCALE);
3494 __cat = static_cast<catalog>((static_cast<size_t>(__cat) >> 1));
3496 #else // !_LIBCPP_HAS_CATOPEN
3498 #endif // _LIBCPP_HAS_CATOPEN
3501 template <class _CharT>
3502 typename messages<_CharT>::string_type
3503 messages<_CharT>::do_get(catalog __c, int __set, int __msgid,
3504 const string_type& __dflt) const
3506 #ifdef _LIBCPP_HAS_CATOPEN
3508 __narrow_to_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__ndflt),
3510 __dflt.c_str() + __dflt.size());
3513 nl_catd __cat = (nl_catd)__c;
3514 char* __n = catgets(__cat, __set, __msgid, __ndflt.c_str());
3516 __widen_from_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__w),
3517 __n, __n + strlen(__n));
3519 #else // !_LIBCPP_HAS_CATOPEN
3521 #endif // _LIBCPP_HAS_CATOPEN
3524 template <class _CharT>
3526 messages<_CharT>::do_close(catalog __c) const
3528 #ifdef _LIBCPP_HAS_CATOPEN
3531 nl_catd __cat = (nl_catd)__c;
3533 #endif // _LIBCPP_HAS_CATOPEN
3536 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<char>)
3537 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<wchar_t>)
3539 template <class _CharT>
3540 class _LIBCPP_TEMPLATE_VIS messages_byname
3541 : public messages<_CharT>
3544 typedef messages_base::catalog catalog;
3545 typedef basic_string<_CharT> string_type;
3547 _LIBCPP_ALWAYS_INLINE
3548 explicit messages_byname(const char*, size_t __refs = 0)
3549 : messages<_CharT>(__refs) {}
3551 _LIBCPP_ALWAYS_INLINE
3552 explicit messages_byname(const string&, size_t __refs = 0)
3553 : messages<_CharT>(__refs) {}
3556 _LIBCPP_ALWAYS_INLINE
3557 ~messages_byname() {}
3560 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<char>)
3561 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<wchar_t>)
3563 template<class _Codecvt, class _Elem = wchar_t,
3564 class _Wide_alloc = allocator<_Elem>,
3565 class _Byte_alloc = allocator<char> >
3566 class _LIBCPP_TEMPLATE_VIS wstring_convert
3569 typedef basic_string<char, char_traits<char>, _Byte_alloc> byte_string;
3570 typedef basic_string<_Elem, char_traits<_Elem>, _Wide_alloc> wide_string;
3571 typedef typename _Codecvt::state_type state_type;
3572 typedef typename wide_string::traits_type::int_type int_type;
3575 byte_string __byte_err_string_;
3576 wide_string __wide_err_string_;
3577 _Codecvt* __cvtptr_;
3578 state_type __cvtstate_;
3581 wstring_convert(const wstring_convert& __wc);
3582 wstring_convert& operator=(const wstring_convert& __wc);
3584 _LIBCPP_ALWAYS_INLINE
3585 _LIBCPP_EXPLICIT_AFTER_CXX11 wstring_convert(_Codecvt* __pcvt = new _Codecvt);
3586 _LIBCPP_ALWAYS_INLINE
3587 wstring_convert(_Codecvt* __pcvt, state_type __state);
3588 _LIBCPP_EXPLICIT_AFTER_CXX11 wstring_convert(const byte_string& __byte_err,
3589 const wide_string& __wide_err = wide_string());
3590 #ifndef _LIBCPP_CXX03_LANG
3591 _LIBCPP_ALWAYS_INLINE
3592 wstring_convert(wstring_convert&& __wc);
3596 _LIBCPP_ALWAYS_INLINE
3597 wide_string from_bytes(char __byte)
3598 {return from_bytes(&__byte, &__byte+1);}
3599 _LIBCPP_ALWAYS_INLINE
3600 wide_string from_bytes(const char* __ptr)
3601 {return from_bytes(__ptr, __ptr + char_traits<char>::length(__ptr));}
3602 _LIBCPP_ALWAYS_INLINE
3603 wide_string from_bytes(const byte_string& __str)
3604 {return from_bytes(__str.data(), __str.data() + __str.size());}
3605 wide_string from_bytes(const char* __first, const char* __last);
3607 _LIBCPP_ALWAYS_INLINE
3608 byte_string to_bytes(_Elem __wchar)
3609 {return to_bytes(&__wchar, &__wchar+1);}
3610 _LIBCPP_ALWAYS_INLINE
3611 byte_string to_bytes(const _Elem* __wptr)
3612 {return to_bytes(__wptr, __wptr + char_traits<_Elem>::length(__wptr));}
3613 _LIBCPP_ALWAYS_INLINE
3614 byte_string to_bytes(const wide_string& __wstr)
3615 {return to_bytes(__wstr.data(), __wstr.data() + __wstr.size());}
3616 byte_string to_bytes(const _Elem* __first, const _Elem* __last);
3618 _LIBCPP_ALWAYS_INLINE
3619 size_t converted() const _NOEXCEPT {return __cvtcount_;}
3620 _LIBCPP_ALWAYS_INLINE
3621 state_type state() const {return __cvtstate_;}
3624 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3626 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3627 wstring_convert(_Codecvt* __pcvt)
3628 : __cvtptr_(__pcvt), __cvtstate_(), __cvtcount_(0)
3632 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3634 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3635 wstring_convert(_Codecvt* __pcvt, state_type __state)
3636 : __cvtptr_(__pcvt), __cvtstate_(__state), __cvtcount_(0)
3640 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3641 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3642 wstring_convert(const byte_string& __byte_err, const wide_string& __wide_err)
3643 : __byte_err_string_(__byte_err), __wide_err_string_(__wide_err),
3644 __cvtstate_(), __cvtcount_(0)
3646 __cvtptr_ = new _Codecvt;
3649 #ifndef _LIBCPP_CXX03_LANG
3651 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3653 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3654 wstring_convert(wstring_convert&& __wc)
3655 : __byte_err_string_(_VSTD::move(__wc.__byte_err_string_)),
3656 __wide_err_string_(_VSTD::move(__wc.__wide_err_string_)),
3657 __cvtptr_(__wc.__cvtptr_),
3658 __cvtstate_(__wc.__cvtstate_), __cvtcount_(__wc.__cvtcount_)
3660 __wc.__cvtptr_ = nullptr;
3663 #endif // _LIBCPP_CXX03_LANG
3665 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3666 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::~wstring_convert()
3671 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3672 typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::wide_string
3673 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3674 from_bytes(const char* __frm, const char* __frm_end)
3677 if (__cvtptr_ != nullptr)
3679 wide_string __ws(2*(__frm_end - __frm), _Elem());
3680 if (__frm != __frm_end)
3681 __ws.resize(__ws.capacity());
3682 codecvt_base::result __r = codecvt_base::ok;
3683 state_type __st = __cvtstate_;
3684 if (__frm != __frm_end)
3686 _Elem* __to = &__ws[0];
3687 _Elem* __to_end = __to + __ws.size();
3688 const char* __frm_nxt;
3692 __r = __cvtptr_->in(__st, __frm, __frm_end, __frm_nxt,
3693 __to, __to_end, __to_nxt);
3694 __cvtcount_ += __frm_nxt - __frm;
3695 if (__frm_nxt == __frm)
3697 __r = codecvt_base::error;
3699 else if (__r == codecvt_base::noconv)
3701 __ws.resize(__to - &__ws[0]);
3702 // This only gets executed if _Elem is char
3703 __ws.append((const _Elem*)__frm, (const _Elem*)__frm_end);
3705 __r = codecvt_base::ok;
3707 else if (__r == codecvt_base::ok)
3709 __ws.resize(__to_nxt - &__ws[0]);
3712 else if (__r == codecvt_base::partial)
3714 ptrdiff_t __s = __to_nxt - &__ws[0];
3715 __ws.resize(2 * __s);
3716 __to = &__ws[0] + __s;
3717 __to_end = &__ws[0] + __ws.size();
3720 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3722 if (__r == codecvt_base::ok)
3726 if (__wide_err_string_.empty())
3727 __throw_range_error("wstring_convert: from_bytes error");
3729 return __wide_err_string_;
3732 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3733 typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::byte_string
3734 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3735 to_bytes(const _Elem* __frm, const _Elem* __frm_end)
3738 if (__cvtptr_ != nullptr)
3740 byte_string __bs(2*(__frm_end - __frm), char());
3741 if (__frm != __frm_end)
3742 __bs.resize(__bs.capacity());
3743 codecvt_base::result __r = codecvt_base::ok;
3744 state_type __st = __cvtstate_;
3745 if (__frm != __frm_end)
3747 char* __to = &__bs[0];
3748 char* __to_end = __to + __bs.size();
3749 const _Elem* __frm_nxt;
3753 __r = __cvtptr_->out(__st, __frm, __frm_end, __frm_nxt,
3754 __to, __to_end, __to_nxt);
3755 __cvtcount_ += __frm_nxt - __frm;
3756 if (__frm_nxt == __frm)
3758 __r = codecvt_base::error;
3760 else if (__r == codecvt_base::noconv)
3762 __bs.resize(__to - &__bs[0]);
3763 // This only gets executed if _Elem is char
3764 __bs.append((const char*)__frm, (const char*)__frm_end);
3766 __r = codecvt_base::ok;
3768 else if (__r == codecvt_base::ok)
3770 __bs.resize(__to_nxt - &__bs[0]);
3773 else if (__r == codecvt_base::partial)
3775 ptrdiff_t __s = __to_nxt - &__bs[0];
3776 __bs.resize(2 * __s);
3777 __to = &__bs[0] + __s;
3778 __to_end = &__bs[0] + __bs.size();
3781 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3783 if (__r == codecvt_base::ok)
3785 size_t __s = __bs.size();
3786 __bs.resize(__bs.capacity());
3787 char* __to = &__bs[0] + __s;
3788 char* __to_end = __to + __bs.size();
3792 __r = __cvtptr_->unshift(__st, __to, __to_end, __to_nxt);
3793 if (__r == codecvt_base::noconv)
3795 __bs.resize(__to - &__bs[0]);
3796 __r = codecvt_base::ok;
3798 else if (__r == codecvt_base::ok)
3800 __bs.resize(__to_nxt - &__bs[0]);
3802 else if (__r == codecvt_base::partial)
3804 ptrdiff_t __sp = __to_nxt - &__bs[0];
3805 __bs.resize(2 * __sp);
3806 __to = &__bs[0] + __sp;
3807 __to_end = &__bs[0] + __bs.size();
3809 } while (__r == codecvt_base::partial);
3810 if (__r == codecvt_base::ok)
3815 if (__byte_err_string_.empty())
3816 __throw_range_error("wstring_convert: to_bytes error");
3818 return __byte_err_string_;
3821 template <class _Codecvt, class _Elem = wchar_t, class _Tr = char_traits<_Elem> >
3822 class _LIBCPP_TEMPLATE_VIS wbuffer_convert
3823 : public basic_streambuf<_Elem, _Tr>
3827 typedef _Elem char_type;
3828 typedef _Tr traits_type;
3829 typedef typename traits_type::int_type int_type;
3830 typedef typename traits_type::pos_type pos_type;
3831 typedef typename traits_type::off_type off_type;
3832 typedef typename _Codecvt::state_type state_type;
3836 const char* __extbufnext_;
3837 const char* __extbufend_;
3838 char __extbuf_min_[8];
3840 char_type* __intbuf_;
3842 streambuf* __bufptr_;
3845 ios_base::openmode __cm_;
3848 bool __always_noconv_;
3850 wbuffer_convert(const wbuffer_convert&);
3851 wbuffer_convert& operator=(const wbuffer_convert&);
3853 _LIBCPP_EXPLICIT_AFTER_CXX11 wbuffer_convert(streambuf* __bytebuf = 0,
3854 _Codecvt* __pcvt = new _Codecvt, state_type __state = state_type());
3857 _LIBCPP_INLINE_VISIBILITY
3858 streambuf* rdbuf() const {return __bufptr_;}
3859 _LIBCPP_INLINE_VISIBILITY
3860 streambuf* rdbuf(streambuf* __bytebuf)
3862 streambuf* __r = __bufptr_;
3863 __bufptr_ = __bytebuf;
3867 _LIBCPP_INLINE_VISIBILITY
3868 state_type state() const {return __st_;}
3871 virtual int_type underflow();
3872 virtual int_type pbackfail(int_type __c = traits_type::eof());
3873 virtual int_type overflow (int_type __c = traits_type::eof());
3874 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s,
3876 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
3877 ios_base::openmode __wch = ios_base::in | ios_base::out);
3878 virtual pos_type seekpos(pos_type __sp,
3879 ios_base::openmode __wch = ios_base::in | ios_base::out);
3884 void __write_mode();
3885 wbuffer_convert* __close();
3888 template <class _Codecvt, class _Elem, class _Tr>
3889 wbuffer_convert<_Codecvt, _Elem, _Tr>::
3890 wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt, state_type __state)
3897 __bufptr_(__bytebuf),
3903 __always_noconv_(__cv_ ? __cv_->always_noconv() : false)
3908 template <class _Codecvt, class _Elem, class _Tr>
3909 wbuffer_convert<_Codecvt, _Elem, _Tr>::~wbuffer_convert()
3914 delete [] __extbuf_;
3916 delete [] __intbuf_;
3919 template <class _Codecvt, class _Elem, class _Tr>
3920 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
3921 wbuffer_convert<_Codecvt, _Elem, _Tr>::underflow()
3923 if (__cv_ == 0 || __bufptr_ == 0)
3924 return traits_type::eof();
3925 bool __initial = __read_mode();
3927 if (this->gptr() == 0)
3928 this->setg(&__1buf, &__1buf+1, &__1buf+1);
3929 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
3930 int_type __c = traits_type::eof();
3931 if (this->gptr() == this->egptr())
3933 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
3934 if (__always_noconv_)
3936 streamsize __nmemb = static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz);
3937 __nmemb = __bufptr_->sgetn((char*)this->eback() + __unget_sz, __nmemb);
3940 this->setg(this->eback(),
3941 this->eback() + __unget_sz,
3942 this->eback() + __unget_sz + __nmemb);
3943 __c = *this->gptr();
3948 _LIBCPP_ASSERT(!(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
3949 if (__extbufend_ != __extbufnext_)
3950 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
3951 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
3952 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
3953 streamsize __nmemb = _VSTD::min(static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz),
3954 static_cast<streamsize>(__extbufend_ - __extbufnext_));
3955 codecvt_base::result __r;
3956 // FIXME: Do we ever need to restore the state here?
3957 //state_type __svs = __st_;
3958 streamsize __nr = __bufptr_->sgetn(const_cast<char*>(__extbufnext_), __nmemb);
3961 __extbufend_ = __extbufnext_ + __nr;
3963 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
3964 this->eback() + __unget_sz,
3965 this->egptr(), __inext);
3966 if (__r == codecvt_base::noconv)
3968 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)__extbufend_);
3969 __c = *this->gptr();
3971 else if (__inext != this->eback() + __unget_sz)
3973 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
3974 __c = *this->gptr();
3980 __c = *this->gptr();
3981 if (this->eback() == &__1buf)
3982 this->setg(0, 0, 0);
3986 template <class _Codecvt, class _Elem, class _Tr>
3987 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
3988 wbuffer_convert<_Codecvt, _Elem, _Tr>::pbackfail(int_type __c)
3990 if (__cv_ != 0 && __bufptr_ != 0 && this->eback() < this->gptr())
3992 if (traits_type::eq_int_type(__c, traits_type::eof()))
3995 return traits_type::not_eof(__c);
3997 if (traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
4000 *this->gptr() = traits_type::to_char_type(__c);
4004 return traits_type::eof();
4007 template <class _Codecvt, class _Elem, class _Tr>
4008 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4009 wbuffer_convert<_Codecvt, _Elem, _Tr>::overflow(int_type __c)
4011 if (__cv_ == 0 || __bufptr_ == 0)
4012 return traits_type::eof();
4015 char_type* __pb_save = this->pbase();
4016 char_type* __epb_save = this->epptr();
4017 if (!traits_type::eq_int_type(__c, traits_type::eof()))
4019 if (this->pptr() == 0)
4020 this->setp(&__1buf, &__1buf+1);
4021 *this->pptr() = traits_type::to_char_type(__c);
4024 if (this->pptr() != this->pbase())
4026 if (__always_noconv_)
4028 streamsize __nmemb = static_cast<streamsize>(this->pptr() - this->pbase());
4029 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4030 return traits_type::eof();
4034 char* __extbe = __extbuf_;
4035 codecvt_base::result __r;
4038 const char_type* __e;
4039 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
4040 __extbuf_, __extbuf_ + __ebs_, __extbe);
4041 if (__e == this->pbase())
4042 return traits_type::eof();
4043 if (__r == codecvt_base::noconv)
4045 streamsize __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
4046 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4047 return traits_type::eof();
4049 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
4051 streamsize __nmemb = static_cast<size_t>(__extbe - __extbuf_);
4052 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4053 return traits_type::eof();
4054 if (__r == codecvt_base::partial)
4056 this->setp((char_type*)__e, this->pptr());
4057 this->pbump(this->epptr() - this->pbase());
4061 return traits_type::eof();
4062 } while (__r == codecvt_base::partial);
4064 this->setp(__pb_save, __epb_save);
4066 return traits_type::not_eof(__c);
4069 template <class _Codecvt, class _Elem, class _Tr>
4070 basic_streambuf<_Elem, _Tr>*
4071 wbuffer_convert<_Codecvt, _Elem, _Tr>::setbuf(char_type* __s, streamsize __n)
4073 this->setg(0, 0, 0);
4076 delete [] __extbuf_;
4078 delete [] __intbuf_;
4080 if (__ebs_ > sizeof(__extbuf_min_))
4082 if (__always_noconv_ && __s)
4084 __extbuf_ = (char*)__s;
4089 __extbuf_ = new char[__ebs_];
4095 __extbuf_ = __extbuf_min_;
4096 __ebs_ = sizeof(__extbuf_min_);
4099 if (!__always_noconv_)
4101 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
4102 if (__s && __ibs_ >= sizeof(__extbuf_min_))
4109 __intbuf_ = new char_type[__ibs_];
4122 template <class _Codecvt, class _Elem, class _Tr>
4123 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4124 wbuffer_convert<_Codecvt, _Elem, _Tr>::seekoff(off_type __off, ios_base::seekdir __way,
4125 ios_base::openmode __om)
4127 int __width = __cv_->encoding();
4128 if (__cv_ == 0 || __bufptr_ == 0 || (__width <= 0 && __off != 0) || sync())
4129 return pos_type(off_type(-1));
4130 // __width > 0 || __off == 0, now check __way
4131 if (__way != ios_base::beg && __way != ios_base::cur && __way != ios_base::end)
4132 return pos_type(off_type(-1));
4133 pos_type __r = __bufptr_->pubseekoff(__width * __off, __way, __om);
4138 template <class _Codecvt, class _Elem, class _Tr>
4139 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4140 wbuffer_convert<_Codecvt, _Elem, _Tr>::seekpos(pos_type __sp, ios_base::openmode __wch)
4142 if (__cv_ == 0 || __bufptr_ == 0 || sync())
4143 return pos_type(off_type(-1));
4144 if (__bufptr_->pubseekpos(__sp, __wch) == pos_type(off_type(-1)))
4145 return pos_type(off_type(-1));
4149 template <class _Codecvt, class _Elem, class _Tr>
4151 wbuffer_convert<_Codecvt, _Elem, _Tr>::sync()
4153 if (__cv_ == 0 || __bufptr_ == 0)
4155 if (__cm_ & ios_base::out)
4157 if (this->pptr() != this->pbase())
4158 if (overflow() == traits_type::eof())
4160 codecvt_base::result __r;
4164 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
4165 streamsize __nmemb = static_cast<streamsize>(__extbe - __extbuf_);
4166 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4168 } while (__r == codecvt_base::partial);
4169 if (__r == codecvt_base::error)
4171 if (__bufptr_->pubsync())
4174 else if (__cm_ & ios_base::in)
4177 if (__always_noconv_)
4178 __c = this->egptr() - this->gptr();
4181 int __width = __cv_->encoding();
4182 __c = __extbufend_ - __extbufnext_;
4184 __c += __width * (this->egptr() - this->gptr());
4187 if (this->gptr() != this->egptr())
4189 reverse(this->gptr(), this->egptr());
4190 codecvt_base::result __r;
4191 const char_type* __e = this->gptr();
4195 __r = __cv_->out(__st_, __e, this->egptr(), __e,
4196 __extbuf_, __extbuf_ + __ebs_, __extbe);
4199 case codecvt_base::noconv:
4200 __c += this->egptr() - this->gptr();
4202 case codecvt_base::ok:
4203 case codecvt_base::partial:
4204 __c += __extbe - __extbuf_;
4209 } while (__r == codecvt_base::partial);
4213 if (__bufptr_->pubseekoff(-__c, ios_base::cur, __cm_) == pos_type(off_type(-1)))
4215 this->setg(0, 0, 0);
4221 template <class _Codecvt, class _Elem, class _Tr>
4223 wbuffer_convert<_Codecvt, _Elem, _Tr>::__read_mode()
4225 if (!(__cm_ & ios_base::in))
4228 if (__always_noconv_)
4229 this->setg((char_type*)__extbuf_,
4230 (char_type*)__extbuf_ + __ebs_,
4231 (char_type*)__extbuf_ + __ebs_);
4233 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
4234 __cm_ = ios_base::in;
4240 template <class _Codecvt, class _Elem, class _Tr>
4242 wbuffer_convert<_Codecvt, _Elem, _Tr>::__write_mode()
4244 if (!(__cm_ & ios_base::out))
4246 this->setg(0, 0, 0);
4247 if (__ebs_ > sizeof(__extbuf_min_))
4249 if (__always_noconv_)
4250 this->setp((char_type*)__extbuf_,
4251 (char_type*)__extbuf_ + (__ebs_ - 1));
4253 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
4257 __cm_ = ios_base::out;
4261 template <class _Codecvt, class _Elem, class _Tr>
4262 wbuffer_convert<_Codecvt, _Elem, _Tr>*
4263 wbuffer_convert<_Codecvt, _Elem, _Tr>::__close()
4265 wbuffer_convert* __rt = 0;
4266 if (__cv_ != 0 && __bufptr_ != 0)
4269 if ((__cm_ & ios_base::out) && sync())
4275 _LIBCPP_END_NAMESPACE_STD
4277 #endif // _LIBCPP_LOCALE