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>>
87 class wstring_convert // Removed in C++26
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 // Removed in C++26
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 #if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
191 # include <__cxx03/locale>
195 # if _LIBCPP_HAS_LOCALIZATION
197 # include <__algorithm/copy.h>
198 # include <__algorithm/equal.h>
199 # include <__algorithm/find.h>
200 # include <__algorithm/max.h>
201 # include <__algorithm/reverse.h>
202 # include <__algorithm/unwrap_iter.h>
204 # include <__iterator/access.h>
205 # include <__iterator/back_insert_iterator.h>
206 # include <__iterator/istreambuf_iterator.h>
207 # include <__iterator/ostreambuf_iterator.h>
209 # include <__locale_dir/pad_and_output.h>
210 # include <__memory/unique_ptr.h>
211 # include <__new/exceptions.h>
212 # include <__type_traits/make_unsigned.h>
219 # include <streambuf>
222 // TODO: Properly qualify calls now that __bsd_locale_defaults.h defines functions instead of macros
223 // NOLINTBEGIN(libcpp-robust-against-adl)
225 # if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
226 // Most unix variants have catopen. These are the specific ones that don't.
227 # if !defined(__BIONIC__) && !defined(_NEWLIB_VERSION) && !defined(__EMSCRIPTEN__)
228 # define _LIBCPP_HAS_CATOPEN 1
229 # include <nl_types.h>
231 # define _LIBCPP_HAS_CATOPEN 0
234 # define _LIBCPP_HAS_CATOPEN 0
237 # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
238 # pragma GCC system_header
242 # include <__undef_macros>
244 _LIBCPP_BEGIN_NAMESPACE_STD
246 # if defined(__APPLE__) || defined(__FreeBSD__)
247 # define _LIBCPP_GET_C_LOCALE 0
248 # elif defined(__NetBSD__)
249 # define _LIBCPP_GET_C_LOCALE LC_C_LOCALE
251 # define _LIBCPP_GET_C_LOCALE __cloc()
252 // Get the C locale object
253 _LIBCPP_EXPORTED_FROM_ABI __locale::__locale_t __cloc();
254 # define __cloc_defined
258 // Scans [__b, __e) until a match is found in the basic_strings range
259 // [__kb, __ke) or until it can be shown that there is no match in [__kb, __ke).
260 // __b will be incremented (visibly), consuming CharT until a match is found
261 // or proved to not exist. A keyword may be "", in which will match anything.
262 // If one keyword is a prefix of another, and the next CharT in the input
263 // might match another keyword, the algorithm will attempt to find the longest
264 // matching keyword. If the longer matching keyword ends up not matching, then
265 // no keyword match is found. If no keyword match is found, __ke is returned
266 // and failbit is set in __err.
267 // Else an iterator pointing to the matching keyword is found. If more than
268 // one keyword matches, an iterator to the first matching keyword is returned.
269 // If on exit __b == __e, eofbit is set in __err. If __case_sensitive is false,
270 // __ct is used to force to lower case before comparing characters.
272 // Keywords: "a", "abb"
273 // If the input is "a", the first keyword matches and eofbit is set.
274 // If the input is "abc", no match is found and "ab" are consumed.
275 template <class _InputIterator, class _ForwardIterator, class _Ctype>
276 _LIBCPP_HIDE_FROM_ABI _ForwardIterator __scan_keyword(
279 _ForwardIterator __kb,
280 _ForwardIterator __ke,
282 ios_base::iostate& __err,
283 bool __case_sensitive = true) {
284 typedef typename iterator_traits<_InputIterator>::value_type _CharT;
285 size_t __nkw = static_cast<size_t>(std::distance(__kb, __ke));
286 const unsigned char __doesnt_match = '\0';
287 const unsigned char __might_match = '\1';
288 const unsigned char __does_match = '\2';
289 unsigned char __statbuf[100];
290 unsigned char* __status = __statbuf;
291 unique_ptr<unsigned char, void (*)(void*)> __stat_hold(nullptr, free);
292 if (__nkw > sizeof(__statbuf)) {
293 __status = (unsigned char*)malloc(__nkw);
294 if (__status == nullptr)
296 __stat_hold.reset(__status);
298 size_t __n_might_match = __nkw; // At this point, any keyword might match
299 size_t __n_does_match = 0; // but none of them definitely do
300 // Initialize all statuses to __might_match, except for "" keywords are __does_match
301 unsigned char* __st = __status;
302 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void)++__st) {
304 *__st = __might_match;
306 *__st = __does_match;
311 // While there might be a match, test keywords against the next CharT
312 for (size_t __indx = 0; __b != __e && __n_might_match > 0; ++__indx) {
313 // Peek at the next CharT but don't consume it
315 if (!__case_sensitive)
316 __c = __ct.toupper(__c);
317 bool __consume = false;
318 // For each keyword which might match, see if the __indx character is __c
319 // If a match if found, consume __c
320 // If a match is found, and that is the last character in the keyword,
321 // then that keyword matches.
322 // If the keyword doesn't match this character, then change the keyword
325 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void)++__st) {
326 if (*__st == __might_match) {
327 _CharT __kc = (*__ky)[__indx];
328 if (!__case_sensitive)
329 __kc = __ct.toupper(__kc);
332 if (__ky->size() == __indx + 1) {
333 *__st = __does_match;
338 *__st = __doesnt_match;
343 // consume if we matched a character
346 // If we consumed a character and there might be a matched keyword that
347 // was marked matched on a previous iteration, then such keywords
348 // which are now marked as not matching.
349 if (__n_might_match + __n_does_match > 1) {
351 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void)++__st) {
352 if (*__st == __does_match && __ky->size() != __indx + 1) {
353 *__st = __doesnt_match;
360 // We've exited the loop because we hit eof and/or we have no more "might matches".
362 __err |= ios_base::eofbit;
363 // Return the first matching result
364 for (__st = __status; __kb != __ke; ++__kb, (void)++__st)
365 if (*__st == __does_match)
368 __err |= ios_base::failbit;
372 struct _LIBCPP_EXPORTED_FROM_ABI __num_get_base {
373 static const int __num_get_buf_sz = 40;
375 static int __get_base(ios_base&);
376 static const char __src[33]; // "0123456789abcdefABCDEFxX+-pPiInN"
377 // count of leading characters in __src used for parsing integers ("012..X+-")
378 static const size_t __int_chr_cnt = 26;
379 // count of leading characters in __src used for parsing floating-point values ("012..-pP")
380 static const size_t __fp_chr_cnt = 28;
383 _LIBCPP_EXPORTED_FROM_ABI void
384 __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end, ios_base::iostate& __err);
386 template <class _CharT>
387 struct __num_get : protected __num_get_base {
388 static string __stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point, _CharT& __thousands_sep);
390 static int __stage2_float_loop(
396 _CharT __decimal_point,
397 _CharT __thousands_sep,
398 const string& __grouping,
403 # ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
404 static string __stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep);
405 static int __stage2_int_loop(
411 _CharT __thousands_sep,
412 const string& __grouping,
418 static string __stage2_int_prep(ios_base& __iob, _CharT& __thousands_sep) {
419 locale __loc = __iob.getloc();
420 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
421 __thousands_sep = __np.thousands_sep();
422 return __np.grouping();
425 const _CharT* __do_widen(ios_base& __iob, _CharT* __atoms) const { return __do_widen_p(__iob, __atoms); }
427 static int __stage2_int_loop(
433 _CharT __thousands_sep,
434 const string& __grouping,
437 const _CharT* __atoms);
440 template <typename _Tp>
441 const _Tp* __do_widen_p(ios_base& __iob, _Tp* __atoms) const {
442 locale __loc = __iob.getloc();
443 use_facet<ctype<_Tp> >(__loc).widen(__src, __src + __int_chr_cnt, __atoms);
447 const char* __do_widen_p(ios_base& __iob, char* __atoms) const {
455 # ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
456 template <class _CharT>
457 string __num_get<_CharT>::__stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep) {
458 locale __loc = __iob.getloc();
459 std::use_facet<ctype<_CharT> >(__loc).widen(__src, __src + __int_chr_cnt, __atoms);
460 const numpunct<_CharT>& __np = std::use_facet<numpunct<_CharT> >(__loc);
461 __thousands_sep = __np.thousands_sep();
462 return __np.grouping();
466 template <class _CharT>
467 string __num_get<_CharT>::__stage2_float_prep(
468 ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point, _CharT& __thousands_sep) {
469 locale __loc = __iob.getloc();
470 std::use_facet<ctype<_CharT> >(__loc).widen(__src, __src + __fp_chr_cnt, __atoms);
471 const numpunct<_CharT>& __np = std::use_facet<numpunct<_CharT> >(__loc);
472 __decimal_point = __np.decimal_point();
473 __thousands_sep = __np.thousands_sep();
474 return __np.grouping();
477 template <class _CharT>
479 # ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
480 __num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
481 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
482 unsigned* __g, unsigned*& __g_end, _CharT* __atoms)
484 __num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
485 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
486 unsigned* __g, unsigned*& __g_end, const _CharT* __atoms)
490 if (__a_end == __a && (__ct == __atoms[24] || __ct == __atoms[25])) {
491 *__a_end++ = __ct == __atoms[24] ? '+' : '-';
495 if (__grouping.size() != 0 && __ct == __thousands_sep) {
496 if (__g_end - __g < __num_get_buf_sz) {
502 ptrdiff_t __f = std::find(__atoms, __atoms + __int_chr_cnt, __ct) - __atoms;
514 if (__a_end != __a && __a_end - __a <= 2 && __a_end[-1] == '0') {
516 *__a_end++ = __src[__f];
521 *__a_end++ = __src[__f];
526 template <class _CharT>
527 int __num_get<_CharT>::__stage2_float_loop(
533 _CharT __decimal_point,
534 _CharT __thousands_sep,
535 const string& __grouping,
540 if (__ct == __decimal_point) {
545 if (__grouping.size() != 0 && __g_end - __g < __num_get_buf_sz)
549 if (__ct == __thousands_sep && __grouping.size() != 0) {
552 if (__g_end - __g < __num_get_buf_sz) {
558 ptrdiff_t __f = std::find(__atoms, __atoms + __num_get_base::__fp_chr_cnt, __ct) - __atoms;
559 if (__f >= static_cast<ptrdiff_t>(__num_get_base::__fp_chr_cnt))
561 char __x = __src[__f];
562 if (__x == '-' || __x == '+') {
563 if (__a_end == __a || (std::toupper(__a_end[-1]) == std::toupper(__exp))) {
569 if (__x == 'x' || __x == 'X')
571 else if (std::toupper(__x) == __exp) {
572 __exp = std::tolower(__exp);
575 if (__grouping.size() != 0 && __g_end - __g < __num_get_buf_sz)
586 extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<char>;
587 # if _LIBCPP_HAS_WIDE_CHARACTERS
588 extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<wchar_t>;
591 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
592 class _LIBCPP_TEMPLATE_VIS num_get : public locale::facet, private __num_get<_CharT> {
594 typedef _CharT char_type;
595 typedef _InputIterator iter_type;
597 _LIBCPP_HIDE_FROM_ABI explicit num_get(size_t __refs = 0) : locale::facet(__refs) {}
599 _LIBCPP_HIDE_FROM_ABI iter_type
600 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, bool& __v) const {
601 return do_get(__b, __e, __iob, __err, __v);
604 _LIBCPP_HIDE_FROM_ABI iter_type
605 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long& __v) const {
606 return do_get(__b, __e, __iob, __err, __v);
609 _LIBCPP_HIDE_FROM_ABI iter_type
610 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long long& __v) const {
611 return do_get(__b, __e, __iob, __err, __v);
614 _LIBCPP_HIDE_FROM_ABI iter_type
615 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned short& __v) const {
616 return do_get(__b, __e, __iob, __err, __v);
619 _LIBCPP_HIDE_FROM_ABI iter_type
620 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned int& __v) const {
621 return do_get(__b, __e, __iob, __err, __v);
624 _LIBCPP_HIDE_FROM_ABI iter_type
625 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned long& __v) const {
626 return do_get(__b, __e, __iob, __err, __v);
629 _LIBCPP_HIDE_FROM_ABI iter_type
630 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned long long& __v) const {
631 return do_get(__b, __e, __iob, __err, __v);
634 _LIBCPP_HIDE_FROM_ABI iter_type
635 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, float& __v) const {
636 return do_get(__b, __e, __iob, __err, __v);
639 _LIBCPP_HIDE_FROM_ABI iter_type
640 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, double& __v) const {
641 return do_get(__b, __e, __iob, __err, __v);
644 _LIBCPP_HIDE_FROM_ABI iter_type
645 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long double& __v) const {
646 return do_get(__b, __e, __iob, __err, __v);
649 _LIBCPP_HIDE_FROM_ABI iter_type
650 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, void*& __v) const {
651 return do_get(__b, __e, __iob, __err, __v);
654 static locale::id id;
657 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~num_get() override {}
660 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS iter_type
661 __do_get_floating_point(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, _Fp& __v) const;
663 template <class _Signed>
664 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS iter_type
665 __do_get_signed(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, _Signed& __v) const;
667 template <class _Unsigned>
668 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS iter_type
669 __do_get_unsigned(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, _Unsigned& __v) const;
671 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, bool& __v) const;
673 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long& __v) const {
674 return this->__do_get_signed(__b, __e, __iob, __err, __v);
678 do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long long& __v) const {
679 return this->__do_get_signed(__b, __e, __iob, __err, __v);
683 do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned short& __v) const {
684 return this->__do_get_unsigned(__b, __e, __iob, __err, __v);
688 do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned int& __v) const {
689 return this->__do_get_unsigned(__b, __e, __iob, __err, __v);
693 do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned long& __v) const {
694 return this->__do_get_unsigned(__b, __e, __iob, __err, __v);
698 do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned long long& __v) const {
699 return this->__do_get_unsigned(__b, __e, __iob, __err, __v);
702 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, float& __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, double& __v) const {
707 return this->__do_get_floating_point(__b, __e, __iob, __err, __v);
711 do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long double& __v) const {
712 return this->__do_get_floating_point(__b, __e, __iob, __err, __v);
715 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, void*& __v) const;
718 template <class _CharT, class _InputIterator>
719 locale::id num_get<_CharT, _InputIterator>::id;
722 _LIBCPP_HIDE_FROM_ABI _Tp
723 __num_get_signed_integral(const char* __a, const char* __a_end, ios_base::iostate& __err, int __base) {
724 if (__a != __a_end) {
725 __libcpp_remove_reference_t<decltype(errno)> __save_errno = errno;
728 long long __ll = __locale::__strtoll(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
729 __libcpp_remove_reference_t<decltype(errno)> __current_errno = errno;
730 if (__current_errno == 0)
731 errno = __save_errno;
732 if (__p2 != __a_end) {
733 __err = ios_base::failbit;
735 } else if (__current_errno == ERANGE || __ll < numeric_limits<_Tp>::min() || numeric_limits<_Tp>::max() < __ll) {
736 __err = ios_base::failbit;
738 return numeric_limits<_Tp>::max();
740 return numeric_limits<_Tp>::min();
742 return static_cast<_Tp>(__ll);
744 __err = ios_base::failbit;
749 _LIBCPP_HIDE_FROM_ABI _Tp
750 __num_get_unsigned_integral(const char* __a, const char* __a_end, ios_base::iostate& __err, int __base) {
751 if (__a != __a_end) {
752 const bool __negate = *__a == '-';
753 if (__negate && ++__a == __a_end) {
754 __err = ios_base::failbit;
757 __libcpp_remove_reference_t<decltype(errno)> __save_errno = errno;
760 unsigned long long __ll = __locale::__strtoull(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
761 __libcpp_remove_reference_t<decltype(errno)> __current_errno = errno;
762 if (__current_errno == 0)
763 errno = __save_errno;
764 if (__p2 != __a_end) {
765 __err = ios_base::failbit;
767 } else if (__current_errno == ERANGE || numeric_limits<_Tp>::max() < __ll) {
768 __err = ios_base::failbit;
769 return numeric_limits<_Tp>::max();
771 _Tp __res = static_cast<_Tp>(__ll);
776 __err = ios_base::failbit;
781 _LIBCPP_HIDE_FROM_ABI _Tp __do_strtod(const char* __a, char** __p2);
784 inline _LIBCPP_HIDE_FROM_ABI float __do_strtod<float>(const char* __a, char** __p2) {
785 return __locale::__strtof(__a, __p2, _LIBCPP_GET_C_LOCALE);
789 inline _LIBCPP_HIDE_FROM_ABI double __do_strtod<double>(const char* __a, char** __p2) {
790 return __locale::__strtod(__a, __p2, _LIBCPP_GET_C_LOCALE);
794 inline _LIBCPP_HIDE_FROM_ABI long double __do_strtod<long double>(const char* __a, char** __p2) {
795 return __locale::__strtold(__a, __p2, _LIBCPP_GET_C_LOCALE);
799 _LIBCPP_HIDE_FROM_ABI _Tp __num_get_float(const char* __a, const char* __a_end, ios_base::iostate& __err) {
800 if (__a != __a_end) {
801 __libcpp_remove_reference_t<decltype(errno)> __save_errno = errno;
804 _Tp __ld = std::__do_strtod<_Tp>(__a, &__p2);
805 __libcpp_remove_reference_t<decltype(errno)> __current_errno = errno;
806 if (__current_errno == 0)
807 errno = __save_errno;
808 if (__p2 != __a_end) {
809 __err = ios_base::failbit;
811 } else if (__current_errno == ERANGE)
812 __err = ios_base::failbit;
815 __err = ios_base::failbit;
819 template <class _CharT, class _InputIterator>
820 _InputIterator num_get<_CharT, _InputIterator>::do_get(
821 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, bool& __v) const {
822 if ((__iob.flags() & ios_base::boolalpha) == 0) {
824 __b = do_get(__b, __e, __iob, __err, __lv);
834 __err = ios_base::failbit;
839 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__iob.getloc());
840 const numpunct<_CharT>& __np = std::use_facet<numpunct<_CharT> >(__iob.getloc());
841 typedef typename numpunct<_CharT>::string_type string_type;
842 const string_type __names[2] = {__np.truename(), __np.falsename()};
843 const string_type* __i = std::__scan_keyword(__b, __e, __names, __names + 2, __ct, __err);
844 __v = __i == __names;
850 template <class _CharT, class _InputIterator>
851 template <class _Signed>
852 _InputIterator num_get<_CharT, _InputIterator>::__do_get_signed(
853 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, _Signed& __v) const {
855 int __base = this->__get_base(__iob);
857 char_type __thousands_sep;
858 const int __atoms_size = __num_get_base::__int_chr_cnt;
859 # ifdef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
860 char_type __atoms1[__atoms_size];
861 const char_type* __atoms = this->__do_widen(__iob, __atoms1);
862 string __grouping = this->__stage2_int_prep(__iob, __thousands_sep);
864 char_type __atoms[__atoms_size];
865 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
868 __buf.resize(__buf.capacity());
869 char* __a = &__buf[0];
871 unsigned __g[__num_get_base::__num_get_buf_sz];
872 unsigned* __g_end = __g;
874 for (; __b != __e; ++__b) {
875 if (__a_end == __a + __buf.size()) {
876 size_t __tmp = __buf.size();
877 __buf.resize(2 * __buf.size());
878 __buf.resize(__buf.capacity());
880 __a_end = __a + __tmp;
882 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc, __thousands_sep, __grouping, __g, __g_end, __atoms))
885 if (__grouping.size() != 0 && __g_end - __g < __num_get_base::__num_get_buf_sz)
888 __v = std::__num_get_signed_integral<_Signed>(__a, __a_end, __err, __base);
889 // Digit grouping checked
890 __check_grouping(__grouping, __g, __g_end, __err);
893 __err |= ios_base::eofbit;
899 template <class _CharT, class _InputIterator>
900 template <class _Unsigned>
901 _InputIterator num_get<_CharT, _InputIterator>::__do_get_unsigned(
902 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, _Unsigned& __v) const {
904 int __base = this->__get_base(__iob);
906 char_type __thousands_sep;
907 const int __atoms_size = __num_get_base::__int_chr_cnt;
908 # ifdef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
909 char_type __atoms1[__atoms_size];
910 const char_type* __atoms = this->__do_widen(__iob, __atoms1);
911 string __grouping = this->__stage2_int_prep(__iob, __thousands_sep);
913 char_type __atoms[__atoms_size];
914 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
917 __buf.resize(__buf.capacity());
918 char* __a = &__buf[0];
920 unsigned __g[__num_get_base::__num_get_buf_sz];
921 unsigned* __g_end = __g;
923 for (; __b != __e; ++__b) {
924 if (__a_end == __a + __buf.size()) {
925 size_t __tmp = __buf.size();
926 __buf.resize(2 * __buf.size());
927 __buf.resize(__buf.capacity());
929 __a_end = __a + __tmp;
931 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc, __thousands_sep, __grouping, __g, __g_end, __atoms))
934 if (__grouping.size() != 0 && __g_end - __g < __num_get_base::__num_get_buf_sz)
937 __v = std::__num_get_unsigned_integral<_Unsigned>(__a, __a_end, __err, __base);
938 // Digit grouping checked
939 __check_grouping(__grouping, __g, __g_end, __err);
942 __err |= ios_base::eofbit;
948 template <class _CharT, class _InputIterator>
950 _InputIterator num_get<_CharT, _InputIterator>::__do_get_floating_point(
951 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, _Fp& __v) const {
952 // Stage 1, nothing to do
954 char_type __atoms[__num_get_base::__fp_chr_cnt];
955 char_type __decimal_point;
956 char_type __thousands_sep;
957 string __grouping = this->__stage2_float_prep(__iob, __atoms, __decimal_point, __thousands_sep);
959 __buf.resize(__buf.capacity());
960 char* __a = &__buf[0];
962 unsigned __g[__num_get_base::__num_get_buf_sz];
963 unsigned* __g_end = __g;
965 bool __in_units = true;
967 bool __is_leading_parsed = false;
968 for (; __b != __e; ++__b) {
969 if (__a_end == __a + __buf.size()) {
970 size_t __tmp = __buf.size();
971 __buf.resize(2 * __buf.size());
972 __buf.resize(__buf.capacity());
974 __a_end = __a + __tmp;
976 if (this->__stage2_float_loop(
991 // the leading character excluding the sign must be a decimal digit
992 if (!__is_leading_parsed) {
993 if (__a_end - __a >= 1 && __a[0] != '-' && __a[0] != '+') {
994 if (('0' <= __a[0] && __a[0] <= '9') || __a[0] == '.')
995 __is_leading_parsed = true;
998 } else if (__a_end - __a >= 2 && (__a[0] == '-' || __a[0] == '+')) {
999 if (('0' <= __a[1] && __a[1] <= '9') || __a[1] == '.')
1000 __is_leading_parsed = true;
1006 if (__grouping.size() != 0 && __in_units && __g_end - __g < __num_get_base::__num_get_buf_sz)
1009 __v = std::__num_get_float<_Fp>(__a, __a_end, __err);
1010 // Digit grouping checked
1011 __check_grouping(__grouping, __g, __g_end, __err);
1014 __err |= ios_base::eofbit;
1018 template <class _CharT, class _InputIterator>
1019 _InputIterator num_get<_CharT, _InputIterator>::do_get(
1020 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, void*& __v) const {
1024 char_type __atoms[__num_get_base::__int_chr_cnt];
1025 char_type __thousands_sep = char_type();
1027 std::use_facet<ctype<_CharT> >(__iob.getloc())
1028 .widen(__num_get_base::__src, __num_get_base::__src + __num_get_base::__int_chr_cnt, __atoms);
1030 __buf.resize(__buf.capacity());
1031 char* __a = &__buf[0];
1032 char* __a_end = __a;
1033 unsigned __g[__num_get_base::__num_get_buf_sz];
1034 unsigned* __g_end = __g;
1036 for (; __b != __e; ++__b) {
1037 if (__a_end == __a + __buf.size()) {
1038 size_t __tmp = __buf.size();
1039 __buf.resize(2 * __buf.size());
1040 __buf.resize(__buf.capacity());
1042 __a_end = __a + __tmp;
1044 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc, __thousands_sep, __grouping, __g, __g_end, __atoms))
1048 __buf.resize(__a_end - __a);
1049 if (__locale::__sscanf(__buf.c_str(), _LIBCPP_GET_C_LOCALE, "%p", &__v) != 1)
1050 __err = ios_base::failbit;
1053 __err |= ios_base::eofbit;
1057 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<char>;
1058 # if _LIBCPP_HAS_WIDE_CHARACTERS
1059 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<wchar_t>;
1062 struct _LIBCPP_EXPORTED_FROM_ABI __num_put_base {
1064 static void __format_int(char* __fmt, const char* __len, bool __signd, ios_base::fmtflags __flags);
1065 static bool __format_float(char* __fmt, const char* __len, ios_base::fmtflags __flags);
1066 static char* __identify_padding(char* __nb, char* __ne, const ios_base& __iob);
1069 template <class _CharT>
1070 struct __num_put : protected __num_put_base {
1071 static void __widen_and_group_int(
1072 char* __nb, char* __np, char* __ne, _CharT* __ob, _CharT*& __op, _CharT*& __oe, const locale& __loc);
1073 static void __widen_and_group_float(
1074 char* __nb, char* __np, char* __ne, _CharT* __ob, _CharT*& __op, _CharT*& __oe, const locale& __loc);
1077 template <class _CharT>
1078 void __num_put<_CharT>::__widen_and_group_int(
1079 char* __nb, char* __np, char* __ne, _CharT* __ob, _CharT*& __op, _CharT*& __oe, const locale& __loc) {
1080 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__loc);
1081 const numpunct<_CharT>& __npt = std::use_facet<numpunct<_CharT> >(__loc);
1082 string __grouping = __npt.grouping();
1083 if (__grouping.empty()) {
1084 __ct.widen(__nb, __ne, __ob);
1085 __oe = __ob + (__ne - __nb);
1089 if (*__nf == '-' || *__nf == '+')
1090 *__oe++ = __ct.widen(*__nf++);
1091 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' || __nf[1] == 'X')) {
1092 *__oe++ = __ct.widen(*__nf++);
1093 *__oe++ = __ct.widen(*__nf++);
1095 std::reverse(__nf, __ne);
1096 _CharT __thousands_sep = __npt.thousands_sep();
1099 for (char* __p = __nf; __p < __ne; ++__p) {
1100 if (static_cast<unsigned>(__grouping[__dg]) > 0 && __dc == static_cast<unsigned>(__grouping[__dg])) {
1101 *__oe++ = __thousands_sep;
1103 if (__dg < __grouping.size() - 1)
1106 *__oe++ = __ct.widen(*__p);
1109 std::reverse(__ob + (__nf - __nb), __oe);
1114 __op = __ob + (__np - __nb);
1117 template <class _CharT>
1118 void __num_put<_CharT>::__widen_and_group_float(
1119 char* __nb, char* __np, char* __ne, _CharT* __ob, _CharT*& __op, _CharT*& __oe, const locale& __loc) {
1120 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__loc);
1121 const numpunct<_CharT>& __npt = std::use_facet<numpunct<_CharT> >(__loc);
1122 string __grouping = __npt.grouping();
1125 if (*__nf == '-' || *__nf == '+')
1126 *__oe++ = __ct.widen(*__nf++);
1128 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' || __nf[1] == 'X')) {
1129 *__oe++ = __ct.widen(*__nf++);
1130 *__oe++ = __ct.widen(*__nf++);
1131 for (__ns = __nf; __ns < __ne; ++__ns)
1132 if (!__locale::__isxdigit(*__ns, _LIBCPP_GET_C_LOCALE))
1135 for (__ns = __nf; __ns < __ne; ++__ns)
1136 if (!__locale::__isdigit(*__ns, _LIBCPP_GET_C_LOCALE))
1139 if (__grouping.empty()) {
1140 __ct.widen(__nf, __ns, __oe);
1141 __oe += __ns - __nf;
1143 std::reverse(__nf, __ns);
1144 _CharT __thousands_sep = __npt.thousands_sep();
1147 for (char* __p = __nf; __p < __ns; ++__p) {
1148 if (__grouping[__dg] > 0 && __dc == static_cast<unsigned>(__grouping[__dg])) {
1149 *__oe++ = __thousands_sep;
1151 if (__dg < __grouping.size() - 1)
1154 *__oe++ = __ct.widen(*__p);
1157 std::reverse(__ob + (__nf - __nb), __oe);
1159 for (__nf = __ns; __nf < __ne; ++__nf) {
1161 *__oe++ = __npt.decimal_point();
1165 *__oe++ = __ct.widen(*__nf);
1167 __ct.widen(__nf, __ne, __oe);
1168 __oe += __ne - __nf;
1172 __op = __ob + (__np - __nb);
1175 extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<char>;
1176 # if _LIBCPP_HAS_WIDE_CHARACTERS
1177 extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<wchar_t>;
1180 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
1181 class _LIBCPP_TEMPLATE_VIS num_put : public locale::facet, private __num_put<_CharT> {
1183 typedef _CharT char_type;
1184 typedef _OutputIterator iter_type;
1186 _LIBCPP_HIDE_FROM_ABI explicit num_put(size_t __refs = 0) : locale::facet(__refs) {}
1188 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, bool __v) const {
1189 return do_put(__s, __iob, __fl, __v);
1192 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, long __v) const {
1193 return do_put(__s, __iob, __fl, __v);
1196 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, long long __v) const {
1197 return do_put(__s, __iob, __fl, __v);
1200 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long __v) const {
1201 return do_put(__s, __iob, __fl, __v);
1204 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long long __v) const {
1205 return do_put(__s, __iob, __fl, __v);
1208 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, double __v) const {
1209 return do_put(__s, __iob, __fl, __v);
1212 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, long double __v) const {
1213 return do_put(__s, __iob, __fl, __v);
1216 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, const void* __v) const {
1217 return do_put(__s, __iob, __fl, __v);
1220 static locale::id id;
1223 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~num_put() override {}
1225 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, bool __v) const;
1226 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, long __v) const;
1227 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, long long __v) const;
1228 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long) const;
1229 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long long) const;
1230 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, double __v) const;
1231 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, long double __v) const;
1232 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, const void* __v) const;
1234 template <class _Integral>
1235 _LIBCPP_HIDE_FROM_ABI inline _OutputIterator
1236 __do_put_integral(iter_type __s, ios_base& __iob, char_type __fl, _Integral __v, char const* __len) const;
1238 template <class _Float>
1239 _LIBCPP_HIDE_FROM_ABI inline _OutputIterator
1240 __do_put_floating_point(iter_type __s, ios_base& __iob, char_type __fl, _Float __v, char const* __len) const;
1243 template <class _CharT, class _OutputIterator>
1244 locale::id num_put<_CharT, _OutputIterator>::id;
1246 template <class _CharT, class _OutputIterator>
1248 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, bool __v) const {
1249 if ((__iob.flags() & ios_base::boolalpha) == 0)
1250 return do_put(__s, __iob, __fl, (unsigned long)__v);
1251 const numpunct<char_type>& __np = std::use_facet<numpunct<char_type> >(__iob.getloc());
1252 typedef typename numpunct<char_type>::string_type string_type;
1253 string_type __nm = __v ? __np.truename() : __np.falsename();
1254 for (typename string_type::iterator __i = __nm.begin(); __i != __nm.end(); ++__i, ++__s)
1259 template <class _CharT, class _OutputIterator>
1260 template <class _Integral>
1261 _LIBCPP_HIDE_FROM_ABI inline _OutputIterator num_put<_CharT, _OutputIterator>::__do_put_integral(
1262 iter_type __s, ios_base& __iob, char_type __fl, _Integral __v, char const* __len) const {
1263 // Stage 1 - Get number in narrow char
1264 char __fmt[8] = {'%', 0};
1265 this->__format_int(__fmt + 1, __len, is_signed<_Integral>::value, __iob.flags());
1266 // Worst case is octal, with showbase enabled. Note that octal is always
1267 // printed as an unsigned value.
1268 using _Unsigned = typename make_unsigned<_Integral>::type;
1269 _LIBCPP_CONSTEXPR const unsigned __nbuf =
1270 (numeric_limits<_Unsigned>::digits / 3) // 1 char per 3 bits
1271 + ((numeric_limits<_Unsigned>::digits % 3) != 0) // round up
1272 + 2; // base prefix + terminating null character
1274 _LIBCPP_DIAGNOSTIC_PUSH
1275 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
1276 _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
1277 int __nc = __locale::__snprintf(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1278 _LIBCPP_DIAGNOSTIC_POP
1279 char* __ne = __nar + __nc;
1280 char* __np = this->__identify_padding(__nar, __ne, __iob);
1281 // Stage 2 - Widen __nar while adding thousands separators
1282 char_type __o[2 * (__nbuf - 1) - 1];
1283 char_type* __op; // pad here
1284 char_type* __oe; // end of output
1285 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1286 // [__o, __oe) contains thousands_sep'd wide number
1288 return std::__pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1291 template <class _CharT, class _OutputIterator>
1293 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, long __v) const {
1294 return this->__do_put_integral(__s, __iob, __fl, __v, "l");
1297 template <class _CharT, class _OutputIterator>
1299 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, long long __v) const {
1300 return this->__do_put_integral(__s, __iob, __fl, __v, "ll");
1303 template <class _CharT, class _OutputIterator>
1305 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long __v) const {
1306 return this->__do_put_integral(__s, __iob, __fl, __v, "l");
1309 template <class _CharT, class _OutputIterator>
1311 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long long __v) const {
1312 return this->__do_put_integral(__s, __iob, __fl, __v, "ll");
1315 template <class _CharT, class _OutputIterator>
1316 template <class _Float>
1317 _LIBCPP_HIDE_FROM_ABI inline _OutputIterator num_put<_CharT, _OutputIterator>::__do_put_floating_point(
1318 iter_type __s, ios_base& __iob, char_type __fl, _Float __v, char const* __len) const {
1319 // Stage 1 - Get number in narrow char
1320 char __fmt[8] = {'%', 0};
1321 bool __specify_precision = this->__format_float(__fmt + 1, __len, __iob.flags());
1322 const unsigned __nbuf = 30;
1326 _LIBCPP_DIAGNOSTIC_PUSH
1327 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
1328 _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
1329 if (__specify_precision)
1330 __nc = __locale::__snprintf(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
1332 __nc = __locale::__snprintf(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1333 unique_ptr<char, void (*)(void*)> __nbh(nullptr, free);
1334 if (__nc > static_cast<int>(__nbuf - 1)) {
1335 if (__specify_precision)
1336 __nc = __locale::__asprintf(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
1338 __nc = __locale::__asprintf(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1340 __throw_bad_alloc();
1343 _LIBCPP_DIAGNOSTIC_POP
1344 char* __ne = __nb + __nc;
1345 char* __np = this->__identify_padding(__nb, __ne, __iob);
1346 // Stage 2 - Widen __nar while adding thousands separators
1347 char_type __o[2 * (__nbuf - 1) - 1];
1348 char_type* __ob = __o;
1349 unique_ptr<char_type, void (*)(void*)> __obh(0, free);
1350 if (__nb != __nar) {
1351 __ob = (char_type*)malloc(2 * static_cast<size_t>(__nc) * sizeof(char_type));
1353 __throw_bad_alloc();
1356 char_type* __op; // pad here
1357 char_type* __oe; // end of output
1358 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1359 // [__o, __oe) contains thousands_sep'd wide number
1361 __s = std::__pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1365 template <class _CharT, class _OutputIterator>
1367 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, double __v) const {
1368 return this->__do_put_floating_point(__s, __iob, __fl, __v, "");
1371 template <class _CharT, class _OutputIterator>
1373 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, long double __v) const {
1374 return this->__do_put_floating_point(__s, __iob, __fl, __v, "L");
1377 template <class _CharT, class _OutputIterator>
1379 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, const void* __v) const {
1380 // Stage 1 - Get pointer in narrow char
1381 const unsigned __nbuf = 20;
1383 int __nc = __locale::__snprintf(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, "%p", __v);
1384 char* __ne = __nar + __nc;
1385 char* __np = this->__identify_padding(__nar, __ne, __iob);
1386 // Stage 2 - Widen __nar
1387 char_type __o[2 * (__nbuf - 1) - 1];
1388 char_type* __op; // pad here
1389 char_type* __oe; // end of output
1390 const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
1391 __ct.widen(__nar, __ne, __o);
1392 __oe = __o + (__ne - __nar);
1396 __op = __o + (__np - __nar);
1397 // [__o, __oe) contains wide number
1399 return std::__pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1402 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<char>;
1403 # if _LIBCPP_HAS_WIDE_CHARACTERS
1404 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<wchar_t>;
1407 template <class _CharT, class _InputIterator>
1408 _LIBCPP_HIDE_FROM_ABI int __get_up_to_n_digits(
1409 _InputIterator& __b, _InputIterator __e, ios_base::iostate& __err, const ctype<_CharT>& __ct, int __n) {
1410 // Precondition: __n >= 1
1412 __err |= ios_base::eofbit | ios_base::failbit;
1417 if (!__ct.is(ctype_base::digit, __c)) {
1418 __err |= ios_base::failbit;
1421 int __r = __ct.narrow(__c, 0) - '0';
1422 for (++__b, (void)--__n; __b != __e && __n > 0; ++__b, (void)--__n) {
1425 if (!__ct.is(ctype_base::digit, __c))
1427 __r = __r * 10 + __ct.narrow(__c, 0) - '0';
1430 __err |= ios_base::eofbit;
1434 class _LIBCPP_EXPORTED_FROM_ABI time_base {
1436 enum dateorder { no_order, dmy, mdy, ymd, ydm };
1439 template <class _CharT>
1440 class _LIBCPP_TEMPLATE_VIS __time_get_c_storage {
1442 typedef basic_string<_CharT> string_type;
1444 virtual const string_type* __weeks() const;
1445 virtual const string_type* __months() const;
1446 virtual const string_type* __am_pm() const;
1447 virtual const string_type& __c() const;
1448 virtual const string_type& __r() const;
1449 virtual const string_type& __x() const;
1450 virtual const string_type& __X() const;
1452 _LIBCPP_HIDE_FROM_ABI ~__time_get_c_storage() {}
1456 _LIBCPP_EXPORTED_FROM_ABI const string* __time_get_c_storage<char>::__weeks() const;
1458 _LIBCPP_EXPORTED_FROM_ABI const string* __time_get_c_storage<char>::__months() const;
1460 _LIBCPP_EXPORTED_FROM_ABI const string* __time_get_c_storage<char>::__am_pm() const;
1462 _LIBCPP_EXPORTED_FROM_ABI const string& __time_get_c_storage<char>::__c() const;
1464 _LIBCPP_EXPORTED_FROM_ABI const string& __time_get_c_storage<char>::__r() const;
1466 _LIBCPP_EXPORTED_FROM_ABI const string& __time_get_c_storage<char>::__x() const;
1468 _LIBCPP_EXPORTED_FROM_ABI const string& __time_get_c_storage<char>::__X() const;
1470 # if _LIBCPP_HAS_WIDE_CHARACTERS
1472 _LIBCPP_EXPORTED_FROM_ABI const wstring* __time_get_c_storage<wchar_t>::__weeks() const;
1474 _LIBCPP_EXPORTED_FROM_ABI const wstring* __time_get_c_storage<wchar_t>::__months() const;
1476 _LIBCPP_EXPORTED_FROM_ABI const wstring* __time_get_c_storage<wchar_t>::__am_pm() const;
1478 _LIBCPP_EXPORTED_FROM_ABI const wstring& __time_get_c_storage<wchar_t>::__c() const;
1480 _LIBCPP_EXPORTED_FROM_ABI const wstring& __time_get_c_storage<wchar_t>::__r() const;
1482 _LIBCPP_EXPORTED_FROM_ABI const wstring& __time_get_c_storage<wchar_t>::__x() const;
1484 _LIBCPP_EXPORTED_FROM_ABI const wstring& __time_get_c_storage<wchar_t>::__X() const;
1487 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
1488 class _LIBCPP_TEMPLATE_VIS time_get : public locale::facet, public time_base, private __time_get_c_storage<_CharT> {
1490 typedef _CharT char_type;
1491 typedef _InputIterator iter_type;
1492 typedef time_base::dateorder dateorder;
1493 typedef basic_string<char_type> string_type;
1495 _LIBCPP_HIDE_FROM_ABI explicit time_get(size_t __refs = 0) : locale::facet(__refs) {}
1497 _LIBCPP_HIDE_FROM_ABI dateorder date_order() const { return this->do_date_order(); }
1499 _LIBCPP_HIDE_FROM_ABI iter_type
1500 get_time(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1501 return do_get_time(__b, __e, __iob, __err, __tm);
1504 _LIBCPP_HIDE_FROM_ABI iter_type
1505 get_date(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1506 return do_get_date(__b, __e, __iob, __err, __tm);
1509 _LIBCPP_HIDE_FROM_ABI iter_type
1510 get_weekday(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1511 return do_get_weekday(__b, __e, __iob, __err, __tm);
1514 _LIBCPP_HIDE_FROM_ABI iter_type
1515 get_monthname(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1516 return do_get_monthname(__b, __e, __iob, __err, __tm);
1519 _LIBCPP_HIDE_FROM_ABI iter_type
1520 get_year(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1521 return do_get_year(__b, __e, __iob, __err, __tm);
1524 _LIBCPP_HIDE_FROM_ABI iter_type
1525 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm, char __fmt, char __mod = 0)
1527 return do_get(__b, __e, __iob, __err, __tm, __fmt, __mod);
1534 ios_base::iostate& __err,
1536 const char_type* __fmtb,
1537 const char_type* __fmte) const;
1539 static locale::id id;
1542 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~time_get() override {}
1544 virtual dateorder do_date_order() const;
1546 do_get_time(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const;
1548 do_get_date(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const;
1550 do_get_weekday(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const;
1552 do_get_monthname(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const;
1554 do_get_year(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const;
1555 virtual iter_type do_get(
1556 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm, char __fmt, char __mod) const;
1559 void __get_white_space(iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1560 void __get_percent(iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1562 void __get_weekdayname(
1563 int& __m, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1564 void __get_monthname(
1565 int& __m, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1566 void __get_day(int& __d, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1568 __get_month(int& __m, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1570 __get_year(int& __y, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1572 __get_year4(int& __y, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1574 __get_hour(int& __d, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1576 __get_12_hour(int& __h, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1578 __get_am_pm(int& __h, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1580 __get_minute(int& __m, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1582 __get_second(int& __s, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1584 __get_weekday(int& __w, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1585 void __get_day_year_num(
1586 int& __w, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1589 template <class _CharT, class _InputIterator>
1590 locale::id time_get<_CharT, _InputIterator>::id;
1592 // time_get primitives
1594 template <class _CharT, class _InputIterator>
1595 void time_get<_CharT, _InputIterator>::__get_weekdayname(
1596 int& __w, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1597 // Note: ignoring case comes from the POSIX strptime spec
1598 const string_type* __wk = this->__weeks();
1599 ptrdiff_t __i = std::__scan_keyword(__b, __e, __wk, __wk + 14, __ct, __err, false) - __wk;
1604 template <class _CharT, class _InputIterator>
1605 void time_get<_CharT, _InputIterator>::__get_monthname(
1606 int& __m, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1607 // Note: ignoring case comes from the POSIX strptime spec
1608 const string_type* __month = this->__months();
1609 ptrdiff_t __i = std::__scan_keyword(__b, __e, __month, __month + 24, __ct, __err, false) - __month;
1614 template <class _CharT, class _InputIterator>
1615 void time_get<_CharT, _InputIterator>::__get_day(
1616 int& __d, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1617 int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
1618 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 31)
1621 __err |= ios_base::failbit;
1624 template <class _CharT, class _InputIterator>
1625 void time_get<_CharT, _InputIterator>::__get_month(
1626 int& __m, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1627 int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2) - 1;
1628 if (!(__err & ios_base::failbit) && 0 <= __t && __t <= 11)
1631 __err |= ios_base::failbit;
1634 template <class _CharT, class _InputIterator>
1635 void time_get<_CharT, _InputIterator>::__get_year(
1636 int& __y, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1637 int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 4);
1638 if (!(__err & ios_base::failbit)) {
1641 else if (69 <= __t && __t <= 99)
1647 template <class _CharT, class _InputIterator>
1648 void time_get<_CharT, _InputIterator>::__get_year4(
1649 int& __y, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1650 int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 4);
1651 if (!(__err & ios_base::failbit))
1655 template <class _CharT, class _InputIterator>
1656 void time_get<_CharT, _InputIterator>::__get_hour(
1657 int& __h, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1658 int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
1659 if (!(__err & ios_base::failbit) && __t <= 23)
1662 __err |= ios_base::failbit;
1665 template <class _CharT, class _InputIterator>
1666 void time_get<_CharT, _InputIterator>::__get_12_hour(
1667 int& __h, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1668 int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
1669 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 12)
1672 __err |= ios_base::failbit;
1675 template <class _CharT, class _InputIterator>
1676 void time_get<_CharT, _InputIterator>::__get_minute(
1677 int& __m, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1678 int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
1679 if (!(__err & ios_base::failbit) && __t <= 59)
1682 __err |= ios_base::failbit;
1685 template <class _CharT, class _InputIterator>
1686 void time_get<_CharT, _InputIterator>::__get_second(
1687 int& __s, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1688 int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
1689 if (!(__err & ios_base::failbit) && __t <= 60)
1692 __err |= ios_base::failbit;
1695 template <class _CharT, class _InputIterator>
1696 void time_get<_CharT, _InputIterator>::__get_weekday(
1697 int& __w, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1698 int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 1);
1699 if (!(__err & ios_base::failbit) && __t <= 6)
1702 __err |= ios_base::failbit;
1705 template <class _CharT, class _InputIterator>
1706 void time_get<_CharT, _InputIterator>::__get_day_year_num(
1707 int& __d, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1708 int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 3);
1709 if (!(__err & ios_base::failbit) && __t <= 365)
1712 __err |= ios_base::failbit;
1715 template <class _CharT, class _InputIterator>
1716 void time_get<_CharT, _InputIterator>::__get_white_space(
1717 iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1718 for (; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
1721 __err |= ios_base::eofbit;
1724 template <class _CharT, class _InputIterator>
1725 void time_get<_CharT, _InputIterator>::__get_am_pm(
1726 int& __h, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1727 const string_type* __ap = this->__am_pm();
1728 if (__ap[0].size() + __ap[1].size() == 0) {
1729 __err |= ios_base::failbit;
1732 ptrdiff_t __i = std::__scan_keyword(__b, __e, __ap, __ap + 2, __ct, __err, false) - __ap;
1733 if (__i == 0 && __h == 12)
1735 else if (__i == 1 && __h < 12)
1739 template <class _CharT, class _InputIterator>
1740 void time_get<_CharT, _InputIterator>::__get_percent(
1741 iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1743 __err |= ios_base::eofbit | ios_base::failbit;
1746 if (__ct.narrow(*__b, 0) != '%')
1747 __err |= ios_base::failbit;
1748 else if (++__b == __e)
1749 __err |= ios_base::eofbit;
1752 // time_get end primitives
1754 template <class _CharT, class _InputIterator>
1755 _InputIterator time_get<_CharT, _InputIterator>::get(
1759 ios_base::iostate& __err,
1761 const char_type* __fmtb,
1762 const char_type* __fmte) const {
1763 const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
1764 __err = ios_base::goodbit;
1765 while (__fmtb != __fmte && __err == ios_base::goodbit) {
1767 __err = ios_base::failbit;
1770 if (__ct.narrow(*__fmtb, 0) == '%') {
1771 if (++__fmtb == __fmte) {
1772 __err = ios_base::failbit;
1775 char __cmd = __ct.narrow(*__fmtb, 0);
1777 if (__cmd == 'E' || __cmd == '0') {
1778 if (++__fmtb == __fmte) {
1779 __err = ios_base::failbit;
1783 __cmd = __ct.narrow(*__fmtb, 0);
1785 __b = do_get(__b, __e, __iob, __err, __tm, __cmd, __opt);
1787 } else if (__ct.is(ctype_base::space, *__fmtb)) {
1788 for (++__fmtb; __fmtb != __fmte && __ct.is(ctype_base::space, *__fmtb); ++__fmtb)
1790 for (; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
1792 } else if (__ct.toupper(*__b) == __ct.toupper(*__fmtb)) {
1796 __err = ios_base::failbit;
1799 __err |= ios_base::eofbit;
1803 template <class _CharT, class _InputIterator>
1804 typename time_get<_CharT, _InputIterator>::dateorder time_get<_CharT, _InputIterator>::do_date_order() const {
1808 template <class _CharT, class _InputIterator>
1809 _InputIterator time_get<_CharT, _InputIterator>::do_get_time(
1810 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1811 const char_type __fmt[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
1812 return get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt) / sizeof(__fmt[0]));
1815 template <class _CharT, class _InputIterator>
1816 _InputIterator time_get<_CharT, _InputIterator>::do_get_date(
1817 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1818 const string_type& __fmt = this->__x();
1819 return get(__b, __e, __iob, __err, __tm, __fmt.data(), __fmt.data() + __fmt.size());
1822 template <class _CharT, class _InputIterator>
1823 _InputIterator time_get<_CharT, _InputIterator>::do_get_weekday(
1824 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1825 const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
1826 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
1830 template <class _CharT, class _InputIterator>
1831 _InputIterator time_get<_CharT, _InputIterator>::do_get_monthname(
1832 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1833 const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
1834 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
1838 template <class _CharT, class _InputIterator>
1839 _InputIterator time_get<_CharT, _InputIterator>::do_get_year(
1840 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1841 const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
1842 __get_year(__tm->tm_year, __b, __e, __err, __ct);
1846 template <class _CharT, class _InputIterator>
1847 _InputIterator time_get<_CharT, _InputIterator>::do_get(
1848 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm, char __fmt, char) const {
1849 __err = ios_base::goodbit;
1850 const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
1854 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
1859 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
1862 const string_type& __fm = this->__c();
1863 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
1867 __get_day(__tm->tm_mday, __b, __e, __err, __ct);
1870 const char_type __fm[] = {'%', 'm', '/', '%', 'd', '/', '%', 'y'};
1871 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm) / sizeof(__fm[0]));
1874 const char_type __fm[] = {'%', 'Y', '-', '%', 'm', '-', '%', 'd'};
1875 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm) / sizeof(__fm[0]));
1878 __get_hour(__tm->tm_hour, __b, __e, __err, __ct);
1881 __get_12_hour(__tm->tm_hour, __b, __e, __err, __ct);
1884 __get_day_year_num(__tm->tm_yday, __b, __e, __err, __ct);
1887 __get_month(__tm->tm_mon, __b, __e, __err, __ct);
1890 __get_minute(__tm->tm_min, __b, __e, __err, __ct);
1894 __get_white_space(__b, __e, __err, __ct);
1897 __get_am_pm(__tm->tm_hour, __b, __e, __err, __ct);
1900 const char_type __fm[] = {'%', 'I', ':', '%', 'M', ':', '%', 'S', ' ', '%', 'p'};
1901 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm) / sizeof(__fm[0]));
1904 const char_type __fm[] = {'%', 'H', ':', '%', 'M'};
1905 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm) / sizeof(__fm[0]));
1908 __get_second(__tm->tm_sec, __b, __e, __err, __ct);
1911 const char_type __fm[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
1912 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm) / sizeof(__fm[0]));
1915 __get_weekday(__tm->tm_wday, __b, __e, __err, __ct);
1918 return do_get_date(__b, __e, __iob, __err, __tm);
1920 const string_type& __fm = this->__X();
1921 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
1924 __get_year(__tm->tm_year, __b, __e, __err, __ct);
1927 __get_year4(__tm->tm_year, __b, __e, __err, __ct);
1930 __get_percent(__b, __e, __err, __ct);
1933 __err |= ios_base::failbit;
1938 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get<char>;
1939 # if _LIBCPP_HAS_WIDE_CHARACTERS
1940 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get<wchar_t>;
1943 class _LIBCPP_EXPORTED_FROM_ABI __time_get {
1945 __locale::__locale_t __loc_;
1947 __time_get(const char* __nm);
1948 __time_get(const string& __nm);
1952 template <class _CharT>
1953 class _LIBCPP_TEMPLATE_VIS __time_get_storage : public __time_get {
1955 typedef basic_string<_CharT> string_type;
1957 string_type __weeks_[14];
1958 string_type __months_[24];
1959 string_type __am_pm_[2];
1965 explicit __time_get_storage(const char* __nm);
1966 explicit __time_get_storage(const string& __nm);
1968 _LIBCPP_HIDE_FROM_ABI ~__time_get_storage() {}
1970 time_base::dateorder __do_date_order() const;
1973 void init(const ctype<_CharT>&);
1974 string_type __analyze(char __fmt, const ctype<_CharT>&);
1977 # define _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(_CharT) \
1979 _LIBCPP_EXPORTED_FROM_ABI time_base::dateorder __time_get_storage<_CharT>::__do_date_order() const; \
1981 _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::__time_get_storage(const char*); \
1983 _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::__time_get_storage(const string&); \
1985 _LIBCPP_EXPORTED_FROM_ABI void __time_get_storage<_CharT>::init(const ctype<_CharT>&); \
1987 _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::string_type __time_get_storage<_CharT>::__analyze( \
1988 char, const ctype<_CharT>&); \
1989 extern template _LIBCPP_EXPORTED_FROM_ABI time_base::dateorder __time_get_storage<_CharT>::__do_date_order() \
1991 extern template _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::__time_get_storage(const char*); \
1992 extern template _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::__time_get_storage(const string&); \
1993 extern template _LIBCPP_EXPORTED_FROM_ABI void __time_get_storage<_CharT>::init(const ctype<_CharT>&); \
1994 extern template _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::string_type \
1995 __time_get_storage<_CharT>::__analyze(char, const ctype<_CharT>&); \
1998 _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(char)
1999 # if _LIBCPP_HAS_WIDE_CHARACTERS
2000 _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(wchar_t)
2002 # undef _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION
2004 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2005 class _LIBCPP_TEMPLATE_VIS time_get_byname
2006 : public time_get<_CharT, _InputIterator>,
2007 private __time_get_storage<_CharT> {
2009 typedef time_base::dateorder dateorder;
2010 typedef _InputIterator iter_type;
2011 typedef _CharT char_type;
2012 typedef basic_string<char_type> string_type;
2014 _LIBCPP_HIDE_FROM_ABI explicit time_get_byname(const char* __nm, size_t __refs = 0)
2015 : time_get<_CharT, _InputIterator>(__refs), __time_get_storage<_CharT>(__nm) {}
2016 _LIBCPP_HIDE_FROM_ABI explicit time_get_byname(const string& __nm, size_t __refs = 0)
2017 : time_get<_CharT, _InputIterator>(__refs), __time_get_storage<_CharT>(__nm) {}
2020 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~time_get_byname() override {}
2022 _LIBCPP_HIDE_FROM_ABI_VIRTUAL dateorder do_date_order() const override { return this->__do_date_order(); }
2025 _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type* __weeks() const override { return this->__weeks_; }
2026 _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type* __months() const override { return this->__months_; }
2027 _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type* __am_pm() const override { return this->__am_pm_; }
2028 _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type& __c() const override { return this->__c_; }
2029 _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type& __r() const override { return this->__r_; }
2030 _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type& __x() const override { return this->__x_; }
2031 _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type& __X() const override { return this->__X_; }
2034 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<char>;
2035 # if _LIBCPP_HAS_WIDE_CHARACTERS
2036 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<wchar_t>;
2039 class _LIBCPP_EXPORTED_FROM_ABI __time_put {
2040 __locale::__locale_t __loc_;
2043 _LIBCPP_HIDE_FROM_ABI __time_put() : __loc_(_LIBCPP_GET_C_LOCALE) {}
2044 __time_put(const char* __nm);
2045 __time_put(const string& __nm);
2047 void __do_put(char* __nb, char*& __ne, const tm* __tm, char __fmt, char __mod) const;
2048 # if _LIBCPP_HAS_WIDE_CHARACTERS
2049 void __do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm, char __fmt, char __mod) const;
2053 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2054 class _LIBCPP_TEMPLATE_VIS time_put : public locale::facet, private __time_put {
2056 typedef _CharT char_type;
2057 typedef _OutputIterator iter_type;
2059 _LIBCPP_HIDE_FROM_ABI explicit time_put(size_t __refs = 0) : locale::facet(__refs) {}
2062 put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm, const char_type* __pb, const char_type* __pe)
2065 _LIBCPP_HIDE_FROM_ABI iter_type
2066 put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm, char __fmt, char __mod = 0) const {
2067 return do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2070 static locale::id id;
2073 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~time_put() override {}
2074 virtual iter_type do_put(iter_type __s, ios_base&, char_type, const tm* __tm, char __fmt, char __mod) const;
2076 _LIBCPP_HIDE_FROM_ABI explicit time_put(const char* __nm, size_t __refs) : locale::facet(__refs), __time_put(__nm) {}
2077 _LIBCPP_HIDE_FROM_ABI explicit time_put(const string& __nm, size_t __refs)
2078 : locale::facet(__refs), __time_put(__nm) {}
2081 template <class _CharT, class _OutputIterator>
2082 locale::id time_put<_CharT, _OutputIterator>::id;
2084 template <class _CharT, class _OutputIterator>
2085 _OutputIterator time_put<_CharT, _OutputIterator>::put(
2086 iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm, const char_type* __pb, const char_type* __pe)
2088 const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
2089 for (; __pb != __pe; ++__pb) {
2090 if (__ct.narrow(*__pb, 0) == '%') {
2091 if (++__pb == __pe) {
2096 char __fmt = __ct.narrow(*__pb, 0);
2097 if (__fmt == 'E' || __fmt == 'O') {
2098 if (++__pb == __pe) {
2104 __fmt = __ct.narrow(*__pb, 0);
2106 __s = do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2113 template <class _CharT, class _OutputIterator>
2114 _OutputIterator time_put<_CharT, _OutputIterator>::do_put(
2115 iter_type __s, ios_base&, char_type, const tm* __tm, char __fmt, char __mod) const {
2116 char_type __nar[100];
2117 char_type* __nb = __nar;
2118 char_type* __ne = __nb + 100;
2119 __do_put(__nb, __ne, __tm, __fmt, __mod);
2120 return std::copy(__nb, __ne, __s);
2123 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<char>;
2124 # if _LIBCPP_HAS_WIDE_CHARACTERS
2125 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<wchar_t>;
2128 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2129 class _LIBCPP_TEMPLATE_VIS time_put_byname : public time_put<_CharT, _OutputIterator> {
2131 _LIBCPP_HIDE_FROM_ABI explicit time_put_byname(const char* __nm, size_t __refs = 0)
2132 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2134 _LIBCPP_HIDE_FROM_ABI explicit time_put_byname(const string& __nm, size_t __refs = 0)
2135 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2138 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~time_put_byname() override {}
2141 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<char>;
2142 # if _LIBCPP_HAS_WIDE_CHARACTERS
2143 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<wchar_t>;
2148 class _LIBCPP_EXPORTED_FROM_ABI money_base {
2150 enum part { none, space, symbol, sign, value };
2155 _LIBCPP_HIDE_FROM_ABI money_base() {}
2160 template <class _CharT, bool _International = false>
2161 class _LIBCPP_TEMPLATE_VIS moneypunct : public locale::facet, public money_base {
2163 typedef _CharT char_type;
2164 typedef basic_string<char_type> string_type;
2166 _LIBCPP_HIDE_FROM_ABI explicit moneypunct(size_t __refs = 0) : locale::facet(__refs) {}
2168 _LIBCPP_HIDE_FROM_ABI char_type decimal_point() const { return do_decimal_point(); }
2169 _LIBCPP_HIDE_FROM_ABI char_type thousands_sep() const { return do_thousands_sep(); }
2170 _LIBCPP_HIDE_FROM_ABI string grouping() const { return do_grouping(); }
2171 _LIBCPP_HIDE_FROM_ABI string_type curr_symbol() const { return do_curr_symbol(); }
2172 _LIBCPP_HIDE_FROM_ABI string_type positive_sign() const { return do_positive_sign(); }
2173 _LIBCPP_HIDE_FROM_ABI string_type negative_sign() const { return do_negative_sign(); }
2174 _LIBCPP_HIDE_FROM_ABI int frac_digits() const { return do_frac_digits(); }
2175 _LIBCPP_HIDE_FROM_ABI pattern pos_format() const { return do_pos_format(); }
2176 _LIBCPP_HIDE_FROM_ABI pattern neg_format() const { return do_neg_format(); }
2178 static locale::id id;
2179 static const bool intl = _International;
2182 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~moneypunct() override {}
2184 virtual char_type do_decimal_point() const { return numeric_limits<char_type>::max(); }
2185 virtual char_type do_thousands_sep() const { return numeric_limits<char_type>::max(); }
2186 virtual string do_grouping() const { return string(); }
2187 virtual string_type do_curr_symbol() const { return string_type(); }
2188 virtual string_type do_positive_sign() const { return string_type(); }
2189 virtual string_type do_negative_sign() const { return string_type(1, '-'); }
2190 virtual int do_frac_digits() const { return 0; }
2191 virtual pattern do_pos_format() const {
2192 pattern __p = {{symbol, sign, none, value}};
2195 virtual pattern do_neg_format() const {
2196 pattern __p = {{symbol, sign, none, value}};
2201 template <class _CharT, bool _International>
2202 locale::id moneypunct<_CharT, _International>::id;
2204 template <class _CharT, bool _International>
2205 const bool moneypunct<_CharT, _International>::intl;
2207 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, false>;
2208 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, true>;
2209 # if _LIBCPP_HAS_WIDE_CHARACTERS
2210 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, false>;
2211 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, true>;
2214 // moneypunct_byname
2216 template <class _CharT, bool _International = false>
2217 class _LIBCPP_TEMPLATE_VIS moneypunct_byname : public moneypunct<_CharT, _International> {
2219 typedef money_base::pattern pattern;
2220 typedef _CharT char_type;
2221 typedef basic_string<char_type> string_type;
2223 _LIBCPP_HIDE_FROM_ABI explicit moneypunct_byname(const char* __nm, size_t __refs = 0)
2224 : moneypunct<_CharT, _International>(__refs) {
2228 _LIBCPP_HIDE_FROM_ABI explicit moneypunct_byname(const string& __nm, size_t __refs = 0)
2229 : moneypunct<_CharT, _International>(__refs) {
2234 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~moneypunct_byname() override {}
2236 char_type do_decimal_point() const override { return __decimal_point_; }
2237 char_type do_thousands_sep() const override { return __thousands_sep_; }
2238 string do_grouping() const override { return __grouping_; }
2239 string_type do_curr_symbol() const override { return __curr_symbol_; }
2240 string_type do_positive_sign() const override { return __positive_sign_; }
2241 string_type do_negative_sign() const override { return __negative_sign_; }
2242 int do_frac_digits() const override { return __frac_digits_; }
2243 pattern do_pos_format() const override { return __pos_format_; }
2244 pattern do_neg_format() const override { return __neg_format_; }
2247 char_type __decimal_point_;
2248 char_type __thousands_sep_;
2250 string_type __curr_symbol_;
2251 string_type __positive_sign_;
2252 string_type __negative_sign_;
2254 pattern __pos_format_;
2255 pattern __neg_format_;
2257 void init(const char*);
2261 _LIBCPP_EXPORTED_FROM_ABI void moneypunct_byname<char, false>::init(const char*);
2263 _LIBCPP_EXPORTED_FROM_ABI void moneypunct_byname<char, true>::init(const char*);
2264 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, false>;
2265 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, true>;
2267 # if _LIBCPP_HAS_WIDE_CHARACTERS
2269 _LIBCPP_EXPORTED_FROM_ABI void moneypunct_byname<wchar_t, false>::init(const char*);
2271 _LIBCPP_EXPORTED_FROM_ABI void moneypunct_byname<wchar_t, true>::init(const char*);
2272 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, false>;
2273 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, true>;
2278 template <class _CharT>
2281 typedef _CharT char_type;
2282 typedef basic_string<char_type> string_type;
2284 _LIBCPP_HIDE_FROM_ABI __money_get() {}
2286 static void __gather_info(
2288 const locale& __loc,
2289 money_base::pattern& __pat,
2299 template <class _CharT>
2300 void __money_get<_CharT>::__gather_info(
2302 const locale& __loc,
2303 money_base::pattern& __pat,
2312 const moneypunct<char_type, true>& __mp = std::use_facet<moneypunct<char_type, true> >(__loc);
2313 __pat = __mp.neg_format();
2314 __nsn = __mp.negative_sign();
2315 __psn = __mp.positive_sign();
2316 __dp = __mp.decimal_point();
2317 __ts = __mp.thousands_sep();
2318 __grp = __mp.grouping();
2319 __sym = __mp.curr_symbol();
2320 __fd = __mp.frac_digits();
2322 const moneypunct<char_type, false>& __mp = std::use_facet<moneypunct<char_type, false> >(__loc);
2323 __pat = __mp.neg_format();
2324 __nsn = __mp.negative_sign();
2325 __psn = __mp.positive_sign();
2326 __dp = __mp.decimal_point();
2327 __ts = __mp.thousands_sep();
2328 __grp = __mp.grouping();
2329 __sym = __mp.curr_symbol();
2330 __fd = __mp.frac_digits();
2334 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<char>;
2335 # if _LIBCPP_HAS_WIDE_CHARACTERS
2336 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<wchar_t>;
2339 template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2340 class _LIBCPP_TEMPLATE_VIS money_get : public locale::facet, private __money_get<_CharT> {
2342 typedef _CharT char_type;
2343 typedef _InputIterator iter_type;
2344 typedef basic_string<char_type> string_type;
2346 _LIBCPP_HIDE_FROM_ABI explicit money_get(size_t __refs = 0) : locale::facet(__refs) {}
2348 _LIBCPP_HIDE_FROM_ABI iter_type
2349 get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob, ios_base::iostate& __err, long double& __v) const {
2350 return do_get(__b, __e, __intl, __iob, __err, __v);
2353 _LIBCPP_HIDE_FROM_ABI iter_type
2354 get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob, ios_base::iostate& __err, string_type& __v) const {
2355 return do_get(__b, __e, __intl, __iob, __err, __v);
2358 static locale::id id;
2361 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~money_get() override {}
2364 do_get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob, ios_base::iostate& __err, long double& __v) const;
2366 do_get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob, ios_base::iostate& __err, string_type& __v) const;
2369 static bool __do_get(
2373 const locale& __loc,
2374 ios_base::fmtflags __flags,
2375 ios_base::iostate& __err,
2377 const ctype<char_type>& __ct,
2378 unique_ptr<char_type, void (*)(void*)>& __wb,
2383 template <class _CharT, class _InputIterator>
2384 locale::id money_get<_CharT, _InputIterator>::id;
2386 _LIBCPP_EXPORTED_FROM_ABI void __do_nothing(void*);
2388 template <class _Tp>
2389 _LIBCPP_HIDE_FROM_ABI void __double_or_nothing(unique_ptr<_Tp, void (*)(void*)>& __b, _Tp*& __n, _Tp*& __e) {
2390 bool __owns = __b.get_deleter() != __do_nothing;
2391 size_t __cur_cap = static_cast<size_t>(__e - __b.get()) * sizeof(_Tp);
2392 size_t __new_cap = __cur_cap < numeric_limits<size_t>::max() / 2 ? 2 * __cur_cap : numeric_limits<size_t>::max();
2394 __new_cap = sizeof(_Tp);
2395 size_t __n_off = static_cast<size_t>(__n - __b.get());
2396 _Tp* __t = (_Tp*)std::realloc(__owns ? __b.get() : 0, __new_cap);
2398 __throw_bad_alloc();
2401 __b = unique_ptr<_Tp, void (*)(void*)>(__t, free);
2402 __new_cap /= sizeof(_Tp);
2403 __n = __b.get() + __n_off;
2404 __e = __b.get() + __new_cap;
2408 template <class _CharT, class _InputIterator>
2409 bool money_get<_CharT, _InputIterator>::__do_get(
2413 const locale& __loc,
2414 ios_base::fmtflags __flags,
2415 ios_base::iostate& __err,
2417 const ctype<char_type>& __ct,
2418 unique_ptr<char_type, void (*)(void*)>& __wb,
2422 __err |= ios_base::failbit;
2425 const unsigned __bz = 100;
2426 unsigned __gbuf[__bz];
2427 unique_ptr<unsigned, void (*)(void*)> __gb(__gbuf, __do_nothing);
2428 unsigned* __gn = __gb.get();
2429 unsigned* __ge = __gn + __bz;
2430 money_base::pattern __pat;
2437 // Capture the spaces read into money_base::{space,none} so they
2438 // can be compared to initial spaces in __sym.
2439 string_type __spaces;
2441 __money_get<_CharT>::__gather_info(__intl, __loc, __pat, __dp, __ts, __grp, __sym, __psn, __nsn, __fd);
2442 const string_type* __trailing_sign = 0;
2444 for (unsigned __p = 0; __p < 4 && __b != __e; ++__p) {
2445 switch (__pat.field[__p]) {
2446 case money_base::space:
2448 if (__ct.is(ctype_base::space, *__b))
2449 __spaces.push_back(*__b++);
2451 __err |= ios_base::failbit;
2455 _LIBCPP_FALLTHROUGH();
2456 case money_base::none:
2458 while (__b != __e && __ct.is(ctype_base::space, *__b))
2459 __spaces.push_back(*__b++);
2462 case money_base::sign:
2463 if (__psn.size() > 0 && *__b == __psn[0]) {
2466 if (__psn.size() > 1)
2467 __trailing_sign = &__psn;
2470 if (__nsn.size() > 0 && *__b == __nsn[0]) {
2473 if (__nsn.size() > 1)
2474 __trailing_sign = &__nsn;
2477 if (__psn.size() > 0 && __nsn.size() > 0) { // sign is required
2478 __err |= ios_base::failbit;
2481 if (__psn.size() == 0 && __nsn.size() == 0)
2482 // locale has no way of specifying a sign. Use the initial value of __neg as a default
2484 __neg = (__nsn.size() == 0);
2486 case money_base::symbol: {
2487 bool __more_needed =
2488 __trailing_sign || (__p < 2) || (__p == 2 && __pat.field[3] != static_cast<char>(money_base::none));
2489 bool __sb = (__flags & ios_base::showbase) != 0;
2490 if (__sb || __more_needed) {
2491 typename string_type::const_iterator __sym_space_end = __sym.begin();
2492 if (__p > 0 && (__pat.field[__p - 1] == money_base::none || __pat.field[__p - 1] == money_base::space)) {
2493 // Match spaces we've already read against spaces at
2494 // the beginning of __sym.
2495 while (__sym_space_end != __sym.end() && __ct.is(ctype_base::space, *__sym_space_end))
2497 const size_t __num_spaces = __sym_space_end - __sym.begin();
2498 if (__num_spaces > __spaces.size() ||
2499 !std::equal(__spaces.end() - __num_spaces, __spaces.end(), __sym.begin())) {
2500 // No match. Put __sym_space_end back at the
2501 // beginning of __sym, which will prevent a
2502 // match in the next loop.
2503 __sym_space_end = __sym.begin();
2506 typename string_type::const_iterator __sym_curr_char = __sym_space_end;
2507 while (__sym_curr_char != __sym.end() && __b != __e && *__b == *__sym_curr_char) {
2511 if (__sb && __sym_curr_char != __sym.end()) {
2512 __err |= ios_base::failbit;
2517 case money_base::value: {
2519 for (; __b != __e; ++__b) {
2520 char_type __c = *__b;
2521 if (__ct.is(ctype_base::digit, __c)) {
2523 std::__double_or_nothing(__wb, __wn, __we);
2526 } else if (__grp.size() > 0 && __ng > 0 && __c == __ts) {
2528 std::__double_or_nothing(__gb, __gn, __ge);
2534 if (__gb.get() != __gn && __ng > 0) {
2536 std::__double_or_nothing(__gb, __gn, __ge);
2540 if (__b == __e || *__b != __dp) {
2541 __err |= ios_base::failbit;
2544 for (++__b; __fd > 0; --__fd, ++__b) {
2545 if (__b == __e || !__ct.is(ctype_base::digit, *__b)) {
2546 __err |= ios_base::failbit;
2550 std::__double_or_nothing(__wb, __wn, __we);
2554 if (__wn == __wb.get()) {
2555 __err |= ios_base::failbit;
2561 if (__trailing_sign) {
2562 for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, ++__b) {
2563 if (__b == __e || *__b != (*__trailing_sign)[__i]) {
2564 __err |= ios_base::failbit;
2569 if (__gb.get() != __gn) {
2570 ios_base::iostate __et = ios_base::goodbit;
2571 __check_grouping(__grp, __gb.get(), __gn, __et);
2573 __err |= ios_base::failbit;
2580 template <class _CharT, class _InputIterator>
2581 _InputIterator money_get<_CharT, _InputIterator>::do_get(
2582 iter_type __b, iter_type __e, bool __intl, ios_base& __iob, ios_base::iostate& __err, long double& __v) const {
2583 const int __bz = 100;
2584 char_type __wbuf[__bz];
2585 unique_ptr<char_type, void (*)(void*)> __wb(__wbuf, __do_nothing);
2587 char_type* __we = __wbuf + __bz;
2588 locale __loc = __iob.getloc();
2589 const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__loc);
2591 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct, __wb, __wn, __we)) {
2592 const char __src[] = "0123456789";
2593 char_type __atoms[sizeof(__src) - 1];
2594 __ct.widen(__src, __src + (sizeof(__src) - 1), __atoms);
2596 char* __nc = __nbuf;
2597 unique_ptr<char, void (*)(void*)> __h(nullptr, free);
2598 if (__wn - __wb.get() > __bz - 2) {
2599 __h.reset((char*)malloc(static_cast<size_t>(__wn - __wb.get() + 2)));
2600 if (__h.get() == nullptr)
2601 __throw_bad_alloc();
2606 for (const char_type* __w = __wb.get(); __w < __wn; ++__w, ++__nc)
2607 *__nc = __src[std::find(__atoms, std::end(__atoms), *__w) - __atoms];
2609 if (sscanf(__nbuf, "%Lf", &__v) != 1)
2610 __throw_runtime_error("money_get error");
2613 __err |= ios_base::eofbit;
2617 template <class _CharT, class _InputIterator>
2618 _InputIterator money_get<_CharT, _InputIterator>::do_get(
2619 iter_type __b, iter_type __e, bool __intl, ios_base& __iob, ios_base::iostate& __err, string_type& __v) const {
2620 const int __bz = 100;
2621 char_type __wbuf[__bz];
2622 unique_ptr<char_type, void (*)(void*)> __wb(__wbuf, __do_nothing);
2624 char_type* __we = __wbuf + __bz;
2625 locale __loc = __iob.getloc();
2626 const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__loc);
2628 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct, __wb, __wn, __we)) {
2631 __v.push_back(__ct.widen('-'));
2632 char_type __z = __ct.widen('0');
2634 for (__w = __wb.get(); __w < __wn - 1; ++__w)
2637 __v.append(__w, __wn);
2640 __err |= ios_base::eofbit;
2644 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<char>;
2645 # if _LIBCPP_HAS_WIDE_CHARACTERS
2646 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<wchar_t>;
2651 template <class _CharT>
2654 typedef _CharT char_type;
2655 typedef basic_string<char_type> string_type;
2657 _LIBCPP_HIDE_FROM_ABI __money_put() {}
2659 static void __gather_info(
2662 const locale& __loc,
2663 money_base::pattern& __pat,
2670 static void __format(
2674 ios_base::fmtflags __flags,
2675 const char_type* __db,
2676 const char_type* __de,
2677 const ctype<char_type>& __ct,
2679 const money_base::pattern& __pat,
2682 const string& __grp,
2683 const string_type& __sym,
2684 const string_type& __sn,
2688 template <class _CharT>
2689 void __money_put<_CharT>::__gather_info(
2692 const locale& __loc,
2693 money_base::pattern& __pat,
2701 const moneypunct<char_type, true>& __mp = std::use_facet<moneypunct<char_type, true> >(__loc);
2703 __pat = __mp.neg_format();
2704 __sn = __mp.negative_sign();
2706 __pat = __mp.pos_format();
2707 __sn = __mp.positive_sign();
2709 __dp = __mp.decimal_point();
2710 __ts = __mp.thousands_sep();
2711 __grp = __mp.grouping();
2712 __sym = __mp.curr_symbol();
2713 __fd = __mp.frac_digits();
2715 const moneypunct<char_type, false>& __mp = std::use_facet<moneypunct<char_type, false> >(__loc);
2717 __pat = __mp.neg_format();
2718 __sn = __mp.negative_sign();
2720 __pat = __mp.pos_format();
2721 __sn = __mp.positive_sign();
2723 __dp = __mp.decimal_point();
2724 __ts = __mp.thousands_sep();
2725 __grp = __mp.grouping();
2726 __sym = __mp.curr_symbol();
2727 __fd = __mp.frac_digits();
2731 template <class _CharT>
2732 void __money_put<_CharT>::__format(
2736 ios_base::fmtflags __flags,
2737 const char_type* __db,
2738 const char_type* __de,
2739 const ctype<char_type>& __ct,
2741 const money_base::pattern& __pat,
2744 const string& __grp,
2745 const string_type& __sym,
2746 const string_type& __sn,
2749 for (char __p : __pat.field) {
2751 case money_base::none:
2754 case money_base::space:
2756 *__me++ = __ct.widen(' ');
2758 case money_base::sign:
2762 case money_base::symbol:
2763 if (!__sym.empty() && (__flags & ios_base::showbase))
2764 __me = std::copy(__sym.begin(), __sym.end(), __me);
2766 case money_base::value: {
2767 // remember start of value so we can reverse it
2768 char_type* __t = __me;
2769 // find beginning of digits
2772 // find end of digits
2773 const char_type* __d;
2774 for (__d = __db; __d < __de; ++__d)
2775 if (!__ct.is(ctype_base::digit, *__d))
2777 // print fractional part
2780 for (__f = __fd; __d > __db && __f > 0; --__f)
2782 char_type __z = __f > 0 ? __ct.widen('0') : char_type();
2783 for (; __f > 0; --__f)
2789 *__me++ = __ct.widen('0');
2793 unsigned __gl = __grp.empty() ? numeric_limits<unsigned>::max() : static_cast<unsigned>(__grp[__ig]);
2794 while (__d != __db) {
2798 if (++__ig < __grp.size())
2799 __gl = __grp[__ig] == numeric_limits<char>::max()
2800 ? numeric_limits<unsigned>::max()
2801 : static_cast<unsigned>(__grp[__ig]);
2808 std::reverse(__t, __me);
2812 // print rest of sign, if any
2813 if (__sn.size() > 1)
2814 __me = std::copy(__sn.begin() + 1, __sn.end(), __me);
2816 if ((__flags & ios_base::adjustfield) == ios_base::left)
2818 else if ((__flags & ios_base::adjustfield) != ios_base::internal)
2822 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<char>;
2823 # if _LIBCPP_HAS_WIDE_CHARACTERS
2824 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<wchar_t>;
2827 template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2828 class _LIBCPP_TEMPLATE_VIS money_put : public locale::facet, private __money_put<_CharT> {
2830 typedef _CharT char_type;
2831 typedef _OutputIterator iter_type;
2832 typedef basic_string<char_type> string_type;
2834 _LIBCPP_HIDE_FROM_ABI explicit money_put(size_t __refs = 0) : locale::facet(__refs) {}
2836 _LIBCPP_HIDE_FROM_ABI iter_type
2837 put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl, long double __units) const {
2838 return do_put(__s, __intl, __iob, __fl, __units);
2841 _LIBCPP_HIDE_FROM_ABI iter_type
2842 put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl, const string_type& __digits) const {
2843 return do_put(__s, __intl, __iob, __fl, __digits);
2846 static locale::id id;
2849 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~money_put() override {}
2851 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl, long double __units) const;
2853 do_put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl, const string_type& __digits) const;
2856 template <class _CharT, class _OutputIterator>
2857 locale::id money_put<_CharT, _OutputIterator>::id;
2859 template <class _CharT, class _OutputIterator>
2860 _OutputIterator money_put<_CharT, _OutputIterator>::do_put(
2861 iter_type __s, bool __intl, ios_base& __iob, char_type __fl, long double __units) const {
2863 const size_t __bs = 100;
2866 char_type __digits[__bs];
2867 char_type* __db = __digits;
2868 int __n = snprintf(__bb, __bs, "%.0Lf", __units);
2869 unique_ptr<char, void (*)(void*)> __hn(nullptr, free);
2870 unique_ptr<char_type, void (*)(void*)> __hd(0, free);
2871 // secure memory for digit storage
2872 if (static_cast<size_t>(__n) > __bs - 1) {
2873 __n = __locale::__asprintf(&__bb, _LIBCPP_GET_C_LOCALE, "%.0Lf", __units);
2875 __throw_bad_alloc();
2877 __hd.reset((char_type*)malloc(static_cast<size_t>(__n) * sizeof(char_type)));
2878 if (__hd == nullptr)
2879 __throw_bad_alloc();
2883 locale __loc = __iob.getloc();
2884 const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__loc);
2885 __ct.widen(__bb, __bb + __n, __db);
2886 bool __neg = __n > 0 && __bb[0] == '-';
2887 money_base::pattern __pat;
2894 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
2895 // secure memory for formatting
2896 char_type __mbuf[__bs];
2897 char_type* __mb = __mbuf;
2898 unique_ptr<char_type, void (*)(void*)> __hw(0, free);
2899 size_t __exn = __n > __fd ? (static_cast<size_t>(__n) - static_cast<size_t>(__fd)) * 2 + __sn.size() + __sym.size() +
2900 static_cast<size_t>(__fd) + 1
2901 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
2903 __hw.reset((char_type*)malloc(__exn * sizeof(char_type)));
2906 __throw_bad_alloc();
2912 __mb, __mi, __me, __iob.flags(), __db, __db + __n, __ct, __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
2913 return std::__pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
2916 template <class _CharT, class _OutputIterator>
2917 _OutputIterator money_put<_CharT, _OutputIterator>::do_put(
2918 iter_type __s, bool __intl, ios_base& __iob, char_type __fl, const string_type& __digits) const {
2920 locale __loc = __iob.getloc();
2921 const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__loc);
2922 bool __neg = __digits.size() > 0 && __digits[0] == __ct.widen('-');
2923 money_base::pattern __pat;
2930 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
2931 // secure memory for formatting
2932 char_type __mbuf[100];
2933 char_type* __mb = __mbuf;
2934 unique_ptr<char_type, void (*)(void*)> __h(0, free);
2936 static_cast<int>(__digits.size()) > __fd
2937 ? (__digits.size() - static_cast<size_t>(__fd)) * 2 + __sn.size() + __sym.size() + static_cast<size_t>(__fd) +
2939 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
2941 __h.reset((char_type*)malloc(__exn * sizeof(char_type)));
2944 __throw_bad_alloc();
2955 __digits.data() + __digits.size(),
2965 return std::__pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
2968 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<char>;
2969 # if _LIBCPP_HAS_WIDE_CHARACTERS
2970 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<wchar_t>;
2975 class _LIBCPP_EXPORTED_FROM_ABI messages_base {
2977 typedef intptr_t catalog;
2979 _LIBCPP_HIDE_FROM_ABI messages_base() {}
2982 template <class _CharT>
2983 class _LIBCPP_TEMPLATE_VIS messages : public locale::facet, public messages_base {
2985 typedef _CharT char_type;
2986 typedef basic_string<_CharT> string_type;
2988 _LIBCPP_HIDE_FROM_ABI explicit messages(size_t __refs = 0) : locale::facet(__refs) {}
2990 _LIBCPP_HIDE_FROM_ABI catalog open(const basic_string<char>& __nm, const locale& __loc) const {
2991 return do_open(__nm, __loc);
2994 _LIBCPP_HIDE_FROM_ABI string_type get(catalog __c, int __set, int __msgid, const string_type& __dflt) const {
2995 return do_get(__c, __set, __msgid, __dflt);
2998 _LIBCPP_HIDE_FROM_ABI void close(catalog __c) const { do_close(__c); }
3000 static locale::id id;
3003 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~messages() override {}
3005 virtual catalog do_open(const basic_string<char>&, const locale&) const;
3006 virtual string_type do_get(catalog, int __set, int __msgid, const string_type& __dflt) const;
3007 virtual void do_close(catalog) const;
3010 template <class _CharT>
3011 locale::id messages<_CharT>::id;
3013 template <class _CharT>
3014 typename messages<_CharT>::catalog messages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const {
3015 # if _LIBCPP_HAS_CATOPEN
3016 return (catalog)catopen(__nm.c_str(), NL_CAT_LOCALE);
3017 # else // !_LIBCPP_HAS_CATOPEN
3020 # endif // _LIBCPP_HAS_CATOPEN
3023 template <class _CharT>
3024 typename messages<_CharT>::string_type
3025 messages<_CharT>::do_get(catalog __c, int __set, int __msgid, const string_type& __dflt) const {
3026 # if _LIBCPP_HAS_CATOPEN
3028 __narrow_to_utf8<sizeof(char_type) * __CHAR_BIT__>()(
3029 std::back_inserter(__ndflt), __dflt.c_str(), __dflt.c_str() + __dflt.size());
3030 nl_catd __cat = (nl_catd)__c;
3031 static_assert(sizeof(catalog) >= sizeof(nl_catd), "Unexpected nl_catd type");
3032 char* __n = catgets(__cat, __set, __msgid, __ndflt.c_str());
3034 __widen_from_utf8<sizeof(char_type) * __CHAR_BIT__>()(std::back_inserter(__w), __n, __n + std::strlen(__n));
3036 # else // !_LIBCPP_HAS_CATOPEN
3041 # endif // _LIBCPP_HAS_CATOPEN
3044 template <class _CharT>
3045 void messages<_CharT>::do_close(catalog __c) const {
3046 # if _LIBCPP_HAS_CATOPEN
3047 catclose((nl_catd)__c);
3048 # else // !_LIBCPP_HAS_CATOPEN
3050 # endif // _LIBCPP_HAS_CATOPEN
3053 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<char>;
3054 # if _LIBCPP_HAS_WIDE_CHARACTERS
3055 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<wchar_t>;
3058 template <class _CharT>
3059 class _LIBCPP_TEMPLATE_VIS messages_byname : public messages<_CharT> {
3061 typedef messages_base::catalog catalog;
3062 typedef basic_string<_CharT> string_type;
3064 _LIBCPP_HIDE_FROM_ABI explicit messages_byname(const char*, size_t __refs = 0) : messages<_CharT>(__refs) {}
3066 _LIBCPP_HIDE_FROM_ABI explicit messages_byname(const string&, size_t __refs = 0) : messages<_CharT>(__refs) {}
3069 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~messages_byname() override {}
3072 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<char>;
3073 # if _LIBCPP_HAS_WIDE_CHARACTERS
3074 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<wchar_t>;
3077 # if _LIBCPP_STD_VER < 26 || defined(_LIBCPP_ENABLE_CXX26_REMOVED_WSTRING_CONVERT)
3079 template <class _Codecvt,
3080 class _Elem = wchar_t,
3081 class _WideAlloc = allocator<_Elem>,
3082 class _ByteAlloc = allocator<char> >
3083 class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 wstring_convert {
3085 typedef basic_string<char, char_traits<char>, _ByteAlloc> byte_string;
3086 typedef basic_string<_Elem, char_traits<_Elem>, _WideAlloc> wide_string;
3087 typedef typename _Codecvt::state_type state_type;
3088 typedef typename wide_string::traits_type::int_type int_type;
3091 byte_string __byte_err_string_;
3092 wide_string __wide_err_string_;
3093 _Codecvt* __cvtptr_;
3094 state_type __cvtstate_;
3098 # ifndef _LIBCPP_CXX03_LANG
3099 _LIBCPP_HIDE_FROM_ABI wstring_convert() : wstring_convert(new _Codecvt) {}
3100 _LIBCPP_HIDE_FROM_ABI explicit wstring_convert(_Codecvt* __pcvt);
3102 _LIBCPP_HIDE_FROM_ABI _LIBCPP_EXPLICIT_SINCE_CXX14 wstring_convert(_Codecvt* __pcvt = new _Codecvt);
3105 _LIBCPP_HIDE_FROM_ABI wstring_convert(_Codecvt* __pcvt, state_type __state);
3106 _LIBCPP_EXPLICIT_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI
3107 wstring_convert(const byte_string& __byte_err, const wide_string& __wide_err = wide_string());
3108 # ifndef _LIBCPP_CXX03_LANG
3109 _LIBCPP_HIDE_FROM_ABI wstring_convert(wstring_convert&& __wc);
3111 _LIBCPP_HIDE_FROM_ABI ~wstring_convert();
3113 wstring_convert(const wstring_convert& __wc) = delete;
3114 wstring_convert& operator=(const wstring_convert& __wc) = delete;
3116 _LIBCPP_HIDE_FROM_ABI wide_string from_bytes(char __byte) { return from_bytes(&__byte, &__byte + 1); }
3117 _LIBCPP_HIDE_FROM_ABI wide_string from_bytes(const char* __ptr) {
3118 return from_bytes(__ptr, __ptr + char_traits<char>::length(__ptr));
3120 _LIBCPP_HIDE_FROM_ABI wide_string from_bytes(const byte_string& __str) {
3121 return from_bytes(__str.data(), __str.data() + __str.size());
3123 _LIBCPP_HIDE_FROM_ABI wide_string from_bytes(const char* __first, const char* __last);
3125 _LIBCPP_HIDE_FROM_ABI byte_string to_bytes(_Elem __wchar) { return to_bytes(&__wchar, &__wchar + 1); }
3126 _LIBCPP_HIDE_FROM_ABI byte_string to_bytes(const _Elem* __wptr) {
3127 return to_bytes(__wptr, __wptr + char_traits<_Elem>::length(__wptr));
3129 _LIBCPP_HIDE_FROM_ABI byte_string to_bytes(const wide_string& __wstr) {
3130 return to_bytes(__wstr.data(), __wstr.data() + __wstr.size());
3132 _LIBCPP_HIDE_FROM_ABI byte_string to_bytes(const _Elem* __first, const _Elem* __last);
3134 _LIBCPP_HIDE_FROM_ABI size_t converted() const _NOEXCEPT { return __cvtcount_; }
3135 _LIBCPP_HIDE_FROM_ABI state_type state() const { return __cvtstate_; }
3138 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
3139 template <class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3140 inline wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::wstring_convert(_Codecvt* __pcvt)
3141 : __cvtptr_(__pcvt), __cvtstate_(), __cvtcount_(0) {}
3142 _LIBCPP_SUPPRESS_DEPRECATED_POP
3144 template <class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3145 inline wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::wstring_convert(_Codecvt* __pcvt, state_type __state)
3146 : __cvtptr_(__pcvt), __cvtstate_(__state), __cvtcount_(0) {}
3148 template <class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3149 wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::wstring_convert(
3150 const byte_string& __byte_err, const wide_string& __wide_err)
3151 : __byte_err_string_(__byte_err), __wide_err_string_(__wide_err), __cvtstate_(), __cvtcount_(0) {
3152 __cvtptr_ = new _Codecvt;
3155 # ifndef _LIBCPP_CXX03_LANG
3157 template <class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3158 inline wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::wstring_convert(wstring_convert&& __wc)
3159 : __byte_err_string_(std::move(__wc.__byte_err_string_)),
3160 __wide_err_string_(std::move(__wc.__wide_err_string_)),
3161 __cvtptr_(__wc.__cvtptr_),
3162 __cvtstate_(__wc.__cvtstate_),
3163 __cvtcount_(__wc.__cvtcount_) {
3164 __wc.__cvtptr_ = nullptr;
3167 # endif // _LIBCPP_CXX03_LANG
3169 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
3170 template <class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3171 wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::~wstring_convert() {
3175 template <class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3176 typename wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::wide_string
3177 wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::from_bytes(const char* __frm, const char* __frm_end) {
3178 _LIBCPP_SUPPRESS_DEPRECATED_POP
3180 if (__cvtptr_ != nullptr) {
3181 wide_string __ws(2 * (__frm_end - __frm), _Elem());
3182 if (__frm != __frm_end)
3183 __ws.resize(__ws.capacity());
3184 codecvt_base::result __r = codecvt_base::ok;
3185 state_type __st = __cvtstate_;
3186 if (__frm != __frm_end) {
3187 _Elem* __to = &__ws[0];
3188 _Elem* __to_end = __to + __ws.size();
3189 const char* __frm_nxt;
3192 __r = __cvtptr_->in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
3193 __cvtcount_ += __frm_nxt - __frm;
3194 if (__frm_nxt == __frm) {
3195 __r = codecvt_base::error;
3196 } else if (__r == codecvt_base::noconv) {
3197 __ws.resize(__to - &__ws[0]);
3198 // This only gets executed if _Elem is char
3199 __ws.append((const _Elem*)__frm, (const _Elem*)__frm_end);
3201 __r = codecvt_base::ok;
3202 } else if (__r == codecvt_base::ok) {
3203 __ws.resize(__to_nxt - &__ws[0]);
3205 } else if (__r == codecvt_base::partial) {
3206 ptrdiff_t __s = __to_nxt - &__ws[0];
3207 __ws.resize(2 * __s);
3208 __to = &__ws[0] + __s;
3209 __to_end = &__ws[0] + __ws.size();
3212 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3214 if (__r == codecvt_base::ok)
3218 if (__wide_err_string_.empty())
3219 __throw_range_error("wstring_convert: from_bytes error");
3221 return __wide_err_string_;
3224 template <class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3225 typename wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::byte_string
3226 wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::to_bytes(const _Elem* __frm, const _Elem* __frm_end) {
3228 if (__cvtptr_ != nullptr) {
3229 byte_string __bs(2 * (__frm_end - __frm), char());
3230 if (__frm != __frm_end)
3231 __bs.resize(__bs.capacity());
3232 codecvt_base::result __r = codecvt_base::ok;
3233 state_type __st = __cvtstate_;
3234 if (__frm != __frm_end) {
3235 char* __to = &__bs[0];
3236 char* __to_end = __to + __bs.size();
3237 const _Elem* __frm_nxt;
3240 __r = __cvtptr_->out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
3241 __cvtcount_ += __frm_nxt - __frm;
3242 if (__frm_nxt == __frm) {
3243 __r = codecvt_base::error;
3244 } else if (__r == codecvt_base::noconv) {
3245 __bs.resize(__to - &__bs[0]);
3246 // This only gets executed if _Elem is char
3247 __bs.append((const char*)__frm, (const char*)__frm_end);
3249 __r = codecvt_base::ok;
3250 } else if (__r == codecvt_base::ok) {
3251 __bs.resize(__to_nxt - &__bs[0]);
3253 } else if (__r == codecvt_base::partial) {
3254 ptrdiff_t __s = __to_nxt - &__bs[0];
3255 __bs.resize(2 * __s);
3256 __to = &__bs[0] + __s;
3257 __to_end = &__bs[0] + __bs.size();
3260 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3262 if (__r == codecvt_base::ok) {
3263 size_t __s = __bs.size();
3264 __bs.resize(__bs.capacity());
3265 char* __to = &__bs[0] + __s;
3266 char* __to_end = __to + __bs.size();
3269 __r = __cvtptr_->unshift(__st, __to, __to_end, __to_nxt);
3270 if (__r == codecvt_base::noconv) {
3271 __bs.resize(__to - &__bs[0]);
3272 __r = codecvt_base::ok;
3273 } else if (__r == codecvt_base::ok) {
3274 __bs.resize(__to_nxt - &__bs[0]);
3275 } else if (__r == codecvt_base::partial) {
3276 ptrdiff_t __sp = __to_nxt - &__bs[0];
3277 __bs.resize(2 * __sp);
3278 __to = &__bs[0] + __sp;
3279 __to_end = &__bs[0] + __bs.size();
3281 } while (__r == codecvt_base::partial);
3282 if (__r == codecvt_base::ok)
3287 if (__byte_err_string_.empty())
3288 __throw_range_error("wstring_convert: to_bytes error");
3290 return __byte_err_string_;
3293 template <class _Codecvt, class _Elem = wchar_t, class _Tr = char_traits<_Elem> >
3294 class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 wbuffer_convert : public basic_streambuf<_Elem, _Tr> {
3297 typedef _Elem char_type;
3298 typedef _Tr traits_type;
3299 typedef typename traits_type::int_type int_type;
3300 typedef typename traits_type::pos_type pos_type;
3301 typedef typename traits_type::off_type off_type;
3302 typedef typename _Codecvt::state_type state_type;
3306 const char* __extbufnext_;
3307 const char* __extbufend_;
3308 char __extbuf_min_[8];
3310 char_type* __intbuf_;
3312 streambuf* __bufptr_;
3315 ios_base::openmode __cm_;
3318 bool __always_noconv_;
3321 # ifndef _LIBCPP_CXX03_LANG
3322 _LIBCPP_HIDE_FROM_ABI wbuffer_convert() : wbuffer_convert(nullptr) {}
3323 explicit _LIBCPP_HIDE_FROM_ABI
3324 wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt = new _Codecvt, state_type __state = state_type());
3326 _LIBCPP_EXPLICIT_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI
3327 wbuffer_convert(streambuf* __bytebuf = nullptr, _Codecvt* __pcvt = new _Codecvt, state_type __state = state_type());
3330 _LIBCPP_HIDE_FROM_ABI ~wbuffer_convert();
3332 _LIBCPP_HIDE_FROM_ABI streambuf* rdbuf() const { return __bufptr_; }
3333 _LIBCPP_HIDE_FROM_ABI streambuf* rdbuf(streambuf* __bytebuf) {
3334 streambuf* __r = __bufptr_;
3335 __bufptr_ = __bytebuf;
3339 wbuffer_convert(const wbuffer_convert&) = delete;
3340 wbuffer_convert& operator=(const wbuffer_convert&) = delete;
3342 _LIBCPP_HIDE_FROM_ABI state_type state() const { return __st_; }
3345 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual int_type underflow();
3346 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual int_type pbackfail(int_type __c = traits_type::eof());
3347 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual int_type overflow(int_type __c = traits_type::eof());
3348 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
3349 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual pos_type
3350 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __wch = ios_base::in | ios_base::out);
3351 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual pos_type
3352 seekpos(pos_type __sp, ios_base::openmode __wch = ios_base::in | ios_base::out);
3353 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual int sync();
3356 _LIBCPP_HIDE_FROM_ABI_VIRTUAL bool __read_mode();
3357 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __write_mode();
3358 _LIBCPP_HIDE_FROM_ABI_VIRTUAL wbuffer_convert* __close();
3361 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
3362 template <class _Codecvt, class _Elem, class _Tr>
3363 wbuffer_convert<_Codecvt, _Elem, _Tr>::wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt, state_type __state)
3364 : __extbuf_(nullptr),
3365 __extbufnext_(nullptr),
3366 __extbufend_(nullptr),
3370 __bufptr_(__bytebuf),
3376 __always_noconv_(__cv_ ? __cv_->always_noconv() : false) {
3380 template <class _Codecvt, class _Elem, class _Tr>
3381 wbuffer_convert<_Codecvt, _Elem, _Tr>::~wbuffer_convert() {
3390 template <class _Codecvt, class _Elem, class _Tr>
3391 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type wbuffer_convert<_Codecvt, _Elem, _Tr>::underflow() {
3392 _LIBCPP_SUPPRESS_DEPRECATED_POP
3393 if (__cv_ == 0 || __bufptr_ == nullptr)
3394 return traits_type::eof();
3395 bool __initial = __read_mode();
3397 if (this->gptr() == 0)
3398 this->setg(&__1buf, &__1buf + 1, &__1buf + 1);
3399 const size_t __unget_sz = __initial ? 0 : std::min<size_t>((this->egptr() - this->eback()) / 2, 4);
3400 int_type __c = traits_type::eof();
3401 if (this->gptr() == this->egptr()) {
3402 std::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
3403 if (__always_noconv_) {
3404 streamsize __nmemb = static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz);
3405 __nmemb = __bufptr_->sgetn((char*)this->eback() + __unget_sz, __nmemb);
3407 this->setg(this->eback(), this->eback() + __unget_sz, this->eback() + __unget_sz + __nmemb);
3408 __c = *this->gptr();
3411 if (__extbufend_ != __extbufnext_) {
3412 _LIBCPP_ASSERT_NON_NULL(__extbufnext_ != nullptr, "underflow moving from nullptr");
3413 _LIBCPP_ASSERT_NON_NULL(__extbuf_ != nullptr, "underflow moving into nullptr");
3414 std::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
3416 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
3417 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
3418 streamsize __nmemb = std::min(static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz),
3419 static_cast<streamsize>(__extbufend_ - __extbufnext_));
3420 codecvt_base::result __r;
3421 // FIXME: Do we ever need to restore the state here?
3422 // state_type __svs = __st_;
3423 streamsize __nr = __bufptr_->sgetn(const_cast<char*>(__extbufnext_), __nmemb);
3425 __extbufend_ = __extbufnext_ + __nr;
3428 __st_, __extbuf_, __extbufend_, __extbufnext_, this->eback() + __unget_sz, this->egptr(), __inext);
3429 if (__r == codecvt_base::noconv) {
3430 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)const_cast<char*>(__extbufend_));
3431 __c = *this->gptr();
3432 } else if (__inext != this->eback() + __unget_sz) {
3433 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
3434 __c = *this->gptr();
3439 __c = *this->gptr();
3440 if (this->eback() == &__1buf)
3441 this->setg(0, 0, 0);
3445 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
3446 template <class _Codecvt, class _Elem, class _Tr>
3447 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
3448 wbuffer_convert<_Codecvt, _Elem, _Tr>::pbackfail(int_type __c) {
3449 _LIBCPP_SUPPRESS_DEPRECATED_POP
3450 if (__cv_ != 0 && __bufptr_ && this->eback() < this->gptr()) {
3451 if (traits_type::eq_int_type(__c, traits_type::eof())) {
3453 return traits_type::not_eof(__c);
3455 if (traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1])) {
3457 *this->gptr() = traits_type::to_char_type(__c);
3461 return traits_type::eof();
3464 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
3465 template <class _Codecvt, class _Elem, class _Tr>
3466 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type wbuffer_convert<_Codecvt, _Elem, _Tr>::overflow(int_type __c) {
3467 _LIBCPP_SUPPRESS_DEPRECATED_POP
3468 if (__cv_ == 0 || !__bufptr_)
3469 return traits_type::eof();
3472 char_type* __pb_save = this->pbase();
3473 char_type* __epb_save = this->epptr();
3474 if (!traits_type::eq_int_type(__c, traits_type::eof())) {
3475 if (this->pptr() == 0)
3476 this->setp(&__1buf, &__1buf + 1);
3477 *this->pptr() = traits_type::to_char_type(__c);
3480 if (this->pptr() != this->pbase()) {
3481 if (__always_noconv_) {
3482 streamsize __nmemb = static_cast<streamsize>(this->pptr() - this->pbase());
3483 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
3484 return traits_type::eof();
3486 char* __extbe = __extbuf_;
3487 codecvt_base::result __r;
3489 const char_type* __e;
3490 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e, __extbuf_, __extbuf_ + __ebs_, __extbe);
3491 if (__e == this->pbase())
3492 return traits_type::eof();
3493 if (__r == codecvt_base::noconv) {
3494 streamsize __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
3495 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
3496 return traits_type::eof();
3497 } else if (__r == codecvt_base::ok || __r == codecvt_base::partial) {
3498 streamsize __nmemb = static_cast<size_t>(__extbe - __extbuf_);
3499 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
3500 return traits_type::eof();
3501 if (__r == codecvt_base::partial) {
3502 this->setp(const_cast<char_type*>(__e), this->pptr());
3503 this->__pbump(this->epptr() - this->pbase());
3506 return traits_type::eof();
3507 } while (__r == codecvt_base::partial);
3509 this->setp(__pb_save, __epb_save);
3511 return traits_type::not_eof(__c);
3514 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
3515 template <class _Codecvt, class _Elem, class _Tr>
3516 basic_streambuf<_Elem, _Tr>* wbuffer_convert<_Codecvt, _Elem, _Tr>::setbuf(char_type* __s, streamsize __n) {
3517 _LIBCPP_SUPPRESS_DEPRECATED_POP
3518 this->setg(0, 0, 0);
3525 if (__ebs_ > sizeof(__extbuf_min_)) {
3526 if (__always_noconv_ && __s) {
3527 __extbuf_ = (char*)__s;
3530 __extbuf_ = new char[__ebs_];
3534 __extbuf_ = __extbuf_min_;
3535 __ebs_ = sizeof(__extbuf_min_);
3538 if (!__always_noconv_) {
3539 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
3540 if (__s && __ibs_ >= sizeof(__extbuf_min_)) {
3544 __intbuf_ = new char_type[__ibs_];
3555 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
3556 template <class _Codecvt, class _Elem, class _Tr>
3557 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
3558 wbuffer_convert<_Codecvt, _Elem, _Tr>::seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __om) {
3559 int __width = __cv_->encoding();
3560 if (__cv_ == 0 || !__bufptr_ || (__width <= 0 && __off != 0) || sync())
3561 return pos_type(off_type(-1));
3562 // __width > 0 || __off == 0, now check __way
3563 if (__way != ios_base::beg && __way != ios_base::cur && __way != ios_base::end)
3564 return pos_type(off_type(-1));
3565 pos_type __r = __bufptr_->pubseekoff(__width * __off, __way, __om);
3570 template <class _Codecvt, class _Elem, class _Tr>
3571 typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
3572 wbuffer_convert<_Codecvt, _Elem, _Tr>::seekpos(pos_type __sp, ios_base::openmode __wch) {
3573 if (__cv_ == 0 || !__bufptr_ || sync())
3574 return pos_type(off_type(-1));
3575 if (__bufptr_->pubseekpos(__sp, __wch) == pos_type(off_type(-1)))
3576 return pos_type(off_type(-1));
3580 template <class _Codecvt, class _Elem, class _Tr>
3581 int wbuffer_convert<_Codecvt, _Elem, _Tr>::sync() {
3582 _LIBCPP_SUPPRESS_DEPRECATED_POP
3583 if (__cv_ == 0 || !__bufptr_)
3585 if (__cm_ & ios_base::out) {
3586 if (this->pptr() != this->pbase())
3587 if (overflow() == traits_type::eof())
3589 codecvt_base::result __r;
3592 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
3593 streamsize __nmemb = static_cast<streamsize>(__extbe - __extbuf_);
3594 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
3596 } while (__r == codecvt_base::partial);
3597 if (__r == codecvt_base::error)
3599 if (__bufptr_->pubsync())
3601 } else if (__cm_ & ios_base::in) {
3603 if (__always_noconv_)
3604 __c = this->egptr() - this->gptr();
3606 int __width = __cv_->encoding();
3607 __c = __extbufend_ - __extbufnext_;
3609 __c += __width * (this->egptr() - this->gptr());
3611 if (this->gptr() != this->egptr()) {
3612 std::reverse(this->gptr(), this->egptr());
3613 codecvt_base::result __r;
3614 const char_type* __e = this->gptr();
3617 __r = __cv_->out(__st_, __e, this->egptr(), __e, __extbuf_, __extbuf_ + __ebs_, __extbe);
3619 case codecvt_base::noconv:
3620 __c += this->egptr() - this->gptr();
3622 case codecvt_base::ok:
3623 case codecvt_base::partial:
3624 __c += __extbe - __extbuf_;
3629 } while (__r == codecvt_base::partial);
3633 if (__bufptr_->pubseekoff(-__c, ios_base::cur, __cm_) == pos_type(off_type(-1)))
3635 this->setg(0, 0, 0);
3641 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
3642 template <class _Codecvt, class _Elem, class _Tr>
3643 bool wbuffer_convert<_Codecvt, _Elem, _Tr>::__read_mode() {
3644 if (!(__cm_ & ios_base::in)) {
3646 if (__always_noconv_)
3647 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_ + __ebs_, (char_type*)__extbuf_ + __ebs_);
3649 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
3650 __cm_ = ios_base::in;
3656 template <class _Codecvt, class _Elem, class _Tr>
3657 void wbuffer_convert<_Codecvt, _Elem, _Tr>::__write_mode() {
3658 if (!(__cm_ & ios_base::out)) {
3659 this->setg(0, 0, 0);
3660 if (__ebs_ > sizeof(__extbuf_min_)) {
3661 if (__always_noconv_)
3662 this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__ebs_ - 1));
3664 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
3667 __cm_ = ios_base::out;
3671 template <class _Codecvt, class _Elem, class _Tr>
3672 wbuffer_convert<_Codecvt, _Elem, _Tr>* wbuffer_convert<_Codecvt, _Elem, _Tr>::__close() {
3673 wbuffer_convert* __rt = nullptr;
3674 if (__cv_ != nullptr && __bufptr_ != nullptr) {
3676 if ((__cm_ & ios_base::out) && sync())
3682 _LIBCPP_SUPPRESS_DEPRECATED_POP
3684 # endif // _LIBCPP_STD_VER < 26 || defined(_LIBCPP_ENABLE_CXX26_REMOVED_WSTRING_CONVERT)
3686 _LIBCPP_END_NAMESPACE_STD
3690 // NOLINTEND(libcpp-robust-against-adl)
3692 # endif // _LIBCPP_HAS_LOCALIZATION
3694 # if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
3696 # include <concepts>
3698 # include <iterator>
3700 # include <stdexcept>
3701 # include <type_traits>
3702 # include <typeinfo>
3704 #endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
3706 #endif // _LIBCPP_LOCALE