2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_LOCALE
11 #define _LIBCPP_LOCALE
27 static const category // values assigned here are for exposition only
35 all = collate | ctype | monetary | numeric | time | messages;
37 // construct/copy/destroy:
39 locale(const locale& other) noexcept;
40 explicit locale(const char* std_name);
41 explicit locale(const string& std_name);
42 locale(const locale& other, const char* std_name, category);
43 locale(const locale& other, const string& std_name, category);
44 template <class Facet> locale(const locale& other, Facet* f);
45 locale(const locale& other, const locale& one, category);
47 ~locale(); // not virtual
49 const locale& operator=(const locale& other) noexcept;
51 template <class Facet> locale combine(const locale& other) const;
54 basic_string<char> name() const;
55 bool operator==(const locale& other) const;
56 bool operator!=(const locale& other) const; // removed C++20
57 template <class charT, class Traits, class Allocator>
58 bool operator()(const basic_string<charT,Traits,Allocator>& s1,
59 const basic_string<charT,Traits,Allocator>& s2) const;
61 // global locale objects:
62 static locale global(const locale&);
63 static const locale& classic();
66 template <class Facet> const Facet& use_facet(const locale&);
67 template <class Facet> bool has_facet(const locale&) noexcept;
69 // 22.3.3, convenience interfaces:
70 template <class charT> bool isspace (charT c, const locale& loc);
71 template <class charT> bool isprint (charT c, const locale& loc);
72 template <class charT> bool iscntrl (charT c, const locale& loc);
73 template <class charT> bool isupper (charT c, const locale& loc);
74 template <class charT> bool islower (charT c, const locale& loc);
75 template <class charT> bool isalpha (charT c, const locale& loc);
76 template <class charT> bool isdigit (charT c, const locale& loc);
77 template <class charT> bool ispunct (charT c, const locale& loc);
78 template <class charT> bool isxdigit(charT c, const locale& loc);
79 template <class charT> bool isalnum (charT c, const locale& loc);
80 template <class charT> bool isgraph (charT c, const locale& loc);
81 template <class charT> charT toupper(charT c, const locale& loc);
82 template <class charT> charT tolower(charT c, const locale& loc);
84 template<class Codecvt, class Elem = wchar_t,
85 class Wide_alloc = allocator<Elem>,
86 class Byte_alloc = allocator<char>>
90 typedef basic_string<char, char_traits<char>, Byte_alloc> byte_string;
91 typedef basic_string<Elem, char_traits<Elem>, Wide_alloc> wide_string;
92 typedef typename Codecvt::state_type state_type;
93 typedef typename wide_string::traits_type::int_type int_type;
95 wstring_convert(Codecvt* pcvt = new Codecvt); // before C++14
96 explicit wstring_convert(Codecvt* pcvt = new Codecvt); // before C++20
97 wstring_convert() : wstring_convert(new Codecvt) {} // C++20
98 explicit wstring_convert(Codecvt* pcvt); // C++20
100 wstring_convert(Codecvt* pcvt, state_type state);
101 explicit wstring_convert(const byte_string& byte_err, // explicit in C++14
102 const wide_string& wide_err = wide_string());
103 wstring_convert(const wstring_convert&) = delete; // C++14
104 wstring_convert & operator=(const wstring_convert &) = delete; // C++14
107 wide_string from_bytes(char byte);
108 wide_string from_bytes(const char* ptr);
109 wide_string from_bytes(const byte_string& str);
110 wide_string from_bytes(const char* first, const char* last);
112 byte_string to_bytes(Elem wchar);
113 byte_string to_bytes(const Elem* wptr);
114 byte_string to_bytes(const wide_string& wstr);
115 byte_string to_bytes(const Elem* first, const Elem* last);
117 size_t converted() const; // noexcept in C++14
118 state_type state() const;
121 template <class Codecvt, class Elem = wchar_t, class Tr = char_traits<Elem>>
122 class wbuffer_convert
123 : public basic_streambuf<Elem, Tr>
126 typedef typename Tr::state_type state_type;
128 wbuffer_convert(streambuf* bytebuf = 0, Codecvt* pcvt = new Codecvt,
129 state_type state = state_type()); // before C++14
130 explicit wbuffer_convert(streambuf* bytebuf = nullptr, Codecvt* pcvt = new Codecvt,
131 state_type state = state_type()); // before C++20
132 wbuffer_convert() : wbuffer_convert(nullptr) {} // C++20
133 explicit wbuffer_convert(streambuf* bytebuf, Codecvt* pcvt = new Codecvt,
134 state_type state = state_type()); // C++20
136 wbuffer_convert(const wbuffer_convert&) = delete; // C++14
137 wbuffer_convert & operator=(const wbuffer_convert &) = delete; // C++14
138 ~wbuffer_convert(); // C++14
140 streambuf* rdbuf() const;
141 streambuf* rdbuf(streambuf* bytebuf);
143 state_type state() const;
146 // 22.4.1 and 22.4.1.3, ctype:
148 template <class charT> class ctype;
149 template <> class ctype<char>; // specialization
150 template <class charT> class ctype_byname;
151 template <> class ctype_byname<char>; // specialization
154 template <class internT, class externT, class stateT> class codecvt;
155 template <class internT, class externT, class stateT> class codecvt_byname;
157 // 22.4.2 and 22.4.3, numeric:
158 template <class charT, class InputIterator> class num_get;
159 template <class charT, class OutputIterator> class num_put;
160 template <class charT> class numpunct;
161 template <class charT> class numpunct_byname;
163 // 22.4.4, col lation:
164 template <class charT> class collate;
165 template <class charT> class collate_byname;
167 // 22.4.5, date and time:
169 template <class charT, class InputIterator> class time_get;
170 template <class charT, class InputIterator> class time_get_byname;
171 template <class charT, class OutputIterator> class time_put;
172 template <class charT, class OutputIterator> class time_put_byname;
176 template <class charT, class InputIterator> class money_get;
177 template <class charT, class OutputIterator> class money_put;
178 template <class charT, bool Intl> class moneypunct;
179 template <class charT, bool Intl> class moneypunct_byname;
181 // 22.4.7, message retrieval:
183 template <class charT> class messages;
184 template <class charT> class messages_byname;
190 #include <__algorithm/copy.h>
191 #include <__algorithm/equal.h>
192 #include <__algorithm/find.h>
193 #include <__algorithm/max.h>
194 #include <__algorithm/reverse.h>
195 #include <__algorithm/unwrap_iter.h>
196 #include <__assert> // all public C++ headers provide the assertion handler
198 #include <__iterator/access.h>
199 #include <__iterator/back_insert_iterator.h>
200 #include <__iterator/istreambuf_iterator.h>
201 #include <__iterator/ostreambuf_iterator.h>
203 #include <__memory/unique_ptr.h>
204 #include <__type_traits/make_unsigned.h>
215 // TODO: Fix __bsd_locale_defaults.h
216 // NOLINTBEGIN(libcpp-robust-against-adl)
218 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
219 // Most unix variants have catopen. These are the specific ones that don't.
220 # if !defined(__BIONIC__) && !defined(_NEWLIB_VERSION) && !defined(__EMSCRIPTEN__)
221 # define _LIBCPP_HAS_CATOPEN 1
222 # include <nl_types.h>
226 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
227 # include <__locale_dir/locale_base_api/bsd_locale_defaults.h>
229 # include <__locale_dir/locale_base_api/bsd_locale_fallbacks.h>
232 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
233 # pragma GCC system_header
237 #include <__undef_macros>
239 _LIBCPP_BEGIN_NAMESPACE_STD
241 #if defined(__APPLE__) || defined(__FreeBSD__)
242 # define _LIBCPP_GET_C_LOCALE 0
243 #elif defined(__NetBSD__)
244 # define _LIBCPP_GET_C_LOCALE LC_C_LOCALE
246 # define _LIBCPP_GET_C_LOCALE __cloc()
247 // Get the C locale object
248 _LIBCPP_EXPORTED_FROM_ABI locale_t __cloc();
249 # define __cloc_defined
253 // Scans [__b, __e) until a match is found in the basic_strings range
254 // [__kb, __ke) or until it can be shown that there is no match in [__kb, __ke).
255 // __b will be incremented (visibly), consuming CharT until a match is found
256 // or proved to not exist. A keyword may be "", in which will match anything.
257 // If one keyword is a prefix of another, and the next CharT in the input
258 // might match another keyword, the algorithm will attempt to find the longest
259 // matching keyword. If the longer matching keyword ends up not matching, then
260 // no keyword match is found. If no keyword match is found, __ke is returned
261 // and failbit is set in __err.
262 // Else an iterator pointing to the matching keyword is found. If more than
263 // one keyword matches, an iterator to the first matching keyword is returned.
264 // If on exit __b == __e, eofbit is set in __err. If __case_sensitive is false,
265 // __ct is used to force to lower case before comparing characters.
267 // Keywords: "a", "abb"
268 // If the input is "a", the first keyword matches and eofbit is set.
269 // If the input is "abc", no match is found and "ab" are consumed.
270 template <class _InputIterator, class _ForwardIterator, class _Ctype>
271 _LIBCPP_HIDE_FROM_ABI _ForwardIterator __scan_keyword(
274 _ForwardIterator __kb,
275 _ForwardIterator __ke,
277 ios_base::iostate& __err,
278 bool __case_sensitive = true) {
279 typedef typename iterator_traits<_InputIterator>::value_type _CharT;
280 size_t __nkw = static_cast<size_t>(std::distance(__kb, __ke));
281 const unsigned char __doesnt_match = '\0';
282 const unsigned char __might_match = '\1';
283 const unsigned char __does_match = '\2';
284 unsigned char __statbuf[100];
285 unsigned char* __status = __statbuf;
286 unique_ptr<unsigned char, void (*)(void*)> __stat_hold(nullptr, free);
287 if (__nkw > sizeof(__statbuf)) {
288 __status = (unsigned char*)malloc(__nkw);
289 if (__status == nullptr)
291 __stat_hold.reset(__status);
293 size_t __n_might_match = __nkw; // At this point, any keyword might match
294 size_t __n_does_match = 0; // but none of them definitely do
295 // Initialize all statuses to __might_match, except for "" keywords are __does_match
296 unsigned char* __st = __status;
297 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void)++__st) {
299 *__st = __might_match;
301 *__st = __does_match;
306 // While there might be a match, test keywords against the next CharT
307 for (size_t __indx = 0; __b != __e && __n_might_match > 0; ++__indx) {
308 // Peek at the next CharT but don't consume it
310 if (!__case_sensitive)
311 __c = __ct.toupper(__c);
312 bool __consume = false;
313 // For each keyword which might match, see if the __indx character is __c
314 // If a match if found, consume __c
315 // If a match is found, and that is the last character in the keyword,
316 // then that keyword matches.
317 // If the keyword doesn't match this character, then change the keyword
320 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void)++__st) {
321 if (*__st == __might_match) {
322 _CharT __kc = (*__ky)[__indx];
323 if (!__case_sensitive)
324 __kc = __ct.toupper(__kc);
327 if (__ky->size() == __indx + 1) {
328 *__st = __does_match;
333 *__st = __doesnt_match;
338 // consume if we matched a character
341 // If we consumed a character and there might be a matched keyword that
342 // was marked matched on a previous iteration, then such keywords
343 // which are now marked as not matching.
344 if (__n_might_match + __n_does_match > 1) {
346 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void)++__st) {
347 if (*__st == __does_match && __ky->size() != __indx + 1) {
348 *__st = __doesnt_match;
355 // We've exited the loop because we hit eof and/or we have no more "might matches".
357 __err |= ios_base::eofbit;
358 // Return the first matching result
359 for (__st = __status; __kb != __ke; ++__kb, (void)++__st)
360 if (*__st == __does_match)
363 __err |= ios_base::failbit;
367 struct _LIBCPP_EXPORTED_FROM_ABI __num_get_base {
368 static const int __num_get_buf_sz = 40;
370 static int __get_base(ios_base&);
371 static const char __src[33];
374 _LIBCPP_EXPORTED_FROM_ABI void
375 __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end, ios_base::iostate& __err);
377 template <class _CharT>
378 struct __num_get : protected __num_get_base {
379 static string __stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point, _CharT& __thousands_sep);
381 static int __stage2_float_loop(
387 _CharT __decimal_point,
388 _CharT __thousands_sep,
389 const string& __grouping,
394 #ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
395 static string __stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep);
396 static int __stage2_int_loop(
402 _CharT __thousands_sep,
403 const string& __grouping,
409 static string __stage2_int_prep(ios_base& __iob, _CharT& __thousands_sep) {
410 locale __loc = __iob.getloc();
411 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
412 __thousands_sep = __np.thousands_sep();
413 return __np.grouping();
416 const _CharT* __do_widen(ios_base& __iob, _CharT* __atoms) const { return __do_widen_p(__iob, __atoms); }
418 static int __stage2_int_loop(
424 _CharT __thousands_sep,
425 const string& __grouping,
428 const _CharT* __atoms);
431 template <typename _Tp>
432 const _Tp* __do_widen_p(ios_base& __iob, _Tp* __atoms) const {
433 locale __loc = __iob.getloc();
434 use_facet<ctype<_Tp> >(__loc).widen(__src, __src + 26, __atoms);
438 const char* __do_widen_p(ios_base& __iob, char* __atoms) const {
446 #ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
447 template <class _CharT>
448 string __num_get<_CharT>::__stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep) {
449 locale __loc = __iob.getloc();
450 std::use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 26, __atoms);
451 const numpunct<_CharT>& __np = std::use_facet<numpunct<_CharT> >(__loc);
452 __thousands_sep = __np.thousands_sep();
453 return __np.grouping();
457 template <class _CharT>
458 string __num_get<_CharT>::__stage2_float_prep(
459 ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point, _CharT& __thousands_sep) {
460 locale __loc = __iob.getloc();
461 std::use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 32, __atoms);
462 const numpunct<_CharT>& __np = std::use_facet<numpunct<_CharT> >(__loc);
463 __decimal_point = __np.decimal_point();
464 __thousands_sep = __np.thousands_sep();
465 return __np.grouping();
468 template <class _CharT>
470 #ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
471 __num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
472 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
473 unsigned* __g, unsigned*& __g_end, _CharT* __atoms)
475 __num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
476 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
477 unsigned* __g, unsigned*& __g_end, const _CharT* __atoms)
481 if (__a_end == __a && (__ct == __atoms[24] || __ct == __atoms[25])) {
482 *__a_end++ = __ct == __atoms[24] ? '+' : '-';
486 if (__grouping.size() != 0 && __ct == __thousands_sep) {
487 if (__g_end - __g < __num_get_buf_sz) {
493 ptrdiff_t __f = std::find(__atoms, __atoms + 26, __ct) - __atoms;
505 if (__a_end != __a && __a_end - __a <= 2 && __a_end[-1] == '0') {
507 *__a_end++ = __src[__f];
512 *__a_end++ = __src[__f];
517 template <class _CharT>
518 int __num_get<_CharT>::__stage2_float_loop(
524 _CharT __decimal_point,
525 _CharT __thousands_sep,
526 const string& __grouping,
531 if (__ct == __decimal_point) {
536 if (__grouping.size() != 0 && __g_end - __g < __num_get_buf_sz)
540 if (__ct == __thousands_sep && __grouping.size() != 0) {
543 if (__g_end - __g < __num_get_buf_sz) {
549 ptrdiff_t __f = std::find(__atoms, __atoms + 32, __ct) - __atoms;
552 char __x = __src[__f];
553 if (__x == '-' || __x == '+') {
554 if (__a_end == __a || (std::toupper(__a_end[-1]) == std::toupper(__exp))) {
560 if (__x == 'x' || __x == 'X')
562 else if (std::toupper(__x) == __exp) {
563 __exp = std::tolower(__exp);
566 if (__grouping.size() != 0 && __g_end - __g < __num_get_buf_sz)
577 extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<char>;
578 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
579 extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<wchar_t>;
582 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
583 class _LIBCPP_TEMPLATE_VIS num_get : public locale::facet, private __num_get<_CharT> {
585 typedef _CharT char_type;
586 typedef _InputIterator iter_type;
588 _LIBCPP_HIDE_FROM_ABI explicit num_get(size_t __refs = 0) : locale::facet(__refs) {}
590 _LIBCPP_HIDE_FROM_ABI iter_type
591 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, bool& __v) const {
592 return do_get(__b, __e, __iob, __err, __v);
595 _LIBCPP_HIDE_FROM_ABI iter_type
596 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long& __v) const {
597 return do_get(__b, __e, __iob, __err, __v);
600 _LIBCPP_HIDE_FROM_ABI iter_type
601 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long long& __v) const {
602 return do_get(__b, __e, __iob, __err, __v);
605 _LIBCPP_HIDE_FROM_ABI iter_type
606 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned short& __v) const {
607 return do_get(__b, __e, __iob, __err, __v);
610 _LIBCPP_HIDE_FROM_ABI iter_type
611 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned int& __v) const {
612 return do_get(__b, __e, __iob, __err, __v);
615 _LIBCPP_HIDE_FROM_ABI iter_type
616 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned long& __v) const {
617 return do_get(__b, __e, __iob, __err, __v);
620 _LIBCPP_HIDE_FROM_ABI iter_type
621 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned long long& __v) const {
622 return do_get(__b, __e, __iob, __err, __v);
625 _LIBCPP_HIDE_FROM_ABI iter_type
626 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, float& __v) const {
627 return do_get(__b, __e, __iob, __err, __v);
630 _LIBCPP_HIDE_FROM_ABI iter_type
631 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, double& __v) const {
632 return do_get(__b, __e, __iob, __err, __v);
635 _LIBCPP_HIDE_FROM_ABI iter_type
636 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long double& __v) const {
637 return do_get(__b, __e, __iob, __err, __v);
640 _LIBCPP_HIDE_FROM_ABI iter_type
641 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, void*& __v) const {
642 return do_get(__b, __e, __iob, __err, __v);
645 static locale::id id;
648 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~num_get() override {}
651 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS iter_type
652 __do_get_floating_point(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, _Fp& __v) const;
654 template <class _Signed>
655 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS iter_type
656 __do_get_signed(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, _Signed& __v) const;
658 template <class _Unsigned>
659 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS iter_type
660 __do_get_unsigned(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, _Unsigned& __v) const;
662 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, bool& __v) const;
664 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long& __v) const {
665 return this->__do_get_signed(__b, __e, __iob, __err, __v);
669 do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long long& __v) const {
670 return this->__do_get_signed(__b, __e, __iob, __err, __v);
674 do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned short& __v) const {
675 return this->__do_get_unsigned(__b, __e, __iob, __err, __v);
679 do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned int& __v) const {
680 return this->__do_get_unsigned(__b, __e, __iob, __err, __v);
684 do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned long& __v) const {
685 return this->__do_get_unsigned(__b, __e, __iob, __err, __v);
689 do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned long long& __v) const {
690 return this->__do_get_unsigned(__b, __e, __iob, __err, __v);
693 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, float& __v) const {
694 return this->__do_get_floating_point(__b, __e, __iob, __err, __v);
697 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, double& __v) const {
698 return this->__do_get_floating_point(__b, __e, __iob, __err, __v);
702 do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long double& __v) const {
703 return this->__do_get_floating_point(__b, __e, __iob, __err, __v);
706 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, void*& __v) const;
709 template <class _CharT, class _InputIterator>
710 locale::id num_get<_CharT, _InputIterator>::id;
713 _LIBCPP_HIDE_FROM_ABI _Tp
714 __num_get_signed_integral(const char* __a, const char* __a_end, ios_base::iostate& __err, int __base) {
715 if (__a != __a_end) {
716 __libcpp_remove_reference_t<decltype(errno)> __save_errno = errno;
719 long long __ll = strtoll_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
720 __libcpp_remove_reference_t<decltype(errno)> __current_errno = errno;
721 if (__current_errno == 0)
722 errno = __save_errno;
723 if (__p2 != __a_end) {
724 __err = ios_base::failbit;
726 } else if (__current_errno == ERANGE || __ll < numeric_limits<_Tp>::min() || numeric_limits<_Tp>::max() < __ll) {
727 __err = ios_base::failbit;
729 return numeric_limits<_Tp>::max();
731 return numeric_limits<_Tp>::min();
733 return static_cast<_Tp>(__ll);
735 __err = ios_base::failbit;
740 _LIBCPP_HIDE_FROM_ABI _Tp
741 __num_get_unsigned_integral(const char* __a, const char* __a_end, ios_base::iostate& __err, int __base) {
742 if (__a != __a_end) {
743 const bool __negate = *__a == '-';
744 if (__negate && ++__a == __a_end) {
745 __err = ios_base::failbit;
748 __libcpp_remove_reference_t<decltype(errno)> __save_errno = errno;
751 unsigned long long __ll = strtoull_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
752 __libcpp_remove_reference_t<decltype(errno)> __current_errno = errno;
753 if (__current_errno == 0)
754 errno = __save_errno;
755 if (__p2 != __a_end) {
756 __err = ios_base::failbit;
758 } else if (__current_errno == ERANGE || numeric_limits<_Tp>::max() < __ll) {
759 __err = ios_base::failbit;
760 return numeric_limits<_Tp>::max();
762 _Tp __res = static_cast<_Tp>(__ll);
767 __err = ios_base::failbit;
772 _LIBCPP_HIDE_FROM_ABI _Tp __do_strtod(const char* __a, char** __p2);
775 inline _LIBCPP_HIDE_FROM_ABI float __do_strtod<float>(const char* __a, char** __p2) {
776 return strtof_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
780 inline _LIBCPP_HIDE_FROM_ABI double __do_strtod<double>(const char* __a, char** __p2) {
781 return strtod_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
785 inline _LIBCPP_HIDE_FROM_ABI long double __do_strtod<long double>(const char* __a, char** __p2) {
786 return strtold_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
790 _LIBCPP_HIDE_FROM_ABI _Tp __num_get_float(const char* __a, const char* __a_end, ios_base::iostate& __err) {
791 if (__a != __a_end) {
792 __libcpp_remove_reference_t<decltype(errno)> __save_errno = errno;
795 _Tp __ld = std::__do_strtod<_Tp>(__a, &__p2);
796 __libcpp_remove_reference_t<decltype(errno)> __current_errno = errno;
797 if (__current_errno == 0)
798 errno = __save_errno;
799 if (__p2 != __a_end) {
800 __err = ios_base::failbit;
802 } else if (__current_errno == ERANGE)
803 __err = ios_base::failbit;
806 __err = ios_base::failbit;
810 template <class _CharT, class _InputIterator>
811 _InputIterator num_get<_CharT, _InputIterator>::do_get(
812 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, bool& __v) const {
813 if ((__iob.flags() & ios_base::boolalpha) == 0) {
815 __b = do_get(__b, __e, __iob, __err, __lv);
825 __err = ios_base::failbit;
830 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__iob.getloc());
831 const numpunct<_CharT>& __np = std::use_facet<numpunct<_CharT> >(__iob.getloc());
832 typedef typename numpunct<_CharT>::string_type string_type;
833 const string_type __names[2] = {__np.truename(), __np.falsename()};
834 const string_type* __i = std::__scan_keyword(__b, __e, __names, __names + 2, __ct, __err);
835 __v = __i == __names;
841 template <class _CharT, class _InputIterator>
842 template <class _Signed>
843 _InputIterator num_get<_CharT, _InputIterator>::__do_get_signed(
844 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, _Signed& __v) const {
846 int __base = this->__get_base(__iob);
848 char_type __thousands_sep;
849 const int __atoms_size = 26;
850 #ifdef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
851 char_type __atoms1[__atoms_size];
852 const char_type* __atoms = this->__do_widen(__iob, __atoms1);
853 string __grouping = this->__stage2_int_prep(__iob, __thousands_sep);
855 char_type __atoms[__atoms_size];
856 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
859 __buf.resize(__buf.capacity());
860 char* __a = &__buf[0];
862 unsigned __g[__num_get_base::__num_get_buf_sz];
863 unsigned* __g_end = __g;
865 for (; __b != __e; ++__b) {
866 if (__a_end == __a + __buf.size()) {
867 size_t __tmp = __buf.size();
868 __buf.resize(2 * __buf.size());
869 __buf.resize(__buf.capacity());
871 __a_end = __a + __tmp;
873 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc, __thousands_sep, __grouping, __g, __g_end, __atoms))
876 if (__grouping.size() != 0 && __g_end - __g < __num_get_base::__num_get_buf_sz)
879 __v = std::__num_get_signed_integral<_Signed>(__a, __a_end, __err, __base);
880 // Digit grouping checked
881 __check_grouping(__grouping, __g, __g_end, __err);
884 __err |= ios_base::eofbit;
890 template <class _CharT, class _InputIterator>
891 template <class _Unsigned>
892 _InputIterator num_get<_CharT, _InputIterator>::__do_get_unsigned(
893 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, _Unsigned& __v) const {
895 int __base = this->__get_base(__iob);
897 char_type __thousands_sep;
898 const int __atoms_size = 26;
899 #ifdef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
900 char_type __atoms1[__atoms_size];
901 const char_type* __atoms = this->__do_widen(__iob, __atoms1);
902 string __grouping = this->__stage2_int_prep(__iob, __thousands_sep);
904 char_type __atoms[__atoms_size];
905 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
908 __buf.resize(__buf.capacity());
909 char* __a = &__buf[0];
911 unsigned __g[__num_get_base::__num_get_buf_sz];
912 unsigned* __g_end = __g;
914 for (; __b != __e; ++__b) {
915 if (__a_end == __a + __buf.size()) {
916 size_t __tmp = __buf.size();
917 __buf.resize(2 * __buf.size());
918 __buf.resize(__buf.capacity());
920 __a_end = __a + __tmp;
922 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc, __thousands_sep, __grouping, __g, __g_end, __atoms))
925 if (__grouping.size() != 0 && __g_end - __g < __num_get_base::__num_get_buf_sz)
928 __v = std::__num_get_unsigned_integral<_Unsigned>(__a, __a_end, __err, __base);
929 // Digit grouping checked
930 __check_grouping(__grouping, __g, __g_end, __err);
933 __err |= ios_base::eofbit;
939 template <class _CharT, class _InputIterator>
941 _InputIterator num_get<_CharT, _InputIterator>::__do_get_floating_point(
942 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, _Fp& __v) const {
943 // Stage 1, nothing to do
945 char_type __atoms[32];
946 char_type __decimal_point;
947 char_type __thousands_sep;
948 string __grouping = this->__stage2_float_prep(__iob, __atoms, __decimal_point, __thousands_sep);
950 __buf.resize(__buf.capacity());
951 char* __a = &__buf[0];
953 unsigned __g[__num_get_base::__num_get_buf_sz];
954 unsigned* __g_end = __g;
956 bool __in_units = true;
958 for (; __b != __e; ++__b) {
959 if (__a_end == __a + __buf.size()) {
960 size_t __tmp = __buf.size();
961 __buf.resize(2 * __buf.size());
962 __buf.resize(__buf.capacity());
964 __a_end = __a + __tmp;
966 if (this->__stage2_float_loop(
981 if (__grouping.size() != 0 && __in_units && __g_end - __g < __num_get_base::__num_get_buf_sz)
984 __v = std::__num_get_float<_Fp>(__a, __a_end, __err);
985 // Digit grouping checked
986 __check_grouping(__grouping, __g, __g_end, __err);
989 __err |= ios_base::eofbit;
993 template <class _CharT, class _InputIterator>
994 _InputIterator num_get<_CharT, _InputIterator>::do_get(
995 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, void*& __v) const {
999 char_type __atoms[26];
1000 char_type __thousands_sep = char_type();
1002 std::use_facet<ctype<_CharT> >(__iob.getloc()).widen(__num_get_base::__src, __num_get_base::__src + 26, __atoms);
1004 __buf.resize(__buf.capacity());
1005 char* __a = &__buf[0];
1006 char* __a_end = __a;
1007 unsigned __g[__num_get_base::__num_get_buf_sz];
1008 unsigned* __g_end = __g;
1010 for (; __b != __e; ++__b) {
1011 if (__a_end == __a + __buf.size()) {
1012 size_t __tmp = __buf.size();
1013 __buf.resize(2 * __buf.size());
1014 __buf.resize(__buf.capacity());
1016 __a_end = __a + __tmp;
1018 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc, __thousands_sep, __grouping, __g, __g_end, __atoms))
1022 __buf.resize(__a_end - __a);
1023 if (__libcpp_sscanf_l(__buf.c_str(), _LIBCPP_GET_C_LOCALE, "%p", &__v) != 1)
1024 __err = ios_base::failbit;
1027 __err |= ios_base::eofbit;
1031 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<char>;
1032 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1033 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<wchar_t>;
1036 struct _LIBCPP_EXPORTED_FROM_ABI __num_put_base {
1038 static void __format_int(char* __fmt, const char* __len, bool __signd, ios_base::fmtflags __flags);
1039 static bool __format_float(char* __fmt, const char* __len, ios_base::fmtflags __flags);
1040 static char* __identify_padding(char* __nb, char* __ne, const ios_base& __iob);
1043 template <class _CharT>
1044 struct __num_put : protected __num_put_base {
1045 static void __widen_and_group_int(
1046 char* __nb, char* __np, char* __ne, _CharT* __ob, _CharT*& __op, _CharT*& __oe, const locale& __loc);
1047 static void __widen_and_group_float(
1048 char* __nb, char* __np, char* __ne, _CharT* __ob, _CharT*& __op, _CharT*& __oe, const locale& __loc);
1051 template <class _CharT>
1052 void __num_put<_CharT>::__widen_and_group_int(
1053 char* __nb, char* __np, char* __ne, _CharT* __ob, _CharT*& __op, _CharT*& __oe, const locale& __loc) {
1054 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__loc);
1055 const numpunct<_CharT>& __npt = std::use_facet<numpunct<_CharT> >(__loc);
1056 string __grouping = __npt.grouping();
1057 if (__grouping.empty()) {
1058 __ct.widen(__nb, __ne, __ob);
1059 __oe = __ob + (__ne - __nb);
1063 if (*__nf == '-' || *__nf == '+')
1064 *__oe++ = __ct.widen(*__nf++);
1065 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' || __nf[1] == 'X')) {
1066 *__oe++ = __ct.widen(*__nf++);
1067 *__oe++ = __ct.widen(*__nf++);
1069 std::reverse(__nf, __ne);
1070 _CharT __thousands_sep = __npt.thousands_sep();
1073 for (char* __p = __nf; __p < __ne; ++__p) {
1074 if (static_cast<unsigned>(__grouping[__dg]) > 0 && __dc == static_cast<unsigned>(__grouping[__dg])) {
1075 *__oe++ = __thousands_sep;
1077 if (__dg < __grouping.size() - 1)
1080 *__oe++ = __ct.widen(*__p);
1083 std::reverse(__ob + (__nf - __nb), __oe);
1088 __op = __ob + (__np - __nb);
1091 template <class _CharT>
1092 void __num_put<_CharT>::__widen_and_group_float(
1093 char* __nb, char* __np, char* __ne, _CharT* __ob, _CharT*& __op, _CharT*& __oe, const locale& __loc) {
1094 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__loc);
1095 const numpunct<_CharT>& __npt = std::use_facet<numpunct<_CharT> >(__loc);
1096 string __grouping = __npt.grouping();
1099 if (*__nf == '-' || *__nf == '+')
1100 *__oe++ = __ct.widen(*__nf++);
1102 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' || __nf[1] == 'X')) {
1103 *__oe++ = __ct.widen(*__nf++);
1104 *__oe++ = __ct.widen(*__nf++);
1105 for (__ns = __nf; __ns < __ne; ++__ns)
1106 if (!isxdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
1109 for (__ns = __nf; __ns < __ne; ++__ns)
1110 if (!isdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
1113 if (__grouping.empty()) {
1114 __ct.widen(__nf, __ns, __oe);
1115 __oe += __ns - __nf;
1117 std::reverse(__nf, __ns);
1118 _CharT __thousands_sep = __npt.thousands_sep();
1121 for (char* __p = __nf; __p < __ns; ++__p) {
1122 if (__grouping[__dg] > 0 && __dc == static_cast<unsigned>(__grouping[__dg])) {
1123 *__oe++ = __thousands_sep;
1125 if (__dg < __grouping.size() - 1)
1128 *__oe++ = __ct.widen(*__p);
1131 std::reverse(__ob + (__nf - __nb), __oe);
1133 for (__nf = __ns; __nf < __ne; ++__nf) {
1135 *__oe++ = __npt.decimal_point();
1139 *__oe++ = __ct.widen(*__nf);
1141 __ct.widen(__nf, __ne, __oe);
1142 __oe += __ne - __nf;
1146 __op = __ob + (__np - __nb);
1149 extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<char>;
1150 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1151 extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<wchar_t>;
1154 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
1155 class _LIBCPP_TEMPLATE_VIS num_put : public locale::facet, private __num_put<_CharT> {
1157 typedef _CharT char_type;
1158 typedef _OutputIterator iter_type;
1160 _LIBCPP_HIDE_FROM_ABI explicit num_put(size_t __refs = 0) : locale::facet(__refs) {}
1162 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, bool __v) const {
1163 return do_put(__s, __iob, __fl, __v);
1166 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, long __v) const {
1167 return do_put(__s, __iob, __fl, __v);
1170 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, long long __v) const {
1171 return do_put(__s, __iob, __fl, __v);
1174 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long __v) const {
1175 return do_put(__s, __iob, __fl, __v);
1178 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long long __v) const {
1179 return do_put(__s, __iob, __fl, __v);
1182 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, double __v) const {
1183 return do_put(__s, __iob, __fl, __v);
1186 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, long double __v) const {
1187 return do_put(__s, __iob, __fl, __v);
1190 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, const void* __v) const {
1191 return do_put(__s, __iob, __fl, __v);
1194 static locale::id id;
1197 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~num_put() override {}
1199 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, bool __v) const;
1200 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, long __v) const;
1201 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, long long __v) const;
1202 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long) const;
1203 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long long) const;
1204 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, double __v) const;
1205 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, long double __v) const;
1206 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, const void* __v) const;
1208 template <class _Integral>
1209 _LIBCPP_HIDE_FROM_ABI inline _OutputIterator
1210 __do_put_integral(iter_type __s, ios_base& __iob, char_type __fl, _Integral __v, char const* __len) const;
1212 template <class _Float>
1213 _LIBCPP_HIDE_FROM_ABI inline _OutputIterator
1214 __do_put_floating_point(iter_type __s, ios_base& __iob, char_type __fl, _Float __v, char const* __len) const;
1217 template <class _CharT, class _OutputIterator>
1218 locale::id num_put<_CharT, _OutputIterator>::id;
1220 template <class _CharT, class _OutputIterator>
1221 _LIBCPP_HIDE_FROM_ABI _OutputIterator __pad_and_output(
1222 _OutputIterator __s, const _CharT* __ob, const _CharT* __op, const _CharT* __oe, ios_base& __iob, _CharT __fl) {
1223 streamsize __sz = __oe - __ob;
1224 streamsize __ns = __iob.width();
1229 for (; __ob < __op; ++__ob, ++__s)
1231 for (; __ns; --__ns, ++__s)
1233 for (; __ob < __oe; ++__ob, ++__s)
1239 template <class _CharT, class _Traits>
1240 _LIBCPP_HIDE_FROM_ABI ostreambuf_iterator<_CharT, _Traits> __pad_and_output(
1241 ostreambuf_iterator<_CharT, _Traits> __s,
1247 if (__s.__sbuf_ == nullptr)
1249 streamsize __sz = __oe - __ob;
1250 streamsize __ns = __iob.width();
1255 streamsize __np = __op - __ob;
1257 if (__s.__sbuf_->sputn(__ob, __np) != __np) {
1258 __s.__sbuf_ = nullptr;
1263 basic_string<_CharT, _Traits> __sp(__ns, __fl);
1264 if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns) {
1265 __s.__sbuf_ = nullptr;
1271 if (__s.__sbuf_->sputn(__op, __np) != __np) {
1272 __s.__sbuf_ = nullptr;
1280 template <class _CharT, class _OutputIterator>
1282 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, bool __v) const {
1283 if ((__iob.flags() & ios_base::boolalpha) == 0)
1284 return do_put(__s, __iob, __fl, (unsigned long)__v);
1285 const numpunct<char_type>& __np = std::use_facet<numpunct<char_type> >(__iob.getloc());
1286 typedef typename numpunct<char_type>::string_type string_type;
1287 string_type __nm = __v ? __np.truename() : __np.falsename();
1288 for (typename string_type::iterator __i = __nm.begin(); __i != __nm.end(); ++__i, ++__s)
1293 template <class _CharT, class _OutputIterator>
1294 template <class _Integral>
1295 _LIBCPP_HIDE_FROM_ABI inline _OutputIterator num_put<_CharT, _OutputIterator>::__do_put_integral(
1296 iter_type __s, ios_base& __iob, char_type __fl, _Integral __v, char const* __len) const {
1297 // Stage 1 - Get number in narrow char
1298 char __fmt[8] = {'%', 0};
1299 this->__format_int(__fmt + 1, __len, is_signed<_Integral>::value, __iob.flags());
1300 // Worst case is octal, with showbase enabled. Note that octal is always
1301 // printed as an unsigned value.
1302 using _Unsigned = typename make_unsigned<_Integral>::type;
1303 _LIBCPP_CONSTEXPR const unsigned __nbuf =
1304 (numeric_limits<_Unsigned>::digits / 3) // 1 char per 3 bits
1305 + ((numeric_limits<_Unsigned>::digits % 3) != 0) // round up
1306 + 2; // base prefix + terminating null character
1308 _LIBCPP_DIAGNOSTIC_PUSH
1309 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
1310 _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
1311 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1312 _LIBCPP_DIAGNOSTIC_POP
1313 char* __ne = __nar + __nc;
1314 char* __np = this->__identify_padding(__nar, __ne, __iob);
1315 // Stage 2 - Widen __nar while adding thousands separators
1316 char_type __o[2 * (__nbuf - 1) - 1];
1317 char_type* __op; // pad here
1318 char_type* __oe; // end of output
1319 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1320 // [__o, __oe) contains thousands_sep'd wide number
1322 return std::__pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1325 template <class _CharT, class _OutputIterator>
1327 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, long __v) const {
1328 return this->__do_put_integral(__s, __iob, __fl, __v, "l");
1331 template <class _CharT, class _OutputIterator>
1333 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, long long __v) const {
1334 return this->__do_put_integral(__s, __iob, __fl, __v, "ll");
1337 template <class _CharT, class _OutputIterator>
1339 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long __v) const {
1340 return this->__do_put_integral(__s, __iob, __fl, __v, "l");
1343 template <class _CharT, class _OutputIterator>
1345 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long long __v) const {
1346 return this->__do_put_integral(__s, __iob, __fl, __v, "ll");
1349 template <class _CharT, class _OutputIterator>
1350 template <class _Float>
1351 _LIBCPP_HIDE_FROM_ABI inline _OutputIterator num_put<_CharT, _OutputIterator>::__do_put_floating_point(
1352 iter_type __s, ios_base& __iob, char_type __fl, _Float __v, char const* __len) const {
1353 // Stage 1 - Get number in narrow char
1354 char __fmt[8] = {'%', 0};
1355 bool __specify_precision = this->__format_float(__fmt + 1, __len, __iob.flags());
1356 const unsigned __nbuf = 30;
1360 _LIBCPP_DIAGNOSTIC_PUSH
1361 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
1362 _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
1363 if (__specify_precision)
1364 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
1366 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1367 unique_ptr<char, void (*)(void*)> __nbh(nullptr, free);
1368 if (__nc > static_cast<int>(__nbuf - 1)) {
1369 if (__specify_precision)
1370 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
1372 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1374 __throw_bad_alloc();
1377 _LIBCPP_DIAGNOSTIC_POP
1378 char* __ne = __nb + __nc;
1379 char* __np = this->__identify_padding(__nb, __ne, __iob);
1380 // Stage 2 - Widen __nar while adding thousands separators
1381 char_type __o[2 * (__nbuf - 1) - 1];
1382 char_type* __ob = __o;
1383 unique_ptr<char_type, void (*)(void*)> __obh(0, free);
1384 if (__nb != __nar) {
1385 __ob = (char_type*)malloc(2 * static_cast<size_t>(__nc) * sizeof(char_type));
1387 __throw_bad_alloc();
1390 char_type* __op; // pad here
1391 char_type* __oe; // end of output
1392 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1393 // [__o, __oe) contains thousands_sep'd wide number
1395 __s = std::__pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1399 template <class _CharT, class _OutputIterator>
1401 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, double __v) const {
1402 return this->__do_put_floating_point(__s, __iob, __fl, __v, "");
1405 template <class _CharT, class _OutputIterator>
1407 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, long double __v) const {
1408 return this->__do_put_floating_point(__s, __iob, __fl, __v, "L");
1411 template <class _CharT, class _OutputIterator>
1413 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, const void* __v) const {
1414 // Stage 1 - Get pointer in narrow char
1415 const unsigned __nbuf = 20;
1417 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, "%p", __v);
1418 char* __ne = __nar + __nc;
1419 char* __np = this->__identify_padding(__nar, __ne, __iob);
1420 // Stage 2 - Widen __nar
1421 char_type __o[2 * (__nbuf - 1) - 1];
1422 char_type* __op; // pad here
1423 char_type* __oe; // end of output
1424 const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
1425 __ct.widen(__nar, __ne, __o);
1426 __oe = __o + (__ne - __nar);
1430 __op = __o + (__np - __nar);
1431 // [__o, __oe) contains wide number
1433 return std::__pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1436 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<char>;
1437 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1438 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<wchar_t>;
1441 template <class _CharT, class _InputIterator>
1442 _LIBCPP_HIDE_FROM_ABI int __get_up_to_n_digits(
1443 _InputIterator& __b, _InputIterator __e, ios_base::iostate& __err, const ctype<_CharT>& __ct, int __n) {
1444 // Precondition: __n >= 1
1446 __err |= ios_base::eofbit | ios_base::failbit;
1451 if (!__ct.is(ctype_base::digit, __c)) {
1452 __err |= ios_base::failbit;
1455 int __r = __ct.narrow(__c, 0) - '0';
1456 for (++__b, (void)--__n; __b != __e && __n > 0; ++__b, (void)--__n) {
1459 if (!__ct.is(ctype_base::digit, __c))
1461 __r = __r * 10 + __ct.narrow(__c, 0) - '0';
1464 __err |= ios_base::eofbit;
1468 class _LIBCPP_EXPORTED_FROM_ABI time_base {
1470 enum dateorder { no_order, dmy, mdy, ymd, ydm };
1473 template <class _CharT>
1474 class _LIBCPP_TEMPLATE_VIS __time_get_c_storage {
1476 typedef basic_string<_CharT> string_type;
1478 virtual const string_type* __weeks() const;
1479 virtual const string_type* __months() const;
1480 virtual const string_type* __am_pm() const;
1481 virtual const string_type& __c() const;
1482 virtual const string_type& __r() const;
1483 virtual const string_type& __x() const;
1484 virtual const string_type& __X() const;
1486 _LIBCPP_HIDE_FROM_ABI ~__time_get_c_storage() {}
1490 _LIBCPP_EXPORTED_FROM_ABI const string* __time_get_c_storage<char>::__weeks() const;
1492 _LIBCPP_EXPORTED_FROM_ABI const string* __time_get_c_storage<char>::__months() const;
1494 _LIBCPP_EXPORTED_FROM_ABI const string* __time_get_c_storage<char>::__am_pm() const;
1496 _LIBCPP_EXPORTED_FROM_ABI const string& __time_get_c_storage<char>::__c() const;
1498 _LIBCPP_EXPORTED_FROM_ABI const string& __time_get_c_storage<char>::__r() const;
1500 _LIBCPP_EXPORTED_FROM_ABI const string& __time_get_c_storage<char>::__x() const;
1502 _LIBCPP_EXPORTED_FROM_ABI const string& __time_get_c_storage<char>::__X() const;
1504 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1506 _LIBCPP_EXPORTED_FROM_ABI const wstring* __time_get_c_storage<wchar_t>::__weeks() const;
1508 _LIBCPP_EXPORTED_FROM_ABI const wstring* __time_get_c_storage<wchar_t>::__months() const;
1510 _LIBCPP_EXPORTED_FROM_ABI const wstring* __time_get_c_storage<wchar_t>::__am_pm() const;
1512 _LIBCPP_EXPORTED_FROM_ABI const wstring& __time_get_c_storage<wchar_t>::__c() const;
1514 _LIBCPP_EXPORTED_FROM_ABI const wstring& __time_get_c_storage<wchar_t>::__r() const;
1516 _LIBCPP_EXPORTED_FROM_ABI const wstring& __time_get_c_storage<wchar_t>::__x() const;
1518 _LIBCPP_EXPORTED_FROM_ABI const wstring& __time_get_c_storage<wchar_t>::__X() const;
1521 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
1522 class _LIBCPP_TEMPLATE_VIS time_get : public locale::facet, public time_base, private __time_get_c_storage<_CharT> {
1524 typedef _CharT char_type;
1525 typedef _InputIterator iter_type;
1526 typedef time_base::dateorder dateorder;
1527 typedef basic_string<char_type> string_type;
1529 _LIBCPP_HIDE_FROM_ABI explicit time_get(size_t __refs = 0) : locale::facet(__refs) {}
1531 _LIBCPP_HIDE_FROM_ABI dateorder date_order() const { return this->do_date_order(); }
1533 _LIBCPP_HIDE_FROM_ABI iter_type
1534 get_time(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1535 return do_get_time(__b, __e, __iob, __err, __tm);
1538 _LIBCPP_HIDE_FROM_ABI iter_type
1539 get_date(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1540 return do_get_date(__b, __e, __iob, __err, __tm);
1543 _LIBCPP_HIDE_FROM_ABI iter_type
1544 get_weekday(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1545 return do_get_weekday(__b, __e, __iob, __err, __tm);
1548 _LIBCPP_HIDE_FROM_ABI iter_type
1549 get_monthname(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1550 return do_get_monthname(__b, __e, __iob, __err, __tm);
1553 _LIBCPP_HIDE_FROM_ABI iter_type
1554 get_year(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1555 return do_get_year(__b, __e, __iob, __err, __tm);
1558 _LIBCPP_HIDE_FROM_ABI iter_type
1559 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm, char __fmt, char __mod = 0)
1561 return do_get(__b, __e, __iob, __err, __tm, __fmt, __mod);
1568 ios_base::iostate& __err,
1570 const char_type* __fmtb,
1571 const char_type* __fmte) const;
1573 static locale::id id;
1576 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~time_get() override {}
1578 virtual dateorder do_date_order() const;
1580 do_get_time(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const;
1582 do_get_date(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const;
1584 do_get_weekday(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const;
1586 do_get_monthname(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const;
1588 do_get_year(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const;
1589 virtual iter_type do_get(
1590 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm, char __fmt, char __mod) const;
1593 void __get_white_space(iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1594 void __get_percent(iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1596 void __get_weekdayname(
1597 int& __m, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1598 void __get_monthname(
1599 int& __m, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1600 void __get_day(int& __d, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1602 __get_month(int& __m, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1604 __get_year(int& __y, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1606 __get_year4(int& __y, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1608 __get_hour(int& __d, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1610 __get_12_hour(int& __h, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1612 __get_am_pm(int& __h, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1614 __get_minute(int& __m, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1616 __get_second(int& __s, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1618 __get_weekday(int& __w, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1619 void __get_day_year_num(
1620 int& __w, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1623 template <class _CharT, class _InputIterator>
1624 locale::id time_get<_CharT, _InputIterator>::id;
1626 // time_get primitives
1628 template <class _CharT, class _InputIterator>
1629 void time_get<_CharT, _InputIterator>::__get_weekdayname(
1630 int& __w, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1631 // Note: ignoring case comes from the POSIX strptime spec
1632 const string_type* __wk = this->__weeks();
1633 ptrdiff_t __i = std::__scan_keyword(__b, __e, __wk, __wk + 14, __ct, __err, false) - __wk;
1638 template <class _CharT, class _InputIterator>
1639 void time_get<_CharT, _InputIterator>::__get_monthname(
1640 int& __m, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1641 // Note: ignoring case comes from the POSIX strptime spec
1642 const string_type* __month = this->__months();
1643 ptrdiff_t __i = std::__scan_keyword(__b, __e, __month, __month + 24, __ct, __err, false) - __month;
1648 template <class _CharT, class _InputIterator>
1649 void time_get<_CharT, _InputIterator>::__get_day(
1650 int& __d, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1651 int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
1652 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 31)
1655 __err |= ios_base::failbit;
1658 template <class _CharT, class _InputIterator>
1659 void time_get<_CharT, _InputIterator>::__get_month(
1660 int& __m, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1661 int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2) - 1;
1662 if (!(__err & ios_base::failbit) && 0 <= __t && __t <= 11)
1665 __err |= ios_base::failbit;
1668 template <class _CharT, class _InputIterator>
1669 void time_get<_CharT, _InputIterator>::__get_year(
1670 int& __y, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1671 int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 4);
1672 if (!(__err & ios_base::failbit)) {
1675 else if (69 <= __t && __t <= 99)
1681 template <class _CharT, class _InputIterator>
1682 void time_get<_CharT, _InputIterator>::__get_year4(
1683 int& __y, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1684 int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 4);
1685 if (!(__err & ios_base::failbit))
1689 template <class _CharT, class _InputIterator>
1690 void time_get<_CharT, _InputIterator>::__get_hour(
1691 int& __h, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1692 int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
1693 if (!(__err & ios_base::failbit) && __t <= 23)
1696 __err |= ios_base::failbit;
1699 template <class _CharT, class _InputIterator>
1700 void time_get<_CharT, _InputIterator>::__get_12_hour(
1701 int& __h, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1702 int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
1703 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 12)
1706 __err |= ios_base::failbit;
1709 template <class _CharT, class _InputIterator>
1710 void time_get<_CharT, _InputIterator>::__get_minute(
1711 int& __m, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1712 int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
1713 if (!(__err & ios_base::failbit) && __t <= 59)
1716 __err |= ios_base::failbit;
1719 template <class _CharT, class _InputIterator>
1720 void time_get<_CharT, _InputIterator>::__get_second(
1721 int& __s, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1722 int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
1723 if (!(__err & ios_base::failbit) && __t <= 60)
1726 __err |= ios_base::failbit;
1729 template <class _CharT, class _InputIterator>
1730 void time_get<_CharT, _InputIterator>::__get_weekday(
1731 int& __w, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1732 int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 1);
1733 if (!(__err & ios_base::failbit) && __t <= 6)
1736 __err |= ios_base::failbit;
1739 template <class _CharT, class _InputIterator>
1740 void time_get<_CharT, _InputIterator>::__get_day_year_num(
1741 int& __d, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1742 int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 3);
1743 if (!(__err & ios_base::failbit) && __t <= 365)
1746 __err |= ios_base::failbit;
1749 template <class _CharT, class _InputIterator>
1750 void time_get<_CharT, _InputIterator>::__get_white_space(
1751 iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1752 for (; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
1755 __err |= ios_base::eofbit;
1758 template <class _CharT, class _InputIterator>
1759 void time_get<_CharT, _InputIterator>::__get_am_pm(
1760 int& __h, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1761 const string_type* __ap = this->__am_pm();
1762 if (__ap[0].size() + __ap[1].size() == 0) {
1763 __err |= ios_base::failbit;
1766 ptrdiff_t __i = std::__scan_keyword(__b, __e, __ap, __ap + 2, __ct, __err, false) - __ap;
1767 if (__i == 0 && __h == 12)
1769 else if (__i == 1 && __h < 12)
1773 template <class _CharT, class _InputIterator>
1774 void time_get<_CharT, _InputIterator>::__get_percent(
1775 iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1777 __err |= ios_base::eofbit | ios_base::failbit;
1780 if (__ct.narrow(*__b, 0) != '%')
1781 __err |= ios_base::failbit;
1782 else if (++__b == __e)
1783 __err |= ios_base::eofbit;
1786 // time_get end primitives
1788 template <class _CharT, class _InputIterator>
1789 _InputIterator time_get<_CharT, _InputIterator>::get(
1793 ios_base::iostate& __err,
1795 const char_type* __fmtb,
1796 const char_type* __fmte) const {
1797 const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
1798 __err = ios_base::goodbit;
1799 while (__fmtb != __fmte && __err == ios_base::goodbit) {
1801 __err = ios_base::failbit;
1804 if (__ct.narrow(*__fmtb, 0) == '%') {
1805 if (++__fmtb == __fmte) {
1806 __err = ios_base::failbit;
1809 char __cmd = __ct.narrow(*__fmtb, 0);
1811 if (__cmd == 'E' || __cmd == '0') {
1812 if (++__fmtb == __fmte) {
1813 __err = ios_base::failbit;
1817 __cmd = __ct.narrow(*__fmtb, 0);
1819 __b = do_get(__b, __e, __iob, __err, __tm, __cmd, __opt);
1821 } else if (__ct.is(ctype_base::space, *__fmtb)) {
1822 for (++__fmtb; __fmtb != __fmte && __ct.is(ctype_base::space, *__fmtb); ++__fmtb)
1824 for (; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
1826 } else if (__ct.toupper(*__b) == __ct.toupper(*__fmtb)) {
1830 __err = ios_base::failbit;
1833 __err |= ios_base::eofbit;
1837 template <class _CharT, class _InputIterator>
1838 typename time_get<_CharT, _InputIterator>::dateorder time_get<_CharT, _InputIterator>::do_date_order() const {
1842 template <class _CharT, class _InputIterator>
1843 _InputIterator time_get<_CharT, _InputIterator>::do_get_time(
1844 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1845 const char_type __fmt[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
1846 return get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt) / sizeof(__fmt[0]));
1849 template <class _CharT, class _InputIterator>
1850 _InputIterator time_get<_CharT, _InputIterator>::do_get_date(
1851 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1852 const string_type& __fmt = this->__x();
1853 return get(__b, __e, __iob, __err, __tm, __fmt.data(), __fmt.data() + __fmt.size());
1856 template <class _CharT, class _InputIterator>
1857 _InputIterator time_get<_CharT, _InputIterator>::do_get_weekday(
1858 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1859 const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
1860 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
1864 template <class _CharT, class _InputIterator>
1865 _InputIterator time_get<_CharT, _InputIterator>::do_get_monthname(
1866 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1867 const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
1868 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
1872 template <class _CharT, class _InputIterator>
1873 _InputIterator time_get<_CharT, _InputIterator>::do_get_year(
1874 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1875 const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
1876 __get_year(__tm->tm_year, __b, __e, __err, __ct);
1880 template <class _CharT, class _InputIterator>
1881 _InputIterator time_get<_CharT, _InputIterator>::do_get(
1882 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm, char __fmt, char) const {
1883 __err = ios_base::goodbit;
1884 const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
1888 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
1893 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
1896 const string_type& __fm = this->__c();
1897 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
1901 __get_day(__tm->tm_mday, __b, __e, __err, __ct);
1904 const char_type __fm[] = {'%', 'm', '/', '%', 'd', '/', '%', 'y'};
1905 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm) / sizeof(__fm[0]));
1908 const char_type __fm[] = {'%', 'Y', '-', '%', 'm', '-', '%', 'd'};
1909 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm) / sizeof(__fm[0]));
1912 __get_hour(__tm->tm_hour, __b, __e, __err, __ct);
1915 __get_12_hour(__tm->tm_hour, __b, __e, __err, __ct);
1918 __get_day_year_num(__tm->tm_yday, __b, __e, __err, __ct);
1921 __get_month(__tm->tm_mon, __b, __e, __err, __ct);
1924 __get_minute(__tm->tm_min, __b, __e, __err, __ct);
1928 __get_white_space(__b, __e, __err, __ct);
1931 __get_am_pm(__tm->tm_hour, __b, __e, __err, __ct);
1934 const char_type __fm[] = {'%', 'I', ':', '%', 'M', ':', '%', 'S', ' ', '%', 'p'};
1935 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm) / sizeof(__fm[0]));
1938 const char_type __fm[] = {'%', 'H', ':', '%', 'M'};
1939 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm) / sizeof(__fm[0]));
1942 __get_second(__tm->tm_sec, __b, __e, __err, __ct);
1945 const char_type __fm[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
1946 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm) / sizeof(__fm[0]));
1949 __get_weekday(__tm->tm_wday, __b, __e, __err, __ct);
1952 return do_get_date(__b, __e, __iob, __err, __tm);
1954 const string_type& __fm = this->__X();
1955 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
1958 __get_year(__tm->tm_year, __b, __e, __err, __ct);
1961 __get_year4(__tm->tm_year, __b, __e, __err, __ct);
1964 __get_percent(__b, __e, __err, __ct);
1967 __err |= ios_base::failbit;
1972 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get<char>;
1973 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1974 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get<wchar_t>;
1977 class _LIBCPP_EXPORTED_FROM_ABI __time_get {
1981 __time_get(const char* __nm);
1982 __time_get(const string& __nm);
1986 template <class _CharT>
1987 class _LIBCPP_TEMPLATE_VIS __time_get_storage : public __time_get {
1989 typedef basic_string<_CharT> string_type;
1991 string_type __weeks_[14];
1992 string_type __months_[24];
1993 string_type __am_pm_[2];
1999 explicit __time_get_storage(const char* __nm);
2000 explicit __time_get_storage(const string& __nm);
2002 _LIBCPP_HIDE_FROM_ABI ~__time_get_storage() {}
2004 time_base::dateorder __do_date_order() const;
2007 void init(const ctype<_CharT>&);
2008 string_type __analyze(char __fmt, const ctype<_CharT>&);
2011 #define _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(_CharT) \
2013 _LIBCPP_EXPORTED_FROM_ABI time_base::dateorder __time_get_storage<_CharT>::__do_date_order() const; \
2015 _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::__time_get_storage(const char*); \
2017 _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::__time_get_storage(const string&); \
2019 _LIBCPP_EXPORTED_FROM_ABI void __time_get_storage<_CharT>::init(const ctype<_CharT>&); \
2021 _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::string_type __time_get_storage<_CharT>::__analyze( \
2022 char, const ctype<_CharT>&); \
2023 extern template _LIBCPP_EXPORTED_FROM_ABI time_base::dateorder __time_get_storage<_CharT>::__do_date_order() const; \
2024 extern template _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::__time_get_storage(const char*); \
2025 extern template _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::__time_get_storage(const string&); \
2026 extern template _LIBCPP_EXPORTED_FROM_ABI void __time_get_storage<_CharT>::init(const ctype<_CharT>&); \
2027 extern template _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::string_type \
2028 __time_get_storage<_CharT>::__analyze(char, const ctype<_CharT>&); \
2031 _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(char)
2032 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2033 _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(wchar_t)
2035 #undef _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION
2037 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2038 class _LIBCPP_TEMPLATE_VIS time_get_byname
2039 : public time_get<_CharT, _InputIterator>,
2040 private __time_get_storage<_CharT> {
2042 typedef time_base::dateorder dateorder;
2043 typedef _InputIterator iter_type;
2044 typedef _CharT char_type;
2045 typedef basic_string<char_type> string_type;
2047 _LIBCPP_HIDE_FROM_ABI explicit time_get_byname(const char* __nm, size_t __refs = 0)
2048 : time_get<_CharT, _InputIterator>(__refs), __time_get_storage<_CharT>(__nm) {}
2049 _LIBCPP_HIDE_FROM_ABI explicit time_get_byname(const string& __nm, size_t __refs = 0)
2050 : time_get<_CharT, _InputIterator>(__refs), __time_get_storage<_CharT>(__nm) {}
2053 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~time_get_byname() override {}
2055 _LIBCPP_HIDE_FROM_ABI_VIRTUAL dateorder do_date_order() const override { return this->__do_date_order(); }
2058 _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type* __weeks() const override { return this->__weeks_; }
2059 _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type* __months() const override { return this->__months_; }
2060 _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type* __am_pm() const override { return this->__am_pm_; }
2061 _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type& __c() const override { return this->__c_; }
2062 _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type& __r() const override { return this->__r_; }
2063 _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type& __x() const override { return this->__x_; }
2064 _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type& __X() const override { return this->__X_; }
2067 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<char>;
2068 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2069 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<wchar_t>;
2072 class _LIBCPP_EXPORTED_FROM_ABI __time_put {
2076 _LIBCPP_HIDE_FROM_ABI __time_put() : __loc_(_LIBCPP_GET_C_LOCALE) {}
2077 __time_put(const char* __nm);
2078 __time_put(const string& __nm);
2080 void __do_put(char* __nb, char*& __ne, const tm* __tm, char __fmt, char __mod) const;
2081 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2082 void __do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm, char __fmt, char __mod) const;
2086 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2087 class _LIBCPP_TEMPLATE_VIS time_put : public locale::facet, private __time_put {
2089 typedef _CharT char_type;
2090 typedef _OutputIterator iter_type;
2092 _LIBCPP_HIDE_FROM_ABI explicit time_put(size_t __refs = 0) : locale::facet(__refs) {}
2095 put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm, const char_type* __pb, const char_type* __pe)
2098 _LIBCPP_HIDE_FROM_ABI iter_type
2099 put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm, char __fmt, char __mod = 0) const {
2100 return do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2103 static locale::id id;
2106 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~time_put() override {}
2107 virtual iter_type do_put(iter_type __s, ios_base&, char_type, const tm* __tm, char __fmt, char __mod) const;
2109 _LIBCPP_HIDE_FROM_ABI explicit time_put(const char* __nm, size_t __refs) : locale::facet(__refs), __time_put(__nm) {}
2110 _LIBCPP_HIDE_FROM_ABI explicit time_put(const string& __nm, size_t __refs)
2111 : locale::facet(__refs), __time_put(__nm) {}
2114 template <class _CharT, class _OutputIterator>
2115 locale::id time_put<_CharT, _OutputIterator>::id;
2117 template <class _CharT, class _OutputIterator>
2118 _OutputIterator time_put<_CharT, _OutputIterator>::put(
2119 iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm, const char_type* __pb, const char_type* __pe)
2121 const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
2122 for (; __pb != __pe; ++__pb) {
2123 if (__ct.narrow(*__pb, 0) == '%') {
2124 if (++__pb == __pe) {
2129 char __fmt = __ct.narrow(*__pb, 0);
2130 if (__fmt == 'E' || __fmt == 'O') {
2131 if (++__pb == __pe) {
2137 __fmt = __ct.narrow(*__pb, 0);
2139 __s = do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2146 template <class _CharT, class _OutputIterator>
2147 _OutputIterator time_put<_CharT, _OutputIterator>::do_put(
2148 iter_type __s, ios_base&, char_type, const tm* __tm, char __fmt, char __mod) const {
2149 char_type __nar[100];
2150 char_type* __nb = __nar;
2151 char_type* __ne = __nb + 100;
2152 __do_put(__nb, __ne, __tm, __fmt, __mod);
2153 return std::copy(__nb, __ne, __s);
2156 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<char>;
2157 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2158 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<wchar_t>;
2161 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2162 class _LIBCPP_TEMPLATE_VIS time_put_byname : public time_put<_CharT, _OutputIterator> {
2164 _LIBCPP_HIDE_FROM_ABI explicit time_put_byname(const char* __nm, size_t __refs = 0)
2165 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2167 _LIBCPP_HIDE_FROM_ABI explicit time_put_byname(const string& __nm, size_t __refs = 0)
2168 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2171 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~time_put_byname() override {}
2174 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<char>;
2175 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2176 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<wchar_t>;
2181 class _LIBCPP_EXPORTED_FROM_ABI money_base {
2183 enum part { none, space, symbol, sign, value };
2188 _LIBCPP_HIDE_FROM_ABI money_base() {}
2193 template <class _CharT, bool _International = false>
2194 class _LIBCPP_TEMPLATE_VIS moneypunct : public locale::facet, public money_base {
2196 typedef _CharT char_type;
2197 typedef basic_string<char_type> string_type;
2199 _LIBCPP_HIDE_FROM_ABI explicit moneypunct(size_t __refs = 0) : locale::facet(__refs) {}
2201 _LIBCPP_HIDE_FROM_ABI char_type decimal_point() const { return do_decimal_point(); }
2202 _LIBCPP_HIDE_FROM_ABI char_type thousands_sep() const { return do_thousands_sep(); }
2203 _LIBCPP_HIDE_FROM_ABI string grouping() const { return do_grouping(); }
2204 _LIBCPP_HIDE_FROM_ABI string_type curr_symbol() const { return do_curr_symbol(); }
2205 _LIBCPP_HIDE_FROM_ABI string_type positive_sign() const { return do_positive_sign(); }
2206 _LIBCPP_HIDE_FROM_ABI string_type negative_sign() const { return do_negative_sign(); }
2207 _LIBCPP_HIDE_FROM_ABI int frac_digits() const { return do_frac_digits(); }
2208 _LIBCPP_HIDE_FROM_ABI pattern pos_format() const { return do_pos_format(); }
2209 _LIBCPP_HIDE_FROM_ABI pattern neg_format() const { return do_neg_format(); }
2211 static locale::id id;
2212 static const bool intl = _International;
2215 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~moneypunct() override {}
2217 virtual char_type do_decimal_point() const { return numeric_limits<char_type>::max(); }
2218 virtual char_type do_thousands_sep() const { return numeric_limits<char_type>::max(); }
2219 virtual string do_grouping() const { return string(); }
2220 virtual string_type do_curr_symbol() const { return string_type(); }
2221 virtual string_type do_positive_sign() const { return string_type(); }
2222 virtual string_type do_negative_sign() const { return string_type(1, '-'); }
2223 virtual int do_frac_digits() const { return 0; }
2224 virtual pattern do_pos_format() const {
2225 pattern __p = {{symbol, sign, none, value}};
2228 virtual pattern do_neg_format() const {
2229 pattern __p = {{symbol, sign, none, value}};
2234 template <class _CharT, bool _International>
2235 locale::id moneypunct<_CharT, _International>::id;
2237 template <class _CharT, bool _International>
2238 const bool moneypunct<_CharT, _International>::intl;
2240 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, false>;
2241 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, true>;
2242 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2243 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, false>;
2244 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, true>;
2247 // moneypunct_byname
2249 template <class _CharT, bool _International = false>
2250 class _LIBCPP_TEMPLATE_VIS moneypunct_byname : public moneypunct<_CharT, _International> {
2252 typedef money_base::pattern pattern;
2253 typedef _CharT char_type;
2254 typedef basic_string<char_type> string_type;
2256 _LIBCPP_HIDE_FROM_ABI explicit moneypunct_byname(const char* __nm, size_t __refs = 0)
2257 : moneypunct<_CharT, _International>(__refs) {
2261 _LIBCPP_HIDE_FROM_ABI explicit moneypunct_byname(const string& __nm, size_t __refs = 0)
2262 : moneypunct<_CharT, _International>(__refs) {
2267 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~moneypunct_byname() override {}
2269 char_type do_decimal_point() const override { return __decimal_point_; }
2270 char_type do_thousands_sep() const override { return __thousands_sep_; }
2271 string do_grouping() const override { return __grouping_; }
2272 string_type do_curr_symbol() const override { return __curr_symbol_; }
2273 string_type do_positive_sign() const override { return __positive_sign_; }
2274 string_type do_negative_sign() const override { return __negative_sign_; }
2275 int do_frac_digits() const override { return __frac_digits_; }
2276 pattern do_pos_format() const override { return __pos_format_; }
2277 pattern do_neg_format() const override { return __neg_format_; }
2280 char_type __decimal_point_;
2281 char_type __thousands_sep_;
2283 string_type __curr_symbol_;
2284 string_type __positive_sign_;
2285 string_type __negative_sign_;
2287 pattern __pos_format_;
2288 pattern __neg_format_;
2290 void init(const char*);
2294 _LIBCPP_EXPORTED_FROM_ABI void moneypunct_byname<char, false>::init(const char*);
2296 _LIBCPP_EXPORTED_FROM_ABI void moneypunct_byname<char, true>::init(const char*);
2297 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, false>;
2298 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, true>;
2300 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2302 _LIBCPP_EXPORTED_FROM_ABI void moneypunct_byname<wchar_t, false>::init(const char*);
2304 _LIBCPP_EXPORTED_FROM_ABI void moneypunct_byname<wchar_t, true>::init(const char*);
2305 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, false>;
2306 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, true>;
2311 template <class _CharT>
2314 typedef _CharT char_type;
2315 typedef basic_string<char_type> string_type;
2317 _LIBCPP_HIDE_FROM_ABI __money_get() {}
2319 static void __gather_info(
2321 const locale& __loc,
2322 money_base::pattern& __pat,
2332 template <class _CharT>
2333 void __money_get<_CharT>::__gather_info(
2335 const locale& __loc,
2336 money_base::pattern& __pat,
2345 const moneypunct<char_type, true>& __mp = std::use_facet<moneypunct<char_type, true> >(__loc);
2346 __pat = __mp.neg_format();
2347 __nsn = __mp.negative_sign();
2348 __psn = __mp.positive_sign();
2349 __dp = __mp.decimal_point();
2350 __ts = __mp.thousands_sep();
2351 __grp = __mp.grouping();
2352 __sym = __mp.curr_symbol();
2353 __fd = __mp.frac_digits();
2355 const moneypunct<char_type, false>& __mp = std::use_facet<moneypunct<char_type, false> >(__loc);
2356 __pat = __mp.neg_format();
2357 __nsn = __mp.negative_sign();
2358 __psn = __mp.positive_sign();
2359 __dp = __mp.decimal_point();
2360 __ts = __mp.thousands_sep();
2361 __grp = __mp.grouping();
2362 __sym = __mp.curr_symbol();
2363 __fd = __mp.frac_digits();
2367 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<char>;
2368 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2369 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<wchar_t>;
2372 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2373 class _LIBCPP_TEMPLATE_VIS money_get : public locale::facet, private __money_get<_CharT> {
2375 typedef _CharT char_type;
2376 typedef _InputIterator iter_type;
2377 typedef basic_string<char_type> string_type;
2379 _LIBCPP_HIDE_FROM_ABI explicit money_get(size_t __refs = 0) : locale::facet(__refs) {}
2381 _LIBCPP_HIDE_FROM_ABI iter_type
2382 get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob, ios_base::iostate& __err, long double& __v) const {
2383 return do_get(__b, __e, __intl, __iob, __err, __v);
2386 _LIBCPP_HIDE_FROM_ABI iter_type
2387 get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob, ios_base::iostate& __err, string_type& __v) const {
2388 return do_get(__b, __e, __intl, __iob, __err, __v);
2391 static locale::id id;
2394 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~money_get() override {}
2397 do_get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob, ios_base::iostate& __err, long double& __v) const;
2399 do_get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob, ios_base::iostate& __err, string_type& __v) const;
2402 static bool __do_get(
2406 const locale& __loc,
2407 ios_base::fmtflags __flags,
2408 ios_base::iostate& __err,
2410 const ctype<char_type>& __ct,
2411 unique_ptr<char_type, void (*)(void*)>& __wb,
2416 template <class _CharT, class _InputIterator>
2417 locale::id money_get<_CharT, _InputIterator>::id;
2419 _LIBCPP_EXPORTED_FROM_ABI void __do_nothing(void*);
2421 template <class _Tp>
2422 _LIBCPP_HIDE_FROM_ABI void __double_or_nothing(unique_ptr<_Tp, void (*)(void*)>& __b, _Tp*& __n, _Tp*& __e) {
2423 bool __owns = __b.get_deleter() != __do_nothing;
2424 size_t __cur_cap = static_cast<size_t>(__e - __b.get()) * sizeof(_Tp);
2425 size_t __new_cap = __cur_cap < numeric_limits<size_t>::max() / 2 ? 2 * __cur_cap : numeric_limits<size_t>::max();
2427 __new_cap = sizeof(_Tp);
2428 size_t __n_off = static_cast<size_t>(__n - __b.get());
2429 _Tp* __t = (_Tp*)std::realloc(__owns ? __b.get() : 0, __new_cap);
2431 __throw_bad_alloc();
2434 __b = unique_ptr<_Tp, void (*)(void*)>(__t, free);
2435 __new_cap /= sizeof(_Tp);
2436 __n = __b.get() + __n_off;
2437 __e = __b.get() + __new_cap;
2441 template <class _CharT, class _InputIterator>
2442 bool money_get<_CharT, _InputIterator>::__do_get(
2446 const locale& __loc,
2447 ios_base::fmtflags __flags,
2448 ios_base::iostate& __err,
2450 const ctype<char_type>& __ct,
2451 unique_ptr<char_type, void (*)(void*)>& __wb,
2455 __err |= ios_base::failbit;
2458 const unsigned __bz = 100;
2459 unsigned __gbuf[__bz];
2460 unique_ptr<unsigned, void (*)(void*)> __gb(__gbuf, __do_nothing);
2461 unsigned* __gn = __gb.get();
2462 unsigned* __ge = __gn + __bz;
2463 money_base::pattern __pat;
2470 // Capture the spaces read into money_base::{space,none} so they
2471 // can be compared to initial spaces in __sym.
2472 string_type __spaces;
2474 __money_get<_CharT>::__gather_info(__intl, __loc, __pat, __dp, __ts, __grp, __sym, __psn, __nsn, __fd);
2475 const string_type* __trailing_sign = 0;
2477 for (unsigned __p = 0; __p < 4 && __b != __e; ++__p) {
2478 switch (__pat.field[__p]) {
2479 case money_base::space:
2481 if (__ct.is(ctype_base::space, *__b))
2482 __spaces.push_back(*__b++);
2484 __err |= ios_base::failbit;
2488 _LIBCPP_FALLTHROUGH();
2489 case money_base::none:
2491 while (__b != __e && __ct.is(ctype_base::space, *__b))
2492 __spaces.push_back(*__b++);
2495 case money_base::sign:
2496 if (__psn.size() > 0 && *__b == __psn[0]) {
2499 if (__psn.size() > 1)
2500 __trailing_sign = &__psn;
2503 if (__nsn.size() > 0 && *__b == __nsn[0]) {
2506 if (__nsn.size() > 1)
2507 __trailing_sign = &__nsn;
2510 if (__psn.size() > 0 && __nsn.size() > 0) { // sign is required
2511 __err |= ios_base::failbit;
2514 if (__psn.size() == 0 && __nsn.size() == 0)
2515 // locale has no way of specifying a sign. Use the initial value of __neg as a default
2517 __neg = (__nsn.size() == 0);
2519 case money_base::symbol: {
2520 bool __more_needed =
2521 __trailing_sign || (__p < 2) || (__p == 2 && __pat.field[3] != static_cast<char>(money_base::none));
2522 bool __sb = (__flags & ios_base::showbase) != 0;
2523 if (__sb || __more_needed) {
2524 typename string_type::const_iterator __sym_space_end = __sym.begin();
2525 if (__p > 0 && (__pat.field[__p - 1] == money_base::none || __pat.field[__p - 1] == money_base::space)) {
2526 // Match spaces we've already read against spaces at
2527 // the beginning of __sym.
2528 while (__sym_space_end != __sym.end() && __ct.is(ctype_base::space, *__sym_space_end))
2530 const size_t __num_spaces = __sym_space_end - __sym.begin();
2531 if (__num_spaces > __spaces.size() ||
2532 !std::equal(__spaces.end() - __num_spaces, __spaces.end(), __sym.begin())) {
2533 // No match. Put __sym_space_end back at the
2534 // beginning of __sym, which will prevent a
2535 // match in the next loop.
2536 __sym_space_end = __sym.begin();
2539 typename string_type::const_iterator __sym_curr_char = __sym_space_end;
2540 while (__sym_curr_char != __sym.end() && __b != __e && *__b == *__sym_curr_char) {
2544 if (__sb && __sym_curr_char != __sym.end()) {
2545 __err |= ios_base::failbit;
2550 case money_base::value: {
2552 for (; __b != __e; ++__b) {
2553 char_type __c = *__b;
2554 if (__ct.is(ctype_base::digit, __c)) {
2556 std::__double_or_nothing(__wb, __wn, __we);
2559 } else if (__grp.size() > 0 && __ng > 0 && __c == __ts) {
2561 std::__double_or_nothing(__gb, __gn, __ge);
2567 if (__gb.get() != __gn && __ng > 0) {
2569 std::__double_or_nothing(__gb, __gn, __ge);
2573 if (__b == __e || *__b != __dp) {
2574 __err |= ios_base::failbit;
2577 for (++__b; __fd > 0; --__fd, ++__b) {
2578 if (__b == __e || !__ct.is(ctype_base::digit, *__b)) {
2579 __err |= ios_base::failbit;
2583 std::__double_or_nothing(__wb, __wn, __we);
2587 if (__wn == __wb.get()) {
2588 __err |= ios_base::failbit;
2594 if (__trailing_sign) {
2595 for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, ++__b) {
2596 if (__b == __e || *__b != (*__trailing_sign)[__i]) {
2597 __err |= ios_base::failbit;
2602 if (__gb.get() != __gn) {
2603 ios_base::iostate __et = ios_base::goodbit;
2604 __check_grouping(__grp, __gb.get(), __gn, __et);
2606 __err |= ios_base::failbit;
2613 template <class _CharT, class _InputIterator>
2614 _InputIterator money_get<_CharT, _InputIterator>::do_get(
2615 iter_type __b, iter_type __e, bool __intl, ios_base& __iob, ios_base::iostate& __err, long double& __v) const {
2616 const int __bz = 100;
2617 char_type __wbuf[__bz];
2618 unique_ptr<char_type, void (*)(void*)> __wb(__wbuf, __do_nothing);
2620 char_type* __we = __wbuf + __bz;
2621 locale __loc = __iob.getloc();
2622 const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__loc);
2624 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct, __wb, __wn, __we)) {
2625 const char __src[] = "0123456789";
2626 char_type __atoms[sizeof(__src) - 1];
2627 __ct.widen(__src, __src + (sizeof(__src) - 1), __atoms);
2629 char* __nc = __nbuf;
2630 unique_ptr<char, void (*)(void*)> __h(nullptr, free);
2631 if (__wn - __wb.get() > __bz - 2) {
2632 __h.reset((char*)malloc(static_cast<size_t>(__wn - __wb.get() + 2)));
2633 if (__h.get() == nullptr)
2634 __throw_bad_alloc();
2639 for (const char_type* __w = __wb.get(); __w < __wn; ++__w, ++__nc)
2640 *__nc = __src[std::find(__atoms, std::end(__atoms), *__w) - __atoms];
2642 if (sscanf(__nbuf, "%Lf", &__v) != 1)
2643 __throw_runtime_error("money_get error");
2646 __err |= ios_base::eofbit;
2650 template <class _CharT, class _InputIterator>
2651 _InputIterator money_get<_CharT, _InputIterator>::do_get(
2652 iter_type __b, iter_type __e, bool __intl, ios_base& __iob, ios_base::iostate& __err, string_type& __v) const {
2653 const int __bz = 100;
2654 char_type __wbuf[__bz];
2655 unique_ptr<char_type, void (*)(void*)> __wb(__wbuf, __do_nothing);
2657 char_type* __we = __wbuf + __bz;
2658 locale __loc = __iob.getloc();
2659 const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__loc);
2661 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct, __wb, __wn, __we)) {
2664 __v.push_back(__ct.widen('-'));
2665 char_type __z = __ct.widen('0');
2667 for (__w = __wb.get(); __w < __wn - 1; ++__w)
2670 __v.append(__w, __wn);
2673 __err |= ios_base::eofbit;
2677 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<char>;
2678 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2679 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<wchar_t>;
2684 template <class _CharT>
2687 typedef _CharT char_type;
2688 typedef basic_string<char_type> string_type;
2690 _LIBCPP_HIDE_FROM_ABI __money_put() {}
2692 static void __gather_info(
2695 const locale& __loc,
2696 money_base::pattern& __pat,
2703 static void __format(
2707 ios_base::fmtflags __flags,
2708 const char_type* __db,
2709 const char_type* __de,
2710 const ctype<char_type>& __ct,
2712 const money_base::pattern& __pat,
2715 const string& __grp,
2716 const string_type& __sym,
2717 const string_type& __sn,
2721 template <class _CharT>
2722 void __money_put<_CharT>::__gather_info(
2725 const locale& __loc,
2726 money_base::pattern& __pat,
2734 const moneypunct<char_type, true>& __mp = std::use_facet<moneypunct<char_type, true> >(__loc);
2736 __pat = __mp.neg_format();
2737 __sn = __mp.negative_sign();
2739 __pat = __mp.pos_format();
2740 __sn = __mp.positive_sign();
2742 __dp = __mp.decimal_point();
2743 __ts = __mp.thousands_sep();
2744 __grp = __mp.grouping();
2745 __sym = __mp.curr_symbol();
2746 __fd = __mp.frac_digits();
2748 const moneypunct<char_type, false>& __mp = std::use_facet<moneypunct<char_type, false> >(__loc);
2750 __pat = __mp.neg_format();
2751 __sn = __mp.negative_sign();
2753 __pat = __mp.pos_format();
2754 __sn = __mp.positive_sign();
2756 __dp = __mp.decimal_point();
2757 __ts = __mp.thousands_sep();
2758 __grp = __mp.grouping();
2759 __sym = __mp.curr_symbol();
2760 __fd = __mp.frac_digits();
2764 template <class _CharT>
2765 void __money_put<_CharT>::__format(
2769 ios_base::fmtflags __flags,
2770 const char_type* __db,
2771 const char_type* __de,
2772 const ctype<char_type>& __ct,
2774 const money_base::pattern& __pat,
2777 const string& __grp,
2778 const string_type& __sym,
2779 const string_type& __sn,
2782 for (char __p : __pat.field) {
2784 case money_base::none:
2787 case money_base::space:
2789 *__me++ = __ct.widen(' ');
2791 case money_base::sign:
2795 case money_base::symbol:
2796 if (!__sym.empty() && (__flags & ios_base::showbase))
2797 __me = std::copy(__sym.begin(), __sym.end(), __me);
2799 case money_base::value: {
2800 // remember start of value so we can reverse it
2801 char_type* __t = __me;
2802 // find beginning of digits
2805 // find end of digits
2806 const char_type* __d;
2807 for (__d = __db; __d < __de; ++__d)
2808 if (!__ct.is(ctype_base::digit, *__d))
2810 // print fractional part
2813 for (__f = __fd; __d > __db && __f > 0; --__f)
2815 char_type __z = __f > 0 ? __ct.widen('0') : char_type();
2816 for (; __f > 0; --__f)
2822 *__me++ = __ct.widen('0');
2826 unsigned __gl = __grp.empty() ? numeric_limits<unsigned>::max() : static_cast<unsigned>(__grp[__ig]);
2827 while (__d != __db) {
2831 if (++__ig < __grp.size())
2832 __gl = __grp[__ig] == numeric_limits<char>::max()
2833 ? numeric_limits<unsigned>::max()
2834 : static_cast<unsigned>(__grp[__ig]);
2841 std::reverse(__t, __me);
2845 // print rest of sign, if any
2846 if (__sn.size() > 1)
2847 __me = std::copy(__sn.begin() + 1, __sn.end(), __me);
2849 if ((__flags & ios_base::adjustfield) == ios_base::left)
2851 else if ((__flags & ios_base::adjustfield) != ios_base::internal)
2855 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<char>;
2856 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2857 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<wchar_t>;
2860 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2861 class _LIBCPP_TEMPLATE_VIS money_put : public locale::facet, private __money_put<_CharT> {
2863 typedef _CharT char_type;
2864 typedef _OutputIterator iter_type;
2865 typedef basic_string<char_type> string_type;
2867 _LIBCPP_HIDE_FROM_ABI explicit money_put(size_t __refs = 0) : locale::facet(__refs) {}
2869 _LIBCPP_HIDE_FROM_ABI iter_type
2870 put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl, long double __units) const {
2871 return do_put(__s, __intl, __iob, __fl, __units);
2874 _LIBCPP_HIDE_FROM_ABI iter_type
2875 put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl, const string_type& __digits) const {
2876 return do_put(__s, __intl, __iob, __fl, __digits);
2879 static locale::id id;
2882 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~money_put() override {}
2884 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl, long double __units) const;
2886 do_put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl, const string_type& __digits) const;
2889 template <class _CharT, class _OutputIterator>
2890 locale::id money_put<_CharT, _OutputIterator>::id;
2892 template <class _CharT, class _OutputIterator>
2893 _OutputIterator money_put<_CharT, _OutputIterator>::do_put(
2894 iter_type __s, bool __intl, ios_base& __iob, char_type __fl, long double __units) const {
2896 const size_t __bs = 100;
2899 char_type __digits[__bs];
2900 char_type* __db = __digits;
2901 int __n = snprintf(__bb, __bs, "%.0Lf", __units);
2902 unique_ptr<char, void (*)(void*)> __hn(nullptr, free);
2903 unique_ptr<char_type, void (*)(void*)> __hd(0, free);
2904 // secure memory for digit storage
2905 if (static_cast<size_t>(__n) > __bs - 1) {
2906 __n = __libcpp_asprintf_l(&__bb, _LIBCPP_GET_C_LOCALE, "%.0Lf", __units);
2908 __throw_bad_alloc();
2910 __hd.reset((char_type*)malloc(static_cast<size_t>(__n) * sizeof(char_type)));
2911 if (__hd == nullptr)
2912 __throw_bad_alloc();
2916 locale __loc = __iob.getloc();
2917 const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__loc);
2918 __ct.widen(__bb, __bb + __n, __db);
2919 bool __neg = __n > 0 && __bb[0] == '-';
2920 money_base::pattern __pat;
2927 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
2928 // secure memory for formatting
2929 char_type __mbuf[__bs];
2930 char_type* __mb = __mbuf;
2931 unique_ptr<char_type, void (*)(void*)> __hw(0, free);
2932 size_t __exn = __n > __fd ? (static_cast<size_t>(__n) - static_cast<size_t>(__fd)) * 2 + __sn.size() + __sym.size() +
2933 static_cast<size_t>(__fd) + 1
2934 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
2936 __hw.reset((char_type*)malloc(__exn * sizeof(char_type)));
2939 __throw_bad_alloc();
2945 __mb, __mi, __me, __iob.flags(), __db, __db + __n, __ct, __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
2946 return std::__pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
2949 template <class _CharT, class _OutputIterator>
2950 _OutputIterator money_put<_CharT, _OutputIterator>::do_put(
2951 iter_type __s, bool __intl, ios_base& __iob, char_type __fl, const string_type& __digits) const {
2953 locale __loc = __iob.getloc();
2954 const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__loc);
2955 bool __neg = __digits.size() > 0 && __digits[0] == __ct.widen('-');
2956 money_base::pattern __pat;
2963 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
2964 // secure memory for formatting
2965 char_type __mbuf[100];
2966 char_type* __mb = __mbuf;
2967 unique_ptr<char_type, void (*)(void*)> __h(0, free);
2969 static_cast<int>(__digits.size()) > __fd
2970 ? (__digits.size() - static_cast<size_t>(__fd)) * 2 + __sn.size() + __sym.size() + static_cast<size_t>(__fd) +
2972 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
2974 __h.reset((char_type*)malloc(__exn * sizeof(char_type)));
2977 __throw_bad_alloc();
2988 __digits.data() + __digits.size(),
2998 return std::__pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3001 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<char>;
3002 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
3003 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<wchar_t>;
3008 class _LIBCPP_EXPORTED_FROM_ABI messages_base {
3010 typedef intptr_t catalog;
3012 _LIBCPP_HIDE_FROM_ABI messages_base() {}
3015 template <class _CharT>
3016 class _LIBCPP_TEMPLATE_VIS messages : public locale::facet, public messages_base {
3018 typedef _CharT char_type;
3019 typedef basic_string<_CharT> string_type;
3021 _LIBCPP_HIDE_FROM_ABI explicit messages(size_t __refs = 0) : locale::facet(__refs) {}
3023 _LIBCPP_HIDE_FROM_ABI catalog open(const basic_string<char>& __nm, const locale& __loc) const {
3024 return do_open(__nm, __loc);
3027 _LIBCPP_HIDE_FROM_ABI string_type get(catalog __c, int __set, int __msgid, const string_type& __dflt) const {
3028 return do_get(__c, __set, __msgid, __dflt);
3031 _LIBCPP_HIDE_FROM_ABI void close(catalog __c) const { do_close(__c); }
3033 static locale::id id;
3036 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~messages() override {}
3038 virtual catalog do_open(const basic_string<char>&, const locale&) const;
3039 virtual string_type do_get(catalog, int __set, int __msgid, const string_type& __dflt) const;
3040 virtual void do_close(catalog) const;
3043 template <class _CharT>
3044 locale::id messages<_CharT>::id;
3046 template <class _CharT>
3047 typename messages<_CharT>::catalog messages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const {
3048 #ifdef _LIBCPP_HAS_CATOPEN
3049 return (catalog)catopen(__nm.c_str(), NL_CAT_LOCALE);
3050 #else // !_LIBCPP_HAS_CATOPEN
3053 #endif // _LIBCPP_HAS_CATOPEN
3056 template <class _CharT>
3057 typename messages<_CharT>::string_type
3058 messages<_CharT>::do_get(catalog __c, int __set, int __msgid, const string_type& __dflt) const {
3059 #ifdef _LIBCPP_HAS_CATOPEN
3061 __narrow_to_utf8<sizeof(char_type) * __CHAR_BIT__>()(
3062 std::back_inserter(__ndflt), __dflt.c_str(), __dflt.c_str() + __dflt.size());
3063 nl_catd __cat = (nl_catd)__c;
3064 static_assert(sizeof(catalog) >= sizeof(nl_catd), "Unexpected nl_catd type");
3065 char* __n = catgets(__cat, __set, __msgid, __ndflt.c_str());
3067 __widen_from_utf8<sizeof(char_type) * __CHAR_BIT__>()(std::back_inserter(__w), __n, __n + std::strlen(__n));
3069 #else // !_LIBCPP_HAS_CATOPEN
3074 #endif // _LIBCPP_HAS_CATOPEN
3077 template <class _CharT>
3078 void messages<_CharT>::do_close(catalog __c) const {
3079 #ifdef _LIBCPP_HAS_CATOPEN
3080 catclose((nl_catd)__c);
3081 #else // !_LIBCPP_HAS_CATOPEN
3083 #endif // _LIBCPP_HAS_CATOPEN
3086 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<char>;
3087 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
3088 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<wchar_t>;
3091 template <class _CharT>
3092 class _LIBCPP_TEMPLATE_VIS messages_byname : public messages<_CharT> {
3094 typedef messages_base::catalog catalog;
3095 typedef basic_string<_CharT> string_type;
3097 _LIBCPP_HIDE_FROM_ABI explicit messages_byname(const char*, size_t __refs = 0) : messages<_CharT>(__refs) {}
3099 _LIBCPP_HIDE_FROM_ABI explicit messages_byname(const string&, size_t __refs = 0) : messages<_CharT>(__refs) {}
3102 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~messages_byname() override {}
3105 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<char>;
3106 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
3107 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<wchar_t>;
3110 template <class _Codecvt,
3111 class _Elem = wchar_t,
3112 class _WideAlloc = allocator<_Elem>,
3113 class _ByteAlloc = allocator<char> >
3114 class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 wstring_convert {
3116 typedef basic_string<char, char_traits<char>, _ByteAlloc> byte_string;
3117 typedef basic_string<_Elem, char_traits<_Elem>, _WideAlloc> wide_string;
3118 typedef typename _Codecvt::state_type state_type;
3119 typedef typename wide_string::traits_type::int_type int_type;
3122 byte_string __byte_err_string_;
3123 wide_string __wide_err_string_;
3124 _Codecvt* __cvtptr_;
3125 state_type __cvtstate_;
3128 wstring_convert(const wstring_convert& __wc);
3129 wstring_convert& operator=(const wstring_convert& __wc);
3132 #ifndef _LIBCPP_CXX03_LANG
3133 _LIBCPP_HIDE_FROM_ABI wstring_convert() : wstring_convert(new _Codecvt) {}
3134 _LIBCPP_HIDE_FROM_ABI explicit wstring_convert(_Codecvt* __pcvt);
3136 _LIBCPP_HIDE_FROM_ABI _LIBCPP_EXPLICIT_SINCE_CXX14 wstring_convert(_Codecvt* __pcvt = new _Codecvt);
3139 _LIBCPP_HIDE_FROM_ABI wstring_convert(_Codecvt* __pcvt, state_type __state);
3140 _LIBCPP_EXPLICIT_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI
3141 wstring_convert(const byte_string& __byte_err, const wide_string& __wide_err = wide_string());
3142 #ifndef _LIBCPP_CXX03_LANG
3143 _LIBCPP_HIDE_FROM_ABI wstring_convert(wstring_convert&& __wc);
3145 _LIBCPP_HIDE_FROM_ABI ~wstring_convert();
3147 _LIBCPP_HIDE_FROM_ABI wide_string from_bytes(char __byte) { return from_bytes(&__byte, &__byte + 1); }
3148 _LIBCPP_HIDE_FROM_ABI wide_string from_bytes(const char* __ptr) {
3149 return from_bytes(__ptr, __ptr + char_traits<char>::length(__ptr));
3151 _LIBCPP_HIDE_FROM_ABI wide_string from_bytes(const byte_string& __str) {
3152 return from_bytes(__str.data(), __str.data() + __str.size());
3154 _LIBCPP_HIDE_FROM_ABI wide_string from_bytes(const char* __first, const char* __last);
3156 _LIBCPP_HIDE_FROM_ABI byte_string to_bytes(_Elem __wchar) { return to_bytes(&__wchar, &__wchar + 1); }
3157 _LIBCPP_HIDE_FROM_ABI byte_string to_bytes(const _Elem* __wptr) {
3158 return to_bytes(__wptr, __wptr + char_traits<_Elem>::length(__wptr));
3160 _LIBCPP_HIDE_FROM_ABI byte_string to_bytes(const wide_string& __wstr) {
3161 return to_bytes(__wstr.data(), __wstr.data() + __wstr.size());
3163 _LIBCPP_HIDE_FROM_ABI byte_string to_bytes(const _Elem* __first, const _Elem* __last);
3165 _LIBCPP_HIDE_FROM_ABI size_t converted() const _NOEXCEPT { return __cvtcount_; }
3166 _LIBCPP_HIDE_FROM_ABI state_type state() const { return __cvtstate_; }
3169 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
3170 template <class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3171 inline wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::wstring_convert(_Codecvt* __pcvt)
3172 : __cvtptr_(__pcvt), __cvtstate_(), __cvtcount_(0) {}
3173 _LIBCPP_SUPPRESS_DEPRECATED_POP
3175 template <class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3176 inline wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::wstring_convert(_Codecvt* __pcvt, state_type __state)
3177 : __cvtptr_(__pcvt), __cvtstate_(__state), __cvtcount_(0) {}
3179 template <class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3180 wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::wstring_convert(
3181 const byte_string& __byte_err, const wide_string& __wide_err)
3182 : __byte_err_string_(__byte_err), __wide_err_string_(__wide_err), __cvtstate_(), __cvtcount_(0) {
3183 __cvtptr_ = new _Codecvt;
3186 #ifndef _LIBCPP_CXX03_LANG
3188 template <class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3189 inline wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::wstring_convert(wstring_convert&& __wc)
3190 : __byte_err_string_(std::move(__wc.__byte_err_string_)),
3191 __wide_err_string_(std::move(__wc.__wide_err_string_)),
3192 __cvtptr_(__wc.__cvtptr_),
3193 __cvtstate_(__wc.__cvtstate_),
3194 __cvtcount_(__wc.__cvtcount_) {
3195 __wc.__cvtptr_ = nullptr;
3198 #endif // _LIBCPP_CXX03_LANG
3200 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
3201 template <class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3202 wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::~wstring_convert() {
3206 template <class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3207 typename wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::wide_string
3208 wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::from_bytes(const char* __frm, const char* __frm_end) {
3209 _LIBCPP_SUPPRESS_DEPRECATED_POP
3211 if (__cvtptr_ != nullptr) {
3212 wide_string __ws(2 * (__frm_end - __frm), _Elem());
3213 if (__frm != __frm_end)
3214 __ws.resize(__ws.capacity());
3215 codecvt_base::result __r = codecvt_base::ok;
3216 state_type __st = __cvtstate_;
3217 if (__frm != __frm_end) {
3218 _Elem* __to = &__ws[0];
3219 _Elem* __to_end = __to + __ws.size();
3220 const char* __frm_nxt;
3223 __r = __cvtptr_->in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
3224 __cvtcount_ += __frm_nxt - __frm;
3225 if (__frm_nxt == __frm) {
3226 __r = codecvt_base::error;
3227 } else if (__r == codecvt_base::noconv) {
3228 __ws.resize(__to - &__ws[0]);
3229 // This only gets executed if _Elem is char
3230 __ws.append((const _Elem*)__frm, (const _Elem*)__frm_end);
3232 __r = codecvt_base::ok;
3233 } else if (__r == codecvt_base::ok) {
3234 __ws.resize(__to_nxt - &__ws[0]);
3236 } else if (__r == codecvt_base::partial) {
3237 ptrdiff_t __s = __to_nxt - &__ws[0];
3238 __ws.resize(2 * __s);
3239 __to = &__ws[0] + __s;
3240 __to_end = &__ws[0] + __ws.size();
3243 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3245 if (__r == codecvt_base::ok)
3249 if (__wide_err_string_.empty())
3250 __throw_range_error("wstring_convert: from_bytes error");
3252 return __wide_err_string_;
3255 template <class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3256 typename wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::byte_string
3257 wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::to_bytes(const _Elem* __frm, const _Elem* __frm_end) {
3259 if (__cvtptr_ != nullptr) {
3260 byte_string __bs(2 * (__frm_end - __frm), char());
3261 if (__frm != __frm_end)
3262 __bs.resize(__bs.capacity());
3263 codecvt_base::result __r = codecvt_base::ok;
3264 state_type __st = __cvtstate_;
3265 if (__frm != __frm_end) {
3266 char* __to = &__bs[0];
3267 char* __to_end = __to + __bs.size();
3268 const _Elem* __frm_nxt;
3271 __r = __cvtptr_->out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
3272 __cvtcount_ += __frm_nxt - __frm;
3273 if (__frm_nxt == __frm) {
3274 __r = codecvt_base::error;
3275 } else if (__r == codecvt_base::noconv) {
3276 __bs.resize(__to - &__bs[0]);
3277 // This only gets executed if _Elem is char
3278 __bs.append((const char*)__frm, (const char*)__frm_end);
3280 __r = codecvt_base::ok;
3281 } else if (__r == codecvt_base::ok) {
3282 __bs.resize(__to_nxt - &__bs[0]);
3284 } else if (__r == codecvt_base::partial) {
3285 ptrdiff_t __s = __to_nxt - &__bs[0];
3286 __bs.resize(2 * __s);
3287 __to = &__bs[0] + __s;
3288 __to_end = &__bs[0] + __bs.size();
3291 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3293 if (__r == codecvt_base::ok) {
3294 size_t __s = __bs.size();
3295 __bs.resize(__bs.capacity());
3296 char* __to = &__bs[0] + __s;
3297 char* __to_end = __to + __bs.size();
3300 __r = __cvtptr_->unshift(__st, __to, __to_end, __to_nxt);
3301 if (__r == codecvt_base::noconv) {
3302 __bs.resize(__to - &__bs[0]);
3303 __r = codecvt_base::ok;
3304 } else if (__r == codecvt_base::ok) {
3305 __bs.resize(__to_nxt - &__bs[0]);
3306 } else if (__r == codecvt_base::partial) {
3307 ptrdiff_t __sp = __to_nxt - &__bs[0];
3308 __bs.resize(2 * __sp);
3309 __to = &__bs[0] + __sp;
3310 __to_end = &__bs[0] + __bs.size();
3312 } while (__r == codecvt_base::partial);
3313 if (__r == codecvt_base::ok)
3318 if (__byte_err_string_.empty())
3319 __throw_range_error("wstring_convert: to_bytes error");
3321 return __byte_err_string_;
3324 template <class _Codecvt, class _Elem = wchar_t, class _Tr = char_traits<_Elem> >
3325 class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 wbuffer_convert : public basic_streambuf<_Elem, _Tr> {
3328 typedef _Elem char_type;
3329 typedef _Tr traits_type;
3330 typedef typename traits_type::int_type int_type;
3331 typedef typename traits_type::pos_type pos_type;
3332 typedef typename traits_type::off_type off_type;
3333 typedef typename _Codecvt::state_type state_type;
3337 const char* __extbufnext_;
3338 const char* __extbufend_;
3339 char __extbuf_min_[8];
3341 char_type* __intbuf_;
3343 streambuf* __bufptr_;
3346 ios_base::openmode __cm_;
3349 bool __always_noconv_;
3351 wbuffer_convert(const wbuffer_convert&);
3352 wbuffer_convert& operator=(const wbuffer_convert&);
3355 #ifndef _LIBCPP_CXX03_LANG
3356 _LIBCPP_HIDE_FROM_ABI wbuffer_convert() : wbuffer_convert(nullptr) {}
3357 explicit _LIBCPP_HIDE_FROM_ABI
3358 wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt = new _Codecvt, state_type __state = state_type());
3360 _LIBCPP_EXPLICIT_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI
3361 wbuffer_convert(streambuf* __bytebuf = nullptr, _Codecvt* __pcvt = new _Codecvt, state_type __state = state_type());
3364 _LIBCPP_HIDE_FROM_ABI ~wbuffer_convert();
3366 _LIBCPP_HIDE_FROM_ABI streambuf* rdbuf() const { return __bufptr_; }
3367 _LIBCPP_HIDE_FROM_ABI streambuf* rdbuf(streambuf* __bytebuf) {
3368 streambuf* __r = __bufptr_;
3369 __bufptr_ = __bytebuf;
3373 _LIBCPP_HIDE_FROM_ABI state_type state() const { return __st_; }
3376 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual int_type underflow();
3377 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual int_type pbackfail(int_type __c = traits_type::eof());
3378 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual int_type overflow(int_type __c = traits_type::eof());
3379 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
3380 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual pos_type
3381 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __wch = ios_base::in | ios_base::out);
3382 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual pos_type
3383 seekpos(pos_type __sp, ios_base::openmode __wch = ios_base::in | ios_base::out);
3384 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual int sync();
3387 _LIBCPP_HIDE_FROM_ABI_VIRTUAL bool __read_mode();
3388 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __write_mode();
3389 _LIBCPP_HIDE_FROM_ABI_VIRTUAL wbuffer_convert* __close();
3392 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
3393 template <class _Codecvt, class _Elem, class _Tr>
3394 wbuffer_convert<_Codecvt, _Elem, _Tr>::wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt, state_type __state)
3395 : __extbuf_(nullptr),
3396 __extbufnext_(nullptr),
3397 __extbufend_(nullptr),
3401 __bufptr_(__bytebuf),
3407 __always_noconv_(__cv_ ? __cv_->always_noconv() : false) {
3411 template <class _Codecvt, class _Elem, class _Tr>
3412 wbuffer_convert<_Codecvt, _Elem, _Tr>::~wbuffer_convert() {
3421 template <class _Codecvt, class _Elem, class _Tr>
3422 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type wbuffer_convert<_Codecvt, _Elem, _Tr>::underflow() {
3423 _LIBCPP_SUPPRESS_DEPRECATED_POP
3424 if (__cv_ == 0 || __bufptr_ == nullptr)
3425 return traits_type::eof();
3426 bool __initial = __read_mode();
3428 if (this->gptr() == 0)
3429 this->setg(&__1buf, &__1buf + 1, &__1buf + 1);
3430 const size_t __unget_sz = __initial ? 0 : std::min<size_t>((this->egptr() - this->eback()) / 2, 4);
3431 int_type __c = traits_type::eof();
3432 if (this->gptr() == this->egptr()) {
3433 std::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
3434 if (__always_noconv_) {
3435 streamsize __nmemb = static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz);
3436 __nmemb = __bufptr_->sgetn((char*)this->eback() + __unget_sz, __nmemb);
3438 this->setg(this->eback(), this->eback() + __unget_sz, this->eback() + __unget_sz + __nmemb);
3439 __c = *this->gptr();
3442 if (__extbufend_ != __extbufnext_) {
3443 _LIBCPP_ASSERT_NON_NULL(__extbufnext_ != nullptr, "underflow moving from nullptr");
3444 _LIBCPP_ASSERT_NON_NULL(__extbuf_ != nullptr, "underflow moving into nullptr");
3445 std::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
3447 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
3448 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
3449 streamsize __nmemb = std::min(static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz),
3450 static_cast<streamsize>(__extbufend_ - __extbufnext_));
3451 codecvt_base::result __r;
3452 // FIXME: Do we ever need to restore the state here?
3453 // state_type __svs = __st_;
3454 streamsize __nr = __bufptr_->sgetn(const_cast<char*>(__extbufnext_), __nmemb);
3456 __extbufend_ = __extbufnext_ + __nr;
3459 __st_, __extbuf_, __extbufend_, __extbufnext_, this->eback() + __unget_sz, this->egptr(), __inext);
3460 if (__r == codecvt_base::noconv) {
3461 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)const_cast<char*>(__extbufend_));
3462 __c = *this->gptr();
3463 } else if (__inext != this->eback() + __unget_sz) {
3464 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
3465 __c = *this->gptr();
3470 __c = *this->gptr();
3471 if (this->eback() == &__1buf)
3472 this->setg(0, 0, 0);
3476 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
3477 template <class _Codecvt, class _Elem, class _Tr>
3478 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
3479 wbuffer_convert<_Codecvt, _Elem, _Tr>::pbackfail(int_type __c) {
3480 _LIBCPP_SUPPRESS_DEPRECATED_POP
3481 if (__cv_ != 0 && __bufptr_ && this->eback() < this->gptr()) {
3482 if (traits_type::eq_int_type(__c, traits_type::eof())) {
3484 return traits_type::not_eof(__c);
3486 if (traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1])) {
3488 *this->gptr() = traits_type::to_char_type(__c);
3492 return traits_type::eof();
3495 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
3496 template <class _Codecvt, class _Elem, class _Tr>
3497 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type wbuffer_convert<_Codecvt, _Elem, _Tr>::overflow(int_type __c) {
3498 _LIBCPP_SUPPRESS_DEPRECATED_POP
3499 if (__cv_ == 0 || !__bufptr_)
3500 return traits_type::eof();
3503 char_type* __pb_save = this->pbase();
3504 char_type* __epb_save = this->epptr();
3505 if (!traits_type::eq_int_type(__c, traits_type::eof())) {
3506 if (this->pptr() == 0)
3507 this->setp(&__1buf, &__1buf + 1);
3508 *this->pptr() = traits_type::to_char_type(__c);
3511 if (this->pptr() != this->pbase()) {
3512 if (__always_noconv_) {
3513 streamsize __nmemb = static_cast<streamsize>(this->pptr() - this->pbase());
3514 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
3515 return traits_type::eof();
3517 char* __extbe = __extbuf_;
3518 codecvt_base::result __r;
3520 const char_type* __e;
3521 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e, __extbuf_, __extbuf_ + __ebs_, __extbe);
3522 if (__e == this->pbase())
3523 return traits_type::eof();
3524 if (__r == codecvt_base::noconv) {
3525 streamsize __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
3526 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
3527 return traits_type::eof();
3528 } else if (__r == codecvt_base::ok || __r == codecvt_base::partial) {
3529 streamsize __nmemb = static_cast<size_t>(__extbe - __extbuf_);
3530 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
3531 return traits_type::eof();
3532 if (__r == codecvt_base::partial) {
3533 this->setp(const_cast<char_type*>(__e), this->pptr());
3534 this->__pbump(this->epptr() - this->pbase());
3537 return traits_type::eof();
3538 } while (__r == codecvt_base::partial);
3540 this->setp(__pb_save, __epb_save);
3542 return traits_type::not_eof(__c);
3545 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
3546 template <class _Codecvt, class _Elem, class _Tr>
3547 basic_streambuf<_Elem, _Tr>* wbuffer_convert<_Codecvt, _Elem, _Tr>::setbuf(char_type* __s, streamsize __n) {
3548 _LIBCPP_SUPPRESS_DEPRECATED_POP
3549 this->setg(0, 0, 0);
3556 if (__ebs_ > sizeof(__extbuf_min_)) {
3557 if (__always_noconv_ && __s) {
3558 __extbuf_ = (char*)__s;
3561 __extbuf_ = new char[__ebs_];
3565 __extbuf_ = __extbuf_min_;
3566 __ebs_ = sizeof(__extbuf_min_);
3569 if (!__always_noconv_) {
3570 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
3571 if (__s && __ibs_ >= sizeof(__extbuf_min_)) {
3575 __intbuf_ = new char_type[__ibs_];
3586 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
3587 template <class _Codecvt, class _Elem, class _Tr>
3588 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
3589 wbuffer_convert<_Codecvt, _Elem, _Tr>::seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __om) {
3590 int __width = __cv_->encoding();
3591 if (__cv_ == 0 || !__bufptr_ || (__width <= 0 && __off != 0) || sync())
3592 return pos_type(off_type(-1));
3593 // __width > 0 || __off == 0, now check __way
3594 if (__way != ios_base::beg && __way != ios_base::cur && __way != ios_base::end)
3595 return pos_type(off_type(-1));
3596 pos_type __r = __bufptr_->pubseekoff(__width * __off, __way, __om);
3601 template <class _Codecvt, class _Elem, class _Tr>
3602 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
3603 wbuffer_convert<_Codecvt, _Elem, _Tr>::seekpos(pos_type __sp, ios_base::openmode __wch) {
3604 if (__cv_ == 0 || !__bufptr_ || sync())
3605 return pos_type(off_type(-1));
3606 if (__bufptr_->pubseekpos(__sp, __wch) == pos_type(off_type(-1)))
3607 return pos_type(off_type(-1));
3611 template <class _Codecvt, class _Elem, class _Tr>
3612 int wbuffer_convert<_Codecvt, _Elem, _Tr>::sync() {
3613 _LIBCPP_SUPPRESS_DEPRECATED_POP
3614 if (__cv_ == 0 || !__bufptr_)
3616 if (__cm_ & ios_base::out) {
3617 if (this->pptr() != this->pbase())
3618 if (overflow() == traits_type::eof())
3620 codecvt_base::result __r;
3623 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
3624 streamsize __nmemb = static_cast<streamsize>(__extbe - __extbuf_);
3625 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
3627 } while (__r == codecvt_base::partial);
3628 if (__r == codecvt_base::error)
3630 if (__bufptr_->pubsync())
3632 } else if (__cm_ & ios_base::in) {
3634 if (__always_noconv_)
3635 __c = this->egptr() - this->gptr();
3637 int __width = __cv_->encoding();
3638 __c = __extbufend_ - __extbufnext_;
3640 __c += __width * (this->egptr() - this->gptr());
3642 if (this->gptr() != this->egptr()) {
3643 std::reverse(this->gptr(), this->egptr());
3644 codecvt_base::result __r;
3645 const char_type* __e = this->gptr();
3648 __r = __cv_->out(__st_, __e, this->egptr(), __e, __extbuf_, __extbuf_ + __ebs_, __extbe);
3650 case codecvt_base::noconv:
3651 __c += this->egptr() - this->gptr();
3653 case codecvt_base::ok:
3654 case codecvt_base::partial:
3655 __c += __extbe - __extbuf_;
3660 } while (__r == codecvt_base::partial);
3664 if (__bufptr_->pubseekoff(-__c, ios_base::cur, __cm_) == pos_type(off_type(-1)))
3666 this->setg(0, 0, 0);
3672 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
3673 template <class _Codecvt, class _Elem, class _Tr>
3674 bool wbuffer_convert<_Codecvt, _Elem, _Tr>::__read_mode() {
3675 if (!(__cm_ & ios_base::in)) {
3677 if (__always_noconv_)
3678 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_ + __ebs_, (char_type*)__extbuf_ + __ebs_);
3680 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
3681 __cm_ = ios_base::in;
3687 template <class _Codecvt, class _Elem, class _Tr>
3688 void wbuffer_convert<_Codecvt, _Elem, _Tr>::__write_mode() {
3689 if (!(__cm_ & ios_base::out)) {
3690 this->setg(0, 0, 0);
3691 if (__ebs_ > sizeof(__extbuf_min_)) {
3692 if (__always_noconv_)
3693 this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__ebs_ - 1));
3695 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
3698 __cm_ = ios_base::out;
3702 template <class _Codecvt, class _Elem, class _Tr>
3703 wbuffer_convert<_Codecvt, _Elem, _Tr>* wbuffer_convert<_Codecvt, _Elem, _Tr>::__close() {
3704 wbuffer_convert* __rt = nullptr;
3705 if (__cv_ != nullptr && __bufptr_ != nullptr) {
3707 if ((__cm_ & ios_base::out) && sync())
3713 _LIBCPP_SUPPRESS_DEPRECATED_POP
3715 _LIBCPP_END_NAMESPACE_STD
3719 // NOLINTEND(libcpp-robust-against-adl)
3721 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
3723 # include <concepts>
3725 # include <iterator>
3727 # include <stdexcept>
3728 # include <type_traits>
3729 # include <typeinfo>
3732 #endif // _LIBCPP_LOCALE