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 wstring_convert(Codecvt* pcvt = new Codecvt);
97 wstring_convert(Codecvt* pcvt, state_type state);
98 wstring_convert(const byte_string& byte_err,
99 const wide_string& wide_err = wide_string());
102 wide_string from_bytes(char byte);
103 wide_string from_bytes(const char* ptr);
104 wide_string from_bytes(const byte_string& str);
105 wide_string from_bytes(const char* first, const char* last);
107 byte_string to_bytes(Elem wchar);
108 byte_string to_bytes(const Elem* wptr);
109 byte_string to_bytes(const wide_string& wstr);
110 byte_string to_bytes(const Elem* first, const Elem* last);
112 size_t converted() const;
113 state_type state() const;
116 template <class Codecvt, class Elem = wchar_t, class Tr = char_traits<Elem>>
117 class wbuffer_convert
118 : public basic_streambuf<Elem, Tr>
121 typedef typename Tr::state_type state_type;
123 wbuffer_convert(streambuf* bytebuf = 0, Codecvt* pcvt = new Codecvt,
124 state_type state = state_type());
126 streambuf* rdbuf() const;
127 streambuf* rdbuf(streambuf* bytebuf);
129 state_type state() const;
132 // 22.4.1 and 22.4.1.3, ctype:
134 template <class charT> class ctype;
135 template <> class ctype<char>; // specialization
136 template <class charT> class ctype_byname;
137 template <> class ctype_byname<char>; // specialization
140 template <class internT, class externT, class stateT> class codecvt;
141 template <class internT, class externT, class stateT> class codecvt_byname;
143 // 22.4.2 and 22.4.3, numeric:
144 template <class charT, class InputIterator> class num_get;
145 template <class charT, class OutputIterator> class num_put;
146 template <class charT> class numpunct;
147 template <class charT> class numpunct_byname;
149 // 22.4.4, col lation:
150 template <class charT> class collate;
151 template <class charT> class collate_byname;
153 // 22.4.5, date and time:
155 template <class charT, class InputIterator> class time_get;
156 template <class charT, class InputIterator> class time_get_byname;
157 template <class charT, class OutputIterator> class time_put;
158 template <class charT, class OutputIterator> class time_put_byname;
162 template <class charT, class InputIterator> class money_get;
163 template <class charT, class OutputIterator> class money_put;
164 template <class charT, bool Intl> class moneypunct;
165 template <class charT, bool Intl> class moneypunct_byname;
167 // 22.4.7, message retrieval:
169 template <class charT> class messages;
170 template <class charT> class messages_byname;
190 #include <support/win32/locale_win32.h>
192 #include <nl_types.h>
196 #include <Availability.h>
199 #include <__undef_min_max>
201 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
202 #pragma GCC system_header
205 _LIBCPP_BEGIN_NAMESPACE_STD
207 #if __APPLE__ || __FreeBSD__
208 # define _LIBCPP_GET_C_LOCALE 0
210 # define _LIBCPP_GET_C_LOCALE __cloc()
211 // Get the C locale object
213 #define __cloc_defined
216 typedef _VSTD::remove_pointer<locale_t>::type __locale_struct;
217 typedef _VSTD::unique_ptr<__locale_struct, decltype(&freelocale)> __locale_unique_ptr;
218 #ifndef _LIBCPP_LOCALE__L_EXTENSIONS
219 typedef _VSTD::unique_ptr<__locale_struct, decltype(&uselocale)> __locale_raii;
222 // OSX has nice foo_l() functions that let you turn off use of the global
223 // locale. Linux, not so much. The following functions avoid the locale when
224 // that's possible and otherwise do the wrong thing. FIXME.
227 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
228 decltype(MB_CUR_MAX_L(_VSTD::declval<locale_t>()))
229 inline _LIBCPP_INLINE_VISIBILITY
230 __mb_cur_max_l(locale_t __l)
232 return MB_CUR_MAX_L(__l);
234 #else // _LIBCPP_LOCALE__L_EXTENSIONS
235 _LIBCPP_ALWAYS_INLINE inline
236 decltype(MB_CUR_MAX) __mb_cur_max_l(locale_t __l)
238 __locale_raii __current(uselocale(__l), uselocale);
241 #endif // _LIBCPP_LOCALE__L_EXTENSIONS
243 _LIBCPP_ALWAYS_INLINE inline
244 wint_t __btowc_l(int __c, locale_t __l)
246 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
247 return btowc_l(__c, __l);
249 __locale_raii __current(uselocale(__l), uselocale);
254 _LIBCPP_ALWAYS_INLINE inline
255 int __wctob_l(wint_t __c, locale_t __l)
257 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
258 return wctob_l(__c, __l);
260 __locale_raii __current(uselocale(__l), uselocale);
265 _LIBCPP_ALWAYS_INLINE inline
266 size_t __wcsnrtombs_l(char *__dest, const wchar_t **__src, size_t __nwc,
267 size_t __len, mbstate_t *__ps, locale_t __l)
269 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
270 return wcsnrtombs_l(__dest, __src, __nwc, __len, __ps, __l);
272 __locale_raii __current(uselocale(__l), uselocale);
273 return wcsnrtombs(__dest, __src, __nwc, __len, __ps);
277 _LIBCPP_ALWAYS_INLINE inline
278 size_t __wcrtomb_l(char *__s, wchar_t __wc, mbstate_t *__ps, locale_t __l)
280 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
281 return wcrtomb_l(__s, __wc, __ps, __l);
283 __locale_raii __current(uselocale(__l), uselocale);
284 return wcrtomb(__s, __wc, __ps);
288 _LIBCPP_ALWAYS_INLINE inline
289 size_t __mbsnrtowcs_l(wchar_t * __dest, const char **__src, size_t __nms,
290 size_t __len, mbstate_t *__ps, locale_t __l)
292 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
293 return mbsnrtowcs_l(__dest, __src, __nms, __len, __ps, __l);
295 __locale_raii __current(uselocale(__l), uselocale);
296 return mbsnrtowcs(__dest, __src, __nms, __len, __ps);
300 _LIBCPP_ALWAYS_INLINE inline
301 size_t __mbrtowc_l(wchar_t *__pwc, const char *__s, size_t __n,
302 mbstate_t *__ps, locale_t __l)
304 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
305 return mbrtowc_l(__pwc, __s, __n, __ps, __l);
307 __locale_raii __current(uselocale(__l), uselocale);
308 return mbrtowc(__pwc, __s, __n, __ps);
312 _LIBCPP_ALWAYS_INLINE inline
313 int __mbtowc_l(wchar_t *__pwc, const char *__pmb, size_t __max, locale_t __l)
315 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
316 return mbtowc_l(__pwc, __pmb, __max, __l);
318 __locale_raii __current(uselocale(__l), uselocale);
319 return mbtowc(__pwc, __pmb, __max);
323 _LIBCPP_ALWAYS_INLINE inline
324 size_t __mbrlen_l(const char *__s, size_t __n, mbstate_t *__ps, locale_t __l)
326 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
327 return mbrlen_l(__s, __n, __ps, __l);
329 __locale_raii __current(uselocale(__l), uselocale);
330 return mbrlen(__s, __n, __ps);
334 _LIBCPP_ALWAYS_INLINE inline
335 lconv *__localeconv_l(locale_t __l)
337 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
338 return localeconv_l(__l);
340 __locale_raii __current(uselocale(__l), uselocale);
345 _LIBCPP_ALWAYS_INLINE inline
346 size_t __mbsrtowcs_l(wchar_t *__dest, const char **__src, size_t __len,
347 mbstate_t *__ps, locale_t __l)
349 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
350 return mbsrtowcs_l(__dest, __src, __len, __ps, __l);
352 __locale_raii __current(uselocale(__l), uselocale);
353 return mbsrtowcs(__dest, __src, __len, __ps);
357 _LIBCPP_ALWAYS_INLINE inline
358 int __sprintf_l(char *__s, locale_t __l, const char *__format, ...) {
360 va_start(__va, __format);
361 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
362 int __res = vsprintf_l(__s, __l, __format, __va);
364 __locale_raii __current(uselocale(__l), uselocale);
365 int __res = vsprintf(__s, __format, __va);
371 _LIBCPP_ALWAYS_INLINE inline
372 int __snprintf_l(char *__s, size_t __n, locale_t __l, const char *__format, ...) {
374 va_start(__va, __format);
375 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
376 int __res = vsnprintf_l(__s, __n, __l, __format, __va);
378 __locale_raii __current(uselocale(__l), uselocale);
379 int __res = vsnprintf(__s, __n, __format, __va);
385 _LIBCPP_ALWAYS_INLINE inline
386 int __asprintf_l(char **__s, locale_t __l, const char *__format, ...) {
388 va_start(__va, __format);
389 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
390 int __res = vasprintf_l(__s, __l, __format, __va);
392 __locale_raii __current(uselocale(__l), uselocale);
393 int __res = vasprintf(__s, __format, __va);
399 _LIBCPP_ALWAYS_INLINE inline
400 int __sscanf_l(const char *__s, locale_t __l, const char *__format, ...) {
402 va_start(__va, __format);
403 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
404 int __res = vsscanf_l(__s, __l, __format, __va);
406 __locale_raii __current(uselocale(__l), uselocale);
407 int __res = vsscanf(__s, __format, __va);
416 // Scans [__b, __e) until a match is found in the basic_strings range
417 // [__kb, __ke) or until it can be shown that there is no match in [__kb, __ke).
418 // __b will be incremented (visibly), consuming CharT until a match is found
419 // or proved to not exist. A keyword may be "", in which will match anything.
420 // If one keyword is a prefix of another, and the next CharT in the input
421 // might match another keyword, the algorithm will attempt to find the longest
422 // matching keyword. If the longer matching keyword ends up not matching, then
423 // no keyword match is found. If no keyword match is found, __ke is returned
424 // and failbit is set in __err.
425 // Else an iterator pointing to the matching keyword is found. If more than
426 // one keyword matches, an iterator to the first matching keyword is returned.
427 // If on exit __b == __e, eofbit is set in __err. If __case_senstive is false,
428 // __ct is used to force to lower case before comparing characters.
430 // Keywords: "a", "abb"
431 // If the input is "a", the first keyword matches and eofbit is set.
432 // If the input is "abc", no match is found and "ab" are consumed.
433 template <class _InputIterator, class _ForwardIterator, class _Ctype>
436 __scan_keyword(_InputIterator& __b, _InputIterator __e,
437 _ForwardIterator __kb, _ForwardIterator __ke,
438 const _Ctype& __ct, ios_base::iostate& __err,
439 bool __case_sensitive = true)
441 typedef typename iterator_traits<_InputIterator>::value_type _CharT;
442 size_t __nkw = static_cast<size_t>(_VSTD::distance(__kb, __ke));
443 const unsigned char __doesnt_match = '\0';
444 const unsigned char __might_match = '\1';
445 const unsigned char __does_match = '\2';
446 unsigned char __statbuf[100];
447 unsigned char* __status = __statbuf;
448 unique_ptr<unsigned char, void(*)(void*)> __stat_hold(0, free);
449 if (__nkw > sizeof(__statbuf))
451 __status = (unsigned char*)malloc(__nkw);
454 __stat_hold.reset(__status);
456 size_t __n_might_match = __nkw; // At this point, any keyword might match
457 size_t __n_does_match = 0; // but none of them definitely do
458 // Initialize all statuses to __might_match, except for "" keywords are __does_match
459 unsigned char* __st = __status;
460 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, ++__st)
463 *__st = __might_match;
466 *__st = __does_match;
471 // While there might be a match, test keywords against the next CharT
472 for (size_t __indx = 0; __b != __e && __n_might_match > 0; ++__indx)
474 // Peek at the next CharT but don't consume it
476 if (!__case_sensitive)
477 __c = __ct.toupper(__c);
478 bool __consume = false;
479 // For each keyword which might match, see if the __indx character is __c
480 // If a match if found, consume __c
481 // If a match is found, and that is the last character in the keyword,
482 // then that keyword matches.
483 // If the keyword doesn't match this character, then change the keyword
486 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, ++__st)
488 if (*__st == __might_match)
490 _CharT __kc = (*__ky)[__indx];
491 if (!__case_sensitive)
492 __kc = __ct.toupper(__kc);
496 if (__ky->size() == __indx+1)
498 *__st = __does_match;
505 *__st = __doesnt_match;
510 // consume if we matched a character
514 // If we consumed a character and there might be a matched keyword that
515 // was marked matched on a previous iteration, then such keywords
516 // which are now marked as not matching.
517 if (__n_might_match + __n_does_match > 1)
520 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, ++__st)
522 if (*__st == __does_match && __ky->size() != __indx+1)
524 *__st = __doesnt_match;
531 // We've exited the loop because we hit eof and/or we have no more "might matches".
533 __err |= ios_base::eofbit;
534 // Return the first matching result
535 for (__st = __status; __kb != __ke; ++__kb, ++__st)
536 if (*__st == __does_match)
539 __err |= ios_base::failbit;
543 struct __num_get_base
545 static const int __num_get_buf_sz = 40;
547 static int __get_base(ios_base&);
548 static const char __src[33];
551 void __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end,
552 ios_base::iostate& __err);
554 template <class _CharT>
556 : protected __num_get_base
558 static string __stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep);
559 static string __stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
560 _CharT& __thousands_sep);
561 static int __stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
562 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
563 unsigned* __g, unsigned*& __g_end, _CharT* __atoms);
564 static int __stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp,
565 char* __a, char*& __a_end,
566 _CharT __decimal_point, _CharT __thousands_sep,
567 const string& __grouping, unsigned* __g,
568 unsigned*& __g_end, unsigned& __dc, _CharT* __atoms);
571 template <class _CharT>
573 __num_get<_CharT>::__stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep)
575 locale __loc = __iob.getloc();
576 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 26, __atoms);
577 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
578 __thousands_sep = __np.thousands_sep();
579 return __np.grouping();
582 template <class _CharT>
584 __num_get<_CharT>::__stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
585 _CharT& __thousands_sep)
587 locale __loc = __iob.getloc();
588 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 32, __atoms);
589 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
590 __decimal_point = __np.decimal_point();
591 __thousands_sep = __np.thousands_sep();
592 return __np.grouping();
595 template <class _CharT>
597 __num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
598 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
599 unsigned* __g, unsigned*& __g_end, _CharT* __atoms)
601 if (__a_end == __a && (__ct == __atoms[24] || __ct == __atoms[25]))
603 *__a_end++ = __ct == __atoms[24] ? '+' : '-';
607 if (__grouping.size() != 0 && __ct == __thousands_sep)
609 if (__g_end-__g < __num_get_buf_sz)
616 ptrdiff_t __f = find(__atoms, __atoms + 26, __ct) - __atoms;
629 if (__a_end != __a && __a_end - __a <= 2 && __a_end[-1] == '0')
632 *__a_end++ = __src[__f];
637 if (__a_end-__a < __num_get_buf_sz - 1)
638 *__a_end++ = __src[__f];
643 template <class _CharT>
645 __num_get<_CharT>::__stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp, char* __a, char*& __a_end,
646 _CharT __decimal_point, _CharT __thousands_sep, const string& __grouping,
647 unsigned* __g, unsigned*& __g_end, unsigned& __dc, _CharT* __atoms)
649 if (__ct == __decimal_point)
655 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
659 if (__ct == __thousands_sep && __grouping.size() != 0)
663 if (__g_end-__g < __num_get_buf_sz)
670 ptrdiff_t __f = find(__atoms, __atoms + 32, __ct) - __atoms;
673 char __x = __src[__f];
674 if (__x == '-' || __x == '+')
676 if (__a_end == __a || (__a_end[-1] & 0xDF) == __exp)
683 if (__a_end-__a < __num_get_buf_sz - 1)
685 if (__x == 'x' || __x == 'X')
687 else if ((__x & 0xDF) == __exp)
690 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
699 _LIBCPP_EXTERN_TEMPLATE(struct __num_get<char>)
700 _LIBCPP_EXTERN_TEMPLATE(struct __num_get<wchar_t>)
702 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
703 class _LIBCPP_VISIBLE num_get
704 : public locale::facet,
705 private __num_get<_CharT>
708 typedef _CharT char_type;
709 typedef _InputIterator iter_type;
711 _LIBCPP_ALWAYS_INLINE
712 explicit num_get(size_t __refs = 0)
713 : locale::facet(__refs) {}
715 _LIBCPP_ALWAYS_INLINE
716 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
717 ios_base::iostate& __err, bool& __v) const
719 return do_get(__b, __e, __iob, __err, __v);
722 _LIBCPP_ALWAYS_INLINE
723 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
724 ios_base::iostate& __err, long& __v) const
726 return do_get(__b, __e, __iob, __err, __v);
729 _LIBCPP_ALWAYS_INLINE
730 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
731 ios_base::iostate& __err, long long& __v) const
733 return do_get(__b, __e, __iob, __err, __v);
736 _LIBCPP_ALWAYS_INLINE
737 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
738 ios_base::iostate& __err, unsigned short& __v) const
740 return do_get(__b, __e, __iob, __err, __v);
743 _LIBCPP_ALWAYS_INLINE
744 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
745 ios_base::iostate& __err, unsigned int& __v) const
747 return do_get(__b, __e, __iob, __err, __v);
750 _LIBCPP_ALWAYS_INLINE
751 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
752 ios_base::iostate& __err, unsigned long& __v) const
754 return do_get(__b, __e, __iob, __err, __v);
757 _LIBCPP_ALWAYS_INLINE
758 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
759 ios_base::iostate& __err, unsigned long long& __v) const
761 return do_get(__b, __e, __iob, __err, __v);
764 _LIBCPP_ALWAYS_INLINE
765 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
766 ios_base::iostate& __err, float& __v) const
768 return do_get(__b, __e, __iob, __err, __v);
771 _LIBCPP_ALWAYS_INLINE
772 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
773 ios_base::iostate& __err, double& __v) const
775 return do_get(__b, __e, __iob, __err, __v);
778 _LIBCPP_ALWAYS_INLINE
779 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
780 ios_base::iostate& __err, long double& __v) const
782 return do_get(__b, __e, __iob, __err, __v);
785 _LIBCPP_ALWAYS_INLINE
786 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
787 ios_base::iostate& __err, void*& __v) const
789 return do_get(__b, __e, __iob, __err, __v);
792 static locale::id id;
795 _LIBCPP_ALWAYS_INLINE
798 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
799 ios_base::iostate& __err, bool& __v) const;
800 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
801 ios_base::iostate& __err, long& __v) const;
802 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
803 ios_base::iostate& __err, long long& __v) const;
804 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
805 ios_base::iostate& __err, unsigned short& __v) const;
806 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
807 ios_base::iostate& __err, unsigned int& __v) const;
808 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
809 ios_base::iostate& __err, unsigned long& __v) const;
810 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
811 ios_base::iostate& __err, unsigned long long& __v) const;
812 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
813 ios_base::iostate& __err, float& __v) const;
814 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
815 ios_base::iostate& __err, double& __v) const;
816 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
817 ios_base::iostate& __err, long double& __v) const;
818 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
819 ios_base::iostate& __err, void*& __v) const;
822 template <class _CharT, class _InputIterator>
824 num_get<_CharT, _InputIterator>::id;
828 __num_get_signed_integral(const char* __a, const char* __a_end,
829 ios_base::iostate& __err, int __base)
833 int __save_errno = errno;
836 long long __ll = strtoll_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
837 int __current_errno = errno;
838 if (__current_errno == 0)
839 errno = __save_errno;
842 __err = ios_base::failbit;
845 else if (__current_errno == ERANGE ||
846 __ll < numeric_limits<_Tp>::min() ||
847 numeric_limits<_Tp>::max() < __ll)
849 __err = ios_base::failbit;
851 return numeric_limits<_Tp>::max();
853 return numeric_limits<_Tp>::min();
855 return static_cast<_Tp>(__ll);
857 __err = ios_base::failbit;
863 __num_get_unsigned_integral(const char* __a, const char* __a_end,
864 ios_base::iostate& __err, int __base)
870 __err = ios_base::failbit;
873 int __save_errno = errno;
876 unsigned long long __ll = strtoull_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
877 int __current_errno = errno;
878 if (__current_errno == 0)
879 errno = __save_errno;
882 __err = ios_base::failbit;
885 else if (__current_errno == ERANGE ||
886 numeric_limits<_Tp>::max() < __ll)
888 __err = ios_base::failbit;
889 return numeric_limits<_Tp>::max();
891 return static_cast<_Tp>(__ll);
893 __err = ios_base::failbit;
899 __num_get_float(const char* __a, const char* __a_end, ios_base::iostate& __err)
904 long double __ld = strtold_l(__a, &__p2, _LIBCPP_GET_C_LOCALE);
907 __err = ios_base::failbit;
910 return static_cast<_Tp>(__ld);
912 __err = ios_base::failbit;
916 template <class _CharT, class _InputIterator>
918 num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
920 ios_base::iostate& __err,
923 if ((__iob.flags() & ios_base::boolalpha) == 0)
926 __b = do_get(__b, __e, __iob, __err, __lv);
937 __err = ios_base::failbit;
942 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__iob.getloc());
943 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__iob.getloc());
944 typedef typename numpunct<_CharT>::string_type string_type;
945 const string_type __names[2] = {__np.truename(), __np.falsename()};
946 const string_type* __i = __scan_keyword(__b, __e, __names, __names+2,
948 __v = __i == __names;
952 template <class _CharT, class _InputIterator>
954 num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
956 ios_base::iostate& __err,
960 int __base = this->__get_base(__iob);
962 char_type __atoms[26];
963 char_type __thousands_sep;
964 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
965 char __a[__num_get_base::__num_get_buf_sz] = {0};
967 unsigned __g[__num_get_base::__num_get_buf_sz];
968 unsigned* __g_end = __g;
970 for (; __b != __e; ++__b)
971 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
972 __thousands_sep, __grouping, __g, __g_end,
975 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
978 __v = __num_get_signed_integral<long>(__a, __a_end, __err, __base);
979 // Digit grouping checked
980 __check_grouping(__grouping, __g, __g_end, __err);
983 __err |= ios_base::eofbit;
987 template <class _CharT, class _InputIterator>
989 num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
991 ios_base::iostate& __err,
992 long long& __v) const
995 int __base = this->__get_base(__iob);
997 char_type __atoms[26];
998 char_type __thousands_sep;
999 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
1000 char __a[__num_get_base::__num_get_buf_sz] = {0};
1001 char* __a_end = __a;
1002 unsigned __g[__num_get_base::__num_get_buf_sz];
1003 unsigned* __g_end = __g;
1005 for (; __b != __e; ++__b)
1006 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1007 __thousands_sep, __grouping, __g, __g_end,
1010 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1013 __v = __num_get_signed_integral<long long>(__a, __a_end, __err, __base);
1014 // Digit grouping checked
1015 __check_grouping(__grouping, __g, __g_end, __err);
1018 __err |= ios_base::eofbit;
1022 template <class _CharT, class _InputIterator>
1024 num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1026 ios_base::iostate& __err,
1027 unsigned short& __v) const
1030 int __base = this->__get_base(__iob);
1032 char_type __atoms[26];
1033 char_type __thousands_sep;
1034 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
1035 char __a[__num_get_base::__num_get_buf_sz] = {0};
1036 char* __a_end = __a;
1037 unsigned __g[__num_get_base::__num_get_buf_sz];
1038 unsigned* __g_end = __g;
1040 for (; __b != __e; ++__b)
1041 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1042 __thousands_sep, __grouping, __g, __g_end,
1045 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1048 __v = __num_get_unsigned_integral<unsigned short>(__a, __a_end, __err, __base);
1049 // Digit grouping checked
1050 __check_grouping(__grouping, __g, __g_end, __err);
1053 __err |= ios_base::eofbit;
1057 template <class _CharT, class _InputIterator>
1059 num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1061 ios_base::iostate& __err,
1062 unsigned int& __v) const
1065 int __base = this->__get_base(__iob);
1067 char_type __atoms[26];
1068 char_type __thousands_sep;
1069 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
1070 char __a[__num_get_base::__num_get_buf_sz] = {0};
1071 char* __a_end = __a;
1072 unsigned __g[__num_get_base::__num_get_buf_sz];
1073 unsigned* __g_end = __g;
1075 for (; __b != __e; ++__b)
1076 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1077 __thousands_sep, __grouping, __g, __g_end,
1080 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1083 __v = __num_get_unsigned_integral<unsigned int>(__a, __a_end, __err, __base);
1084 // Digit grouping checked
1085 __check_grouping(__grouping, __g, __g_end, __err);
1088 __err |= ios_base::eofbit;
1092 template <class _CharT, class _InputIterator>
1094 num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1096 ios_base::iostate& __err,
1097 unsigned long& __v) const
1100 int __base = this->__get_base(__iob);
1102 char_type __atoms[26];
1103 char_type __thousands_sep;
1104 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
1105 char __a[__num_get_base::__num_get_buf_sz] = {0};
1106 char* __a_end = __a;
1107 unsigned __g[__num_get_base::__num_get_buf_sz];
1108 unsigned* __g_end = __g;
1110 for (; __b != __e; ++__b)
1111 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1112 __thousands_sep, __grouping, __g, __g_end,
1115 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1118 __v = __num_get_unsigned_integral<unsigned long>(__a, __a_end, __err, __base);
1119 // Digit grouping checked
1120 __check_grouping(__grouping, __g, __g_end, __err);
1123 __err |= ios_base::eofbit;
1127 template <class _CharT, class _InputIterator>
1129 num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1131 ios_base::iostate& __err,
1132 unsigned long long& __v) const
1135 int __base = this->__get_base(__iob);
1137 char_type __atoms[26];
1138 char_type __thousands_sep;
1139 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
1140 char __a[__num_get_base::__num_get_buf_sz] = {0};
1141 char* __a_end = __a;
1142 unsigned __g[__num_get_base::__num_get_buf_sz];
1143 unsigned* __g_end = __g;
1145 for (; __b != __e; ++__b)
1146 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1147 __thousands_sep, __grouping, __g, __g_end,
1150 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1153 __v = __num_get_unsigned_integral<unsigned long long>(__a, __a_end, __err, __base);
1154 // Digit grouping checked
1155 __check_grouping(__grouping, __g, __g_end, __err);
1158 __err |= ios_base::eofbit;
1162 template <class _CharT, class _InputIterator>
1164 num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1166 ios_base::iostate& __err,
1169 // Stage 1, nothing to do
1171 char_type __atoms[32];
1172 char_type __decimal_point;
1173 char_type __thousands_sep;
1174 string __grouping = this->__stage2_float_prep(__iob, __atoms,
1177 char __a[__num_get_base::__num_get_buf_sz] = {0};
1178 char* __a_end = __a;
1179 unsigned __g[__num_get_base::__num_get_buf_sz];
1180 unsigned* __g_end = __g;
1182 bool __in_units = true;
1184 for (; __b != __e; ++__b)
1185 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1186 __decimal_point, __thousands_sep,
1187 __grouping, __g, __g_end,
1190 if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1193 __v = __num_get_float<float>(__a, __a_end, __err);
1194 // Digit grouping checked
1195 __check_grouping(__grouping, __g, __g_end, __err);
1198 __err |= ios_base::eofbit;
1202 template <class _CharT, class _InputIterator>
1204 num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1206 ios_base::iostate& __err,
1209 // Stage 1, nothing to do
1211 char_type __atoms[32];
1212 char_type __decimal_point;
1213 char_type __thousands_sep;
1214 string __grouping = this->__stage2_float_prep(__iob, __atoms,
1217 char __a[__num_get_base::__num_get_buf_sz] = {0};
1218 char* __a_end = __a;
1219 unsigned __g[__num_get_base::__num_get_buf_sz];
1220 unsigned* __g_end = __g;
1222 bool __in_units = true;
1224 for (; __b != __e; ++__b)
1225 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1226 __decimal_point, __thousands_sep,
1227 __grouping, __g, __g_end,
1230 if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1233 __v = __num_get_float<double>(__a, __a_end, __err);
1234 // Digit grouping checked
1235 __check_grouping(__grouping, __g, __g_end, __err);
1238 __err |= ios_base::eofbit;
1242 template <class _CharT, class _InputIterator>
1244 num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1246 ios_base::iostate& __err,
1247 long double& __v) const
1249 // Stage 1, nothing to do
1251 char_type __atoms[32];
1252 char_type __decimal_point;
1253 char_type __thousands_sep;
1254 string __grouping = this->__stage2_float_prep(__iob, __atoms,
1257 char __a[__num_get_base::__num_get_buf_sz] = {0};
1258 char* __a_end = __a;
1259 unsigned __g[__num_get_base::__num_get_buf_sz];
1260 unsigned* __g_end = __g;
1262 bool __in_units = true;
1264 for (; __b != __e; ++__b)
1265 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1266 __decimal_point, __thousands_sep,
1267 __grouping, __g, __g_end,
1270 if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1273 __v = __num_get_float<long double>(__a, __a_end, __err);
1274 // Digit grouping checked
1275 __check_grouping(__grouping, __g, __g_end, __err);
1278 __err |= ios_base::eofbit;
1282 template <class _CharT, class _InputIterator>
1284 num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1286 ios_base::iostate& __err,
1292 char_type __atoms[26];
1293 char_type __thousands_sep = 0;
1295 use_facet<ctype<_CharT> >(__iob.getloc()).widen(__num_get_base::__src,
1296 __num_get_base::__src + 26, __atoms);
1297 char __a[__num_get_base::__num_get_buf_sz] = {0};
1298 char* __a_end = __a;
1299 unsigned __g[__num_get_base::__num_get_buf_sz];
1300 unsigned* __g_end = __g;
1302 for (; __b != __e; ++__b)
1303 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1304 __thousands_sep, __grouping,
1305 __g, __g_end, __atoms))
1308 __a[sizeof(__a)-1] = 0;
1309 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1310 if (sscanf_l(__a, _LIBCPP_GET_C_LOCALE, "%p", &__v) != 1)
1312 if (__sscanf_l(__a, __cloc(), "%p", &__v) != 1)
1314 __err = ios_base::failbit;
1317 __err |= ios_base::eofbit;
1321 _LIBCPP_EXTERN_TEMPLATE(class num_get<char>)
1322 _LIBCPP_EXTERN_TEMPLATE(class num_get<wchar_t>)
1324 struct __num_put_base
1327 static void __format_int(char* __fmt, const char* __len, bool __signd,
1328 ios_base::fmtflags __flags);
1329 static bool __format_float(char* __fmt, const char* __len,
1330 ios_base::fmtflags __flags);
1331 static char* __identify_padding(char* __nb, char* __ne,
1332 const ios_base& __iob);
1335 template <class _CharT>
1337 : protected __num_put_base
1339 static void __widen_and_group_int(char* __nb, char* __np, char* __ne,
1340 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1341 const locale& __loc);
1342 static void __widen_and_group_float(char* __nb, char* __np, char* __ne,
1343 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1344 const locale& __loc);
1347 template <class _CharT>
1349 __num_put<_CharT>::__widen_and_group_int(char* __nb, char* __np, char* __ne,
1350 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1351 const locale& __loc)
1353 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1354 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1355 string __grouping = __npt.grouping();
1356 if (__grouping.empty())
1358 __ct.widen(__nb, __ne, __ob);
1359 __oe = __ob + (__ne - __nb);
1365 if (*__nf == '-' || *__nf == '+')
1366 *__oe++ = __ct.widen(*__nf++);
1367 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1370 *__oe++ = __ct.widen(*__nf++);
1371 *__oe++ = __ct.widen(*__nf++);
1373 reverse(__nf, __ne);
1374 _CharT __thousands_sep = __npt.thousands_sep();
1377 for (char* __p = __nf; __p < __ne; ++__p)
1379 if (static_cast<unsigned>(__grouping[__dg]) > 0 &&
1380 __dc == static_cast<unsigned>(__grouping[__dg]))
1382 *__oe++ = __thousands_sep;
1384 if (__dg < __grouping.size()-1)
1387 *__oe++ = __ct.widen(*__p);
1390 reverse(__ob + (__nf - __nb), __oe);
1395 __op = __ob + (__np - __nb);
1398 template <class _CharT>
1400 __num_put<_CharT>::__widen_and_group_float(char* __nb, char* __np, char* __ne,
1401 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1402 const locale& __loc)
1404 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1405 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1406 string __grouping = __npt.grouping();
1409 if (*__nf == '-' || *__nf == '+')
1410 *__oe++ = __ct.widen(*__nf++);
1412 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1415 *__oe++ = __ct.widen(*__nf++);
1416 *__oe++ = __ct.widen(*__nf++);
1417 for (__ns = __nf; __ns < __ne; ++__ns)
1418 if (!isxdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
1423 for (__ns = __nf; __ns < __ne; ++__ns)
1424 if (!isdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
1427 if (__grouping.empty())
1429 __ct.widen(__nf, __ns, __oe);
1430 __oe += __ns - __nf;
1434 reverse(__nf, __ns);
1435 _CharT __thousands_sep = __npt.thousands_sep();
1438 for (char* __p = __nf; __p < __ns; ++__p)
1440 if (__grouping[__dg] > 0 && __dc == static_cast<unsigned>(__grouping[__dg]))
1442 *__oe++ = __thousands_sep;
1444 if (__dg < __grouping.size()-1)
1447 *__oe++ = __ct.widen(*__p);
1450 reverse(__ob + (__nf - __nb), __oe);
1452 for (__nf = __ns; __nf < __ne; ++__nf)
1456 *__oe++ = __npt.decimal_point();
1461 *__oe++ = __ct.widen(*__nf);
1463 __ct.widen(__nf, __ne, __oe);
1464 __oe += __ne - __nf;
1468 __op = __ob + (__np - __nb);
1471 _LIBCPP_EXTERN_TEMPLATE(struct __num_put<char>)
1472 _LIBCPP_EXTERN_TEMPLATE(struct __num_put<wchar_t>)
1474 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
1475 class _LIBCPP_VISIBLE num_put
1476 : public locale::facet,
1477 private __num_put<_CharT>
1480 typedef _CharT char_type;
1481 typedef _OutputIterator iter_type;
1483 _LIBCPP_ALWAYS_INLINE
1484 explicit num_put(size_t __refs = 0)
1485 : locale::facet(__refs) {}
1487 _LIBCPP_ALWAYS_INLINE
1488 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1491 return do_put(__s, __iob, __fl, __v);
1494 _LIBCPP_ALWAYS_INLINE
1495 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1498 return do_put(__s, __iob, __fl, __v);
1501 _LIBCPP_ALWAYS_INLINE
1502 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1503 long long __v) const
1505 return do_put(__s, __iob, __fl, __v);
1508 _LIBCPP_ALWAYS_INLINE
1509 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1510 unsigned long __v) const
1512 return do_put(__s, __iob, __fl, __v);
1515 _LIBCPP_ALWAYS_INLINE
1516 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1517 unsigned long long __v) const
1519 return do_put(__s, __iob, __fl, __v);
1522 _LIBCPP_ALWAYS_INLINE
1523 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1526 return do_put(__s, __iob, __fl, __v);
1529 _LIBCPP_ALWAYS_INLINE
1530 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1531 long double __v) const
1533 return do_put(__s, __iob, __fl, __v);
1536 _LIBCPP_ALWAYS_INLINE
1537 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1538 const void* __v) const
1540 return do_put(__s, __iob, __fl, __v);
1543 static locale::id id;
1546 _LIBCPP_ALWAYS_INLINE
1549 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1551 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1553 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1554 long long __v) const;
1555 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1556 unsigned long) const;
1557 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1558 unsigned long long) const;
1559 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1561 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1562 long double __v) const;
1563 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1564 const void* __v) const;
1567 template <class _CharT, class _OutputIterator>
1569 num_put<_CharT, _OutputIterator>::id;
1571 template <class _CharT, class _OutputIterator>
1574 __pad_and_output(_OutputIterator __s,
1575 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1576 ios_base& __iob, _CharT __fl)
1578 streamsize __sz = __oe - __ob;
1579 streamsize __ns = __iob.width();
1584 for (;__ob < __op; ++__ob, ++__s)
1586 for (; __ns; --__ns, ++__s)
1588 for (; __ob < __oe; ++__ob, ++__s)
1594 #if !defined(__APPLE__) || \
1595 (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED > __MAC_10_8) || \
1596 (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_6_0)
1598 template <class _CharT, class _Traits>
1600 ostreambuf_iterator<_CharT, _Traits>
1601 __pad_and_output(ostreambuf_iterator<_CharT, _Traits> __s,
1602 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1603 ios_base& __iob, _CharT __fl)
1605 if (__s.__sbuf_ == nullptr)
1607 streamsize __sz = __oe - __ob;
1608 streamsize __ns = __iob.width();
1613 streamsize __np = __op - __ob;
1616 if (__s.__sbuf_->sputn(__ob, __np) != __np)
1618 __s.__sbuf_ = nullptr;
1624 basic_string<_CharT, _Traits> __sp(__ns, __fl);
1625 if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns)
1627 __s.__sbuf_ = nullptr;
1634 if (__s.__sbuf_->sputn(__op, __np) != __np)
1636 __s.__sbuf_ = nullptr;
1646 template <class _CharT, class _OutputIterator>
1648 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1649 char_type __fl, bool __v) const
1651 if ((__iob.flags() & ios_base::boolalpha) == 0)
1652 return do_put(__s, __iob, __fl, (unsigned long)__v);
1653 const numpunct<char_type>& __np = use_facet<numpunct<char_type> >(__iob.getloc());
1654 typedef typename numpunct<char_type>::string_type string_type;
1655 string_type __nm = __v ? __np.truename() : __np.falsename();
1656 for (typename string_type::iterator __i = __nm.begin(); __i != __nm.end(); ++__i, ++__s)
1661 template <class _CharT, class _OutputIterator>
1663 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1664 char_type __fl, long __v) const
1666 // Stage 1 - Get number in narrow char
1667 char __fmt[6] = {'%', 0};
1668 const char* __len = "l";
1669 this->__format_int(__fmt+1, __len, true, __iob.flags());
1670 const unsigned __nbuf = (numeric_limits<long>::digits / 3)
1671 + ((numeric_limits<long>::digits % 3) != 0)
1674 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1675 int __nc = sprintf_l(__nar, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1677 int __nc = __sprintf_l(__nar, __cloc(), __fmt, __v);
1679 char* __ne = __nar + __nc;
1680 char* __np = this->__identify_padding(__nar, __ne, __iob);
1681 // Stage 2 - Widen __nar while adding thousands separators
1682 char_type __o[2*(__nbuf-1) - 1];
1683 char_type* __op; // pad here
1684 char_type* __oe; // end of output
1685 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1686 // [__o, __oe) contains thousands_sep'd wide number
1688 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1691 template <class _CharT, class _OutputIterator>
1693 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1694 char_type __fl, long long __v) const
1696 // Stage 1 - Get number in narrow char
1697 char __fmt[8] = {'%', 0};
1698 const char* __len = "ll";
1699 this->__format_int(__fmt+1, __len, true, __iob.flags());
1700 const unsigned __nbuf = (numeric_limits<long long>::digits / 3)
1701 + ((numeric_limits<long long>::digits % 3) != 0)
1704 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1705 int __nc = sprintf_l(__nar, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1707 int __nc = __sprintf_l(__nar, __cloc(), __fmt, __v);
1709 char* __ne = __nar + __nc;
1710 char* __np = this->__identify_padding(__nar, __ne, __iob);
1711 // Stage 2 - Widen __nar while adding thousands separators
1712 char_type __o[2*(__nbuf-1) - 1];
1713 char_type* __op; // pad here
1714 char_type* __oe; // end of output
1715 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1716 // [__o, __oe) contains thousands_sep'd wide number
1718 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1721 template <class _CharT, class _OutputIterator>
1723 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1724 char_type __fl, unsigned long __v) const
1726 // Stage 1 - Get number in narrow char
1727 char __fmt[6] = {'%', 0};
1728 const char* __len = "l";
1729 this->__format_int(__fmt+1, __len, false, __iob.flags());
1730 const unsigned __nbuf = (numeric_limits<unsigned long>::digits / 3)
1731 + ((numeric_limits<unsigned long>::digits % 3) != 0)
1734 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1735 int __nc = sprintf_l(__nar, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1737 int __nc = __sprintf_l(__nar, __cloc(), __fmt, __v);
1739 char* __ne = __nar + __nc;
1740 char* __np = this->__identify_padding(__nar, __ne, __iob);
1741 // Stage 2 - Widen __nar while adding thousands separators
1742 char_type __o[2*(__nbuf-1) - 1];
1743 char_type* __op; // pad here
1744 char_type* __oe; // end of output
1745 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1746 // [__o, __oe) contains thousands_sep'd wide number
1748 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1751 template <class _CharT, class _OutputIterator>
1753 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1754 char_type __fl, unsigned long long __v) const
1756 // Stage 1 - Get number in narrow char
1757 char __fmt[8] = {'%', 0};
1758 const char* __len = "ll";
1759 this->__format_int(__fmt+1, __len, false, __iob.flags());
1760 const unsigned __nbuf = (numeric_limits<unsigned long long>::digits / 3)
1761 + ((numeric_limits<unsigned long long>::digits % 3) != 0)
1764 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1765 int __nc = sprintf_l(__nar, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1767 int __nc = __sprintf_l(__nar, __cloc(), __fmt, __v);
1769 char* __ne = __nar + __nc;
1770 char* __np = this->__identify_padding(__nar, __ne, __iob);
1771 // Stage 2 - Widen __nar while adding thousands separators
1772 char_type __o[2*(__nbuf-1) - 1];
1773 char_type* __op; // pad here
1774 char_type* __oe; // end of output
1775 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1776 // [__o, __oe) contains thousands_sep'd wide number
1778 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1781 template <class _CharT, class _OutputIterator>
1783 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1784 char_type __fl, double __v) const
1786 // Stage 1 - Get number in narrow char
1787 char __fmt[8] = {'%', 0};
1788 const char* __len = "";
1789 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1790 const unsigned __nbuf = 30;
1794 if (__specify_precision)
1795 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1796 __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
1797 (int)__iob.precision(), __v);
1799 __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt,
1800 (int)__iob.precision(), __v);
1803 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1804 __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1806 __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt, __v);
1808 unique_ptr<char, void(*)(void*)> __nbh(0, free);
1809 if (__nc > static_cast<int>(__nbuf-1))
1811 if (__specify_precision)
1812 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1813 __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
1815 __nc = __asprintf_l(&__nb, __cloc(), __fmt,
1816 (int)__iob.precision(), __v);
1819 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1820 __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1822 __nc = __asprintf_l(&__nb, __cloc(), __fmt, (int)__iob.precision(), __v);
1825 __throw_bad_alloc();
1828 char* __ne = __nb + __nc;
1829 char* __np = this->__identify_padding(__nb, __ne, __iob);
1830 // Stage 2 - Widen __nar while adding thousands separators
1831 char_type __o[2*(__nbuf-1) - 1];
1832 char_type* __ob = __o;
1833 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1836 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
1838 __throw_bad_alloc();
1841 char_type* __op; // pad here
1842 char_type* __oe; // end of output
1843 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1844 // [__o, __oe) contains thousands_sep'd wide number
1846 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1850 template <class _CharT, class _OutputIterator>
1852 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1853 char_type __fl, long double __v) const
1855 // Stage 1 - Get number in narrow char
1856 char __fmt[8] = {'%', 0};
1857 const char* __len = "L";
1858 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1859 const unsigned __nbuf = 30;
1863 if (__specify_precision)
1864 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1865 __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
1866 (int)__iob.precision(), __v);
1868 __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt,
1869 (int)__iob.precision(), __v);
1872 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1873 __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1875 __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt, __v);
1877 unique_ptr<char, void(*)(void*)> __nbh(0, free);
1878 if (__nc > static_cast<int>(__nbuf-1))
1880 if (__specify_precision)
1881 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1882 __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
1884 __nc = __asprintf_l(&__nb, __cloc(), __fmt,
1885 (int)__iob.precision(), __v);
1888 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1889 __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1891 __nc = __asprintf_l(&__nb, __cloc(), __fmt, __v);
1894 __throw_bad_alloc();
1897 char* __ne = __nb + __nc;
1898 char* __np = this->__identify_padding(__nb, __ne, __iob);
1899 // Stage 2 - Widen __nar while adding thousands separators
1900 char_type __o[2*(__nbuf-1) - 1];
1901 char_type* __ob = __o;
1902 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1905 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
1907 __throw_bad_alloc();
1910 char_type* __op; // pad here
1911 char_type* __oe; // end of output
1912 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1913 // [__o, __oe) contains thousands_sep'd wide number
1915 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1919 template <class _CharT, class _OutputIterator>
1921 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1922 char_type __fl, const void* __v) const
1924 // Stage 1 - Get pointer in narrow char
1925 char __fmt[6] = "%p";
1926 const unsigned __nbuf = 20;
1928 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1929 int __nc = sprintf_l(__nar, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1931 int __nc = __sprintf_l(__nar, __cloc(), __fmt, __v);
1933 char* __ne = __nar + __nc;
1934 char* __np = this->__identify_padding(__nar, __ne, __iob);
1935 // Stage 2 - Widen __nar
1936 char_type __o[2*(__nbuf-1) - 1];
1937 char_type* __op; // pad here
1938 char_type* __oe; // end of output
1939 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
1940 __ct.widen(__nar, __ne, __o);
1941 __oe = __o + (__ne - __nar);
1945 __op = __o + (__np - __nar);
1946 // [__o, __oe) contains wide number
1948 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1951 _LIBCPP_EXTERN_TEMPLATE(class num_put<char>)
1952 _LIBCPP_EXTERN_TEMPLATE(class num_put<wchar_t>)
1954 template <class _CharT, class _InputIterator>
1957 __get_up_to_n_digits(_InputIterator& __b, _InputIterator __e,
1958 ios_base::iostate& __err, const ctype<_CharT>& __ct, int __n)
1960 // Precondition: __n >= 1
1963 __err |= ios_base::eofbit | ios_base::failbit;
1968 if (!__ct.is(ctype_base::digit, __c))
1970 __err |= ios_base::failbit;
1973 int __r = __ct.narrow(__c, 0) - '0';
1974 for (++__b, --__n; __b != __e && __n > 0; ++__b, --__n)
1978 if (!__ct.is(ctype_base::digit, __c))
1980 __r = __r * 10 + __ct.narrow(__c, 0) - '0';
1983 __err |= ios_base::eofbit;
1987 class _LIBCPP_VISIBLE time_base
1990 enum dateorder {no_order, dmy, mdy, ymd, ydm};
1993 template <class _CharT>
1994 class __time_get_c_storage // purposefully not decorated
1997 typedef basic_string<_CharT> string_type;
1999 virtual const string_type* __weeks() const;
2000 virtual const string_type* __months() const;
2001 virtual const string_type* __am_pm() const;
2002 virtual const string_type& __c() const;
2003 virtual const string_type& __r() const;
2004 virtual const string_type& __x() const;
2005 virtual const string_type& __X() const;
2008 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2009 class _LIBCPP_VISIBLE time_get
2010 : public locale::facet,
2012 private __time_get_c_storage<_CharT>
2015 typedef _CharT char_type;
2016 typedef _InputIterator iter_type;
2017 typedef time_base::dateorder dateorder;
2018 typedef basic_string<char_type> string_type;
2020 _LIBCPP_ALWAYS_INLINE
2021 explicit time_get(size_t __refs = 0)
2022 : locale::facet(__refs) {}
2024 _LIBCPP_ALWAYS_INLINE
2025 dateorder date_order() const
2027 return this->do_date_order();
2030 _LIBCPP_ALWAYS_INLINE
2031 iter_type get_time(iter_type __b, iter_type __e, ios_base& __iob,
2032 ios_base::iostate& __err, tm* __tm) const
2034 return do_get_time(__b, __e, __iob, __err, __tm);
2037 _LIBCPP_ALWAYS_INLINE
2038 iter_type get_date(iter_type __b, iter_type __e, ios_base& __iob,
2039 ios_base::iostate& __err, tm* __tm) const
2041 return do_get_date(__b, __e, __iob, __err, __tm);
2044 _LIBCPP_ALWAYS_INLINE
2045 iter_type get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
2046 ios_base::iostate& __err, tm* __tm) const
2048 return do_get_weekday(__b, __e, __iob, __err, __tm);
2051 _LIBCPP_ALWAYS_INLINE
2052 iter_type get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
2053 ios_base::iostate& __err, tm* __tm) const
2055 return do_get_monthname(__b, __e, __iob, __err, __tm);
2058 _LIBCPP_ALWAYS_INLINE
2059 iter_type get_year(iter_type __b, iter_type __e, ios_base& __iob,
2060 ios_base::iostate& __err, tm* __tm) const
2062 return do_get_year(__b, __e, __iob, __err, __tm);
2065 _LIBCPP_ALWAYS_INLINE
2066 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
2067 ios_base::iostate& __err, tm *__tm,
2068 char __fmt, char __mod = 0) const
2070 return do_get(__b, __e, __iob, __err, __tm, __fmt, __mod);
2073 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
2074 ios_base::iostate& __err, tm* __tm,
2075 const char_type* __fmtb, const char_type* __fmte) const;
2077 static locale::id id;
2080 _LIBCPP_ALWAYS_INLINE
2083 virtual dateorder do_date_order() const;
2084 virtual iter_type do_get_time(iter_type __b, iter_type __e, ios_base& __iob,
2085 ios_base::iostate& __err, tm* __tm) const;
2086 virtual iter_type do_get_date(iter_type __b, iter_type __e, ios_base& __iob,
2087 ios_base::iostate& __err, tm* __tm) const;
2088 virtual iter_type do_get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
2089 ios_base::iostate& __err, tm* __tm) const;
2090 virtual iter_type do_get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
2091 ios_base::iostate& __err, tm* __tm) const;
2092 virtual iter_type do_get_year(iter_type __b, iter_type __e, ios_base& __iob,
2093 ios_base::iostate& __err, tm* __tm) const;
2094 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
2095 ios_base::iostate& __err, tm* __tm,
2096 char __fmt, char __mod) const;
2098 void __get_white_space(iter_type& __b, iter_type __e,
2099 ios_base::iostate& __err, const ctype<char_type>& __ct) const;
2100 void __get_percent(iter_type& __b, iter_type __e, ios_base::iostate& __err,
2101 const ctype<char_type>& __ct) const;
2103 void __get_weekdayname(int& __m,
2104 iter_type& __b, iter_type __e,
2105 ios_base::iostate& __err,
2106 const ctype<char_type>& __ct) const;
2107 void __get_monthname(int& __m,
2108 iter_type& __b, iter_type __e,
2109 ios_base::iostate& __err,
2110 const ctype<char_type>& __ct) const;
2111 void __get_day(int& __d,
2112 iter_type& __b, iter_type __e,
2113 ios_base::iostate& __err,
2114 const ctype<char_type>& __ct) const;
2115 void __get_month(int& __m,
2116 iter_type& __b, iter_type __e,
2117 ios_base::iostate& __err,
2118 const ctype<char_type>& __ct) const;
2119 void __get_year(int& __y,
2120 iter_type& __b, iter_type __e,
2121 ios_base::iostate& __err,
2122 const ctype<char_type>& __ct) const;
2123 void __get_year4(int& __y,
2124 iter_type& __b, iter_type __e,
2125 ios_base::iostate& __err,
2126 const ctype<char_type>& __ct) const;
2127 void __get_hour(int& __d,
2128 iter_type& __b, iter_type __e,
2129 ios_base::iostate& __err,
2130 const ctype<char_type>& __ct) const;
2131 void __get_12_hour(int& __h,
2132 iter_type& __b, iter_type __e,
2133 ios_base::iostate& __err,
2134 const ctype<char_type>& __ct) const;
2135 void __get_am_pm(int& __h,
2136 iter_type& __b, iter_type __e,
2137 ios_base::iostate& __err,
2138 const ctype<char_type>& __ct) const;
2139 void __get_minute(int& __m,
2140 iter_type& __b, iter_type __e,
2141 ios_base::iostate& __err,
2142 const ctype<char_type>& __ct) const;
2143 void __get_second(int& __s,
2144 iter_type& __b, iter_type __e,
2145 ios_base::iostate& __err,
2146 const ctype<char_type>& __ct) const;
2147 void __get_weekday(int& __w,
2148 iter_type& __b, iter_type __e,
2149 ios_base::iostate& __err,
2150 const ctype<char_type>& __ct) const;
2151 void __get_day_year_num(int& __w,
2152 iter_type& __b, iter_type __e,
2153 ios_base::iostate& __err,
2154 const ctype<char_type>& __ct) const;
2157 template <class _CharT, class _InputIterator>
2159 time_get<_CharT, _InputIterator>::id;
2161 // time_get primatives
2163 template <class _CharT, class _InputIterator>
2165 time_get<_CharT, _InputIterator>::__get_weekdayname(int& __w,
2166 iter_type& __b, iter_type __e,
2167 ios_base::iostate& __err,
2168 const ctype<char_type>& __ct) const
2170 // Note: ignoring case comes from the POSIX strptime spec
2171 const string_type* __wk = this->__weeks();
2172 ptrdiff_t __i = __scan_keyword(__b, __e, __wk, __wk+14, __ct, __err, false) - __wk;
2177 template <class _CharT, class _InputIterator>
2179 time_get<_CharT, _InputIterator>::__get_monthname(int& __m,
2180 iter_type& __b, iter_type __e,
2181 ios_base::iostate& __err,
2182 const ctype<char_type>& __ct) const
2184 // Note: ignoring case comes from the POSIX strptime spec
2185 const string_type* __month = this->__months();
2186 ptrdiff_t __i = __scan_keyword(__b, __e, __month, __month+24, __ct, __err, false) - __month;
2191 template <class _CharT, class _InputIterator>
2193 time_get<_CharT, _InputIterator>::__get_day(int& __d,
2194 iter_type& __b, iter_type __e,
2195 ios_base::iostate& __err,
2196 const ctype<char_type>& __ct) const
2198 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2199 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 31)
2202 __err |= ios_base::failbit;
2205 template <class _CharT, class _InputIterator>
2207 time_get<_CharT, _InputIterator>::__get_month(int& __m,
2208 iter_type& __b, iter_type __e,
2209 ios_base::iostate& __err,
2210 const ctype<char_type>& __ct) const
2212 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2) - 1;
2213 if (!(__err & ios_base::failbit) && __t <= 11)
2216 __err |= ios_base::failbit;
2219 template <class _CharT, class _InputIterator>
2221 time_get<_CharT, _InputIterator>::__get_year(int& __y,
2222 iter_type& __b, iter_type __e,
2223 ios_base::iostate& __err,
2224 const ctype<char_type>& __ct) const
2226 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
2227 if (!(__err & ios_base::failbit))
2231 else if (69 <= __t && __t <= 99)
2237 template <class _CharT, class _InputIterator>
2239 time_get<_CharT, _InputIterator>::__get_year4(int& __y,
2240 iter_type& __b, iter_type __e,
2241 ios_base::iostate& __err,
2242 const ctype<char_type>& __ct) const
2244 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
2245 if (!(__err & ios_base::failbit))
2249 template <class _CharT, class _InputIterator>
2251 time_get<_CharT, _InputIterator>::__get_hour(int& __h,
2252 iter_type& __b, iter_type __e,
2253 ios_base::iostate& __err,
2254 const ctype<char_type>& __ct) const
2256 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2257 if (!(__err & ios_base::failbit) && __t <= 23)
2260 __err |= ios_base::failbit;
2263 template <class _CharT, class _InputIterator>
2265 time_get<_CharT, _InputIterator>::__get_12_hour(int& __h,
2266 iter_type& __b, iter_type __e,
2267 ios_base::iostate& __err,
2268 const ctype<char_type>& __ct) const
2270 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2271 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 12)
2274 __err |= ios_base::failbit;
2277 template <class _CharT, class _InputIterator>
2279 time_get<_CharT, _InputIterator>::__get_minute(int& __m,
2280 iter_type& __b, iter_type __e,
2281 ios_base::iostate& __err,
2282 const ctype<char_type>& __ct) const
2284 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2285 if (!(__err & ios_base::failbit) && __t <= 59)
2288 __err |= ios_base::failbit;
2291 template <class _CharT, class _InputIterator>
2293 time_get<_CharT, _InputIterator>::__get_second(int& __s,
2294 iter_type& __b, iter_type __e,
2295 ios_base::iostate& __err,
2296 const ctype<char_type>& __ct) const
2298 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2299 if (!(__err & ios_base::failbit) && __t <= 60)
2302 __err |= ios_base::failbit;
2305 template <class _CharT, class _InputIterator>
2307 time_get<_CharT, _InputIterator>::__get_weekday(int& __w,
2308 iter_type& __b, iter_type __e,
2309 ios_base::iostate& __err,
2310 const ctype<char_type>& __ct) const
2312 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 1);
2313 if (!(__err & ios_base::failbit) && __t <= 6)
2316 __err |= ios_base::failbit;
2319 template <class _CharT, class _InputIterator>
2321 time_get<_CharT, _InputIterator>::__get_day_year_num(int& __d,
2322 iter_type& __b, iter_type __e,
2323 ios_base::iostate& __err,
2324 const ctype<char_type>& __ct) const
2326 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 3);
2327 if (!(__err & ios_base::failbit) && __t <= 365)
2330 __err |= ios_base::failbit;
2333 template <class _CharT, class _InputIterator>
2335 time_get<_CharT, _InputIterator>::__get_white_space(iter_type& __b, iter_type __e,
2336 ios_base::iostate& __err,
2337 const ctype<char_type>& __ct) const
2339 for (; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2342 __err |= ios_base::eofbit;
2345 template <class _CharT, class _InputIterator>
2347 time_get<_CharT, _InputIterator>::__get_am_pm(int& __h,
2348 iter_type& __b, iter_type __e,
2349 ios_base::iostate& __err,
2350 const ctype<char_type>& __ct) const
2352 const string_type* __ap = this->__am_pm();
2353 if (__ap[0].size() + __ap[1].size() == 0)
2355 __err |= ios_base::failbit;
2358 ptrdiff_t __i = __scan_keyword(__b, __e, __ap, __ap+2, __ct, __err, false) - __ap;
2359 if (__i == 0 && __h == 12)
2361 else if (__i == 1 && __h < 12)
2365 template <class _CharT, class _InputIterator>
2367 time_get<_CharT, _InputIterator>::__get_percent(iter_type& __b, iter_type __e,
2368 ios_base::iostate& __err,
2369 const ctype<char_type>& __ct) const
2373 __err |= ios_base::eofbit | ios_base::failbit;
2376 if (__ct.narrow(*__b, 0) != '%')
2377 __err |= ios_base::failbit;
2378 else if(++__b == __e)
2379 __err |= ios_base::eofbit;
2382 // time_get end primatives
2384 template <class _CharT, class _InputIterator>
2386 time_get<_CharT, _InputIterator>::get(iter_type __b, iter_type __e,
2388 ios_base::iostate& __err, tm* __tm,
2389 const char_type* __fmtb, const char_type* __fmte) const
2391 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2392 __err = ios_base::goodbit;
2393 while (__fmtb != __fmte && __err == ios_base::goodbit)
2397 __err = ios_base::failbit;
2400 if (__ct.narrow(*__fmtb, 0) == '%')
2402 if (++__fmtb == __fmte)
2404 __err = ios_base::failbit;
2407 char __cmd = __ct.narrow(*__fmtb, 0);
2409 if (__cmd == 'E' || __cmd == '0')
2411 if (++__fmtb == __fmte)
2413 __err = ios_base::failbit;
2417 __cmd = __ct.narrow(*__fmtb, 0);
2419 __b = do_get(__b, __e, __iob, __err, __tm, __cmd, __opt);
2422 else if (__ct.is(ctype_base::space, *__fmtb))
2424 for (++__fmtb; __fmtb != __fmte && __ct.is(ctype_base::space, *__fmtb); ++__fmtb)
2426 for ( ; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2429 else if (__ct.toupper(*__b) == __ct.toupper(*__fmtb))
2435 __err = ios_base::failbit;
2438 __err |= ios_base::eofbit;
2442 template <class _CharT, class _InputIterator>
2443 typename time_get<_CharT, _InputIterator>::dateorder
2444 time_get<_CharT, _InputIterator>::do_date_order() const
2449 template <class _CharT, class _InputIterator>
2451 time_get<_CharT, _InputIterator>::do_get_time(iter_type __b, iter_type __e,
2453 ios_base::iostate& __err,
2456 const char_type __fmt[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2457 return get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt)/sizeof(__fmt[0]));
2460 template <class _CharT, class _InputIterator>
2462 time_get<_CharT, _InputIterator>::do_get_date(iter_type __b, iter_type __e,
2464 ios_base::iostate& __err,
2467 const string_type& __fmt = this->__x();
2468 return get(__b, __e, __iob, __err, __tm, __fmt.data(), __fmt.data() + __fmt.size());
2471 template <class _CharT, class _InputIterator>
2473 time_get<_CharT, _InputIterator>::do_get_weekday(iter_type __b, iter_type __e,
2475 ios_base::iostate& __err,
2478 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2479 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2483 template <class _CharT, class _InputIterator>
2485 time_get<_CharT, _InputIterator>::do_get_monthname(iter_type __b, iter_type __e,
2487 ios_base::iostate& __err,
2490 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2491 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2495 template <class _CharT, class _InputIterator>
2497 time_get<_CharT, _InputIterator>::do_get_year(iter_type __b, iter_type __e,
2499 ios_base::iostate& __err,
2502 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2503 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2507 template <class _CharT, class _InputIterator>
2509 time_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
2511 ios_base::iostate& __err, tm* __tm,
2512 char __fmt, char) const
2514 __err = ios_base::goodbit;
2515 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2520 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2525 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2529 const string_type& __fm = this->__c();
2530 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
2535 __get_day(__tm->tm_mday, __b, __e, __err, __ct);
2539 const char_type __fm[] = {'%', 'm', '/', '%', 'd', '/', '%', 'y'};
2540 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2545 const char_type __fm[] = {'%', 'Y', '-', '%', 'm', '-', '%', 'd'};
2546 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2550 __get_hour(__tm->tm_hour, __b, __e, __err, __ct);
2553 __get_12_hour(__tm->tm_hour, __b, __e, __err, __ct);
2556 __get_day_year_num(__tm->tm_yday, __b, __e, __err, __ct);
2559 __get_month(__tm->tm_mon, __b, __e, __err, __ct);
2562 __get_minute(__tm->tm_min, __b, __e, __err, __ct);
2566 __get_white_space(__b, __e, __err, __ct);
2569 __get_am_pm(__tm->tm_hour, __b, __e, __err, __ct);
2573 const char_type __fm[] = {'%', 'I', ':', '%', 'M', ':', '%', 'S', ' ', '%', 'p'};
2574 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2579 const char_type __fm[] = {'%', 'H', ':', '%', 'M'};
2580 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2584 __get_second(__tm->tm_sec, __b, __e, __err, __ct);
2588 const char_type __fm[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2589 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2593 __get_weekday(__tm->tm_wday, __b, __e, __err, __ct);
2596 return do_get_date(__b, __e, __iob, __err, __tm);
2599 const string_type& __fm = this->__X();
2600 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
2604 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2607 __get_year4(__tm->tm_year, __b, __e, __err, __ct);
2610 __get_percent(__b, __e, __err, __ct);
2613 __err |= ios_base::failbit;
2618 _LIBCPP_EXTERN_TEMPLATE(class time_get<char>)
2619 _LIBCPP_EXTERN_TEMPLATE(class time_get<wchar_t>)
2626 __time_get(const char* __nm);
2627 __time_get(const string& __nm);
2631 template <class _CharT>
2632 class __time_get_storage
2636 typedef basic_string<_CharT> string_type;
2638 string_type __weeks_[14];
2639 string_type __months_[24];
2640 string_type __am_pm_[2];
2646 explicit __time_get_storage(const char* __nm);
2647 explicit __time_get_storage(const string& __nm);
2649 _LIBCPP_ALWAYS_INLINE ~__time_get_storage() {}
2651 time_base::dateorder __do_date_order() const;
2654 void init(const ctype<_CharT>&);
2655 string_type __analyze(char __fmt, const ctype<_CharT>&);
2658 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2659 class _LIBCPP_VISIBLE time_get_byname
2660 : public time_get<_CharT, _InputIterator>,
2661 private __time_get_storage<_CharT>
2664 typedef time_base::dateorder dateorder;
2665 typedef _InputIterator iter_type;
2666 typedef _CharT char_type;
2667 typedef basic_string<char_type> string_type;
2669 _LIBCPP_INLINE_VISIBILITY
2670 explicit time_get_byname(const char* __nm, size_t __refs = 0)
2671 : time_get<_CharT, _InputIterator>(__refs),
2672 __time_get_storage<_CharT>(__nm) {}
2673 _LIBCPP_INLINE_VISIBILITY
2674 explicit time_get_byname(const string& __nm, size_t __refs = 0)
2675 : time_get<_CharT, _InputIterator>(__refs),
2676 __time_get_storage<_CharT>(__nm) {}
2679 _LIBCPP_INLINE_VISIBILITY
2680 ~time_get_byname() {}
2682 _LIBCPP_INLINE_VISIBILITY
2683 virtual dateorder do_date_order() const {return this->__do_date_order();}
2685 _LIBCPP_INLINE_VISIBILITY
2686 virtual const string_type* __weeks() const {return this->__weeks_;}
2687 _LIBCPP_INLINE_VISIBILITY
2688 virtual const string_type* __months() const {return this->__months_;}
2689 _LIBCPP_INLINE_VISIBILITY
2690 virtual const string_type* __am_pm() const {return this->__am_pm_;}
2691 _LIBCPP_INLINE_VISIBILITY
2692 virtual const string_type& __c() const {return this->__c_;}
2693 _LIBCPP_INLINE_VISIBILITY
2694 virtual const string_type& __r() const {return this->__r_;}
2695 _LIBCPP_INLINE_VISIBILITY
2696 virtual const string_type& __x() const {return this->__x_;}
2697 _LIBCPP_INLINE_VISIBILITY
2698 virtual const string_type& __X() const {return this->__X_;}
2701 _LIBCPP_EXTERN_TEMPLATE(class time_get_byname<char>)
2702 _LIBCPP_EXTERN_TEMPLATE(class time_get_byname<wchar_t>)
2708 _LIBCPP_ALWAYS_INLINE __time_put() : __loc_(_LIBCPP_GET_C_LOCALE) {}
2709 __time_put(const char* __nm);
2710 __time_put(const string& __nm);
2712 void __do_put(char* __nb, char*& __ne, const tm* __tm,
2713 char __fmt, char __mod) const;
2714 void __do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm,
2715 char __fmt, char __mod) const;
2718 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2719 class _LIBCPP_VISIBLE time_put
2720 : public locale::facet,
2724 typedef _CharT char_type;
2725 typedef _OutputIterator iter_type;
2727 _LIBCPP_ALWAYS_INLINE
2728 explicit time_put(size_t __refs = 0)
2729 : locale::facet(__refs) {}
2731 iter_type put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm,
2732 const char_type* __pb, const char_type* __pe) const;
2734 _LIBCPP_ALWAYS_INLINE
2735 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
2736 const tm* __tm, char __fmt, char __mod = 0) const
2738 return do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2741 static locale::id id;
2744 _LIBCPP_ALWAYS_INLINE
2746 virtual iter_type do_put(iter_type __s, ios_base&, char_type, const tm* __tm,
2747 char __fmt, char __mod) const;
2749 _LIBCPP_ALWAYS_INLINE
2750 explicit time_put(const char* __nm, size_t __refs)
2751 : locale::facet(__refs),
2753 _LIBCPP_ALWAYS_INLINE
2754 explicit time_put(const string& __nm, size_t __refs)
2755 : locale::facet(__refs),
2759 template <class _CharT, class _OutputIterator>
2761 time_put<_CharT, _OutputIterator>::id;
2763 template <class _CharT, class _OutputIterator>
2765 time_put<_CharT, _OutputIterator>::put(iter_type __s, ios_base& __iob,
2766 char_type __fl, const tm* __tm,
2767 const char_type* __pb,
2768 const char_type* __pe) const
2770 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2771 for (; __pb != __pe; ++__pb)
2773 if (__ct.narrow(*__pb, 0) == '%')
2781 char __fmt = __ct.narrow(*__pb, 0);
2782 if (__fmt == 'E' || __fmt == 'O')
2791 __fmt = __ct.narrow(*__pb, 0);
2793 __s = do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2801 template <class _CharT, class _OutputIterator>
2803 time_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base&,
2804 char_type, const tm* __tm,
2805 char __fmt, char __mod) const
2807 char_type __nar[100];
2808 char_type* __nb = __nar;
2809 char_type* __ne = __nb + 100;
2810 __do_put(__nb, __ne, __tm, __fmt, __mod);
2811 return _VSTD::copy(__nb, __ne, __s);
2814 _LIBCPP_EXTERN_TEMPLATE(class time_put<char>)
2815 _LIBCPP_EXTERN_TEMPLATE(class time_put<wchar_t>)
2817 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2818 class _LIBCPP_VISIBLE time_put_byname
2819 : public time_put<_CharT, _OutputIterator>
2822 _LIBCPP_ALWAYS_INLINE
2823 explicit time_put_byname(const char* __nm, size_t __refs = 0)
2824 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2826 _LIBCPP_ALWAYS_INLINE
2827 explicit time_put_byname(const string& __nm, size_t __refs = 0)
2828 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2831 _LIBCPP_ALWAYS_INLINE
2832 ~time_put_byname() {}
2835 _LIBCPP_EXTERN_TEMPLATE(class time_put_byname<char>)
2836 _LIBCPP_EXTERN_TEMPLATE(class time_put_byname<wchar_t>)
2840 class _LIBCPP_VISIBLE money_base
2843 enum part {none, space, symbol, sign, value};
2844 struct pattern {char field[4];};
2846 _LIBCPP_ALWAYS_INLINE money_base() {}
2851 template <class _CharT, bool _International = false>
2852 class _LIBCPP_VISIBLE moneypunct
2853 : public locale::facet,
2857 typedef _CharT char_type;
2858 typedef basic_string<char_type> string_type;
2860 _LIBCPP_ALWAYS_INLINE
2861 explicit moneypunct(size_t __refs = 0)
2862 : locale::facet(__refs) {}
2864 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
2865 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
2866 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
2867 _LIBCPP_ALWAYS_INLINE string_type curr_symbol() const {return do_curr_symbol();}
2868 _LIBCPP_ALWAYS_INLINE string_type positive_sign() const {return do_positive_sign();}
2869 _LIBCPP_ALWAYS_INLINE string_type negative_sign() const {return do_negative_sign();}
2870 _LIBCPP_ALWAYS_INLINE int frac_digits() const {return do_frac_digits();}
2871 _LIBCPP_ALWAYS_INLINE pattern pos_format() const {return do_pos_format();}
2872 _LIBCPP_ALWAYS_INLINE pattern neg_format() const {return do_neg_format();}
2874 static locale::id id;
2875 static const bool intl = _International;
2878 _LIBCPP_ALWAYS_INLINE
2881 virtual char_type do_decimal_point() const {return numeric_limits<char_type>::max();}
2882 virtual char_type do_thousands_sep() const {return numeric_limits<char_type>::max();}
2883 virtual string do_grouping() const {return string();}
2884 virtual string_type do_curr_symbol() const {return string_type();}
2885 virtual string_type do_positive_sign() const {return string_type();}
2886 virtual string_type do_negative_sign() const {return string_type(1, '-');}
2887 virtual int do_frac_digits() const {return 0;}
2888 virtual pattern do_pos_format() const
2889 {pattern __p = {{symbol, sign, none, value}}; return __p;}
2890 virtual pattern do_neg_format() const
2891 {pattern __p = {{symbol, sign, none, value}}; return __p;}
2894 template <class _CharT, bool _International>
2896 moneypunct<_CharT, _International>::id;
2898 _LIBCPP_EXTERN_TEMPLATE(class moneypunct<char, false>)
2899 _LIBCPP_EXTERN_TEMPLATE(class moneypunct<char, true>)
2900 _LIBCPP_EXTERN_TEMPLATE(class moneypunct<wchar_t, false>)
2901 _LIBCPP_EXTERN_TEMPLATE(class moneypunct<wchar_t, true>)
2903 // moneypunct_byname
2905 template <class _CharT, bool _International = false>
2906 class _LIBCPP_VISIBLE moneypunct_byname
2907 : public moneypunct<_CharT, _International>
2910 typedef money_base::pattern pattern;
2911 typedef _CharT char_type;
2912 typedef basic_string<char_type> string_type;
2914 _LIBCPP_ALWAYS_INLINE
2915 explicit moneypunct_byname(const char* __nm, size_t __refs = 0)
2916 : moneypunct<_CharT, _International>(__refs) {init(__nm);}
2918 _LIBCPP_ALWAYS_INLINE
2919 explicit moneypunct_byname(const string& __nm, size_t __refs = 0)
2920 : moneypunct<_CharT, _International>(__refs) {init(__nm.c_str());}
2923 _LIBCPP_ALWAYS_INLINE
2924 ~moneypunct_byname() {}
2926 virtual char_type do_decimal_point() const {return __decimal_point_;}
2927 virtual char_type do_thousands_sep() const {return __thousands_sep_;}
2928 virtual string do_grouping() const {return __grouping_;}
2929 virtual string_type do_curr_symbol() const {return __curr_symbol_;}
2930 virtual string_type do_positive_sign() const {return __positive_sign_;}
2931 virtual string_type do_negative_sign() const {return __negative_sign_;}
2932 virtual int do_frac_digits() const {return __frac_digits_;}
2933 virtual pattern do_pos_format() const {return __pos_format_;}
2934 virtual pattern do_neg_format() const {return __neg_format_;}
2937 char_type __decimal_point_;
2938 char_type __thousands_sep_;
2940 string_type __curr_symbol_;
2941 string_type __positive_sign_;
2942 string_type __negative_sign_;
2944 pattern __pos_format_;
2945 pattern __neg_format_;
2947 void init(const char*);
2950 template<> void moneypunct_byname<char, false>::init(const char*);
2951 template<> void moneypunct_byname<char, true>::init(const char*);
2952 template<> void moneypunct_byname<wchar_t, false>::init(const char*);
2953 template<> void moneypunct_byname<wchar_t, true>::init(const char*);
2955 _LIBCPP_EXTERN_TEMPLATE(class moneypunct_byname<char, false>)
2956 _LIBCPP_EXTERN_TEMPLATE(class moneypunct_byname<char, true>)
2957 _LIBCPP_EXTERN_TEMPLATE(class moneypunct_byname<wchar_t, false>)
2958 _LIBCPP_EXTERN_TEMPLATE(class moneypunct_byname<wchar_t, true>)
2962 template <class _CharT>
2966 typedef _CharT char_type;
2967 typedef basic_string<char_type> string_type;
2969 _LIBCPP_ALWAYS_INLINE __money_get() {}
2971 static void __gather_info(bool __intl, const locale& __loc,
2972 money_base::pattern& __pat, char_type& __dp,
2973 char_type& __ts, string& __grp,
2974 string_type& __sym, string_type& __psn,
2975 string_type& __nsn, int& __fd);
2978 template <class _CharT>
2980 __money_get<_CharT>::__gather_info(bool __intl, const locale& __loc,
2981 money_base::pattern& __pat, char_type& __dp,
2982 char_type& __ts, string& __grp,
2983 string_type& __sym, string_type& __psn,
2984 string_type& __nsn, int& __fd)
2988 const moneypunct<char_type, true>& __mp =
2989 use_facet<moneypunct<char_type, true> >(__loc);
2990 __pat = __mp.neg_format();
2991 __nsn = __mp.negative_sign();
2992 __psn = __mp.positive_sign();
2993 __dp = __mp.decimal_point();
2994 __ts = __mp.thousands_sep();
2995 __grp = __mp.grouping();
2996 __sym = __mp.curr_symbol();
2997 __fd = __mp.frac_digits();
3001 const moneypunct<char_type, false>& __mp =
3002 use_facet<moneypunct<char_type, false> >(__loc);
3003 __pat = __mp.neg_format();
3004 __nsn = __mp.negative_sign();
3005 __psn = __mp.positive_sign();
3006 __dp = __mp.decimal_point();
3007 __ts = __mp.thousands_sep();
3008 __grp = __mp.grouping();
3009 __sym = __mp.curr_symbol();
3010 __fd = __mp.frac_digits();
3014 _LIBCPP_EXTERN_TEMPLATE(class __money_get<char>)
3015 _LIBCPP_EXTERN_TEMPLATE(class __money_get<wchar_t>)
3017 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
3018 class _LIBCPP_VISIBLE money_get
3019 : public locale::facet,
3020 private __money_get<_CharT>
3023 typedef _CharT char_type;
3024 typedef _InputIterator iter_type;
3025 typedef basic_string<char_type> string_type;
3027 _LIBCPP_ALWAYS_INLINE
3028 explicit money_get(size_t __refs = 0)
3029 : locale::facet(__refs) {}
3031 _LIBCPP_ALWAYS_INLINE
3032 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
3033 ios_base::iostate& __err, long double& __v) const
3035 return do_get(__b, __e, __intl, __iob, __err, __v);
3038 _LIBCPP_ALWAYS_INLINE
3039 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
3040 ios_base::iostate& __err, string_type& __v) const
3042 return do_get(__b, __e, __intl, __iob, __err, __v);
3045 static locale::id id;
3049 _LIBCPP_ALWAYS_INLINE
3052 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
3053 ios_base& __iob, ios_base::iostate& __err,
3054 long double& __v) const;
3055 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
3056 ios_base& __iob, ios_base::iostate& __err,
3057 string_type& __v) const;
3060 static bool __do_get(iter_type& __b, iter_type __e,
3061 bool __intl, const locale& __loc,
3062 ios_base::fmtflags __flags, ios_base::iostate& __err,
3063 bool& __neg, const ctype<char_type>& __ct,
3064 unique_ptr<char_type, void(*)(void*)>& __wb,
3065 char_type*& __wn, char_type* __we);
3068 template <class _CharT, class _InputIterator>
3070 money_get<_CharT, _InputIterator>::id;
3072 void __do_nothing(void*);
3074 template <class _Tp>
3077 __double_or_nothing(unique_ptr<_Tp, void(*)(void*)>& __b, _Tp*& __n, _Tp*& __e)
3079 bool __owns = __b.get_deleter() != __do_nothing;
3080 size_t __cur_cap = static_cast<size_t>(__e-__b.get()) * sizeof(_Tp);
3081 size_t __new_cap = __cur_cap < numeric_limits<size_t>::max() / 2 ?
3082 2 * __cur_cap : numeric_limits<size_t>::max();
3083 size_t __n_off = static_cast<size_t>(__n - __b.get());
3084 _Tp* __t = (_Tp*)realloc(__owns ? __b.get() : 0, __new_cap);
3086 __throw_bad_alloc();
3089 __b = unique_ptr<_Tp, void(*)(void*)>(__t, free);
3090 __new_cap /= sizeof(_Tp);
3091 __n = __b.get() + __n_off;
3092 __e = __b.get() + __new_cap;
3096 template <class _CharT, class _InputIterator>
3098 money_get<_CharT, _InputIterator>::__do_get(iter_type& __b, iter_type __e,
3099 bool __intl, const locale& __loc,
3100 ios_base::fmtflags __flags,
3101 ios_base::iostate& __err,
3103 const ctype<char_type>& __ct,
3104 unique_ptr<char_type, void(*)(void*)>& __wb,
3105 char_type*& __wn, char_type* __we)
3107 const unsigned __bz = 100;
3108 unsigned __gbuf[__bz];
3109 unique_ptr<unsigned, void(*)(void*)> __gb(__gbuf, __do_nothing);
3110 unsigned* __gn = __gb.get();
3111 unsigned* __ge = __gn + __bz;
3112 money_base::pattern __pat;
3119 // Capture the spaces read into money_base::{space,none} so they
3120 // can be compared to initial spaces in __sym.
3121 string_type __spaces;
3123 __money_get<_CharT>::__gather_info(__intl, __loc, __pat, __dp, __ts, __grp,
3124 __sym, __psn, __nsn, __fd);
3125 const string_type* __trailing_sign = 0;
3127 for (unsigned __p = 0; __p < 4 && __b != __e; ++__p)
3129 switch (__pat.field[__p])
3131 case money_base::space:
3134 if (__ct.is(ctype_base::space, *__b))
3135 __spaces.push_back(*__b++);
3138 __err |= ios_base::failbit;
3143 case money_base::none:
3146 while (__b != __e && __ct.is(ctype_base::space, *__b))
3147 __spaces.push_back(*__b++);
3150 case money_base::sign:
3151 if (__psn.size() + __nsn.size() > 0)
3153 if (__psn.size() == 0 || __nsn.size() == 0)
3154 { // sign is optional
3155 if (__psn.size() > 0)
3156 { // __nsn.size() == 0
3157 if (*__b == __psn[0])
3160 if (__psn.size() > 1)
3161 __trailing_sign = &__psn;
3166 else if (*__b == __nsn[0]) // __nsn.size() > 0 && __psn.size() == 0
3170 if (__nsn.size() > 1)
3171 __trailing_sign = &__nsn;
3174 else // sign is required
3176 if (*__b == __psn[0])
3179 if (__psn.size() > 1)
3180 __trailing_sign = &__psn;
3182 else if (*__b == __nsn[0])
3186 if (__nsn.size() > 1)
3187 __trailing_sign = &__nsn;
3191 __err |= ios_base::failbit;
3197 case money_base::symbol:
3199 bool __more_needed = __trailing_sign ||
3201 (__p == 2 && __pat.field[3] != static_cast<char>(money_base::none));
3202 bool __sb = __flags & ios_base::showbase;
3203 if (__sb || __more_needed)
3205 typename string_type::const_iterator __sym_space_end = __sym.begin();
3206 if (__p > 0 && (__pat.field[__p - 1] == money_base::none ||
3207 __pat.field[__p - 1] == money_base::space)) {
3208 // Match spaces we've already read against spaces at
3209 // the beginning of __sym.
3210 while (__sym_space_end != __sym.end() &&
3211 __ct.is(ctype_base::space, *__sym_space_end))
3213 const size_t __num_spaces = __sym_space_end - __sym.begin();
3214 if (__num_spaces > __spaces.size() ||
3215 !equal(__spaces.end() - __num_spaces, __spaces.end(),
3217 // No match. Put __sym_space_end back at the
3218 // beginning of __sym, which will prevent a
3219 // match in the next loop.
3220 __sym_space_end = __sym.begin();
3223 typename string_type::const_iterator __sym_curr_char = __sym_space_end;
3224 while (__sym_curr_char != __sym.end() && __b != __e &&
3225 *__b == *__sym_curr_char) {
3229 if (__sb && __sym_curr_char != __sym.end())
3231 __err |= ios_base::failbit;
3237 case money_base::value:
3240 for (; __b != __e; ++__b)
3242 char_type __c = *__b;
3243 if (__ct.is(ctype_base::digit, __c))
3246 __double_or_nothing(__wb, __wn, __we);
3250 else if (__grp.size() > 0 && __ng > 0 && __c == __ts)
3253 __double_or_nothing(__gb, __gn, __ge);
3260 if (__gb.get() != __gn && __ng > 0)
3263 __double_or_nothing(__gb, __gn, __ge);
3268 if (__b == __e || *__b != __dp)
3270 __err |= ios_base::failbit;
3273 for (++__b; __fd > 0; --__fd, ++__b)
3275 if (__b == __e || !__ct.is(ctype_base::digit, *__b))
3277 __err |= ios_base::failbit;
3281 __double_or_nothing(__wb, __wn, __we);
3285 if (__wn == __wb.get())
3287 __err |= ios_base::failbit;
3294 if (__trailing_sign)
3296 for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, ++__b)
3298 if (__b == __e || *__b != (*__trailing_sign)[__i])
3300 __err |= ios_base::failbit;
3305 if (__gb.get() != __gn)
3307 ios_base::iostate __et = ios_base::goodbit;
3308 __check_grouping(__grp, __gb.get(), __gn, __et);
3311 __err |= ios_base::failbit;
3318 template <class _CharT, class _InputIterator>
3320 money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3321 bool __intl, ios_base& __iob,
3322 ios_base::iostate& __err,
3323 long double& __v) const
3325 const int __bz = 100;
3326 char_type __wbuf[__bz];
3327 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3329 char_type* __we = __wbuf + __bz;
3330 locale __loc = __iob.getloc();
3331 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3333 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3336 const char __src[] = "0123456789";
3337 char_type __atoms[sizeof(__src)-1];
3338 __ct.widen(__src, __src + (sizeof(__src)-1), __atoms);
3340 char* __nc = __nbuf;
3341 unique_ptr<char, void(*)(void*)> __h(0, free);
3342 if (__wn - __wb.get() > __bz-2)
3344 __h.reset((char*)malloc(static_cast<size_t>(__wn - __wb.get() + 2)));
3346 __throw_bad_alloc();
3351 for (const char_type* __w = __wb.get(); __w < __wn; ++__w, ++__nc)
3352 *__nc = __src[find(__atoms, __atoms+sizeof(__atoms), *__w) - __atoms];
3354 if (sscanf(__nbuf, "%Lf", &__v) != 1)
3355 __throw_runtime_error("money_get error");
3358 __err |= ios_base::eofbit;
3362 template <class _CharT, class _InputIterator>
3364 money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3365 bool __intl, ios_base& __iob,
3366 ios_base::iostate& __err,
3367 string_type& __v) const
3369 const int __bz = 100;
3370 char_type __wbuf[__bz];
3371 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3373 char_type* __we = __wbuf + __bz;
3374 locale __loc = __iob.getloc();
3375 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3377 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3382 __v.push_back(__ct.widen('-'));
3383 char_type __z = __ct.widen('0');
3385 for (__w = __wb.get(); __w < __wn-1; ++__w)
3388 __v.append(__w, __wn);
3391 __err |= ios_base::eofbit;
3395 _LIBCPP_EXTERN_TEMPLATE(class money_get<char>)
3396 _LIBCPP_EXTERN_TEMPLATE(class money_get<wchar_t>)
3400 template <class _CharT>
3404 typedef _CharT char_type;
3405 typedef basic_string<char_type> string_type;
3407 _LIBCPP_ALWAYS_INLINE __money_put() {}
3409 static void __gather_info(bool __intl, bool __neg, const locale& __loc,
3410 money_base::pattern& __pat, char_type& __dp,
3411 char_type& __ts, string& __grp,
3412 string_type& __sym, string_type& __sn,
3414 static void __format(char_type* __mb, char_type*& __mi, char_type*& __me,
3415 ios_base::fmtflags __flags,
3416 const char_type* __db, const char_type* __de,
3417 const ctype<char_type>& __ct, bool __neg,
3418 const money_base::pattern& __pat, char_type __dp,
3419 char_type __ts, const string& __grp,
3420 const string_type& __sym, const string_type& __sn,
3424 template <class _CharT>
3426 __money_put<_CharT>::__gather_info(bool __intl, bool __neg, const locale& __loc,
3427 money_base::pattern& __pat, char_type& __dp,
3428 char_type& __ts, string& __grp,
3429 string_type& __sym, string_type& __sn,
3434 const moneypunct<char_type, true>& __mp =
3435 use_facet<moneypunct<char_type, true> >(__loc);
3438 __pat = __mp.neg_format();
3439 __sn = __mp.negative_sign();
3443 __pat = __mp.pos_format();
3444 __sn = __mp.positive_sign();
3446 __dp = __mp.decimal_point();
3447 __ts = __mp.thousands_sep();
3448 __grp = __mp.grouping();
3449 __sym = __mp.curr_symbol();
3450 __fd = __mp.frac_digits();
3454 const moneypunct<char_type, false>& __mp =
3455 use_facet<moneypunct<char_type, false> >(__loc);
3458 __pat = __mp.neg_format();
3459 __sn = __mp.negative_sign();
3463 __pat = __mp.pos_format();
3464 __sn = __mp.positive_sign();
3466 __dp = __mp.decimal_point();
3467 __ts = __mp.thousands_sep();
3468 __grp = __mp.grouping();
3469 __sym = __mp.curr_symbol();
3470 __fd = __mp.frac_digits();
3474 template <class _CharT>
3476 __money_put<_CharT>::__format(char_type* __mb, char_type*& __mi, char_type*& __me,
3477 ios_base::fmtflags __flags,
3478 const char_type* __db, const char_type* __de,
3479 const ctype<char_type>& __ct, bool __neg,
3480 const money_base::pattern& __pat, char_type __dp,
3481 char_type __ts, const string& __grp,
3482 const string_type& __sym, const string_type& __sn,
3486 for (unsigned __p = 0; __p < 4; ++__p)
3488 switch (__pat.field[__p])
3490 case money_base::none:
3493 case money_base::space:
3495 *__me++ = __ct.widen(' ');
3497 case money_base::sign:
3501 case money_base::symbol:
3502 if (!__sym.empty() && (__flags & ios_base::showbase))
3503 __me = _VSTD::copy(__sym.begin(), __sym.end(), __me);
3505 case money_base::value:
3507 // remember start of value so we can reverse it
3508 char_type* __t = __me;
3509 // find beginning of digits
3512 // find end of digits
3513 const char_type* __d;
3514 for (__d = __db; __d < __de; ++__d)
3515 if (!__ct.is(ctype_base::digit, *__d))
3517 // print fractional part
3521 for (__f = __fd; __d > __db && __f > 0; --__f)
3523 char_type __z = __f > 0 ? __ct.widen('0') : char_type();
3524 for (; __f > 0; --__f)
3531 *__me++ = __ct.widen('0');
3537 unsigned __gl = __grp.empty() ? numeric_limits<unsigned>::max()
3538 : static_cast<unsigned>(__grp[__ig]);
3545 if (++__ig < __grp.size())
3546 __gl = __grp[__ig] == numeric_limits<char>::max() ?
3547 numeric_limits<unsigned>::max() :
3548 static_cast<unsigned>(__grp[__ig]);
3560 // print rest of sign, if any
3561 if (__sn.size() > 1)
3562 __me = _VSTD::copy(__sn.begin()+1, __sn.end(), __me);
3564 if ((__flags & ios_base::adjustfield) == ios_base::left)
3566 else if ((__flags & ios_base::adjustfield) != ios_base::internal)
3570 _LIBCPP_EXTERN_TEMPLATE(class __money_put<char>)
3571 _LIBCPP_EXTERN_TEMPLATE(class __money_put<wchar_t>)
3573 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
3574 class _LIBCPP_VISIBLE money_put
3575 : public locale::facet,
3576 private __money_put<_CharT>
3579 typedef _CharT char_type;
3580 typedef _OutputIterator iter_type;
3581 typedef basic_string<char_type> string_type;
3583 _LIBCPP_ALWAYS_INLINE
3584 explicit money_put(size_t __refs = 0)
3585 : locale::facet(__refs) {}
3587 _LIBCPP_ALWAYS_INLINE
3588 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3589 long double __units) const
3591 return do_put(__s, __intl, __iob, __fl, __units);
3594 _LIBCPP_ALWAYS_INLINE
3595 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3596 const string_type& __digits) const
3598 return do_put(__s, __intl, __iob, __fl, __digits);
3601 static locale::id id;
3604 _LIBCPP_ALWAYS_INLINE
3607 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3608 char_type __fl, long double __units) const;
3609 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3610 char_type __fl, const string_type& __digits) const;
3613 template <class _CharT, class _OutputIterator>
3615 money_put<_CharT, _OutputIterator>::id;
3617 template <class _CharT, class _OutputIterator>
3619 money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3620 ios_base& __iob, char_type __fl,
3621 long double __units) const
3624 const size_t __bs = 100;
3627 char_type __digits[__bs];
3628 char_type* __db = __digits;
3629 size_t __n = static_cast<size_t>(snprintf(__bb, __bs, "%.0Lf", __units));
3630 unique_ptr<char, void(*)(void*)> __hn(0, free);
3631 unique_ptr<char_type, void(*)(void*)> __hd(0, free);
3632 // secure memory for digit storage
3635 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
3636 __n = static_cast<size_t>(asprintf_l(&__bb, _LIBCPP_GET_C_LOCALE, "%.0Lf", __units));
3638 __n = __asprintf_l(&__bb, __cloc(), "%.0Lf", __units);
3641 __throw_bad_alloc();
3643 __hd.reset((char_type*)malloc(__n * sizeof(char_type)));
3644 if (__hd == nullptr)
3645 __throw_bad_alloc();
3649 locale __loc = __iob.getloc();
3650 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3651 __ct.widen(__bb, __bb + __n, __db);
3652 bool __neg = __n > 0 && __bb[0] == '-';
3653 money_base::pattern __pat;
3660 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3661 // secure memory for formatting
3662 char_type __mbuf[__bs];
3663 char_type* __mb = __mbuf;
3664 unique_ptr<char_type, void(*)(void*)> __hw(0, free);
3665 size_t __exn = static_cast<int>(__n) > __fd ?
3666 (__n - static_cast<size_t>(__fd)) * 2 + __sn.size() +
3667 __sym.size() + static_cast<size_t>(__fd) + 1
3668 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
3671 __hw.reset((char_type*)malloc(__exn * sizeof(char_type)));
3674 __throw_bad_alloc();
3679 this->__format(__mb, __mi, __me, __iob.flags(),
3680 __db, __db + __n, __ct,
3681 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3682 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3685 template <class _CharT, class _OutputIterator>
3687 money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3688 ios_base& __iob, char_type __fl,
3689 const string_type& __digits) const
3692 locale __loc = __iob.getloc();
3693 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3694 bool __neg = __digits.size() > 0 && __digits[0] == __ct.widen('-');
3695 money_base::pattern __pat;
3702 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3703 // secure memory for formatting
3704 char_type __mbuf[100];
3705 char_type* __mb = __mbuf;
3706 unique_ptr<char_type, void(*)(void*)> __h(0, free);
3707 size_t __exn = static_cast<int>(__digits.size()) > __fd ?
3708 (__digits.size() - static_cast<size_t>(__fd)) * 2 +
3709 __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 1
3710 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
3713 __h.reset((char_type*)malloc(__exn * sizeof(char_type)));
3716 __throw_bad_alloc();
3721 this->__format(__mb, __mi, __me, __iob.flags(),
3722 __digits.data(), __digits.data() + __digits.size(), __ct,
3723 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3724 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3727 _LIBCPP_EXTERN_TEMPLATE(class money_put<char>)
3728 _LIBCPP_EXTERN_TEMPLATE(class money_put<wchar_t>)
3732 class _LIBCPP_VISIBLE messages_base
3735 typedef ptrdiff_t catalog;
3737 _LIBCPP_ALWAYS_INLINE messages_base() {}
3740 template <class _CharT>
3741 class _LIBCPP_VISIBLE messages
3742 : public locale::facet,
3743 public messages_base
3746 typedef _CharT char_type;
3747 typedef basic_string<_CharT> string_type;
3749 _LIBCPP_ALWAYS_INLINE
3750 explicit messages(size_t __refs = 0)
3751 : locale::facet(__refs) {}
3753 _LIBCPP_ALWAYS_INLINE
3754 catalog open(const basic_string<char>& __nm, const locale& __loc) const
3756 return do_open(__nm, __loc);
3759 _LIBCPP_ALWAYS_INLINE
3760 string_type get(catalog __c, int __set, int __msgid,
3761 const string_type& __dflt) const
3763 return do_get(__c, __set, __msgid, __dflt);
3766 _LIBCPP_ALWAYS_INLINE
3767 void close(catalog __c) const
3772 static locale::id id;
3775 _LIBCPP_ALWAYS_INLINE
3778 virtual catalog do_open(const basic_string<char>&, const locale&) const;
3779 virtual string_type do_get(catalog, int __set, int __msgid,
3780 const string_type& __dflt) const;
3781 virtual void do_close(catalog) const;
3784 template <class _CharT>
3786 messages<_CharT>::id;
3788 template <class _CharT>
3789 typename messages<_CharT>::catalog
3790 messages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const
3795 catalog __cat = (catalog)catopen(__nm.c_str(), NL_CAT_LOCALE);
3797 __cat = static_cast<catalog>((static_cast<size_t>(__cat) >> 1));
3802 template <class _CharT>
3803 typename messages<_CharT>::string_type
3804 messages<_CharT>::do_get(catalog __c, int __set, int __msgid,
3805 const string_type& __dflt) const
3811 __narrow_to_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__ndflt),
3813 __dflt.c_str() + __dflt.size());
3816 nl_catd __cat = (nl_catd)__c;
3817 char* __n = catgets(__cat, __set, __msgid, __ndflt.c_str());
3819 __widen_from_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__w),
3820 __n, __n + strlen(__n));
3825 template <class _CharT>
3827 messages<_CharT>::do_close(catalog __c) const
3832 nl_catd __cat = (nl_catd)__c;
3837 _LIBCPP_EXTERN_TEMPLATE(class messages<char>)
3838 _LIBCPP_EXTERN_TEMPLATE(class messages<wchar_t>)
3840 template <class _CharT>
3841 class _LIBCPP_VISIBLE messages_byname
3842 : public messages<_CharT>
3845 typedef messages_base::catalog catalog;
3846 typedef basic_string<_CharT> string_type;
3848 _LIBCPP_ALWAYS_INLINE
3849 explicit messages_byname(const char*, size_t __refs = 0)
3850 : messages<_CharT>(__refs) {}
3852 _LIBCPP_ALWAYS_INLINE
3853 explicit messages_byname(const string&, size_t __refs = 0)
3854 : messages<_CharT>(__refs) {}
3857 _LIBCPP_ALWAYS_INLINE
3858 ~messages_byname() {}
3861 _LIBCPP_EXTERN_TEMPLATE(class messages_byname<char>)
3862 _LIBCPP_EXTERN_TEMPLATE(class messages_byname<wchar_t>)
3864 template<class _Codecvt, class _Elem = wchar_t,
3865 class _Wide_alloc = allocator<_Elem>,
3866 class _Byte_alloc = allocator<char> >
3867 class _LIBCPP_VISIBLE wstring_convert
3870 typedef basic_string<char, char_traits<char>, _Byte_alloc> byte_string;
3871 typedef basic_string<_Elem, char_traits<_Elem>, _Wide_alloc> wide_string;
3872 typedef typename _Codecvt::state_type state_type;
3873 typedef typename wide_string::traits_type::int_type int_type;
3876 byte_string __byte_err_string_;
3877 wide_string __wide_err_string_;
3878 _Codecvt* __cvtptr_;
3879 state_type __cvtstate_;
3882 wstring_convert(const wstring_convert& __wc);
3883 wstring_convert& operator=(const wstring_convert& __wc);
3885 wstring_convert(_Codecvt* __pcvt = new _Codecvt);
3886 wstring_convert(_Codecvt* __pcvt, state_type __state);
3887 wstring_convert(const byte_string& __byte_err,
3888 const wide_string& __wide_err = wide_string());
3889 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3890 wstring_convert(wstring_convert&& __wc);
3894 _LIBCPP_ALWAYS_INLINE
3895 wide_string from_bytes(char __byte)
3896 {return from_bytes(&__byte, &__byte+1);}
3897 _LIBCPP_ALWAYS_INLINE
3898 wide_string from_bytes(const char* __ptr)
3899 {return from_bytes(__ptr, __ptr + char_traits<char>::length(__ptr));}
3900 _LIBCPP_ALWAYS_INLINE
3901 wide_string from_bytes(const byte_string& __str)
3902 {return from_bytes(__str.data(), __str.data() + __str.size());}
3903 wide_string from_bytes(const char* __first, const char* __last);
3905 _LIBCPP_ALWAYS_INLINE
3906 byte_string to_bytes(_Elem __wchar)
3907 {return to_bytes(&__wchar, &__wchar+1);}
3908 _LIBCPP_ALWAYS_INLINE
3909 byte_string to_bytes(const _Elem* __wptr)
3910 {return to_bytes(__wptr, __wptr + char_traits<_Elem>::length(__wptr));}
3911 _LIBCPP_ALWAYS_INLINE
3912 byte_string to_bytes(const wide_string& __wstr)
3913 {return to_bytes(__wstr.data(), __wstr.data() + __wstr.size());}
3914 byte_string to_bytes(const _Elem* __first, const _Elem* __last);
3916 _LIBCPP_ALWAYS_INLINE
3917 size_t converted() const {return __cvtcount_;}
3918 _LIBCPP_ALWAYS_INLINE
3919 state_type state() const {return __cvtstate_;}
3922 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3923 inline _LIBCPP_ALWAYS_INLINE
3924 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3925 wstring_convert(_Codecvt* __pcvt)
3926 : __cvtptr_(__pcvt), __cvtstate_(), __cvtcount_(0)
3930 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3931 inline _LIBCPP_ALWAYS_INLINE
3932 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3933 wstring_convert(_Codecvt* __pcvt, state_type __state)
3934 : __cvtptr_(__pcvt), __cvtstate_(__state), __cvtcount_(0)
3938 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3939 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3940 wstring_convert(const byte_string& __byte_err, const wide_string& __wide_err)
3941 : __byte_err_string_(__byte_err), __wide_err_string_(__wide_err),
3942 __cvtstate_(), __cvtcount_(0)
3944 __cvtptr_ = new _Codecvt;
3947 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3949 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3950 inline _LIBCPP_ALWAYS_INLINE
3951 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3952 wstring_convert(wstring_convert&& __wc)
3953 : __byte_err_string_(_VSTD::move(__wc.__byte_err_string_)),
3954 __wide_err_string_(_VSTD::move(__wc.__wide_err_string_)),
3955 __cvtptr_(__wc.__cvtptr_),
3956 __cvtstate_(__wc.__cvtstate_), __cvtcount_(__wc.__cvtstate_)
3958 __wc.__cvtptr_ = nullptr;
3961 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
3963 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3964 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::~wstring_convert()
3969 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3970 typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::wide_string
3971 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3972 from_bytes(const char* __frm, const char* __frm_end)
3975 if (__cvtptr_ != nullptr)
3977 wide_string __ws(2*(__frm_end - __frm), _Elem());
3978 if (__frm != __frm_end)
3979 __ws.resize(__ws.capacity());
3980 codecvt_base::result __r = codecvt_base::ok;
3981 state_type __st = __cvtstate_;
3982 if (__frm != __frm_end)
3984 _Elem* __to = &__ws[0];
3985 _Elem* __to_end = __to + __ws.size();
3986 const char* __frm_nxt;
3990 __r = __cvtptr_->in(__st, __frm, __frm_end, __frm_nxt,
3991 __to, __to_end, __to_nxt);
3992 __cvtcount_ += __frm_nxt - __frm;
3993 if (__frm_nxt == __frm)
3995 __r = codecvt_base::error;
3997 else if (__r == codecvt_base::noconv)
3999 __ws.resize(__to - &__ws[0]);
4000 // This only gets executed if _Elem is char
4001 __ws.append((const _Elem*)__frm, (const _Elem*)__frm_end);
4003 __r = codecvt_base::ok;
4005 else if (__r == codecvt_base::ok)
4007 __ws.resize(__to_nxt - &__ws[0]);
4010 else if (__r == codecvt_base::partial)
4012 ptrdiff_t __s = __to_nxt - &__ws[0];
4013 __ws.resize(2 * __s);
4014 __to = &__ws[0] + __s;
4015 __to_end = &__ws[0] + __ws.size();
4018 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
4020 if (__r == codecvt_base::ok)
4023 #ifndef _LIBCPP_NO_EXCEPTIONS
4024 if (__wide_err_string_.empty())
4025 throw range_error("wstring_convert: from_bytes error");
4026 #endif // _LIBCPP_NO_EXCEPTIONS
4027 return __wide_err_string_;
4030 template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
4031 typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::byte_string
4032 wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4033 to_bytes(const _Elem* __frm, const _Elem* __frm_end)
4036 if (__cvtptr_ != nullptr)
4038 byte_string __bs(2*(__frm_end - __frm), char());
4039 if (__frm != __frm_end)
4040 __bs.resize(__bs.capacity());
4041 codecvt_base::result __r = codecvt_base::ok;
4042 state_type __st = __cvtstate_;
4043 if (__frm != __frm_end)
4045 char* __to = &__bs[0];
4046 char* __to_end = __to + __bs.size();
4047 const _Elem* __frm_nxt;
4051 __r = __cvtptr_->out(__st, __frm, __frm_end, __frm_nxt,
4052 __to, __to_end, __to_nxt);
4053 __cvtcount_ += __frm_nxt - __frm;
4054 if (__frm_nxt == __frm)
4056 __r = codecvt_base::error;
4058 else if (__r == codecvt_base::noconv)
4060 __bs.resize(__to - &__bs[0]);
4061 // This only gets executed if _Elem is char
4062 __bs.append((const char*)__frm, (const char*)__frm_end);
4064 __r = codecvt_base::ok;
4066 else if (__r == codecvt_base::ok)
4068 __bs.resize(__to_nxt - &__bs[0]);
4071 else if (__r == codecvt_base::partial)
4073 ptrdiff_t __s = __to_nxt - &__bs[0];
4074 __bs.resize(2 * __s);
4075 __to = &__bs[0] + __s;
4076 __to_end = &__bs[0] + __bs.size();
4079 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
4081 if (__r == codecvt_base::ok)
4083 size_t __s = __bs.size();
4084 __bs.resize(__bs.capacity());
4085 char* __to = &__bs[0] + __s;
4086 char* __to_end = __to + __bs.size();
4090 __r = __cvtptr_->unshift(__st, __to, __to_end, __to_nxt);
4091 if (__r == codecvt_base::noconv)
4093 __bs.resize(__to - &__bs[0]);
4094 __r = codecvt_base::ok;
4096 else if (__r == codecvt_base::ok)
4098 __bs.resize(__to_nxt - &__bs[0]);
4100 else if (__r == codecvt_base::partial)
4102 ptrdiff_t __sp = __to_nxt - &__bs[0];
4103 __bs.resize(2 * __sp);
4104 __to = &__bs[0] + __sp;
4105 __to_end = &__bs[0] + __bs.size();
4107 } while (__r == codecvt_base::partial);
4108 if (__r == codecvt_base::ok)
4112 #ifndef _LIBCPP_NO_EXCEPTIONS
4113 if (__byte_err_string_.empty())
4114 throw range_error("wstring_convert: to_bytes error");
4115 #endif // _LIBCPP_NO_EXCEPTIONS
4116 return __byte_err_string_;
4119 template <class _Codecvt, class _Elem = wchar_t, class _Tr = char_traits<_Elem> >
4120 class _LIBCPP_VISIBLE wbuffer_convert
4121 : public basic_streambuf<_Elem, _Tr>
4125 typedef _Elem char_type;
4126 typedef _Tr traits_type;
4127 typedef typename traits_type::int_type int_type;
4128 typedef typename traits_type::pos_type pos_type;
4129 typedef typename traits_type::off_type off_type;
4130 typedef typename _Codecvt::state_type state_type;
4134 const char* __extbufnext_;
4135 const char* __extbufend_;
4136 char __extbuf_min_[8];
4138 char_type* __intbuf_;
4140 streambuf* __bufptr_;
4143 ios_base::openmode __cm_;
4146 bool __always_noconv_;
4148 wbuffer_convert(const wbuffer_convert&);
4149 wbuffer_convert& operator=(const wbuffer_convert&);
4151 wbuffer_convert(streambuf* __bytebuf = 0, _Codecvt* __pcvt = new _Codecvt,
4152 state_type __state = state_type());
4155 _LIBCPP_INLINE_VISIBILITY
4156 streambuf* rdbuf() const {return __bufptr_;}
4157 _LIBCPP_INLINE_VISIBILITY
4158 streambuf* rdbuf(streambuf* __bytebuf)
4160 streambuf* __r = __bufptr_;
4161 __bufptr_ = __bytebuf;
4165 _LIBCPP_INLINE_VISIBILITY
4166 state_type state() const {return __st_;}
4169 virtual int_type underflow();
4170 virtual int_type pbackfail(int_type __c = traits_type::eof());
4171 virtual int_type overflow (int_type __c = traits_type::eof());
4172 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s,
4174 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
4175 ios_base::openmode __wch = ios_base::in | ios_base::out);
4176 virtual pos_type seekpos(pos_type __sp,
4177 ios_base::openmode __wch = ios_base::in | ios_base::out);
4182 void __write_mode();
4183 wbuffer_convert* __close();
4186 template <class _Codecvt, class _Elem, class _Tr>
4187 wbuffer_convert<_Codecvt, _Elem, _Tr>::
4188 wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt, state_type __state)
4195 __bufptr_(__bytebuf),
4201 __always_noconv_(__cv_ ? __cv_->always_noconv() : false)
4206 template <class _Codecvt, class _Elem, class _Tr>
4207 wbuffer_convert<_Codecvt, _Elem, _Tr>::~wbuffer_convert()
4212 delete [] __extbuf_;
4214 delete [] __intbuf_;
4217 template <class _Codecvt, class _Elem, class _Tr>
4218 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4219 wbuffer_convert<_Codecvt, _Elem, _Tr>::underflow()
4221 if (__cv_ == 0 || __bufptr_ == 0)
4222 return traits_type::eof();
4223 bool __initial = __read_mode();
4225 if (this->gptr() == 0)
4226 this->setg(&__1buf, &__1buf+1, &__1buf+1);
4227 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
4228 int_type __c = traits_type::eof();
4229 if (this->gptr() == this->egptr())
4231 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
4232 if (__always_noconv_)
4234 streamsize __nmemb = static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz);
4235 __nmemb = __bufptr_->sgetn((char*)this->eback() + __unget_sz, __nmemb);
4238 this->setg(this->eback(),
4239 this->eback() + __unget_sz,
4240 this->eback() + __unget_sz + __nmemb);
4241 __c = *this->gptr();
4246 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
4247 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
4248 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
4249 streamsize __nmemb = _VSTD::min(static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz),
4250 static_cast<streamsize>(__extbufend_ - __extbufnext_));
4251 codecvt_base::result __r;
4252 state_type __svs = __st_;
4253 streamsize __nr = __bufptr_->sgetn(const_cast<char*>(__extbufnext_), __nmemb);
4256 __extbufend_ = __extbufnext_ + __nr;
4258 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
4259 this->eback() + __unget_sz,
4260 this->egptr(), __inext);
4261 if (__r == codecvt_base::noconv)
4263 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)__extbufend_);
4264 __c = *this->gptr();
4266 else if (__inext != this->eback() + __unget_sz)
4268 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
4269 __c = *this->gptr();
4275 __c = *this->gptr();
4276 if (this->eback() == &__1buf)
4277 this->setg(0, 0, 0);
4281 template <class _Codecvt, class _Elem, class _Tr>
4282 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4283 wbuffer_convert<_Codecvt, _Elem, _Tr>::pbackfail(int_type __c)
4285 if (__cv_ != 0 && __bufptr_ != 0 && this->eback() < this->gptr())
4287 if (traits_type::eq_int_type(__c, traits_type::eof()))
4290 return traits_type::not_eof(__c);
4292 if (traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
4295 *this->gptr() = traits_type::to_char_type(__c);
4299 return traits_type::eof();
4302 template <class _Codecvt, class _Elem, class _Tr>
4303 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4304 wbuffer_convert<_Codecvt, _Elem, _Tr>::overflow(int_type __c)
4306 if (__cv_ == 0 || __bufptr_ == 0)
4307 return traits_type::eof();
4310 char_type* __pb_save = this->pbase();
4311 char_type* __epb_save = this->epptr();
4312 if (!traits_type::eq_int_type(__c, traits_type::eof()))
4314 if (this->pptr() == 0)
4315 this->setp(&__1buf, &__1buf+1);
4316 *this->pptr() = traits_type::to_char_type(__c);
4319 if (this->pptr() != this->pbase())
4321 if (__always_noconv_)
4323 streamsize __nmemb = static_cast<streamsize>(this->pptr() - this->pbase());
4324 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4325 return traits_type::eof();
4329 char* __extbe = __extbuf_;
4330 codecvt_base::result __r;
4333 const char_type* __e;
4334 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
4335 __extbuf_, __extbuf_ + __ebs_, __extbe);
4336 if (__e == this->pbase())
4337 return traits_type::eof();
4338 if (__r == codecvt_base::noconv)
4340 streamsize __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
4341 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4342 return traits_type::eof();
4344 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
4346 streamsize __nmemb = static_cast<size_t>(__extbe - __extbuf_);
4347 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4348 return traits_type::eof();
4349 if (__r == codecvt_base::partial)
4351 this->setp((char_type*)__e, this->pptr());
4352 this->pbump(this->epptr() - this->pbase());
4356 return traits_type::eof();
4357 } while (__r == codecvt_base::partial);
4359 this->setp(__pb_save, __epb_save);
4361 return traits_type::not_eof(__c);
4364 template <class _Codecvt, class _Elem, class _Tr>
4365 basic_streambuf<_Elem, _Tr>*
4366 wbuffer_convert<_Codecvt, _Elem, _Tr>::setbuf(char_type* __s, streamsize __n)
4368 this->setg(0, 0, 0);
4371 delete [] __extbuf_;
4373 delete [] __intbuf_;
4375 if (__ebs_ > sizeof(__extbuf_min_))
4377 if (__always_noconv_ && __s)
4379 __extbuf_ = (char*)__s;
4384 __extbuf_ = new char[__ebs_];
4390 __extbuf_ = __extbuf_min_;
4391 __ebs_ = sizeof(__extbuf_min_);
4394 if (!__always_noconv_)
4396 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
4397 if (__s && __ibs_ >= sizeof(__extbuf_min_))
4404 __intbuf_ = new char_type[__ibs_];
4417 template <class _Codecvt, class _Elem, class _Tr>
4418 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4419 wbuffer_convert<_Codecvt, _Elem, _Tr>::seekoff(off_type __off, ios_base::seekdir __way,
4420 ios_base::openmode __om)
4422 int __width = __cv_->encoding();
4423 if (__cv_ == 0 || __bufptr_ == 0 || (__width <= 0 && __off != 0) || sync())
4424 return pos_type(off_type(-1));
4425 // __width > 0 || __off == 0
4435 return pos_type(off_type(-1));
4437 pos_type __r = __bufptr_->pubseekoff(__width * __off, __way, __om);
4442 template <class _Codecvt, class _Elem, class _Tr>
4443 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4444 wbuffer_convert<_Codecvt, _Elem, _Tr>::seekpos(pos_type __sp, ios_base::openmode __wch)
4446 if (__cv_ == 0 || __bufptr_ == 0 || sync())
4447 return pos_type(off_type(-1));
4448 if (__bufptr_->pubseekpos(__sp, __wch) == pos_type(off_type(-1)))
4449 return pos_type(off_type(-1));
4453 template <class _Codecvt, class _Elem, class _Tr>
4455 wbuffer_convert<_Codecvt, _Elem, _Tr>::sync()
4457 if (__cv_ == 0 || __bufptr_ == 0)
4459 if (__cm_ & ios_base::out)
4461 if (this->pptr() != this->pbase())
4462 if (overflow() == traits_type::eof())
4464 codecvt_base::result __r;
4468 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
4469 streamsize __nmemb = static_cast<streamsize>(__extbe - __extbuf_);
4470 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4472 } while (__r == codecvt_base::partial);
4473 if (__r == codecvt_base::error)
4475 if (__bufptr_->pubsync())
4478 else if (__cm_ & ios_base::in)
4481 if (__always_noconv_)
4482 __c = this->egptr() - this->gptr();
4485 int __width = __cv_->encoding();
4486 __c = __extbufend_ - __extbufnext_;
4488 __c += __width * (this->egptr() - this->gptr());
4491 if (this->gptr() != this->egptr())
4493 reverse(this->gptr(), this->egptr());
4494 codecvt_base::result __r;
4495 const char_type* __e = this->gptr();
4499 __r = __cv_->out(__st_, __e, this->egptr(), __e,
4500 __extbuf_, __extbuf_ + __ebs_, __extbe);
4503 case codecvt_base::noconv:
4504 __c += this->egptr() - this->gptr();
4506 case codecvt_base::ok:
4507 case codecvt_base::partial:
4508 __c += __extbe - __extbuf_;
4513 } while (__r == codecvt_base::partial);
4517 if (__bufptr_->pubseekoff(-__c, ios_base::cur, __cm_) == pos_type(off_type(-1)))
4519 this->setg(0, 0, 0);
4525 template <class _Codecvt, class _Elem, class _Tr>
4527 wbuffer_convert<_Codecvt, _Elem, _Tr>::__read_mode()
4529 if (!(__cm_ & ios_base::in))
4532 if (__always_noconv_)
4533 this->setg((char_type*)__extbuf_,
4534 (char_type*)__extbuf_ + __ebs_,
4535 (char_type*)__extbuf_ + __ebs_);
4537 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
4538 __cm_ = ios_base::in;
4544 template <class _Codecvt, class _Elem, class _Tr>
4546 wbuffer_convert<_Codecvt, _Elem, _Tr>::__write_mode()
4548 if (!(__cm_ & ios_base::out))
4550 this->setg(0, 0, 0);
4551 if (__ebs_ > sizeof(__extbuf_min_))
4553 if (__always_noconv_)
4554 this->setp((char_type*)__extbuf_,
4555 (char_type*)__extbuf_ + (__ebs_ - 1));
4557 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
4561 __cm_ = ios_base::out;
4565 template <class _Codecvt, class _Elem, class _Tr>
4566 wbuffer_convert<_Codecvt, _Elem, _Tr>*
4567 wbuffer_convert<_Codecvt, _Elem, _Tr>::__close()
4569 wbuffer_convert* __rt = 0;
4570 if (__cv_ != 0 && __bufptr_ != 0)
4573 if ((__cm_ & ios_base::out) && sync())
4579 _LIBCPP_END_NAMESPACE_STD
4581 #endif // _LIBCPP_LOCALE