• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
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
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_LOCALE
11#define _LIBCPP_LOCALE
12
13/*
14    locale synopsis
15
16namespace std
17{
18
19class locale
20{
21public:
22    // types:
23    class facet;
24    class id;
25
26    typedef int category;
27    static const category // values assigned here are for exposition only
28        none     = 0x000,
29        collate  = 0x010,
30        ctype    = 0x020,
31        monetary = 0x040,
32        numeric  = 0x080,
33        time     = 0x100,
34        messages = 0x200,
35        all = collate | ctype | monetary | numeric | time | messages;
36
37    // construct/copy/destroy:
38    locale() noexcept;
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);
46
47    ~locale(); // not virtual
48
49    const locale& operator=(const locale& other) noexcept;
50
51    template <class Facet> locale combine(const locale& other) const;
52
53    // locale operations:
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;
60
61    // global locale objects:
62    static locale global(const locale&);
63    static const locale& classic();
64};
65
66template <class Facet> const Facet& use_facet(const locale&);
67template <class Facet> bool has_facet(const locale&) noexcept;
68
69// 22.3.3, convenience interfaces:
70template <class charT> bool isspace (charT c, const locale& loc);
71template <class charT> bool isprint (charT c, const locale& loc);
72template <class charT> bool iscntrl (charT c, const locale& loc);
73template <class charT> bool isupper (charT c, const locale& loc);
74template <class charT> bool islower (charT c, const locale& loc);
75template <class charT> bool isalpha (charT c, const locale& loc);
76template <class charT> bool isdigit (charT c, const locale& loc);
77template <class charT> bool ispunct (charT c, const locale& loc);
78template <class charT> bool isxdigit(charT c, const locale& loc);
79template <class charT> bool isalnum (charT c, const locale& loc);
80template <class charT> bool isgraph (charT c, const locale& loc);
81template <class charT> charT toupper(charT c, const locale& loc);
82template <class charT> charT tolower(charT c, const locale& loc);
83
84template<class Codecvt, class Elem = wchar_t,
85         class Wide_alloc = allocator<Elem>,
86         class Byte_alloc = allocator<char>>
87class wstring_convert                                      // Removed in C++26
88{
89public:
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;
94
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
99
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
105    ~wstring_convert();
106
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);
111
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);
116
117    size_t converted() const; // noexcept in C++14
118    state_type state() const;
119};
120
121template <class Codecvt, class Elem = wchar_t, class Tr = char_traits<Elem>>
122class wbuffer_convert                                               // Removed in C++26
123    : public basic_streambuf<Elem, Tr>
124{
125public:
126    typedef typename Tr::state_type state_type;
127
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
135
136    wbuffer_convert(const wbuffer_convert&) = delete;               // C++14
137    wbuffer_convert & operator=(const wbuffer_convert &) = delete;  // C++14
138    ~wbuffer_convert();                                             // C++14
139
140    streambuf* rdbuf() const;
141    streambuf* rdbuf(streambuf* bytebuf);
142
143    state_type state() const;
144};
145
146// 22.4.1 and 22.4.1.3, ctype:
147class ctype_base;
148template <class charT> class ctype;
149template <> class ctype<char>; // specialization
150template <class charT> class ctype_byname;
151template <> class ctype_byname<char>; // specialization
152
153class codecvt_base;
154template <class internT, class externT, class stateT> class codecvt;
155template <class internT, class externT, class stateT> class codecvt_byname;
156
157// 22.4.2 and 22.4.3, numeric:
158template <class charT, class InputIterator> class num_get;
159template <class charT, class OutputIterator> class num_put;
160template <class charT> class numpunct;
161template <class charT> class numpunct_byname;
162
163// 22.4.4, col lation:
164template <class charT> class collate;
165template <class charT> class collate_byname;
166
167// 22.4.5, date and time:
168class time_base;
169template <class charT, class InputIterator> class time_get;
170template <class charT, class InputIterator> class time_get_byname;
171template <class charT, class OutputIterator> class time_put;
172template <class charT, class OutputIterator> class time_put_byname;
173
174// 22.4.6, money:
175class money_base;
176template <class charT, class InputIterator> class money_get;
177template <class charT, class OutputIterator> class money_put;
178template <class charT, bool Intl> class moneypunct;
179template <class charT, bool Intl> class moneypunct_byname;
180
181// 22.4.7, message retrieval:
182class messages_base;
183template <class charT> class messages;
184template <class charT> class messages_byname;
185
186}  // std
187
188*/
189
190#include <__config>
191
192#if _LIBCPP_HAS_LOCALIZATION
193
194#  include <__algorithm/copy.h>
195#  include <__algorithm/equal.h>
196#  include <__algorithm/find.h>
197#  include <__algorithm/max.h>
198#  include <__algorithm/reverse.h>
199#  include <__algorithm/unwrap_iter.h>
200#  include <__assert>
201#  include <__iterator/access.h>
202#  include <__iterator/back_insert_iterator.h>
203#  include <__iterator/istreambuf_iterator.h>
204#  include <__iterator/ostreambuf_iterator.h>
205#  include <__locale>
206#  include <__locale_dir/pad_and_output.h>
207#  include <__memory/unique_ptr.h>
208#  include <__type_traits/make_unsigned.h>
209#  include <cerrno>
210#  include <cstdio>
211#  include <cstdlib>
212#  include <ctime>
213#  include <ios>
214#  include <limits>
215#  include <new>
216#  include <streambuf>
217#  include <version>
218
219// TODO: Properly qualify calls now that __bsd_locale_defaults.h defines functions instead of macros
220// NOLINTBEGIN(libcpp-robust-against-adl)
221
222#  if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
223// Most unix variants have catopen.  These are the specific ones that don't.
224#    if !defined(__BIONIC__) && !defined(_NEWLIB_VERSION) && !defined(__EMSCRIPTEN__)
225#      define _LIBCPP_HAS_CATOPEN 1
226#      include <nl_types.h>
227#    else
228#      define _LIBCPP_HAS_CATOPEN 0
229#    endif
230#  else
231#    define _LIBCPP_HAS_CATOPEN 0
232#  endif
233
234#  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
235#    pragma GCC system_header
236#  endif
237
238_LIBCPP_PUSH_MACROS
239#  include <__undef_macros>
240
241_LIBCPP_BEGIN_NAMESPACE_STD
242
243#  if defined(__APPLE__) || defined(__FreeBSD__)
244#    define _LIBCPP_GET_C_LOCALE 0
245#  elif defined(__NetBSD__)
246#    define _LIBCPP_GET_C_LOCALE LC_C_LOCALE
247#  else
248#    define _LIBCPP_GET_C_LOCALE __cloc()
249// Get the C locale object
250_LIBCPP_EXPORTED_FROM_ABI __locale::__locale_t __cloc();
251#    define __cloc_defined
252#  endif
253
254// __scan_keyword
255// Scans [__b, __e) until a match is found in the basic_strings range
256//  [__kb, __ke) or until it can be shown that there is no match in [__kb, __ke).
257//  __b will be incremented (visibly), consuming CharT until a match is found
258//  or proved to not exist.  A keyword may be "", in which will match anything.
259//  If one keyword is a prefix of another, and the next CharT in the input
260//  might match another keyword, the algorithm will attempt to find the longest
261//  matching keyword.  If the longer matching keyword ends up not matching, then
262//  no keyword match is found.  If no keyword match is found, __ke is returned
263//  and failbit is set in __err.
264//  Else an iterator pointing to the matching keyword is found.  If more than
265//  one keyword matches, an iterator to the first matching keyword is returned.
266//  If on exit __b == __e, eofbit is set in __err.  If __case_sensitive is false,
267//  __ct is used to force to lower case before comparing characters.
268//  Examples:
269//  Keywords:  "a", "abb"
270//  If the input is "a", the first keyword matches and eofbit is set.
271//  If the input is "abc", no match is found and "ab" are consumed.
272template <class _InputIterator, class _ForwardIterator, class _Ctype>
273_LIBCPP_HIDE_FROM_ABI _ForwardIterator __scan_keyword(
274    _InputIterator& __b,
275    _InputIterator __e,
276    _ForwardIterator __kb,
277    _ForwardIterator __ke,
278    const _Ctype& __ct,
279    ios_base::iostate& __err,
280    bool __case_sensitive = true) {
281  typedef typename iterator_traits<_InputIterator>::value_type _CharT;
282  size_t __nkw                       = static_cast<size_t>(std::distance(__kb, __ke));
283  const unsigned char __doesnt_match = '\0';
284  const unsigned char __might_match  = '\1';
285  const unsigned char __does_match   = '\2';
286  unsigned char __statbuf[100];
287  unsigned char* __status = __statbuf;
288  unique_ptr<unsigned char, void (*)(void*)> __stat_hold(nullptr, free);
289  if (__nkw > sizeof(__statbuf)) {
290    __status = (unsigned char*)malloc(__nkw);
291    if (__status == nullptr)
292      __throw_bad_alloc();
293    __stat_hold.reset(__status);
294  }
295  size_t __n_might_match = __nkw; // At this point, any keyword might match
296  size_t __n_does_match  = 0;     // but none of them definitely do
297  // Initialize all statuses to __might_match, except for "" keywords are __does_match
298  unsigned char* __st = __status;
299  for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void)++__st) {
300    if (!__ky->empty())
301      *__st = __might_match;
302    else {
303      *__st = __does_match;
304      --__n_might_match;
305      ++__n_does_match;
306    }
307  }
308  // While there might be a match, test keywords against the next CharT
309  for (size_t __indx = 0; __b != __e && __n_might_match > 0; ++__indx) {
310    // Peek at the next CharT but don't consume it
311    _CharT __c = *__b;
312    if (!__case_sensitive)
313      __c = __ct.toupper(__c);
314    bool __consume = false;
315    // For each keyword which might match, see if the __indx character is __c
316    // If a match if found, consume __c
317    // If a match is found, and that is the last character in the keyword,
318    //    then that keyword matches.
319    // If the keyword doesn't match this character, then change the keyword
320    //    to doesn't match
321    __st = __status;
322    for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void)++__st) {
323      if (*__st == __might_match) {
324        _CharT __kc = (*__ky)[__indx];
325        if (!__case_sensitive)
326          __kc = __ct.toupper(__kc);
327        if (__c == __kc) {
328          __consume = true;
329          if (__ky->size() == __indx + 1) {
330            *__st = __does_match;
331            --__n_might_match;
332            ++__n_does_match;
333          }
334        } else {
335          *__st = __doesnt_match;
336          --__n_might_match;
337        }
338      }
339    }
340    // consume if we matched a character
341    if (__consume) {
342      ++__b;
343      // If we consumed a character and there might be a matched keyword that
344      //   was marked matched on a previous iteration, then such keywords
345      //   which are now marked as not matching.
346      if (__n_might_match + __n_does_match > 1) {
347        __st = __status;
348        for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void)++__st) {
349          if (*__st == __does_match && __ky->size() != __indx + 1) {
350            *__st = __doesnt_match;
351            --__n_does_match;
352          }
353        }
354      }
355    }
356  }
357  // We've exited the loop because we hit eof and/or we have no more "might matches".
358  if (__b == __e)
359    __err |= ios_base::eofbit;
360  // Return the first matching result
361  for (__st = __status; __kb != __ke; ++__kb, (void)++__st)
362    if (*__st == __does_match)
363      break;
364  if (__kb == __ke)
365    __err |= ios_base::failbit;
366  return __kb;
367}
368
369struct _LIBCPP_EXPORTED_FROM_ABI __num_get_base {
370  static const int __num_get_buf_sz = 40;
371
372  static int __get_base(ios_base&);
373  static const char __src[33]; // "0123456789abcdefABCDEFxX+-pPiInN"
374  // count of leading characters in __src used for parsing integers ("012..X+-")
375  static const size_t __int_chr_cnt = 26;
376  // count of leading characters in __src used for parsing floating-point values ("012..-pP")
377  static const size_t __fp_chr_cnt = 28;
378};
379
380_LIBCPP_EXPORTED_FROM_ABI void
381__check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end, ios_base::iostate& __err);
382
383template <class _CharT>
384struct __num_get : protected __num_get_base {
385  static string __stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point, _CharT& __thousands_sep);
386
387  static int __stage2_float_loop(
388      _CharT __ct,
389      bool& __in_units,
390      char& __exp,
391      char* __a,
392      char*& __a_end,
393      _CharT __decimal_point,
394      _CharT __thousands_sep,
395      const string& __grouping,
396      unsigned* __g,
397      unsigned*& __g_end,
398      unsigned& __dc,
399      _CharT* __atoms);
400#  ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
401  static string __stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep);
402  static int __stage2_int_loop(
403      _CharT __ct,
404      int __base,
405      char* __a,
406      char*& __a_end,
407      unsigned& __dc,
408      _CharT __thousands_sep,
409      const string& __grouping,
410      unsigned* __g,
411      unsigned*& __g_end,
412      _CharT* __atoms);
413
414#  else
415  static string __stage2_int_prep(ios_base& __iob, _CharT& __thousands_sep) {
416    locale __loc                 = __iob.getloc();
417    const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
418    __thousands_sep              = __np.thousands_sep();
419    return __np.grouping();
420  }
421
422  const _CharT* __do_widen(ios_base& __iob, _CharT* __atoms) const { return __do_widen_p(__iob, __atoms); }
423
424  static int __stage2_int_loop(
425      _CharT __ct,
426      int __base,
427      char* __a,
428      char*& __a_end,
429      unsigned& __dc,
430      _CharT __thousands_sep,
431      const string& __grouping,
432      unsigned* __g,
433      unsigned*& __g_end,
434      const _CharT* __atoms);
435
436private:
437  template <typename _Tp>
438  const _Tp* __do_widen_p(ios_base& __iob, _Tp* __atoms) const {
439    locale __loc = __iob.getloc();
440    use_facet<ctype<_Tp> >(__loc).widen(__src, __src + __int_chr_cnt, __atoms);
441    return __atoms;
442  }
443
444  const char* __do_widen_p(ios_base& __iob, char* __atoms) const {
445    (void)__iob;
446    (void)__atoms;
447    return __src;
448  }
449#  endif
450};
451
452#  ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
453template <class _CharT>
454string __num_get<_CharT>::__stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep) {
455  locale __loc = __iob.getloc();
456  std::use_facet<ctype<_CharT> >(__loc).widen(__src, __src + __int_chr_cnt, __atoms);
457  const numpunct<_CharT>& __np = std::use_facet<numpunct<_CharT> >(__loc);
458  __thousands_sep              = __np.thousands_sep();
459  return __np.grouping();
460}
461#  endif
462
463template <class _CharT>
464string __num_get<_CharT>::__stage2_float_prep(
465    ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point, _CharT& __thousands_sep) {
466  locale __loc = __iob.getloc();
467  std::use_facet<ctype<_CharT> >(__loc).widen(__src, __src + __fp_chr_cnt, __atoms);
468  const numpunct<_CharT>& __np = std::use_facet<numpunct<_CharT> >(__loc);
469  __decimal_point              = __np.decimal_point();
470  __thousands_sep              = __np.thousands_sep();
471  return __np.grouping();
472}
473
474template <class _CharT>
475int
476#  ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
477__num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
478                  unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
479                  unsigned* __g, unsigned*& __g_end, _CharT* __atoms)
480#  else
481__num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
482                  unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
483                  unsigned* __g, unsigned*& __g_end, const _CharT* __atoms)
484
485#  endif
486{
487  if (__a_end == __a && (__ct == __atoms[24] || __ct == __atoms[25])) {
488    *__a_end++ = __ct == __atoms[24] ? '+' : '-';
489    __dc       = 0;
490    return 0;
491  }
492  if (__grouping.size() != 0 && __ct == __thousands_sep) {
493    if (__g_end - __g < __num_get_buf_sz) {
494      *__g_end++ = __dc;
495      __dc       = 0;
496    }
497    return 0;
498  }
499  ptrdiff_t __f = std::find(__atoms, __atoms + __int_chr_cnt, __ct) - __atoms;
500  if (__f >= 24)
501    return -1;
502  switch (__base) {
503  case 8:
504  case 10:
505    if (__f >= __base)
506      return -1;
507    break;
508  case 16:
509    if (__f < 22)
510      break;
511    if (__a_end != __a && __a_end - __a <= 2 && __a_end[-1] == '0') {
512      __dc       = 0;
513      *__a_end++ = __src[__f];
514      return 0;
515    }
516    return -1;
517  }
518  *__a_end++ = __src[__f];
519  ++__dc;
520  return 0;
521}
522
523template <class _CharT>
524int __num_get<_CharT>::__stage2_float_loop(
525    _CharT __ct,
526    bool& __in_units,
527    char& __exp,
528    char* __a,
529    char*& __a_end,
530    _CharT __decimal_point,
531    _CharT __thousands_sep,
532    const string& __grouping,
533    unsigned* __g,
534    unsigned*& __g_end,
535    unsigned& __dc,
536    _CharT* __atoms) {
537  if (__ct == __decimal_point) {
538    if (!__in_units)
539      return -1;
540    __in_units = false;
541    *__a_end++ = '.';
542    if (__grouping.size() != 0 && __g_end - __g < __num_get_buf_sz)
543      *__g_end++ = __dc;
544    return 0;
545  }
546  if (__ct == __thousands_sep && __grouping.size() != 0) {
547    if (!__in_units)
548      return -1;
549    if (__g_end - __g < __num_get_buf_sz) {
550      *__g_end++ = __dc;
551      __dc       = 0;
552    }
553    return 0;
554  }
555  ptrdiff_t __f = std::find(__atoms, __atoms + __num_get_base::__fp_chr_cnt, __ct) - __atoms;
556  if (__f >= static_cast<ptrdiff_t>(__num_get_base::__fp_chr_cnt))
557    return -1;
558  char __x = __src[__f];
559  if (__x == '-' || __x == '+') {
560    if (__a_end == __a || (std::toupper(__a_end[-1]) == std::toupper(__exp))) {
561      *__a_end++ = __x;
562      return 0;
563    }
564    return -1;
565  }
566  if (__x == 'x' || __x == 'X')
567    __exp = 'P';
568  else if (std::toupper(__x) == __exp) {
569    __exp = std::tolower(__exp);
570    if (__in_units) {
571      __in_units = false;
572      if (__grouping.size() != 0 && __g_end - __g < __num_get_buf_sz)
573        *__g_end++ = __dc;
574    }
575  }
576  *__a_end++ = __x;
577  if (__f >= 22)
578    return 0;
579  ++__dc;
580  return 0;
581}
582
583extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<char>;
584#  if _LIBCPP_HAS_WIDE_CHARACTERS
585extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<wchar_t>;
586#  endif
587
588template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
589class _LIBCPP_TEMPLATE_VIS num_get : public locale::facet, private __num_get<_CharT> {
590public:
591  typedef _CharT char_type;
592  typedef _InputIterator iter_type;
593
594  _LIBCPP_HIDE_FROM_ABI explicit num_get(size_t __refs = 0) : locale::facet(__refs) {}
595
596  _LIBCPP_HIDE_FROM_ABI iter_type
597  get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, bool& __v) const {
598    return do_get(__b, __e, __iob, __err, __v);
599  }
600
601  _LIBCPP_HIDE_FROM_ABI iter_type
602  get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long& __v) const {
603    return do_get(__b, __e, __iob, __err, __v);
604  }
605
606  _LIBCPP_HIDE_FROM_ABI iter_type
607  get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long long& __v) const {
608    return do_get(__b, __e, __iob, __err, __v);
609  }
610
611  _LIBCPP_HIDE_FROM_ABI iter_type
612  get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned short& __v) const {
613    return do_get(__b, __e, __iob, __err, __v);
614  }
615
616  _LIBCPP_HIDE_FROM_ABI iter_type
617  get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned int& __v) const {
618    return do_get(__b, __e, __iob, __err, __v);
619  }
620
621  _LIBCPP_HIDE_FROM_ABI iter_type
622  get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned long& __v) const {
623    return do_get(__b, __e, __iob, __err, __v);
624  }
625
626  _LIBCPP_HIDE_FROM_ABI iter_type
627  get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned long long& __v) const {
628    return do_get(__b, __e, __iob, __err, __v);
629  }
630
631  _LIBCPP_HIDE_FROM_ABI iter_type
632  get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, float& __v) const {
633    return do_get(__b, __e, __iob, __err, __v);
634  }
635
636  _LIBCPP_HIDE_FROM_ABI iter_type
637  get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, double& __v) const {
638    return do_get(__b, __e, __iob, __err, __v);
639  }
640
641  _LIBCPP_HIDE_FROM_ABI iter_type
642  get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long double& __v) const {
643    return do_get(__b, __e, __iob, __err, __v);
644  }
645
646  _LIBCPP_HIDE_FROM_ABI iter_type
647  get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, void*& __v) const {
648    return do_get(__b, __e, __iob, __err, __v);
649  }
650
651  static locale::id id;
652
653protected:
654  _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~num_get() override {}
655
656  template <class _Fp>
657  _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS iter_type
658  __do_get_floating_point(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, _Fp& __v) const;
659
660  template <class _Signed>
661  _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS iter_type
662  __do_get_signed(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, _Signed& __v) const;
663
664  template <class _Unsigned>
665  _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS iter_type
666  __do_get_unsigned(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, _Unsigned& __v) const;
667
668  virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, bool& __v) const;
669
670  virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long& __v) const {
671    return this->__do_get_signed(__b, __e, __iob, __err, __v);
672  }
673
674  virtual iter_type
675  do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long long& __v) const {
676    return this->__do_get_signed(__b, __e, __iob, __err, __v);
677  }
678
679  virtual iter_type
680  do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned short& __v) const {
681    return this->__do_get_unsigned(__b, __e, __iob, __err, __v);
682  }
683
684  virtual iter_type
685  do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned int& __v) const {
686    return this->__do_get_unsigned(__b, __e, __iob, __err, __v);
687  }
688
689  virtual iter_type
690  do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned long& __v) const {
691    return this->__do_get_unsigned(__b, __e, __iob, __err, __v);
692  }
693
694  virtual iter_type
695  do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned long long& __v) const {
696    return this->__do_get_unsigned(__b, __e, __iob, __err, __v);
697  }
698
699  virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, float& __v) const {
700    return this->__do_get_floating_point(__b, __e, __iob, __err, __v);
701  }
702
703  virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, double& __v) const {
704    return this->__do_get_floating_point(__b, __e, __iob, __err, __v);
705  }
706
707  virtual iter_type
708  do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long double& __v) const {
709    return this->__do_get_floating_point(__b, __e, __iob, __err, __v);
710  }
711
712  virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, void*& __v) const;
713};
714
715template <class _CharT, class _InputIterator>
716locale::id num_get<_CharT, _InputIterator>::id;
717
718template <class _Tp>
719_LIBCPP_HIDE_FROM_ABI _Tp
720__num_get_signed_integral(const char* __a, const char* __a_end, ios_base::iostate& __err, int __base) {
721  if (__a != __a_end) {
722    __libcpp_remove_reference_t<decltype(errno)> __save_errno = errno;
723    errno                                                     = 0;
724    char* __p2;
725    long long __ll = __locale::__strtoll(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
726    __libcpp_remove_reference_t<decltype(errno)> __current_errno = errno;
727    if (__current_errno == 0)
728      errno = __save_errno;
729    if (__p2 != __a_end) {
730      __err = ios_base::failbit;
731      return 0;
732    } else if (__current_errno == ERANGE || __ll < numeric_limits<_Tp>::min() || numeric_limits<_Tp>::max() < __ll) {
733      __err = ios_base::failbit;
734      if (__ll > 0)
735        return numeric_limits<_Tp>::max();
736      else
737        return numeric_limits<_Tp>::min();
738    }
739    return static_cast<_Tp>(__ll);
740  }
741  __err = ios_base::failbit;
742  return 0;
743}
744
745template <class _Tp>
746_LIBCPP_HIDE_FROM_ABI _Tp
747__num_get_unsigned_integral(const char* __a, const char* __a_end, ios_base::iostate& __err, int __base) {
748  if (__a != __a_end) {
749    const bool __negate = *__a == '-';
750    if (__negate && ++__a == __a_end) {
751      __err = ios_base::failbit;
752      return 0;
753    }
754    __libcpp_remove_reference_t<decltype(errno)> __save_errno = errno;
755    errno                                                     = 0;
756    char* __p2;
757    unsigned long long __ll = __locale::__strtoull(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
758    __libcpp_remove_reference_t<decltype(errno)> __current_errno = errno;
759    if (__current_errno == 0)
760      errno = __save_errno;
761    if (__p2 != __a_end) {
762      __err = ios_base::failbit;
763      return 0;
764    } else if (__current_errno == ERANGE || numeric_limits<_Tp>::max() < __ll) {
765      __err = ios_base::failbit;
766      return numeric_limits<_Tp>::max();
767    }
768    _Tp __res = static_cast<_Tp>(__ll);
769    if (__negate)
770      __res = -__res;
771    return __res;
772  }
773  __err = ios_base::failbit;
774  return 0;
775}
776
777template <class _Tp>
778_LIBCPP_HIDE_FROM_ABI _Tp __do_strtod(const char* __a, char** __p2);
779
780template <>
781inline _LIBCPP_HIDE_FROM_ABI float __do_strtod<float>(const char* __a, char** __p2) {
782  return __locale::__strtof(__a, __p2, _LIBCPP_GET_C_LOCALE);
783}
784
785template <>
786inline _LIBCPP_HIDE_FROM_ABI double __do_strtod<double>(const char* __a, char** __p2) {
787  return __locale::__strtod(__a, __p2, _LIBCPP_GET_C_LOCALE);
788}
789
790template <>
791inline _LIBCPP_HIDE_FROM_ABI long double __do_strtod<long double>(const char* __a, char** __p2) {
792  return __locale::__strtold(__a, __p2, _LIBCPP_GET_C_LOCALE);
793}
794
795template <class _Tp>
796_LIBCPP_HIDE_FROM_ABI _Tp __num_get_float(const char* __a, const char* __a_end, ios_base::iostate& __err) {
797  if (__a != __a_end) {
798    __libcpp_remove_reference_t<decltype(errno)> __save_errno = errno;
799    errno                                                     = 0;
800    char* __p2;
801    _Tp __ld                                                     = std::__do_strtod<_Tp>(__a, &__p2);
802    __libcpp_remove_reference_t<decltype(errno)> __current_errno = errno;
803    if (__current_errno == 0)
804      errno = __save_errno;
805    if (__p2 != __a_end) {
806      __err = ios_base::failbit;
807      return 0;
808    } else if (__current_errno == ERANGE)
809      __err = ios_base::failbit;
810    return __ld;
811  }
812  __err = ios_base::failbit;
813  return 0;
814}
815
816template <class _CharT, class _InputIterator>
817_InputIterator num_get<_CharT, _InputIterator>::do_get(
818    iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, bool& __v) const {
819  if ((__iob.flags() & ios_base::boolalpha) == 0) {
820    long __lv = -1;
821    __b       = do_get(__b, __e, __iob, __err, __lv);
822    switch (__lv) {
823    case 0:
824      __v = false;
825      break;
826    case 1:
827      __v = true;
828      break;
829    default:
830      __v   = true;
831      __err = ios_base::failbit;
832      break;
833    }
834    return __b;
835  }
836  const ctype<_CharT>& __ct    = std::use_facet<ctype<_CharT> >(__iob.getloc());
837  const numpunct<_CharT>& __np = std::use_facet<numpunct<_CharT> >(__iob.getloc());
838  typedef typename numpunct<_CharT>::string_type string_type;
839  const string_type __names[2] = {__np.truename(), __np.falsename()};
840  const string_type* __i       = std::__scan_keyword(__b, __e, __names, __names + 2, __ct, __err);
841  __v                          = __i == __names;
842  return __b;
843}
844
845// signed
846
847template <class _CharT, class _InputIterator>
848template <class _Signed>
849_InputIterator num_get<_CharT, _InputIterator>::__do_get_signed(
850    iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, _Signed& __v) const {
851  // Stage 1
852  int __base = this->__get_base(__iob);
853  // Stage 2
854  char_type __thousands_sep;
855  const int __atoms_size = __num_get_base::__int_chr_cnt;
856#  ifdef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
857  char_type __atoms1[__atoms_size];
858  const char_type* __atoms = this->__do_widen(__iob, __atoms1);
859  string __grouping        = this->__stage2_int_prep(__iob, __thousands_sep);
860#  else
861  char_type __atoms[__atoms_size];
862  string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
863#  endif
864  string __buf;
865  __buf.resize(__buf.capacity());
866  char* __a     = &__buf[0];
867  char* __a_end = __a;
868  unsigned __g[__num_get_base::__num_get_buf_sz];
869  unsigned* __g_end = __g;
870  unsigned __dc     = 0;
871  for (; __b != __e; ++__b) {
872    if (__a_end == __a + __buf.size()) {
873      size_t __tmp = __buf.size();
874      __buf.resize(2 * __buf.size());
875      __buf.resize(__buf.capacity());
876      __a     = &__buf[0];
877      __a_end = __a + __tmp;
878    }
879    if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc, __thousands_sep, __grouping, __g, __g_end, __atoms))
880      break;
881  }
882  if (__grouping.size() != 0 && __g_end - __g < __num_get_base::__num_get_buf_sz)
883    *__g_end++ = __dc;
884  // Stage 3
885  __v = std::__num_get_signed_integral<_Signed>(__a, __a_end, __err, __base);
886  // Digit grouping checked
887  __check_grouping(__grouping, __g, __g_end, __err);
888  // EOF checked
889  if (__b == __e)
890    __err |= ios_base::eofbit;
891  return __b;
892}
893
894// unsigned
895
896template <class _CharT, class _InputIterator>
897template <class _Unsigned>
898_InputIterator num_get<_CharT, _InputIterator>::__do_get_unsigned(
899    iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, _Unsigned& __v) const {
900  // Stage 1
901  int __base = this->__get_base(__iob);
902  // Stage 2
903  char_type __thousands_sep;
904  const int __atoms_size = __num_get_base::__int_chr_cnt;
905#  ifdef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
906  char_type __atoms1[__atoms_size];
907  const char_type* __atoms = this->__do_widen(__iob, __atoms1);
908  string __grouping        = this->__stage2_int_prep(__iob, __thousands_sep);
909#  else
910  char_type __atoms[__atoms_size];
911  string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
912#  endif
913  string __buf;
914  __buf.resize(__buf.capacity());
915  char* __a     = &__buf[0];
916  char* __a_end = __a;
917  unsigned __g[__num_get_base::__num_get_buf_sz];
918  unsigned* __g_end = __g;
919  unsigned __dc     = 0;
920  for (; __b != __e; ++__b) {
921    if (__a_end == __a + __buf.size()) {
922      size_t __tmp = __buf.size();
923      __buf.resize(2 * __buf.size());
924      __buf.resize(__buf.capacity());
925      __a     = &__buf[0];
926      __a_end = __a + __tmp;
927    }
928    if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc, __thousands_sep, __grouping, __g, __g_end, __atoms))
929      break;
930  }
931  if (__grouping.size() != 0 && __g_end - __g < __num_get_base::__num_get_buf_sz)
932    *__g_end++ = __dc;
933  // Stage 3
934  __v = std::__num_get_unsigned_integral<_Unsigned>(__a, __a_end, __err, __base);
935  // Digit grouping checked
936  __check_grouping(__grouping, __g, __g_end, __err);
937  // EOF checked
938  if (__b == __e)
939    __err |= ios_base::eofbit;
940  return __b;
941}
942
943// floating point
944
945template <class _CharT, class _InputIterator>
946template <class _Fp>
947_InputIterator num_get<_CharT, _InputIterator>::__do_get_floating_point(
948    iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, _Fp& __v) const {
949  // Stage 1, nothing to do
950  // Stage 2
951  char_type __atoms[__num_get_base::__fp_chr_cnt];
952  char_type __decimal_point;
953  char_type __thousands_sep;
954  string __grouping = this->__stage2_float_prep(__iob, __atoms, __decimal_point, __thousands_sep);
955  string __buf;
956  __buf.resize(__buf.capacity());
957  char* __a     = &__buf[0];
958  char* __a_end = __a;
959  unsigned __g[__num_get_base::__num_get_buf_sz];
960  unsigned* __g_end        = __g;
961  unsigned __dc            = 0;
962  bool __in_units          = true;
963  char __exp               = 'E';
964  bool __is_leading_parsed = false;
965  for (; __b != __e; ++__b) {
966    if (__a_end == __a + __buf.size()) {
967      size_t __tmp = __buf.size();
968      __buf.resize(2 * __buf.size());
969      __buf.resize(__buf.capacity());
970      __a     = &__buf[0];
971      __a_end = __a + __tmp;
972    }
973    if (this->__stage2_float_loop(
974            *__b,
975            __in_units,
976            __exp,
977            __a,
978            __a_end,
979            __decimal_point,
980            __thousands_sep,
981            __grouping,
982            __g,
983            __g_end,
984            __dc,
985            __atoms))
986      break;
987
988    // the leading character excluding the sign must be a decimal digit
989    if (!__is_leading_parsed) {
990      if (__a_end - __a >= 1 && __a[0] != '-' && __a[0] != '+') {
991        if (('0' <= __a[0] && __a[0] <= '9') || __a[0] == '.')
992          __is_leading_parsed = true;
993        else
994          break;
995      } else if (__a_end - __a >= 2 && (__a[0] == '-' || __a[0] == '+')) {
996        if (('0' <= __a[1] && __a[1] <= '9') || __a[1] == '.')
997          __is_leading_parsed = true;
998        else
999          break;
1000      }
1001    }
1002  }
1003  if (__grouping.size() != 0 && __in_units && __g_end - __g < __num_get_base::__num_get_buf_sz)
1004    *__g_end++ = __dc;
1005  // Stage 3
1006  __v = std::__num_get_float<_Fp>(__a, __a_end, __err);
1007  // Digit grouping checked
1008  __check_grouping(__grouping, __g, __g_end, __err);
1009  // EOF checked
1010  if (__b == __e)
1011    __err |= ios_base::eofbit;
1012  return __b;
1013}
1014
1015template <class _CharT, class _InputIterator>
1016_InputIterator num_get<_CharT, _InputIterator>::do_get(
1017    iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, void*& __v) const {
1018  // Stage 1
1019  int __base = 16;
1020  // Stage 2
1021  char_type __atoms[__num_get_base::__int_chr_cnt];
1022  char_type __thousands_sep = char_type();
1023  string __grouping;
1024  std::use_facet<ctype<_CharT> >(__iob.getloc())
1025      .widen(__num_get_base::__src, __num_get_base::__src + __num_get_base::__int_chr_cnt, __atoms);
1026  string __buf;
1027  __buf.resize(__buf.capacity());
1028  char* __a     = &__buf[0];
1029  char* __a_end = __a;
1030  unsigned __g[__num_get_base::__num_get_buf_sz];
1031  unsigned* __g_end = __g;
1032  unsigned __dc     = 0;
1033  for (; __b != __e; ++__b) {
1034    if (__a_end == __a + __buf.size()) {
1035      size_t __tmp = __buf.size();
1036      __buf.resize(2 * __buf.size());
1037      __buf.resize(__buf.capacity());
1038      __a     = &__buf[0];
1039      __a_end = __a + __tmp;
1040    }
1041    if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc, __thousands_sep, __grouping, __g, __g_end, __atoms))
1042      break;
1043  }
1044  // Stage 3
1045  __buf.resize(__a_end - __a);
1046  if (__locale::__sscanf(__buf.c_str(), _LIBCPP_GET_C_LOCALE, "%p", &__v) != 1)
1047    __err = ios_base::failbit;
1048  // EOF checked
1049  if (__b == __e)
1050    __err |= ios_base::eofbit;
1051  return __b;
1052}
1053
1054extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<char>;
1055#  if _LIBCPP_HAS_WIDE_CHARACTERS
1056extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<wchar_t>;
1057#  endif
1058
1059struct _LIBCPP_EXPORTED_FROM_ABI __num_put_base {
1060protected:
1061  static void __format_int(char* __fmt, const char* __len, bool __signd, ios_base::fmtflags __flags);
1062  static bool __format_float(char* __fmt, const char* __len, ios_base::fmtflags __flags);
1063  static char* __identify_padding(char* __nb, char* __ne, const ios_base& __iob);
1064};
1065
1066template <class _CharT>
1067struct __num_put : protected __num_put_base {
1068  static void __widen_and_group_int(
1069      char* __nb, char* __np, char* __ne, _CharT* __ob, _CharT*& __op, _CharT*& __oe, const locale& __loc);
1070  static void __widen_and_group_float(
1071      char* __nb, char* __np, char* __ne, _CharT* __ob, _CharT*& __op, _CharT*& __oe, const locale& __loc);
1072};
1073
1074template <class _CharT>
1075void __num_put<_CharT>::__widen_and_group_int(
1076    char* __nb, char* __np, char* __ne, _CharT* __ob, _CharT*& __op, _CharT*& __oe, const locale& __loc) {
1077  const ctype<_CharT>& __ct     = std::use_facet<ctype<_CharT> >(__loc);
1078  const numpunct<_CharT>& __npt = std::use_facet<numpunct<_CharT> >(__loc);
1079  string __grouping             = __npt.grouping();
1080  if (__grouping.empty()) {
1081    __ct.widen(__nb, __ne, __ob);
1082    __oe = __ob + (__ne - __nb);
1083  } else {
1084    __oe       = __ob;
1085    char* __nf = __nb;
1086    if (*__nf == '-' || *__nf == '+')
1087      *__oe++ = __ct.widen(*__nf++);
1088    if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' || __nf[1] == 'X')) {
1089      *__oe++ = __ct.widen(*__nf++);
1090      *__oe++ = __ct.widen(*__nf++);
1091    }
1092    std::reverse(__nf, __ne);
1093    _CharT __thousands_sep = __npt.thousands_sep();
1094    unsigned __dc          = 0;
1095    unsigned __dg          = 0;
1096    for (char* __p = __nf; __p < __ne; ++__p) {
1097      if (static_cast<unsigned>(__grouping[__dg]) > 0 && __dc == static_cast<unsigned>(__grouping[__dg])) {
1098        *__oe++ = __thousands_sep;
1099        __dc    = 0;
1100        if (__dg < __grouping.size() - 1)
1101          ++__dg;
1102      }
1103      *__oe++ = __ct.widen(*__p);
1104      ++__dc;
1105    }
1106    std::reverse(__ob + (__nf - __nb), __oe);
1107  }
1108  if (__np == __ne)
1109    __op = __oe;
1110  else
1111    __op = __ob + (__np - __nb);
1112}
1113
1114template <class _CharT>
1115void __num_put<_CharT>::__widen_and_group_float(
1116    char* __nb, char* __np, char* __ne, _CharT* __ob, _CharT*& __op, _CharT*& __oe, const locale& __loc) {
1117  const ctype<_CharT>& __ct     = std::use_facet<ctype<_CharT> >(__loc);
1118  const numpunct<_CharT>& __npt = std::use_facet<numpunct<_CharT> >(__loc);
1119  string __grouping             = __npt.grouping();
1120  __oe                          = __ob;
1121  char* __nf                    = __nb;
1122  if (*__nf == '-' || *__nf == '+')
1123    *__oe++ = __ct.widen(*__nf++);
1124  char* __ns;
1125  if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' || __nf[1] == 'X')) {
1126    *__oe++ = __ct.widen(*__nf++);
1127    *__oe++ = __ct.widen(*__nf++);
1128    for (__ns = __nf; __ns < __ne; ++__ns)
1129      if (!__locale::__isxdigit(*__ns, _LIBCPP_GET_C_LOCALE))
1130        break;
1131  } else {
1132    for (__ns = __nf; __ns < __ne; ++__ns)
1133      if (!__locale::__isdigit(*__ns, _LIBCPP_GET_C_LOCALE))
1134        break;
1135  }
1136  if (__grouping.empty()) {
1137    __ct.widen(__nf, __ns, __oe);
1138    __oe += __ns - __nf;
1139  } else {
1140    std::reverse(__nf, __ns);
1141    _CharT __thousands_sep = __npt.thousands_sep();
1142    unsigned __dc          = 0;
1143    unsigned __dg          = 0;
1144    for (char* __p = __nf; __p < __ns; ++__p) {
1145      if (__grouping[__dg] > 0 && __dc == static_cast<unsigned>(__grouping[__dg])) {
1146        *__oe++ = __thousands_sep;
1147        __dc    = 0;
1148        if (__dg < __grouping.size() - 1)
1149          ++__dg;
1150      }
1151      *__oe++ = __ct.widen(*__p);
1152      ++__dc;
1153    }
1154    std::reverse(__ob + (__nf - __nb), __oe);
1155  }
1156  for (__nf = __ns; __nf < __ne; ++__nf) {
1157    if (*__nf == '.') {
1158      *__oe++ = __npt.decimal_point();
1159      ++__nf;
1160      break;
1161    } else
1162      *__oe++ = __ct.widen(*__nf);
1163  }
1164  __ct.widen(__nf, __ne, __oe);
1165  __oe += __ne - __nf;
1166  if (__np == __ne)
1167    __op = __oe;
1168  else
1169    __op = __ob + (__np - __nb);
1170}
1171
1172extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<char>;
1173#  if _LIBCPP_HAS_WIDE_CHARACTERS
1174extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<wchar_t>;
1175#  endif
1176
1177template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
1178class _LIBCPP_TEMPLATE_VIS num_put : public locale::facet, private __num_put<_CharT> {
1179public:
1180  typedef _CharT char_type;
1181  typedef _OutputIterator iter_type;
1182
1183  _LIBCPP_HIDE_FROM_ABI explicit num_put(size_t __refs = 0) : locale::facet(__refs) {}
1184
1185  _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, bool __v) const {
1186    return do_put(__s, __iob, __fl, __v);
1187  }
1188
1189  _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, long __v) const {
1190    return do_put(__s, __iob, __fl, __v);
1191  }
1192
1193  _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, long long __v) const {
1194    return do_put(__s, __iob, __fl, __v);
1195  }
1196
1197  _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long __v) const {
1198    return do_put(__s, __iob, __fl, __v);
1199  }
1200
1201  _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long long __v) const {
1202    return do_put(__s, __iob, __fl, __v);
1203  }
1204
1205  _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, double __v) const {
1206    return do_put(__s, __iob, __fl, __v);
1207  }
1208
1209  _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, long double __v) const {
1210    return do_put(__s, __iob, __fl, __v);
1211  }
1212
1213  _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, const void* __v) const {
1214    return do_put(__s, __iob, __fl, __v);
1215  }
1216
1217  static locale::id id;
1218
1219protected:
1220  _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~num_put() override {}
1221
1222  virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, bool __v) const;
1223  virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, long __v) const;
1224  virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, long long __v) const;
1225  virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long) const;
1226  virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long long) const;
1227  virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, double __v) const;
1228  virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, long double __v) const;
1229  virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, const void* __v) const;
1230
1231  template <class _Integral>
1232  _LIBCPP_HIDE_FROM_ABI inline _OutputIterator
1233  __do_put_integral(iter_type __s, ios_base& __iob, char_type __fl, _Integral __v, char const* __len) const;
1234
1235  template <class _Float>
1236  _LIBCPP_HIDE_FROM_ABI inline _OutputIterator
1237  __do_put_floating_point(iter_type __s, ios_base& __iob, char_type __fl, _Float __v, char const* __len) const;
1238};
1239
1240template <class _CharT, class _OutputIterator>
1241locale::id num_put<_CharT, _OutputIterator>::id;
1242
1243template <class _CharT, class _OutputIterator>
1244_OutputIterator
1245num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, bool __v) const {
1246  if ((__iob.flags() & ios_base::boolalpha) == 0)
1247    return do_put(__s, __iob, __fl, (unsigned long)__v);
1248  const numpunct<char_type>& __np = std::use_facet<numpunct<char_type> >(__iob.getloc());
1249  typedef typename numpunct<char_type>::string_type string_type;
1250  string_type __nm = __v ? __np.truename() : __np.falsename();
1251  for (typename string_type::iterator __i = __nm.begin(); __i != __nm.end(); ++__i, ++__s)
1252    *__s = *__i;
1253  return __s;
1254}
1255
1256template <class _CharT, class _OutputIterator>
1257template <class _Integral>
1258_LIBCPP_HIDE_FROM_ABI inline _OutputIterator num_put<_CharT, _OutputIterator>::__do_put_integral(
1259    iter_type __s, ios_base& __iob, char_type __fl, _Integral __v, char const* __len) const {
1260  // Stage 1 - Get number in narrow char
1261  char __fmt[8] = {'%', 0};
1262  this->__format_int(__fmt + 1, __len, is_signed<_Integral>::value, __iob.flags());
1263  // Worst case is octal, with showbase enabled. Note that octal is always
1264  // printed as an unsigned value.
1265  using _Unsigned = typename make_unsigned<_Integral>::type;
1266  _LIBCPP_CONSTEXPR const unsigned __nbuf =
1267      (numeric_limits<_Unsigned>::digits / 3)          // 1 char per 3 bits
1268      + ((numeric_limits<_Unsigned>::digits % 3) != 0) // round up
1269      + 2;                                             // base prefix + terminating null character
1270  char __nar[__nbuf];
1271  _LIBCPP_DIAGNOSTIC_PUSH
1272  _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
1273  _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
1274  int __nc = __locale::__snprintf(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1275  _LIBCPP_DIAGNOSTIC_POP
1276  char* __ne = __nar + __nc;
1277  char* __np = this->__identify_padding(__nar, __ne, __iob);
1278  // Stage 2 - Widen __nar while adding thousands separators
1279  char_type __o[2 * (__nbuf - 1) - 1];
1280  char_type* __op; // pad here
1281  char_type* __oe; // end of output
1282  this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1283  // [__o, __oe) contains thousands_sep'd wide number
1284  // Stage 3 & 4
1285  return std::__pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1286}
1287
1288template <class _CharT, class _OutputIterator>
1289_OutputIterator
1290num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, long __v) const {
1291  return this->__do_put_integral(__s, __iob, __fl, __v, "l");
1292}
1293
1294template <class _CharT, class _OutputIterator>
1295_OutputIterator
1296num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, long long __v) const {
1297  return this->__do_put_integral(__s, __iob, __fl, __v, "ll");
1298}
1299
1300template <class _CharT, class _OutputIterator>
1301_OutputIterator
1302num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long __v) const {
1303  return this->__do_put_integral(__s, __iob, __fl, __v, "l");
1304}
1305
1306template <class _CharT, class _OutputIterator>
1307_OutputIterator
1308num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long long __v) const {
1309  return this->__do_put_integral(__s, __iob, __fl, __v, "ll");
1310}
1311
1312template <class _CharT, class _OutputIterator>
1313template <class _Float>
1314_LIBCPP_HIDE_FROM_ABI inline _OutputIterator num_put<_CharT, _OutputIterator>::__do_put_floating_point(
1315    iter_type __s, ios_base& __iob, char_type __fl, _Float __v, char const* __len) const {
1316  // Stage 1 - Get number in narrow char
1317  char __fmt[8]            = {'%', 0};
1318  bool __specify_precision = this->__format_float(__fmt + 1, __len, __iob.flags());
1319  const unsigned __nbuf    = 30;
1320  char __nar[__nbuf];
1321  char* __nb = __nar;
1322  int __nc;
1323  _LIBCPP_DIAGNOSTIC_PUSH
1324  _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
1325  _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
1326  if (__specify_precision)
1327    __nc = __locale::__snprintf(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
1328  else
1329    __nc = __locale::__snprintf(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1330  unique_ptr<char, void (*)(void*)> __nbh(nullptr, free);
1331  if (__nc > static_cast<int>(__nbuf - 1)) {
1332    if (__specify_precision)
1333      __nc = __locale::__asprintf(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
1334    else
1335      __nc = __locale::__asprintf(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1336    if (__nc == -1)
1337      __throw_bad_alloc();
1338    __nbh.reset(__nb);
1339  }
1340  _LIBCPP_DIAGNOSTIC_POP
1341  char* __ne = __nb + __nc;
1342  char* __np = this->__identify_padding(__nb, __ne, __iob);
1343  // Stage 2 - Widen __nar while adding thousands separators
1344  char_type __o[2 * (__nbuf - 1) - 1];
1345  char_type* __ob = __o;
1346  unique_ptr<char_type, void (*)(void*)> __obh(0, free);
1347  if (__nb != __nar) {
1348    __ob = (char_type*)malloc(2 * static_cast<size_t>(__nc) * sizeof(char_type));
1349    if (__ob == 0)
1350      __throw_bad_alloc();
1351    __obh.reset(__ob);
1352  }
1353  char_type* __op; // pad here
1354  char_type* __oe; // end of output
1355  this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1356  // [__o, __oe) contains thousands_sep'd wide number
1357  // Stage 3 & 4
1358  __s = std::__pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1359  return __s;
1360}
1361
1362template <class _CharT, class _OutputIterator>
1363_OutputIterator
1364num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, double __v) const {
1365  return this->__do_put_floating_point(__s, __iob, __fl, __v, "");
1366}
1367
1368template <class _CharT, class _OutputIterator>
1369_OutputIterator
1370num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, long double __v) const {
1371  return this->__do_put_floating_point(__s, __iob, __fl, __v, "L");
1372}
1373
1374template <class _CharT, class _OutputIterator>
1375_OutputIterator
1376num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, const void* __v) const {
1377  // Stage 1 - Get pointer in narrow char
1378  const unsigned __nbuf = 20;
1379  char __nar[__nbuf];
1380  int __nc   = __locale::__snprintf(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, "%p", __v);
1381  char* __ne = __nar + __nc;
1382  char* __np = this->__identify_padding(__nar, __ne, __iob);
1383  // Stage 2 - Widen __nar
1384  char_type __o[2 * (__nbuf - 1) - 1];
1385  char_type* __op; // pad here
1386  char_type* __oe; // end of output
1387  const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
1388  __ct.widen(__nar, __ne, __o);
1389  __oe = __o + (__ne - __nar);
1390  if (__np == __ne)
1391    __op = __oe;
1392  else
1393    __op = __o + (__np - __nar);
1394  // [__o, __oe) contains wide number
1395  // Stage 3 & 4
1396  return std::__pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1397}
1398
1399extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<char>;
1400#  if _LIBCPP_HAS_WIDE_CHARACTERS
1401extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<wchar_t>;
1402#  endif
1403
1404template <class _CharT, class _InputIterator>
1405_LIBCPP_HIDE_FROM_ABI int __get_up_to_n_digits(
1406    _InputIterator& __b, _InputIterator __e, ios_base::iostate& __err, const ctype<_CharT>& __ct, int __n) {
1407  // Precondition:  __n >= 1
1408  if (__b == __e) {
1409    __err |= ios_base::eofbit | ios_base::failbit;
1410    return 0;
1411  }
1412  // get first digit
1413  _CharT __c = *__b;
1414  if (!__ct.is(ctype_base::digit, __c)) {
1415    __err |= ios_base::failbit;
1416    return 0;
1417  }
1418  int __r = __ct.narrow(__c, 0) - '0';
1419  for (++__b, (void)--__n; __b != __e && __n > 0; ++__b, (void)--__n) {
1420    // get next digit
1421    __c = *__b;
1422    if (!__ct.is(ctype_base::digit, __c))
1423      return __r;
1424    __r = __r * 10 + __ct.narrow(__c, 0) - '0';
1425  }
1426  if (__b == __e)
1427    __err |= ios_base::eofbit;
1428  return __r;
1429}
1430
1431class _LIBCPP_EXPORTED_FROM_ABI time_base {
1432public:
1433  enum dateorder { no_order, dmy, mdy, ymd, ydm };
1434};
1435
1436template <class _CharT>
1437class _LIBCPP_TEMPLATE_VIS __time_get_c_storage {
1438protected:
1439  typedef basic_string<_CharT> string_type;
1440
1441  virtual const string_type* __weeks() const;
1442  virtual const string_type* __months() const;
1443  virtual const string_type* __am_pm() const;
1444  virtual const string_type& __c() const;
1445  virtual const string_type& __r() const;
1446  virtual const string_type& __x() const;
1447  virtual const string_type& __X() const;
1448
1449  _LIBCPP_HIDE_FROM_ABI ~__time_get_c_storage() {}
1450};
1451
1452template <>
1453_LIBCPP_EXPORTED_FROM_ABI const string* __time_get_c_storage<char>::__weeks() const;
1454template <>
1455_LIBCPP_EXPORTED_FROM_ABI const string* __time_get_c_storage<char>::__months() const;
1456template <>
1457_LIBCPP_EXPORTED_FROM_ABI const string* __time_get_c_storage<char>::__am_pm() const;
1458template <>
1459_LIBCPP_EXPORTED_FROM_ABI const string& __time_get_c_storage<char>::__c() const;
1460template <>
1461_LIBCPP_EXPORTED_FROM_ABI const string& __time_get_c_storage<char>::__r() const;
1462template <>
1463_LIBCPP_EXPORTED_FROM_ABI const string& __time_get_c_storage<char>::__x() const;
1464template <>
1465_LIBCPP_EXPORTED_FROM_ABI const string& __time_get_c_storage<char>::__X() const;
1466
1467#  if _LIBCPP_HAS_WIDE_CHARACTERS
1468template <>
1469_LIBCPP_EXPORTED_FROM_ABI const wstring* __time_get_c_storage<wchar_t>::__weeks() const;
1470template <>
1471_LIBCPP_EXPORTED_FROM_ABI const wstring* __time_get_c_storage<wchar_t>::__months() const;
1472template <>
1473_LIBCPP_EXPORTED_FROM_ABI const wstring* __time_get_c_storage<wchar_t>::__am_pm() const;
1474template <>
1475_LIBCPP_EXPORTED_FROM_ABI const wstring& __time_get_c_storage<wchar_t>::__c() const;
1476template <>
1477_LIBCPP_EXPORTED_FROM_ABI const wstring& __time_get_c_storage<wchar_t>::__r() const;
1478template <>
1479_LIBCPP_EXPORTED_FROM_ABI const wstring& __time_get_c_storage<wchar_t>::__x() const;
1480template <>
1481_LIBCPP_EXPORTED_FROM_ABI const wstring& __time_get_c_storage<wchar_t>::__X() const;
1482#  endif
1483
1484template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
1485class _LIBCPP_TEMPLATE_VIS time_get : public locale::facet, public time_base, private __time_get_c_storage<_CharT> {
1486public:
1487  typedef _CharT char_type;
1488  typedef _InputIterator iter_type;
1489  typedef time_base::dateorder dateorder;
1490  typedef basic_string<char_type> string_type;
1491
1492  _LIBCPP_HIDE_FROM_ABI explicit time_get(size_t __refs = 0) : locale::facet(__refs) {}
1493
1494  _LIBCPP_HIDE_FROM_ABI dateorder date_order() const { return this->do_date_order(); }
1495
1496  _LIBCPP_HIDE_FROM_ABI iter_type
1497  get_time(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1498    return do_get_time(__b, __e, __iob, __err, __tm);
1499  }
1500
1501  _LIBCPP_HIDE_FROM_ABI iter_type
1502  get_date(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1503    return do_get_date(__b, __e, __iob, __err, __tm);
1504  }
1505
1506  _LIBCPP_HIDE_FROM_ABI iter_type
1507  get_weekday(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1508    return do_get_weekday(__b, __e, __iob, __err, __tm);
1509  }
1510
1511  _LIBCPP_HIDE_FROM_ABI iter_type
1512  get_monthname(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1513    return do_get_monthname(__b, __e, __iob, __err, __tm);
1514  }
1515
1516  _LIBCPP_HIDE_FROM_ABI iter_type
1517  get_year(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1518    return do_get_year(__b, __e, __iob, __err, __tm);
1519  }
1520
1521  _LIBCPP_HIDE_FROM_ABI iter_type
1522  get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm, char __fmt, char __mod = 0)
1523      const {
1524    return do_get(__b, __e, __iob, __err, __tm, __fmt, __mod);
1525  }
1526
1527  iter_type
1528  get(iter_type __b,
1529      iter_type __e,
1530      ios_base& __iob,
1531      ios_base::iostate& __err,
1532      tm* __tm,
1533      const char_type* __fmtb,
1534      const char_type* __fmte) const;
1535
1536  static locale::id id;
1537
1538protected:
1539  _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~time_get() override {}
1540
1541  virtual dateorder do_date_order() const;
1542  virtual iter_type
1543  do_get_time(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const;
1544  virtual iter_type
1545  do_get_date(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const;
1546  virtual iter_type
1547  do_get_weekday(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const;
1548  virtual iter_type
1549  do_get_monthname(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const;
1550  virtual iter_type
1551  do_get_year(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const;
1552  virtual iter_type do_get(
1553      iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm, char __fmt, char __mod) const;
1554
1555private:
1556  void __get_white_space(iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1557  void __get_percent(iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1558
1559  void __get_weekdayname(
1560      int& __m, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1561  void __get_monthname(
1562      int& __m, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1563  void __get_day(int& __d, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1564  void
1565  __get_month(int& __m, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1566  void
1567  __get_year(int& __y, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1568  void
1569  __get_year4(int& __y, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1570  void
1571  __get_hour(int& __d, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1572  void
1573  __get_12_hour(int& __h, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1574  void
1575  __get_am_pm(int& __h, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1576  void
1577  __get_minute(int& __m, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1578  void
1579  __get_second(int& __s, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1580  void
1581  __get_weekday(int& __w, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1582  void __get_day_year_num(
1583      int& __w, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1584};
1585
1586template <class _CharT, class _InputIterator>
1587locale::id time_get<_CharT, _InputIterator>::id;
1588
1589// time_get primitives
1590
1591template <class _CharT, class _InputIterator>
1592void time_get<_CharT, _InputIterator>::__get_weekdayname(
1593    int& __w, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1594  // Note:  ignoring case comes from the POSIX strptime spec
1595  const string_type* __wk = this->__weeks();
1596  ptrdiff_t __i           = std::__scan_keyword(__b, __e, __wk, __wk + 14, __ct, __err, false) - __wk;
1597  if (__i < 14)
1598    __w = __i % 7;
1599}
1600
1601template <class _CharT, class _InputIterator>
1602void time_get<_CharT, _InputIterator>::__get_monthname(
1603    int& __m, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1604  // Note:  ignoring case comes from the POSIX strptime spec
1605  const string_type* __month = this->__months();
1606  ptrdiff_t __i              = std::__scan_keyword(__b, __e, __month, __month + 24, __ct, __err, false) - __month;
1607  if (__i < 24)
1608    __m = __i % 12;
1609}
1610
1611template <class _CharT, class _InputIterator>
1612void time_get<_CharT, _InputIterator>::__get_day(
1613    int& __d, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1614  int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
1615  if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 31)
1616    __d = __t;
1617  else
1618    __err |= ios_base::failbit;
1619}
1620
1621template <class _CharT, class _InputIterator>
1622void time_get<_CharT, _InputIterator>::__get_month(
1623    int& __m, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1624  int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2) - 1;
1625  if (!(__err & ios_base::failbit) && 0 <= __t && __t <= 11)
1626    __m = __t;
1627  else
1628    __err |= ios_base::failbit;
1629}
1630
1631template <class _CharT, class _InputIterator>
1632void time_get<_CharT, _InputIterator>::__get_year(
1633    int& __y, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1634  int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 4);
1635  if (!(__err & ios_base::failbit)) {
1636    if (__t < 69)
1637      __t += 2000;
1638    else if (69 <= __t && __t <= 99)
1639      __t += 1900;
1640    __y = __t - 1900;
1641  }
1642}
1643
1644template <class _CharT, class _InputIterator>
1645void time_get<_CharT, _InputIterator>::__get_year4(
1646    int& __y, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1647  int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 4);
1648  if (!(__err & ios_base::failbit))
1649    __y = __t - 1900;
1650}
1651
1652template <class _CharT, class _InputIterator>
1653void time_get<_CharT, _InputIterator>::__get_hour(
1654    int& __h, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1655  int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
1656  if (!(__err & ios_base::failbit) && __t <= 23)
1657    __h = __t;
1658  else
1659    __err |= ios_base::failbit;
1660}
1661
1662template <class _CharT, class _InputIterator>
1663void time_get<_CharT, _InputIterator>::__get_12_hour(
1664    int& __h, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1665  int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
1666  if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 12)
1667    __h = __t;
1668  else
1669    __err |= ios_base::failbit;
1670}
1671
1672template <class _CharT, class _InputIterator>
1673void time_get<_CharT, _InputIterator>::__get_minute(
1674    int& __m, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1675  int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
1676  if (!(__err & ios_base::failbit) && __t <= 59)
1677    __m = __t;
1678  else
1679    __err |= ios_base::failbit;
1680}
1681
1682template <class _CharT, class _InputIterator>
1683void time_get<_CharT, _InputIterator>::__get_second(
1684    int& __s, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1685  int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
1686  if (!(__err & ios_base::failbit) && __t <= 60)
1687    __s = __t;
1688  else
1689    __err |= ios_base::failbit;
1690}
1691
1692template <class _CharT, class _InputIterator>
1693void time_get<_CharT, _InputIterator>::__get_weekday(
1694    int& __w, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1695  int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 1);
1696  if (!(__err & ios_base::failbit) && __t <= 6)
1697    __w = __t;
1698  else
1699    __err |= ios_base::failbit;
1700}
1701
1702template <class _CharT, class _InputIterator>
1703void time_get<_CharT, _InputIterator>::__get_day_year_num(
1704    int& __d, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1705  int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 3);
1706  if (!(__err & ios_base::failbit) && __t <= 365)
1707    __d = __t;
1708  else
1709    __err |= ios_base::failbit;
1710}
1711
1712template <class _CharT, class _InputIterator>
1713void time_get<_CharT, _InputIterator>::__get_white_space(
1714    iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1715  for (; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
1716    ;
1717  if (__b == __e)
1718    __err |= ios_base::eofbit;
1719}
1720
1721template <class _CharT, class _InputIterator>
1722void time_get<_CharT, _InputIterator>::__get_am_pm(
1723    int& __h, iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1724  const string_type* __ap = this->__am_pm();
1725  if (__ap[0].size() + __ap[1].size() == 0) {
1726    __err |= ios_base::failbit;
1727    return;
1728  }
1729  ptrdiff_t __i = std::__scan_keyword(__b, __e, __ap, __ap + 2, __ct, __err, false) - __ap;
1730  if (__i == 0 && __h == 12)
1731    __h = 0;
1732  else if (__i == 1 && __h < 12)
1733    __h += 12;
1734}
1735
1736template <class _CharT, class _InputIterator>
1737void time_get<_CharT, _InputIterator>::__get_percent(
1738    iter_type& __b, iter_type __e, ios_base::iostate& __err, const ctype<char_type>& __ct) const {
1739  if (__b == __e) {
1740    __err |= ios_base::eofbit | ios_base::failbit;
1741    return;
1742  }
1743  if (__ct.narrow(*__b, 0) != '%')
1744    __err |= ios_base::failbit;
1745  else if (++__b == __e)
1746    __err |= ios_base::eofbit;
1747}
1748
1749// time_get end primitives
1750
1751template <class _CharT, class _InputIterator>
1752_InputIterator time_get<_CharT, _InputIterator>::get(
1753    iter_type __b,
1754    iter_type __e,
1755    ios_base& __iob,
1756    ios_base::iostate& __err,
1757    tm* __tm,
1758    const char_type* __fmtb,
1759    const char_type* __fmte) const {
1760  const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
1761  __err                        = ios_base::goodbit;
1762  while (__fmtb != __fmte && __err == ios_base::goodbit) {
1763    if (__b == __e) {
1764      __err = ios_base::failbit;
1765      break;
1766    }
1767    if (__ct.narrow(*__fmtb, 0) == '%') {
1768      if (++__fmtb == __fmte) {
1769        __err = ios_base::failbit;
1770        break;
1771      }
1772      char __cmd = __ct.narrow(*__fmtb, 0);
1773      char __opt = '\0';
1774      if (__cmd == 'E' || __cmd == '0') {
1775        if (++__fmtb == __fmte) {
1776          __err = ios_base::failbit;
1777          break;
1778        }
1779        __opt = __cmd;
1780        __cmd = __ct.narrow(*__fmtb, 0);
1781      }
1782      __b = do_get(__b, __e, __iob, __err, __tm, __cmd, __opt);
1783      ++__fmtb;
1784    } else if (__ct.is(ctype_base::space, *__fmtb)) {
1785      for (++__fmtb; __fmtb != __fmte && __ct.is(ctype_base::space, *__fmtb); ++__fmtb)
1786        ;
1787      for (; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
1788        ;
1789    } else if (__ct.toupper(*__b) == __ct.toupper(*__fmtb)) {
1790      ++__b;
1791      ++__fmtb;
1792    } else
1793      __err = ios_base::failbit;
1794  }
1795  if (__b == __e)
1796    __err |= ios_base::eofbit;
1797  return __b;
1798}
1799
1800template <class _CharT, class _InputIterator>
1801typename time_get<_CharT, _InputIterator>::dateorder time_get<_CharT, _InputIterator>::do_date_order() const {
1802  return mdy;
1803}
1804
1805template <class _CharT, class _InputIterator>
1806_InputIterator time_get<_CharT, _InputIterator>::do_get_time(
1807    iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1808  const char_type __fmt[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
1809  return get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt) / sizeof(__fmt[0]));
1810}
1811
1812template <class _CharT, class _InputIterator>
1813_InputIterator time_get<_CharT, _InputIterator>::do_get_date(
1814    iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1815  const string_type& __fmt = this->__x();
1816  return get(__b, __e, __iob, __err, __tm, __fmt.data(), __fmt.data() + __fmt.size());
1817}
1818
1819template <class _CharT, class _InputIterator>
1820_InputIterator time_get<_CharT, _InputIterator>::do_get_weekday(
1821    iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1822  const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
1823  __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
1824  return __b;
1825}
1826
1827template <class _CharT, class _InputIterator>
1828_InputIterator time_get<_CharT, _InputIterator>::do_get_monthname(
1829    iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1830  const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
1831  __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
1832  return __b;
1833}
1834
1835template <class _CharT, class _InputIterator>
1836_InputIterator time_get<_CharT, _InputIterator>::do_get_year(
1837    iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm) const {
1838  const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
1839  __get_year(__tm->tm_year, __b, __e, __err, __ct);
1840  return __b;
1841}
1842
1843template <class _CharT, class _InputIterator>
1844_InputIterator time_get<_CharT, _InputIterator>::do_get(
1845    iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, tm* __tm, char __fmt, char) const {
1846  __err                        = ios_base::goodbit;
1847  const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
1848  switch (__fmt) {
1849  case 'a':
1850  case 'A':
1851    __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
1852    break;
1853  case 'b':
1854  case 'B':
1855  case 'h':
1856    __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
1857    break;
1858  case 'c': {
1859    const string_type& __fm = this->__c();
1860    __b                     = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
1861  } break;
1862  case 'd':
1863  case 'e':
1864    __get_day(__tm->tm_mday, __b, __e, __err, __ct);
1865    break;
1866  case 'D': {
1867    const char_type __fm[] = {'%', 'm', '/', '%', 'd', '/', '%', 'y'};
1868    __b                    = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm) / sizeof(__fm[0]));
1869  } break;
1870  case 'F': {
1871    const char_type __fm[] = {'%', 'Y', '-', '%', 'm', '-', '%', 'd'};
1872    __b                    = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm) / sizeof(__fm[0]));
1873  } break;
1874  case 'H':
1875    __get_hour(__tm->tm_hour, __b, __e, __err, __ct);
1876    break;
1877  case 'I':
1878    __get_12_hour(__tm->tm_hour, __b, __e, __err, __ct);
1879    break;
1880  case 'j':
1881    __get_day_year_num(__tm->tm_yday, __b, __e, __err, __ct);
1882    break;
1883  case 'm':
1884    __get_month(__tm->tm_mon, __b, __e, __err, __ct);
1885    break;
1886  case 'M':
1887    __get_minute(__tm->tm_min, __b, __e, __err, __ct);
1888    break;
1889  case 'n':
1890  case 't':
1891    __get_white_space(__b, __e, __err, __ct);
1892    break;
1893  case 'p':
1894    __get_am_pm(__tm->tm_hour, __b, __e, __err, __ct);
1895    break;
1896  case 'r': {
1897    const char_type __fm[] = {'%', 'I', ':', '%', 'M', ':', '%', 'S', ' ', '%', 'p'};
1898    __b                    = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm) / sizeof(__fm[0]));
1899  } break;
1900  case 'R': {
1901    const char_type __fm[] = {'%', 'H', ':', '%', 'M'};
1902    __b                    = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm) / sizeof(__fm[0]));
1903  } break;
1904  case 'S':
1905    __get_second(__tm->tm_sec, __b, __e, __err, __ct);
1906    break;
1907  case 'T': {
1908    const char_type __fm[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
1909    __b                    = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm) / sizeof(__fm[0]));
1910  } break;
1911  case 'w':
1912    __get_weekday(__tm->tm_wday, __b, __e, __err, __ct);
1913    break;
1914  case 'x':
1915    return do_get_date(__b, __e, __iob, __err, __tm);
1916  case 'X': {
1917    const string_type& __fm = this->__X();
1918    __b                     = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
1919  } break;
1920  case 'y':
1921    __get_year(__tm->tm_year, __b, __e, __err, __ct);
1922    break;
1923  case 'Y':
1924    __get_year4(__tm->tm_year, __b, __e, __err, __ct);
1925    break;
1926  case '%':
1927    __get_percent(__b, __e, __err, __ct);
1928    break;
1929  default:
1930    __err |= ios_base::failbit;
1931  }
1932  return __b;
1933}
1934
1935extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get<char>;
1936#  if _LIBCPP_HAS_WIDE_CHARACTERS
1937extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get<wchar_t>;
1938#  endif
1939
1940class _LIBCPP_EXPORTED_FROM_ABI __time_get {
1941protected:
1942  __locale::__locale_t __loc_;
1943
1944  __time_get(const char* __nm);
1945  __time_get(const string& __nm);
1946  ~__time_get();
1947};
1948
1949template <class _CharT>
1950class _LIBCPP_TEMPLATE_VIS __time_get_storage : public __time_get {
1951protected:
1952  typedef basic_string<_CharT> string_type;
1953
1954  string_type __weeks_[14];
1955  string_type __months_[24];
1956  string_type __am_pm_[2];
1957  string_type __c_;
1958  string_type __r_;
1959  string_type __x_;
1960  string_type __X_;
1961
1962  explicit __time_get_storage(const char* __nm);
1963  explicit __time_get_storage(const string& __nm);
1964
1965  _LIBCPP_HIDE_FROM_ABI ~__time_get_storage() {}
1966
1967  time_base::dateorder __do_date_order() const;
1968
1969private:
1970  void init(const ctype<_CharT>&);
1971  string_type __analyze(char __fmt, const ctype<_CharT>&);
1972};
1973
1974#  define _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(_CharT)                                                      \
1975    template <>                                                                                                        \
1976    _LIBCPP_EXPORTED_FROM_ABI time_base::dateorder __time_get_storage<_CharT>::__do_date_order() const;                \
1977    template <>                                                                                                        \
1978    _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::__time_get_storage(const char*);                             \
1979    template <>                                                                                                        \
1980    _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::__time_get_storage(const string&);                           \
1981    template <>                                                                                                        \
1982    _LIBCPP_EXPORTED_FROM_ABI void __time_get_storage<_CharT>::init(const ctype<_CharT>&);                             \
1983    template <>                                                                                                        \
1984    _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::string_type __time_get_storage<_CharT>::__analyze(           \
1985        char, const ctype<_CharT>&);                                                                                   \
1986    extern template _LIBCPP_EXPORTED_FROM_ABI time_base::dateorder __time_get_storage<_CharT>::__do_date_order()       \
1987        const;                                                                                                         \
1988    extern template _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::__time_get_storage(const char*);             \
1989    extern template _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::__time_get_storage(const string&);           \
1990    extern template _LIBCPP_EXPORTED_FROM_ABI void __time_get_storage<_CharT>::init(const ctype<_CharT>&);             \
1991    extern template _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::string_type                                  \
1992    __time_get_storage<_CharT>::__analyze(char, const ctype<_CharT>&);                                                 \
1993    /**/
1994
1995_LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(char)
1996#  if _LIBCPP_HAS_WIDE_CHARACTERS
1997_LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(wchar_t)
1998#  endif
1999#  undef _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION
2000
2001template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2002class _LIBCPP_TEMPLATE_VIS time_get_byname
2003    : public time_get<_CharT, _InputIterator>,
2004      private __time_get_storage<_CharT> {
2005public:
2006  typedef time_base::dateorder dateorder;
2007  typedef _InputIterator iter_type;
2008  typedef _CharT char_type;
2009  typedef basic_string<char_type> string_type;
2010
2011  _LIBCPP_HIDE_FROM_ABI explicit time_get_byname(const char* __nm, size_t __refs = 0)
2012      : time_get<_CharT, _InputIterator>(__refs), __time_get_storage<_CharT>(__nm) {}
2013  _LIBCPP_HIDE_FROM_ABI explicit time_get_byname(const string& __nm, size_t __refs = 0)
2014      : time_get<_CharT, _InputIterator>(__refs), __time_get_storage<_CharT>(__nm) {}
2015
2016protected:
2017  _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~time_get_byname() override {}
2018
2019  _LIBCPP_HIDE_FROM_ABI_VIRTUAL dateorder do_date_order() const override { return this->__do_date_order(); }
2020
2021private:
2022  _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type* __weeks() const override { return this->__weeks_; }
2023  _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type* __months() const override { return this->__months_; }
2024  _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type* __am_pm() const override { return this->__am_pm_; }
2025  _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type& __c() const override { return this->__c_; }
2026  _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type& __r() const override { return this->__r_; }
2027  _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type& __x() const override { return this->__x_; }
2028  _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type& __X() const override { return this->__X_; }
2029};
2030
2031extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<char>;
2032#  if _LIBCPP_HAS_WIDE_CHARACTERS
2033extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<wchar_t>;
2034#  endif
2035
2036class _LIBCPP_EXPORTED_FROM_ABI __time_put {
2037  __locale::__locale_t __loc_;
2038
2039protected:
2040  _LIBCPP_HIDE_FROM_ABI __time_put() : __loc_(_LIBCPP_GET_C_LOCALE) {}
2041  __time_put(const char* __nm);
2042  __time_put(const string& __nm);
2043  ~__time_put();
2044  void __do_put(char* __nb, char*& __ne, const tm* __tm, char __fmt, char __mod) const;
2045#  if _LIBCPP_HAS_WIDE_CHARACTERS
2046  void __do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm, char __fmt, char __mod) const;
2047#  endif
2048};
2049
2050template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2051class _LIBCPP_TEMPLATE_VIS time_put : public locale::facet, private __time_put {
2052public:
2053  typedef _CharT char_type;
2054  typedef _OutputIterator iter_type;
2055
2056  _LIBCPP_HIDE_FROM_ABI explicit time_put(size_t __refs = 0) : locale::facet(__refs) {}
2057
2058  iter_type
2059  put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm, const char_type* __pb, const char_type* __pe)
2060      const;
2061
2062  _LIBCPP_HIDE_FROM_ABI iter_type
2063  put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm, char __fmt, char __mod = 0) const {
2064    return do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2065  }
2066
2067  static locale::id id;
2068
2069protected:
2070  _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~time_put() override {}
2071  virtual iter_type do_put(iter_type __s, ios_base&, char_type, const tm* __tm, char __fmt, char __mod) const;
2072
2073  _LIBCPP_HIDE_FROM_ABI explicit time_put(const char* __nm, size_t __refs) : locale::facet(__refs), __time_put(__nm) {}
2074  _LIBCPP_HIDE_FROM_ABI explicit time_put(const string& __nm, size_t __refs)
2075      : locale::facet(__refs), __time_put(__nm) {}
2076};
2077
2078template <class _CharT, class _OutputIterator>
2079locale::id time_put<_CharT, _OutputIterator>::id;
2080
2081template <class _CharT, class _OutputIterator>
2082_OutputIterator time_put<_CharT, _OutputIterator>::put(
2083    iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm, const char_type* __pb, const char_type* __pe)
2084    const {
2085  const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
2086  for (; __pb != __pe; ++__pb) {
2087    if (__ct.narrow(*__pb, 0) == '%') {
2088      if (++__pb == __pe) {
2089        *__s++ = __pb[-1];
2090        break;
2091      }
2092      char __mod = 0;
2093      char __fmt = __ct.narrow(*__pb, 0);
2094      if (__fmt == 'E' || __fmt == 'O') {
2095        if (++__pb == __pe) {
2096          *__s++ = __pb[-2];
2097          *__s++ = __pb[-1];
2098          break;
2099        }
2100        __mod = __fmt;
2101        __fmt = __ct.narrow(*__pb, 0);
2102      }
2103      __s = do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2104    } else
2105      *__s++ = *__pb;
2106  }
2107  return __s;
2108}
2109
2110template <class _CharT, class _OutputIterator>
2111_OutputIterator time_put<_CharT, _OutputIterator>::do_put(
2112    iter_type __s, ios_base&, char_type, const tm* __tm, char __fmt, char __mod) const {
2113  char_type __nar[100];
2114  char_type* __nb = __nar;
2115  char_type* __ne = __nb + 100;
2116  __do_put(__nb, __ne, __tm, __fmt, __mod);
2117  return std::copy(__nb, __ne, __s);
2118}
2119
2120extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<char>;
2121#  if _LIBCPP_HAS_WIDE_CHARACTERS
2122extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<wchar_t>;
2123#  endif
2124
2125template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2126class _LIBCPP_TEMPLATE_VIS time_put_byname : public time_put<_CharT, _OutputIterator> {
2127public:
2128  _LIBCPP_HIDE_FROM_ABI explicit time_put_byname(const char* __nm, size_t __refs = 0)
2129      : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2130
2131  _LIBCPP_HIDE_FROM_ABI explicit time_put_byname(const string& __nm, size_t __refs = 0)
2132      : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2133
2134protected:
2135  _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~time_put_byname() override {}
2136};
2137
2138extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<char>;
2139#  if _LIBCPP_HAS_WIDE_CHARACTERS
2140extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<wchar_t>;
2141#  endif
2142
2143// money_base
2144
2145class _LIBCPP_EXPORTED_FROM_ABI money_base {
2146public:
2147  enum part { none, space, symbol, sign, value };
2148  struct pattern {
2149    char field[4];
2150  };
2151
2152  _LIBCPP_HIDE_FROM_ABI money_base() {}
2153};
2154
2155// moneypunct
2156
2157template <class _CharT, bool _International = false>
2158class _LIBCPP_TEMPLATE_VIS moneypunct : public locale::facet, public money_base {
2159public:
2160  typedef _CharT char_type;
2161  typedef basic_string<char_type> string_type;
2162
2163  _LIBCPP_HIDE_FROM_ABI explicit moneypunct(size_t __refs = 0) : locale::facet(__refs) {}
2164
2165  _LIBCPP_HIDE_FROM_ABI char_type decimal_point() const { return do_decimal_point(); }
2166  _LIBCPP_HIDE_FROM_ABI char_type thousands_sep() const { return do_thousands_sep(); }
2167  _LIBCPP_HIDE_FROM_ABI string grouping() const { return do_grouping(); }
2168  _LIBCPP_HIDE_FROM_ABI string_type curr_symbol() const { return do_curr_symbol(); }
2169  _LIBCPP_HIDE_FROM_ABI string_type positive_sign() const { return do_positive_sign(); }
2170  _LIBCPP_HIDE_FROM_ABI string_type negative_sign() const { return do_negative_sign(); }
2171  _LIBCPP_HIDE_FROM_ABI int frac_digits() const { return do_frac_digits(); }
2172  _LIBCPP_HIDE_FROM_ABI pattern pos_format() const { return do_pos_format(); }
2173  _LIBCPP_HIDE_FROM_ABI pattern neg_format() const { return do_neg_format(); }
2174
2175  static locale::id id;
2176  static const bool intl = _International;
2177
2178protected:
2179  _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~moneypunct() override {}
2180
2181  virtual char_type do_decimal_point() const { return numeric_limits<char_type>::max(); }
2182  virtual char_type do_thousands_sep() const { return numeric_limits<char_type>::max(); }
2183  virtual string do_grouping() const { return string(); }
2184  virtual string_type do_curr_symbol() const { return string_type(); }
2185  virtual string_type do_positive_sign() const { return string_type(); }
2186  virtual string_type do_negative_sign() const { return string_type(1, '-'); }
2187  virtual int do_frac_digits() const { return 0; }
2188  virtual pattern do_pos_format() const {
2189    pattern __p = {{symbol, sign, none, value}};
2190    return __p;
2191  }
2192  virtual pattern do_neg_format() const {
2193    pattern __p = {{symbol, sign, none, value}};
2194    return __p;
2195  }
2196};
2197
2198template <class _CharT, bool _International>
2199locale::id moneypunct<_CharT, _International>::id;
2200
2201template <class _CharT, bool _International>
2202const bool moneypunct<_CharT, _International>::intl;
2203
2204extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, false>;
2205extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, true>;
2206#  if _LIBCPP_HAS_WIDE_CHARACTERS
2207extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, false>;
2208extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, true>;
2209#  endif
2210
2211// moneypunct_byname
2212
2213template <class _CharT, bool _International = false>
2214class _LIBCPP_TEMPLATE_VIS moneypunct_byname : public moneypunct<_CharT, _International> {
2215public:
2216  typedef money_base::pattern pattern;
2217  typedef _CharT char_type;
2218  typedef basic_string<char_type> string_type;
2219
2220  _LIBCPP_HIDE_FROM_ABI explicit moneypunct_byname(const char* __nm, size_t __refs = 0)
2221      : moneypunct<_CharT, _International>(__refs) {
2222    init(__nm);
2223  }
2224
2225  _LIBCPP_HIDE_FROM_ABI explicit moneypunct_byname(const string& __nm, size_t __refs = 0)
2226      : moneypunct<_CharT, _International>(__refs) {
2227    init(__nm.c_str());
2228  }
2229
2230protected:
2231  _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~moneypunct_byname() override {}
2232
2233  char_type do_decimal_point() const override { return __decimal_point_; }
2234  char_type do_thousands_sep() const override { return __thousands_sep_; }
2235  string do_grouping() const override { return __grouping_; }
2236  string_type do_curr_symbol() const override { return __curr_symbol_; }
2237  string_type do_positive_sign() const override { return __positive_sign_; }
2238  string_type do_negative_sign() const override { return __negative_sign_; }
2239  int do_frac_digits() const override { return __frac_digits_; }
2240  pattern do_pos_format() const override { return __pos_format_; }
2241  pattern do_neg_format() const override { return __neg_format_; }
2242
2243private:
2244  char_type __decimal_point_;
2245  char_type __thousands_sep_;
2246  string __grouping_;
2247  string_type __curr_symbol_;
2248  string_type __positive_sign_;
2249  string_type __negative_sign_;
2250  int __frac_digits_;
2251  pattern __pos_format_;
2252  pattern __neg_format_;
2253
2254  void init(const char*);
2255};
2256
2257template <>
2258_LIBCPP_EXPORTED_FROM_ABI void moneypunct_byname<char, false>::init(const char*);
2259template <>
2260_LIBCPP_EXPORTED_FROM_ABI void moneypunct_byname<char, true>::init(const char*);
2261extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, false>;
2262extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, true>;
2263
2264#  if _LIBCPP_HAS_WIDE_CHARACTERS
2265template <>
2266_LIBCPP_EXPORTED_FROM_ABI void moneypunct_byname<wchar_t, false>::init(const char*);
2267template <>
2268_LIBCPP_EXPORTED_FROM_ABI void moneypunct_byname<wchar_t, true>::init(const char*);
2269extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, false>;
2270extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, true>;
2271#  endif
2272
2273// money_get
2274
2275template <class _CharT>
2276class __money_get {
2277protected:
2278  typedef _CharT char_type;
2279  typedef basic_string<char_type> string_type;
2280
2281  _LIBCPP_HIDE_FROM_ABI __money_get() {}
2282
2283  static void __gather_info(
2284      bool __intl,
2285      const locale& __loc,
2286      money_base::pattern& __pat,
2287      char_type& __dp,
2288      char_type& __ts,
2289      string& __grp,
2290      string_type& __sym,
2291      string_type& __psn,
2292      string_type& __nsn,
2293      int& __fd);
2294};
2295
2296template <class _CharT>
2297void __money_get<_CharT>::__gather_info(
2298    bool __intl,
2299    const locale& __loc,
2300    money_base::pattern& __pat,
2301    char_type& __dp,
2302    char_type& __ts,
2303    string& __grp,
2304    string_type& __sym,
2305    string_type& __psn,
2306    string_type& __nsn,
2307    int& __fd) {
2308  if (__intl) {
2309    const moneypunct<char_type, true>& __mp = std::use_facet<moneypunct<char_type, true> >(__loc);
2310    __pat                                   = __mp.neg_format();
2311    __nsn                                   = __mp.negative_sign();
2312    __psn                                   = __mp.positive_sign();
2313    __dp                                    = __mp.decimal_point();
2314    __ts                                    = __mp.thousands_sep();
2315    __grp                                   = __mp.grouping();
2316    __sym                                   = __mp.curr_symbol();
2317    __fd                                    = __mp.frac_digits();
2318  } else {
2319    const moneypunct<char_type, false>& __mp = std::use_facet<moneypunct<char_type, false> >(__loc);
2320    __pat                                    = __mp.neg_format();
2321    __nsn                                    = __mp.negative_sign();
2322    __psn                                    = __mp.positive_sign();
2323    __dp                                     = __mp.decimal_point();
2324    __ts                                     = __mp.thousands_sep();
2325    __grp                                    = __mp.grouping();
2326    __sym                                    = __mp.curr_symbol();
2327    __fd                                     = __mp.frac_digits();
2328  }
2329}
2330
2331extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<char>;
2332#  if _LIBCPP_HAS_WIDE_CHARACTERS
2333extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<wchar_t>;
2334#  endif
2335
2336template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2337class _LIBCPP_TEMPLATE_VIS money_get : public locale::facet, private __money_get<_CharT> {
2338public:
2339  typedef _CharT char_type;
2340  typedef _InputIterator iter_type;
2341  typedef basic_string<char_type> string_type;
2342
2343  _LIBCPP_HIDE_FROM_ABI explicit money_get(size_t __refs = 0) : locale::facet(__refs) {}
2344
2345  _LIBCPP_HIDE_FROM_ABI iter_type
2346  get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob, ios_base::iostate& __err, long double& __v) const {
2347    return do_get(__b, __e, __intl, __iob, __err, __v);
2348  }
2349
2350  _LIBCPP_HIDE_FROM_ABI iter_type
2351  get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob, ios_base::iostate& __err, string_type& __v) const {
2352    return do_get(__b, __e, __intl, __iob, __err, __v);
2353  }
2354
2355  static locale::id id;
2356
2357protected:
2358  _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~money_get() override {}
2359
2360  virtual iter_type
2361  do_get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob, ios_base::iostate& __err, long double& __v) const;
2362  virtual iter_type
2363  do_get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob, ios_base::iostate& __err, string_type& __v) const;
2364
2365private:
2366  static bool __do_get(
2367      iter_type& __b,
2368      iter_type __e,
2369      bool __intl,
2370      const locale& __loc,
2371      ios_base::fmtflags __flags,
2372      ios_base::iostate& __err,
2373      bool& __neg,
2374      const ctype<char_type>& __ct,
2375      unique_ptr<char_type, void (*)(void*)>& __wb,
2376      char_type*& __wn,
2377      char_type* __we);
2378};
2379
2380template <class _CharT, class _InputIterator>
2381locale::id money_get<_CharT, _InputIterator>::id;
2382
2383_LIBCPP_EXPORTED_FROM_ABI void __do_nothing(void*);
2384
2385template <class _Tp>
2386_LIBCPP_HIDE_FROM_ABI void __double_or_nothing(unique_ptr<_Tp, void (*)(void*)>& __b, _Tp*& __n, _Tp*& __e) {
2387  bool __owns      = __b.get_deleter() != __do_nothing;
2388  size_t __cur_cap = static_cast<size_t>(__e - __b.get()) * sizeof(_Tp);
2389  size_t __new_cap = __cur_cap < numeric_limits<size_t>::max() / 2 ? 2 * __cur_cap : numeric_limits<size_t>::max();
2390  if (__new_cap == 0)
2391    __new_cap = sizeof(_Tp);
2392  size_t __n_off = static_cast<size_t>(__n - __b.get());
2393  _Tp* __t       = (_Tp*)std::realloc(__owns ? __b.get() : 0, __new_cap);
2394  if (__t == 0)
2395    __throw_bad_alloc();
2396  if (__owns)
2397    __b.release();
2398  __b = unique_ptr<_Tp, void (*)(void*)>(__t, free);
2399  __new_cap /= sizeof(_Tp);
2400  __n = __b.get() + __n_off;
2401  __e = __b.get() + __new_cap;
2402}
2403
2404// true == success
2405template <class _CharT, class _InputIterator>
2406bool money_get<_CharT, _InputIterator>::__do_get(
2407    iter_type& __b,
2408    iter_type __e,
2409    bool __intl,
2410    const locale& __loc,
2411    ios_base::fmtflags __flags,
2412    ios_base::iostate& __err,
2413    bool& __neg,
2414    const ctype<char_type>& __ct,
2415    unique_ptr<char_type, void (*)(void*)>& __wb,
2416    char_type*& __wn,
2417    char_type* __we) {
2418  if (__b == __e) {
2419    __err |= ios_base::failbit;
2420    return false;
2421  }
2422  const unsigned __bz = 100;
2423  unsigned __gbuf[__bz];
2424  unique_ptr<unsigned, void (*)(void*)> __gb(__gbuf, __do_nothing);
2425  unsigned* __gn = __gb.get();
2426  unsigned* __ge = __gn + __bz;
2427  money_base::pattern __pat;
2428  char_type __dp;
2429  char_type __ts;
2430  string __grp;
2431  string_type __sym;
2432  string_type __psn;
2433  string_type __nsn;
2434  // Capture the spaces read into money_base::{space,none} so they
2435  // can be compared to initial spaces in __sym.
2436  string_type __spaces;
2437  int __fd;
2438  __money_get<_CharT>::__gather_info(__intl, __loc, __pat, __dp, __ts, __grp, __sym, __psn, __nsn, __fd);
2439  const string_type* __trailing_sign = 0;
2440  __wn                               = __wb.get();
2441  for (unsigned __p = 0; __p < 4 && __b != __e; ++__p) {
2442    switch (__pat.field[__p]) {
2443    case money_base::space:
2444      if (__p != 3) {
2445        if (__ct.is(ctype_base::space, *__b))
2446          __spaces.push_back(*__b++);
2447        else {
2448          __err |= ios_base::failbit;
2449          return false;
2450        }
2451      }
2452      _LIBCPP_FALLTHROUGH();
2453    case money_base::none:
2454      if (__p != 3) {
2455        while (__b != __e && __ct.is(ctype_base::space, *__b))
2456          __spaces.push_back(*__b++);
2457      }
2458      break;
2459    case money_base::sign:
2460      if (__psn.size() > 0 && *__b == __psn[0]) {
2461        ++__b;
2462        __neg = false;
2463        if (__psn.size() > 1)
2464          __trailing_sign = &__psn;
2465        break;
2466      }
2467      if (__nsn.size() > 0 && *__b == __nsn[0]) {
2468        ++__b;
2469        __neg = true;
2470        if (__nsn.size() > 1)
2471          __trailing_sign = &__nsn;
2472        break;
2473      }
2474      if (__psn.size() > 0 && __nsn.size() > 0) { // sign is required
2475        __err |= ios_base::failbit;
2476        return false;
2477      }
2478      if (__psn.size() == 0 && __nsn.size() == 0)
2479        // locale has no way of specifying a sign. Use the initial value of __neg as a default
2480        break;
2481      __neg = (__nsn.size() == 0);
2482      break;
2483    case money_base::symbol: {
2484      bool __more_needed =
2485          __trailing_sign || (__p < 2) || (__p == 2 && __pat.field[3] != static_cast<char>(money_base::none));
2486      bool __sb = (__flags & ios_base::showbase) != 0;
2487      if (__sb || __more_needed) {
2488        typename string_type::const_iterator __sym_space_end = __sym.begin();
2489        if (__p > 0 && (__pat.field[__p - 1] == money_base::none || __pat.field[__p - 1] == money_base::space)) {
2490          // Match spaces we've already read against spaces at
2491          // the beginning of __sym.
2492          while (__sym_space_end != __sym.end() && __ct.is(ctype_base::space, *__sym_space_end))
2493            ++__sym_space_end;
2494          const size_t __num_spaces = __sym_space_end - __sym.begin();
2495          if (__num_spaces > __spaces.size() ||
2496              !std::equal(__spaces.end() - __num_spaces, __spaces.end(), __sym.begin())) {
2497            // No match. Put __sym_space_end back at the
2498            // beginning of __sym, which will prevent a
2499            // match in the next loop.
2500            __sym_space_end = __sym.begin();
2501          }
2502        }
2503        typename string_type::const_iterator __sym_curr_char = __sym_space_end;
2504        while (__sym_curr_char != __sym.end() && __b != __e && *__b == *__sym_curr_char) {
2505          ++__b;
2506          ++__sym_curr_char;
2507        }
2508        if (__sb && __sym_curr_char != __sym.end()) {
2509          __err |= ios_base::failbit;
2510          return false;
2511        }
2512      }
2513    } break;
2514    case money_base::value: {
2515      unsigned __ng = 0;
2516      for (; __b != __e; ++__b) {
2517        char_type __c = *__b;
2518        if (__ct.is(ctype_base::digit, __c)) {
2519          if (__wn == __we)
2520            std::__double_or_nothing(__wb, __wn, __we);
2521          *__wn++ = __c;
2522          ++__ng;
2523        } else if (__grp.size() > 0 && __ng > 0 && __c == __ts) {
2524          if (__gn == __ge)
2525            std::__double_or_nothing(__gb, __gn, __ge);
2526          *__gn++ = __ng;
2527          __ng    = 0;
2528        } else
2529          break;
2530      }
2531      if (__gb.get() != __gn && __ng > 0) {
2532        if (__gn == __ge)
2533          std::__double_or_nothing(__gb, __gn, __ge);
2534        *__gn++ = __ng;
2535      }
2536      if (__fd > 0) {
2537        if (__b == __e || *__b != __dp) {
2538          __err |= ios_base::failbit;
2539          return false;
2540        }
2541        for (++__b; __fd > 0; --__fd, ++__b) {
2542          if (__b == __e || !__ct.is(ctype_base::digit, *__b)) {
2543            __err |= ios_base::failbit;
2544            return false;
2545          }
2546          if (__wn == __we)
2547            std::__double_or_nothing(__wb, __wn, __we);
2548          *__wn++ = *__b;
2549        }
2550      }
2551      if (__wn == __wb.get()) {
2552        __err |= ios_base::failbit;
2553        return false;
2554      }
2555    } break;
2556    }
2557  }
2558  if (__trailing_sign) {
2559    for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, ++__b) {
2560      if (__b == __e || *__b != (*__trailing_sign)[__i]) {
2561        __err |= ios_base::failbit;
2562        return false;
2563      }
2564    }
2565  }
2566  if (__gb.get() != __gn) {
2567    ios_base::iostate __et = ios_base::goodbit;
2568    __check_grouping(__grp, __gb.get(), __gn, __et);
2569    if (__et) {
2570      __err |= ios_base::failbit;
2571      return false;
2572    }
2573  }
2574  return true;
2575}
2576
2577template <class _CharT, class _InputIterator>
2578_InputIterator money_get<_CharT, _InputIterator>::do_get(
2579    iter_type __b, iter_type __e, bool __intl, ios_base& __iob, ios_base::iostate& __err, long double& __v) const {
2580  const int __bz = 100;
2581  char_type __wbuf[__bz];
2582  unique_ptr<char_type, void (*)(void*)> __wb(__wbuf, __do_nothing);
2583  char_type* __wn;
2584  char_type* __we              = __wbuf + __bz;
2585  locale __loc                 = __iob.getloc();
2586  const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__loc);
2587  bool __neg                   = false;
2588  if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct, __wb, __wn, __we)) {
2589    const char __src[] = "0123456789";
2590    char_type __atoms[sizeof(__src) - 1];
2591    __ct.widen(__src, __src + (sizeof(__src) - 1), __atoms);
2592    char __nbuf[__bz];
2593    char* __nc = __nbuf;
2594    unique_ptr<char, void (*)(void*)> __h(nullptr, free);
2595    if (__wn - __wb.get() > __bz - 2) {
2596      __h.reset((char*)malloc(static_cast<size_t>(__wn - __wb.get() + 2)));
2597      if (__h.get() == nullptr)
2598        __throw_bad_alloc();
2599      __nc = __h.get();
2600    }
2601    if (__neg)
2602      *__nc++ = '-';
2603    for (const char_type* __w = __wb.get(); __w < __wn; ++__w, ++__nc)
2604      *__nc = __src[std::find(__atoms, std::end(__atoms), *__w) - __atoms];
2605    *__nc = char();
2606    if (sscanf(__nbuf, "%Lf", &__v) != 1)
2607      __throw_runtime_error("money_get error");
2608  }
2609  if (__b == __e)
2610    __err |= ios_base::eofbit;
2611  return __b;
2612}
2613
2614template <class _CharT, class _InputIterator>
2615_InputIterator money_get<_CharT, _InputIterator>::do_get(
2616    iter_type __b, iter_type __e, bool __intl, ios_base& __iob, ios_base::iostate& __err, string_type& __v) const {
2617  const int __bz = 100;
2618  char_type __wbuf[__bz];
2619  unique_ptr<char_type, void (*)(void*)> __wb(__wbuf, __do_nothing);
2620  char_type* __wn;
2621  char_type* __we              = __wbuf + __bz;
2622  locale __loc                 = __iob.getloc();
2623  const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__loc);
2624  bool __neg                   = false;
2625  if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct, __wb, __wn, __we)) {
2626    __v.clear();
2627    if (__neg)
2628      __v.push_back(__ct.widen('-'));
2629    char_type __z = __ct.widen('0');
2630    char_type* __w;
2631    for (__w = __wb.get(); __w < __wn - 1; ++__w)
2632      if (*__w != __z)
2633        break;
2634    __v.append(__w, __wn);
2635  }
2636  if (__b == __e)
2637    __err |= ios_base::eofbit;
2638  return __b;
2639}
2640
2641extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<char>;
2642#  if _LIBCPP_HAS_WIDE_CHARACTERS
2643extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<wchar_t>;
2644#  endif
2645
2646// money_put
2647
2648template <class _CharT>
2649class __money_put {
2650protected:
2651  typedef _CharT char_type;
2652  typedef basic_string<char_type> string_type;
2653
2654  _LIBCPP_HIDE_FROM_ABI __money_put() {}
2655
2656  static void __gather_info(
2657      bool __intl,
2658      bool __neg,
2659      const locale& __loc,
2660      money_base::pattern& __pat,
2661      char_type& __dp,
2662      char_type& __ts,
2663      string& __grp,
2664      string_type& __sym,
2665      string_type& __sn,
2666      int& __fd);
2667  static void __format(
2668      char_type* __mb,
2669      char_type*& __mi,
2670      char_type*& __me,
2671      ios_base::fmtflags __flags,
2672      const char_type* __db,
2673      const char_type* __de,
2674      const ctype<char_type>& __ct,
2675      bool __neg,
2676      const money_base::pattern& __pat,
2677      char_type __dp,
2678      char_type __ts,
2679      const string& __grp,
2680      const string_type& __sym,
2681      const string_type& __sn,
2682      int __fd);
2683};
2684
2685template <class _CharT>
2686void __money_put<_CharT>::__gather_info(
2687    bool __intl,
2688    bool __neg,
2689    const locale& __loc,
2690    money_base::pattern& __pat,
2691    char_type& __dp,
2692    char_type& __ts,
2693    string& __grp,
2694    string_type& __sym,
2695    string_type& __sn,
2696    int& __fd) {
2697  if (__intl) {
2698    const moneypunct<char_type, true>& __mp = std::use_facet<moneypunct<char_type, true> >(__loc);
2699    if (__neg) {
2700      __pat = __mp.neg_format();
2701      __sn  = __mp.negative_sign();
2702    } else {
2703      __pat = __mp.pos_format();
2704      __sn  = __mp.positive_sign();
2705    }
2706    __dp  = __mp.decimal_point();
2707    __ts  = __mp.thousands_sep();
2708    __grp = __mp.grouping();
2709    __sym = __mp.curr_symbol();
2710    __fd  = __mp.frac_digits();
2711  } else {
2712    const moneypunct<char_type, false>& __mp = std::use_facet<moneypunct<char_type, false> >(__loc);
2713    if (__neg) {
2714      __pat = __mp.neg_format();
2715      __sn  = __mp.negative_sign();
2716    } else {
2717      __pat = __mp.pos_format();
2718      __sn  = __mp.positive_sign();
2719    }
2720    __dp  = __mp.decimal_point();
2721    __ts  = __mp.thousands_sep();
2722    __grp = __mp.grouping();
2723    __sym = __mp.curr_symbol();
2724    __fd  = __mp.frac_digits();
2725  }
2726}
2727
2728template <class _CharT>
2729void __money_put<_CharT>::__format(
2730    char_type* __mb,
2731    char_type*& __mi,
2732    char_type*& __me,
2733    ios_base::fmtflags __flags,
2734    const char_type* __db,
2735    const char_type* __de,
2736    const ctype<char_type>& __ct,
2737    bool __neg,
2738    const money_base::pattern& __pat,
2739    char_type __dp,
2740    char_type __ts,
2741    const string& __grp,
2742    const string_type& __sym,
2743    const string_type& __sn,
2744    int __fd) {
2745  __me = __mb;
2746  for (char __p : __pat.field) {
2747    switch (__p) {
2748    case money_base::none:
2749      __mi = __me;
2750      break;
2751    case money_base::space:
2752      __mi    = __me;
2753      *__me++ = __ct.widen(' ');
2754      break;
2755    case money_base::sign:
2756      if (!__sn.empty())
2757        *__me++ = __sn[0];
2758      break;
2759    case money_base::symbol:
2760      if (!__sym.empty() && (__flags & ios_base::showbase))
2761        __me = std::copy(__sym.begin(), __sym.end(), __me);
2762      break;
2763    case money_base::value: {
2764      // remember start of value so we can reverse it
2765      char_type* __t = __me;
2766      // find beginning of digits
2767      if (__neg)
2768        ++__db;
2769      // find end of digits
2770      const char_type* __d;
2771      for (__d = __db; __d < __de; ++__d)
2772        if (!__ct.is(ctype_base::digit, *__d))
2773          break;
2774      // print fractional part
2775      if (__fd > 0) {
2776        int __f;
2777        for (__f = __fd; __d > __db && __f > 0; --__f)
2778          *__me++ = *--__d;
2779        char_type __z = __f > 0 ? __ct.widen('0') : char_type();
2780        for (; __f > 0; --__f)
2781          *__me++ = __z;
2782        *__me++ = __dp;
2783      }
2784      // print units part
2785      if (__d == __db) {
2786        *__me++ = __ct.widen('0');
2787      } else {
2788        unsigned __ng = 0;
2789        unsigned __ig = 0;
2790        unsigned __gl = __grp.empty() ? numeric_limits<unsigned>::max() : static_cast<unsigned>(__grp[__ig]);
2791        while (__d != __db) {
2792          if (__ng == __gl) {
2793            *__me++ = __ts;
2794            __ng    = 0;
2795            if (++__ig < __grp.size())
2796              __gl = __grp[__ig] == numeric_limits<char>::max()
2797                       ? numeric_limits<unsigned>::max()
2798                       : static_cast<unsigned>(__grp[__ig]);
2799          }
2800          *__me++ = *--__d;
2801          ++__ng;
2802        }
2803      }
2804      // reverse it
2805      std::reverse(__t, __me);
2806    } break;
2807    }
2808  }
2809  // print rest of sign, if any
2810  if (__sn.size() > 1)
2811    __me = std::copy(__sn.begin() + 1, __sn.end(), __me);
2812  // set alignment
2813  if ((__flags & ios_base::adjustfield) == ios_base::left)
2814    __mi = __me;
2815  else if ((__flags & ios_base::adjustfield) != ios_base::internal)
2816    __mi = __mb;
2817}
2818
2819extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<char>;
2820#  if _LIBCPP_HAS_WIDE_CHARACTERS
2821extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<wchar_t>;
2822#  endif
2823
2824template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2825class _LIBCPP_TEMPLATE_VIS money_put : public locale::facet, private __money_put<_CharT> {
2826public:
2827  typedef _CharT char_type;
2828  typedef _OutputIterator iter_type;
2829  typedef basic_string<char_type> string_type;
2830
2831  _LIBCPP_HIDE_FROM_ABI explicit money_put(size_t __refs = 0) : locale::facet(__refs) {}
2832
2833  _LIBCPP_HIDE_FROM_ABI iter_type
2834  put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl, long double __units) const {
2835    return do_put(__s, __intl, __iob, __fl, __units);
2836  }
2837
2838  _LIBCPP_HIDE_FROM_ABI iter_type
2839  put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl, const string_type& __digits) const {
2840    return do_put(__s, __intl, __iob, __fl, __digits);
2841  }
2842
2843  static locale::id id;
2844
2845protected:
2846  _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~money_put() override {}
2847
2848  virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl, long double __units) const;
2849  virtual iter_type
2850  do_put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl, const string_type& __digits) const;
2851};
2852
2853template <class _CharT, class _OutputIterator>
2854locale::id money_put<_CharT, _OutputIterator>::id;
2855
2856template <class _CharT, class _OutputIterator>
2857_OutputIterator money_put<_CharT, _OutputIterator>::do_put(
2858    iter_type __s, bool __intl, ios_base& __iob, char_type __fl, long double __units) const {
2859  // convert to char
2860  const size_t __bs = 100;
2861  char __buf[__bs];
2862  char* __bb = __buf;
2863  char_type __digits[__bs];
2864  char_type* __db = __digits;
2865  int __n         = snprintf(__bb, __bs, "%.0Lf", __units);
2866  unique_ptr<char, void (*)(void*)> __hn(nullptr, free);
2867  unique_ptr<char_type, void (*)(void*)> __hd(0, free);
2868  // secure memory for digit storage
2869  if (static_cast<size_t>(__n) > __bs - 1) {
2870    __n = __locale::__asprintf(&__bb, _LIBCPP_GET_C_LOCALE, "%.0Lf", __units);
2871    if (__n == -1)
2872      __throw_bad_alloc();
2873    __hn.reset(__bb);
2874    __hd.reset((char_type*)malloc(static_cast<size_t>(__n) * sizeof(char_type)));
2875    if (__hd == nullptr)
2876      __throw_bad_alloc();
2877    __db = __hd.get();
2878  }
2879  // gather info
2880  locale __loc                 = __iob.getloc();
2881  const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__loc);
2882  __ct.widen(__bb, __bb + __n, __db);
2883  bool __neg = __n > 0 && __bb[0] == '-';
2884  money_base::pattern __pat;
2885  char_type __dp;
2886  char_type __ts;
2887  string __grp;
2888  string_type __sym;
2889  string_type __sn;
2890  int __fd;
2891  this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
2892  // secure memory for formatting
2893  char_type __mbuf[__bs];
2894  char_type* __mb = __mbuf;
2895  unique_ptr<char_type, void (*)(void*)> __hw(0, free);
2896  size_t __exn = __n > __fd ? (static_cast<size_t>(__n) - static_cast<size_t>(__fd)) * 2 + __sn.size() + __sym.size() +
2897                                  static_cast<size_t>(__fd) + 1
2898                            : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
2899  if (__exn > __bs) {
2900    __hw.reset((char_type*)malloc(__exn * sizeof(char_type)));
2901    __mb = __hw.get();
2902    if (__mb == 0)
2903      __throw_bad_alloc();
2904  }
2905  // format
2906  char_type* __mi;
2907  char_type* __me;
2908  this->__format(
2909      __mb, __mi, __me, __iob.flags(), __db, __db + __n, __ct, __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
2910  return std::__pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
2911}
2912
2913template <class _CharT, class _OutputIterator>
2914_OutputIterator money_put<_CharT, _OutputIterator>::do_put(
2915    iter_type __s, bool __intl, ios_base& __iob, char_type __fl, const string_type& __digits) const {
2916  // gather info
2917  locale __loc                 = __iob.getloc();
2918  const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__loc);
2919  bool __neg                   = __digits.size() > 0 && __digits[0] == __ct.widen('-');
2920  money_base::pattern __pat;
2921  char_type __dp;
2922  char_type __ts;
2923  string __grp;
2924  string_type __sym;
2925  string_type __sn;
2926  int __fd;
2927  this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
2928  // secure memory for formatting
2929  char_type __mbuf[100];
2930  char_type* __mb = __mbuf;
2931  unique_ptr<char_type, void (*)(void*)> __h(0, free);
2932  size_t __exn =
2933      static_cast<int>(__digits.size()) > __fd
2934          ? (__digits.size() - static_cast<size_t>(__fd)) * 2 + __sn.size() + __sym.size() + static_cast<size_t>(__fd) +
2935                1
2936          : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
2937  if (__exn > 100) {
2938    __h.reset((char_type*)malloc(__exn * sizeof(char_type)));
2939    __mb = __h.get();
2940    if (__mb == 0)
2941      __throw_bad_alloc();
2942  }
2943  // format
2944  char_type* __mi;
2945  char_type* __me;
2946  this->__format(
2947      __mb,
2948      __mi,
2949      __me,
2950      __iob.flags(),
2951      __digits.data(),
2952      __digits.data() + __digits.size(),
2953      __ct,
2954      __neg,
2955      __pat,
2956      __dp,
2957      __ts,
2958      __grp,
2959      __sym,
2960      __sn,
2961      __fd);
2962  return std::__pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
2963}
2964
2965extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<char>;
2966#  if _LIBCPP_HAS_WIDE_CHARACTERS
2967extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<wchar_t>;
2968#  endif
2969
2970// messages
2971
2972class _LIBCPP_EXPORTED_FROM_ABI messages_base {
2973public:
2974  typedef intptr_t catalog;
2975
2976  _LIBCPP_HIDE_FROM_ABI messages_base() {}
2977};
2978
2979template <class _CharT>
2980class _LIBCPP_TEMPLATE_VIS messages : public locale::facet, public messages_base {
2981public:
2982  typedef _CharT char_type;
2983  typedef basic_string<_CharT> string_type;
2984
2985  _LIBCPP_HIDE_FROM_ABI explicit messages(size_t __refs = 0) : locale::facet(__refs) {}
2986
2987  _LIBCPP_HIDE_FROM_ABI catalog open(const basic_string<char>& __nm, const locale& __loc) const {
2988    return do_open(__nm, __loc);
2989  }
2990
2991  _LIBCPP_HIDE_FROM_ABI string_type get(catalog __c, int __set, int __msgid, const string_type& __dflt) const {
2992    return do_get(__c, __set, __msgid, __dflt);
2993  }
2994
2995  _LIBCPP_HIDE_FROM_ABI void close(catalog __c) const { do_close(__c); }
2996
2997  static locale::id id;
2998
2999protected:
3000  _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~messages() override {}
3001
3002  virtual catalog do_open(const basic_string<char>&, const locale&) const;
3003  virtual string_type do_get(catalog, int __set, int __msgid, const string_type& __dflt) const;
3004  virtual void do_close(catalog) const;
3005};
3006
3007template <class _CharT>
3008locale::id messages<_CharT>::id;
3009
3010template <class _CharT>
3011typename messages<_CharT>::catalog messages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const {
3012#  if _LIBCPP_HAS_CATOPEN
3013  return (catalog)catopen(__nm.c_str(), NL_CAT_LOCALE);
3014#  else  // !_LIBCPP_HAS_CATOPEN
3015  (void)__nm;
3016  return -1;
3017#  endif // _LIBCPP_HAS_CATOPEN
3018}
3019
3020template <class _CharT>
3021typename messages<_CharT>::string_type
3022messages<_CharT>::do_get(catalog __c, int __set, int __msgid, const string_type& __dflt) const {
3023#  if _LIBCPP_HAS_CATOPEN
3024  string __ndflt;
3025  __narrow_to_utf8<sizeof(char_type) * __CHAR_BIT__>()(
3026      std::back_inserter(__ndflt), __dflt.c_str(), __dflt.c_str() + __dflt.size());
3027  nl_catd __cat = (nl_catd)__c;
3028  static_assert(sizeof(catalog) >= sizeof(nl_catd), "Unexpected nl_catd type");
3029  char* __n = catgets(__cat, __set, __msgid, __ndflt.c_str());
3030  string_type __w;
3031  __widen_from_utf8<sizeof(char_type) * __CHAR_BIT__>()(std::back_inserter(__w), __n, __n + std::strlen(__n));
3032  return __w;
3033#  else  // !_LIBCPP_HAS_CATOPEN
3034  (void)__c;
3035  (void)__set;
3036  (void)__msgid;
3037  return __dflt;
3038#  endif // _LIBCPP_HAS_CATOPEN
3039}
3040
3041template <class _CharT>
3042void messages<_CharT>::do_close(catalog __c) const {
3043#  if _LIBCPP_HAS_CATOPEN
3044  catclose((nl_catd)__c);
3045#  else  // !_LIBCPP_HAS_CATOPEN
3046  (void)__c;
3047#  endif // _LIBCPP_HAS_CATOPEN
3048}
3049
3050extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<char>;
3051#  if _LIBCPP_HAS_WIDE_CHARACTERS
3052extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<wchar_t>;
3053#  endif
3054
3055template <class _CharT>
3056class _LIBCPP_TEMPLATE_VIS messages_byname : public messages<_CharT> {
3057public:
3058  typedef messages_base::catalog catalog;
3059  typedef basic_string<_CharT> string_type;
3060
3061  _LIBCPP_HIDE_FROM_ABI explicit messages_byname(const char*, size_t __refs = 0) : messages<_CharT>(__refs) {}
3062
3063  _LIBCPP_HIDE_FROM_ABI explicit messages_byname(const string&, size_t __refs = 0) : messages<_CharT>(__refs) {}
3064
3065protected:
3066  _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~messages_byname() override {}
3067};
3068
3069extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<char>;
3070#  if _LIBCPP_HAS_WIDE_CHARACTERS
3071extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<wchar_t>;
3072#  endif
3073
3074#  if _LIBCPP_STD_VER < 26 || defined(_LIBCPP_ENABLE_CXX26_REMOVED_WSTRING_CONVERT)
3075
3076template <class _Codecvt,
3077          class _Elem      = wchar_t,
3078          class _WideAlloc = allocator<_Elem>,
3079          class _ByteAlloc = allocator<char> >
3080class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 wstring_convert {
3081public:
3082  typedef basic_string<char, char_traits<char>, _ByteAlloc> byte_string;
3083  typedef basic_string<_Elem, char_traits<_Elem>, _WideAlloc> wide_string;
3084  typedef typename _Codecvt::state_type state_type;
3085  typedef typename wide_string::traits_type::int_type int_type;
3086
3087private:
3088  byte_string __byte_err_string_;
3089  wide_string __wide_err_string_;
3090  _Codecvt* __cvtptr_;
3091  state_type __cvtstate_;
3092  size_t __cvtcount_;
3093
3094public:
3095#    ifndef _LIBCPP_CXX03_LANG
3096  _LIBCPP_HIDE_FROM_ABI wstring_convert() : wstring_convert(new _Codecvt) {}
3097  _LIBCPP_HIDE_FROM_ABI explicit wstring_convert(_Codecvt* __pcvt);
3098#    else
3099  _LIBCPP_HIDE_FROM_ABI _LIBCPP_EXPLICIT_SINCE_CXX14 wstring_convert(_Codecvt* __pcvt = new _Codecvt);
3100#    endif
3101
3102  _LIBCPP_HIDE_FROM_ABI wstring_convert(_Codecvt* __pcvt, state_type __state);
3103  _LIBCPP_EXPLICIT_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI
3104  wstring_convert(const byte_string& __byte_err, const wide_string& __wide_err = wide_string());
3105#    ifndef _LIBCPP_CXX03_LANG
3106  _LIBCPP_HIDE_FROM_ABI wstring_convert(wstring_convert&& __wc);
3107#    endif
3108  _LIBCPP_HIDE_FROM_ABI ~wstring_convert();
3109
3110  wstring_convert(const wstring_convert& __wc)            = delete;
3111  wstring_convert& operator=(const wstring_convert& __wc) = delete;
3112
3113  _LIBCPP_HIDE_FROM_ABI wide_string from_bytes(char __byte) { return from_bytes(&__byte, &__byte + 1); }
3114  _LIBCPP_HIDE_FROM_ABI wide_string from_bytes(const char* __ptr) {
3115    return from_bytes(__ptr, __ptr + char_traits<char>::length(__ptr));
3116  }
3117  _LIBCPP_HIDE_FROM_ABI wide_string from_bytes(const byte_string& __str) {
3118    return from_bytes(__str.data(), __str.data() + __str.size());
3119  }
3120  _LIBCPP_HIDE_FROM_ABI wide_string from_bytes(const char* __first, const char* __last);
3121
3122  _LIBCPP_HIDE_FROM_ABI byte_string to_bytes(_Elem __wchar) { return to_bytes(&__wchar, &__wchar + 1); }
3123  _LIBCPP_HIDE_FROM_ABI byte_string to_bytes(const _Elem* __wptr) {
3124    return to_bytes(__wptr, __wptr + char_traits<_Elem>::length(__wptr));
3125  }
3126  _LIBCPP_HIDE_FROM_ABI byte_string to_bytes(const wide_string& __wstr) {
3127    return to_bytes(__wstr.data(), __wstr.data() + __wstr.size());
3128  }
3129  _LIBCPP_HIDE_FROM_ABI byte_string to_bytes(const _Elem* __first, const _Elem* __last);
3130
3131  _LIBCPP_HIDE_FROM_ABI size_t converted() const _NOEXCEPT { return __cvtcount_; }
3132  _LIBCPP_HIDE_FROM_ABI state_type state() const { return __cvtstate_; }
3133};
3134
3135_LIBCPP_SUPPRESS_DEPRECATED_PUSH
3136template <class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3137inline wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::wstring_convert(_Codecvt* __pcvt)
3138    : __cvtptr_(__pcvt), __cvtstate_(), __cvtcount_(0) {}
3139_LIBCPP_SUPPRESS_DEPRECATED_POP
3140
3141template <class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3142inline wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::wstring_convert(_Codecvt* __pcvt, state_type __state)
3143    : __cvtptr_(__pcvt), __cvtstate_(__state), __cvtcount_(0) {}
3144
3145template <class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3146wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::wstring_convert(
3147    const byte_string& __byte_err, const wide_string& __wide_err)
3148    : __byte_err_string_(__byte_err), __wide_err_string_(__wide_err), __cvtstate_(), __cvtcount_(0) {
3149  __cvtptr_ = new _Codecvt;
3150}
3151
3152#    ifndef _LIBCPP_CXX03_LANG
3153
3154template <class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3155inline wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::wstring_convert(wstring_convert&& __wc)
3156    : __byte_err_string_(std::move(__wc.__byte_err_string_)),
3157      __wide_err_string_(std::move(__wc.__wide_err_string_)),
3158      __cvtptr_(__wc.__cvtptr_),
3159      __cvtstate_(__wc.__cvtstate_),
3160      __cvtcount_(__wc.__cvtcount_) {
3161  __wc.__cvtptr_ = nullptr;
3162}
3163
3164#    endif // _LIBCPP_CXX03_LANG
3165
3166_LIBCPP_SUPPRESS_DEPRECATED_PUSH
3167template <class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3168wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::~wstring_convert() {
3169  delete __cvtptr_;
3170}
3171
3172template <class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3173typename wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::wide_string
3174wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::from_bytes(const char* __frm, const char* __frm_end) {
3175  _LIBCPP_SUPPRESS_DEPRECATED_POP
3176  __cvtcount_ = 0;
3177  if (__cvtptr_ != nullptr) {
3178    wide_string __ws(2 * (__frm_end - __frm), _Elem());
3179    if (__frm != __frm_end)
3180      __ws.resize(__ws.capacity());
3181    codecvt_base::result __r = codecvt_base::ok;
3182    state_type __st          = __cvtstate_;
3183    if (__frm != __frm_end) {
3184      _Elem* __to     = &__ws[0];
3185      _Elem* __to_end = __to + __ws.size();
3186      const char* __frm_nxt;
3187      do {
3188        _Elem* __to_nxt;
3189        __r = __cvtptr_->in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
3190        __cvtcount_ += __frm_nxt - __frm;
3191        if (__frm_nxt == __frm) {
3192          __r = codecvt_base::error;
3193        } else if (__r == codecvt_base::noconv) {
3194          __ws.resize(__to - &__ws[0]);
3195          // This only gets executed if _Elem is char
3196          __ws.append((const _Elem*)__frm, (const _Elem*)__frm_end);
3197          __frm = __frm_nxt;
3198          __r   = codecvt_base::ok;
3199        } else if (__r == codecvt_base::ok) {
3200          __ws.resize(__to_nxt - &__ws[0]);
3201          __frm = __frm_nxt;
3202        } else if (__r == codecvt_base::partial) {
3203          ptrdiff_t __s = __to_nxt - &__ws[0];
3204          __ws.resize(2 * __s);
3205          __to     = &__ws[0] + __s;
3206          __to_end = &__ws[0] + __ws.size();
3207          __frm    = __frm_nxt;
3208        }
3209      } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3210    }
3211    if (__r == codecvt_base::ok)
3212      return __ws;
3213  }
3214
3215  if (__wide_err_string_.empty())
3216    __throw_range_error("wstring_convert: from_bytes error");
3217
3218  return __wide_err_string_;
3219}
3220
3221template <class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3222typename wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::byte_string
3223wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::to_bytes(const _Elem* __frm, const _Elem* __frm_end) {
3224  __cvtcount_ = 0;
3225  if (__cvtptr_ != nullptr) {
3226    byte_string __bs(2 * (__frm_end - __frm), char());
3227    if (__frm != __frm_end)
3228      __bs.resize(__bs.capacity());
3229    codecvt_base::result __r = codecvt_base::ok;
3230    state_type __st          = __cvtstate_;
3231    if (__frm != __frm_end) {
3232      char* __to     = &__bs[0];
3233      char* __to_end = __to + __bs.size();
3234      const _Elem* __frm_nxt;
3235      do {
3236        char* __to_nxt;
3237        __r = __cvtptr_->out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
3238        __cvtcount_ += __frm_nxt - __frm;
3239        if (__frm_nxt == __frm) {
3240          __r = codecvt_base::error;
3241        } else if (__r == codecvt_base::noconv) {
3242          __bs.resize(__to - &__bs[0]);
3243          // This only gets executed if _Elem is char
3244          __bs.append((const char*)__frm, (const char*)__frm_end);
3245          __frm = __frm_nxt;
3246          __r   = codecvt_base::ok;
3247        } else if (__r == codecvt_base::ok) {
3248          __bs.resize(__to_nxt - &__bs[0]);
3249          __frm = __frm_nxt;
3250        } else if (__r == codecvt_base::partial) {
3251          ptrdiff_t __s = __to_nxt - &__bs[0];
3252          __bs.resize(2 * __s);
3253          __to     = &__bs[0] + __s;
3254          __to_end = &__bs[0] + __bs.size();
3255          __frm    = __frm_nxt;
3256        }
3257      } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3258    }
3259    if (__r == codecvt_base::ok) {
3260      size_t __s = __bs.size();
3261      __bs.resize(__bs.capacity());
3262      char* __to     = &__bs[0] + __s;
3263      char* __to_end = __to + __bs.size();
3264      do {
3265        char* __to_nxt;
3266        __r = __cvtptr_->unshift(__st, __to, __to_end, __to_nxt);
3267        if (__r == codecvt_base::noconv) {
3268          __bs.resize(__to - &__bs[0]);
3269          __r = codecvt_base::ok;
3270        } else if (__r == codecvt_base::ok) {
3271          __bs.resize(__to_nxt - &__bs[0]);
3272        } else if (__r == codecvt_base::partial) {
3273          ptrdiff_t __sp = __to_nxt - &__bs[0];
3274          __bs.resize(2 * __sp);
3275          __to     = &__bs[0] + __sp;
3276          __to_end = &__bs[0] + __bs.size();
3277        }
3278      } while (__r == codecvt_base::partial);
3279      if (__r == codecvt_base::ok)
3280        return __bs;
3281    }
3282  }
3283
3284  if (__byte_err_string_.empty())
3285    __throw_range_error("wstring_convert: to_bytes error");
3286
3287  return __byte_err_string_;
3288}
3289
3290template <class _Codecvt, class _Elem = wchar_t, class _Tr = char_traits<_Elem> >
3291class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 wbuffer_convert : public basic_streambuf<_Elem, _Tr> {
3292public:
3293  // types:
3294  typedef _Elem char_type;
3295  typedef _Tr traits_type;
3296  typedef typename traits_type::int_type int_type;
3297  typedef typename traits_type::pos_type pos_type;
3298  typedef typename traits_type::off_type off_type;
3299  typedef typename _Codecvt::state_type state_type;
3300
3301private:
3302  char* __extbuf_;
3303  const char* __extbufnext_;
3304  const char* __extbufend_;
3305  char __extbuf_min_[8];
3306  size_t __ebs_;
3307  char_type* __intbuf_;
3308  size_t __ibs_;
3309  streambuf* __bufptr_;
3310  _Codecvt* __cv_;
3311  state_type __st_;
3312  ios_base::openmode __cm_;
3313  bool __owns_eb_;
3314  bool __owns_ib_;
3315  bool __always_noconv_;
3316
3317public:
3318#    ifndef _LIBCPP_CXX03_LANG
3319  _LIBCPP_HIDE_FROM_ABI wbuffer_convert() : wbuffer_convert(nullptr) {}
3320  explicit _LIBCPP_HIDE_FROM_ABI
3321  wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt = new _Codecvt, state_type __state = state_type());
3322#    else
3323  _LIBCPP_EXPLICIT_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI
3324  wbuffer_convert(streambuf* __bytebuf = nullptr, _Codecvt* __pcvt = new _Codecvt, state_type __state = state_type());
3325#    endif
3326
3327  _LIBCPP_HIDE_FROM_ABI ~wbuffer_convert();
3328
3329  _LIBCPP_HIDE_FROM_ABI streambuf* rdbuf() const { return __bufptr_; }
3330  _LIBCPP_HIDE_FROM_ABI streambuf* rdbuf(streambuf* __bytebuf) {
3331    streambuf* __r = __bufptr_;
3332    __bufptr_      = __bytebuf;
3333    return __r;
3334  }
3335
3336  wbuffer_convert(const wbuffer_convert&)            = delete;
3337  wbuffer_convert& operator=(const wbuffer_convert&) = delete;
3338
3339  _LIBCPP_HIDE_FROM_ABI state_type state() const { return __st_; }
3340
3341protected:
3342  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual int_type underflow();
3343  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual int_type pbackfail(int_type __c = traits_type::eof());
3344  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual int_type overflow(int_type __c = traits_type::eof());
3345  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
3346  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual pos_type
3347  seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __wch = ios_base::in | ios_base::out);
3348  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual pos_type
3349  seekpos(pos_type __sp, ios_base::openmode __wch = ios_base::in | ios_base::out);
3350  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual int sync();
3351
3352private:
3353  _LIBCPP_HIDE_FROM_ABI_VIRTUAL bool __read_mode();
3354  _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __write_mode();
3355  _LIBCPP_HIDE_FROM_ABI_VIRTUAL wbuffer_convert* __close();
3356};
3357
3358_LIBCPP_SUPPRESS_DEPRECATED_PUSH
3359template <class _Codecvt, class _Elem, class _Tr>
3360wbuffer_convert<_Codecvt, _Elem, _Tr>::wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt, state_type __state)
3361    : __extbuf_(nullptr),
3362      __extbufnext_(nullptr),
3363      __extbufend_(nullptr),
3364      __ebs_(0),
3365      __intbuf_(0),
3366      __ibs_(0),
3367      __bufptr_(__bytebuf),
3368      __cv_(__pcvt),
3369      __st_(__state),
3370      __cm_(0),
3371      __owns_eb_(false),
3372      __owns_ib_(false),
3373      __always_noconv_(__cv_ ? __cv_->always_noconv() : false) {
3374  setbuf(0, 4096);
3375}
3376
3377template <class _Codecvt, class _Elem, class _Tr>
3378wbuffer_convert<_Codecvt, _Elem, _Tr>::~wbuffer_convert() {
3379  __close();
3380  delete __cv_;
3381  if (__owns_eb_)
3382    delete[] __extbuf_;
3383  if (__owns_ib_)
3384    delete[] __intbuf_;
3385}
3386
3387template <class _Codecvt, class _Elem, class _Tr>
3388typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type wbuffer_convert<_Codecvt, _Elem, _Tr>::underflow() {
3389  _LIBCPP_SUPPRESS_DEPRECATED_POP
3390  if (__cv_ == 0 || __bufptr_ == nullptr)
3391    return traits_type::eof();
3392  bool __initial = __read_mode();
3393  char_type __1buf;
3394  if (this->gptr() == 0)
3395    this->setg(&__1buf, &__1buf + 1, &__1buf + 1);
3396  const size_t __unget_sz = __initial ? 0 : std::min<size_t>((this->egptr() - this->eback()) / 2, 4);
3397  int_type __c            = traits_type::eof();
3398  if (this->gptr() == this->egptr()) {
3399    std::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
3400    if (__always_noconv_) {
3401      streamsize __nmemb = static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz);
3402      __nmemb            = __bufptr_->sgetn((char*)this->eback() + __unget_sz, __nmemb);
3403      if (__nmemb != 0) {
3404        this->setg(this->eback(), this->eback() + __unget_sz, this->eback() + __unget_sz + __nmemb);
3405        __c = *this->gptr();
3406      }
3407    } else {
3408      if (__extbufend_ != __extbufnext_) {
3409        _LIBCPP_ASSERT_NON_NULL(__extbufnext_ != nullptr, "underflow moving from nullptr");
3410        _LIBCPP_ASSERT_NON_NULL(__extbuf_ != nullptr, "underflow moving into nullptr");
3411        std::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
3412      }
3413      __extbufnext_      = __extbuf_ + (__extbufend_ - __extbufnext_);
3414      __extbufend_       = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
3415      streamsize __nmemb = std::min(static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz),
3416                                    static_cast<streamsize>(__extbufend_ - __extbufnext_));
3417      codecvt_base::result __r;
3418      // FIXME: Do we ever need to restore the state here?
3419      // state_type __svs = __st_;
3420      streamsize __nr = __bufptr_->sgetn(const_cast<char*>(__extbufnext_), __nmemb);
3421      if (__nr != 0) {
3422        __extbufend_ = __extbufnext_ + __nr;
3423        char_type* __inext;
3424        __r = __cv_->in(
3425            __st_, __extbuf_, __extbufend_, __extbufnext_, this->eback() + __unget_sz, this->egptr(), __inext);
3426        if (__r == codecvt_base::noconv) {
3427          this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)const_cast<char*>(__extbufend_));
3428          __c = *this->gptr();
3429        } else if (__inext != this->eback() + __unget_sz) {
3430          this->setg(this->eback(), this->eback() + __unget_sz, __inext);
3431          __c = *this->gptr();
3432        }
3433      }
3434    }
3435  } else
3436    __c = *this->gptr();
3437  if (this->eback() == &__1buf)
3438    this->setg(0, 0, 0);
3439  return __c;
3440}
3441
3442_LIBCPP_SUPPRESS_DEPRECATED_PUSH
3443template <class _Codecvt, class _Elem, class _Tr>
3444typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
3445wbuffer_convert<_Codecvt, _Elem, _Tr>::pbackfail(int_type __c) {
3446  _LIBCPP_SUPPRESS_DEPRECATED_POP
3447  if (__cv_ != 0 && __bufptr_ && this->eback() < this->gptr()) {
3448    if (traits_type::eq_int_type(__c, traits_type::eof())) {
3449      this->gbump(-1);
3450      return traits_type::not_eof(__c);
3451    }
3452    if (traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1])) {
3453      this->gbump(-1);
3454      *this->gptr() = traits_type::to_char_type(__c);
3455      return __c;
3456    }
3457  }
3458  return traits_type::eof();
3459}
3460
3461_LIBCPP_SUPPRESS_DEPRECATED_PUSH
3462template <class _Codecvt, class _Elem, class _Tr>
3463typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type wbuffer_convert<_Codecvt, _Elem, _Tr>::overflow(int_type __c) {
3464  _LIBCPP_SUPPRESS_DEPRECATED_POP
3465  if (__cv_ == 0 || !__bufptr_)
3466    return traits_type::eof();
3467  __write_mode();
3468  char_type __1buf;
3469  char_type* __pb_save  = this->pbase();
3470  char_type* __epb_save = this->epptr();
3471  if (!traits_type::eq_int_type(__c, traits_type::eof())) {
3472    if (this->pptr() == 0)
3473      this->setp(&__1buf, &__1buf + 1);
3474    *this->pptr() = traits_type::to_char_type(__c);
3475    this->pbump(1);
3476  }
3477  if (this->pptr() != this->pbase()) {
3478    if (__always_noconv_) {
3479      streamsize __nmemb = static_cast<streamsize>(this->pptr() - this->pbase());
3480      if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
3481        return traits_type::eof();
3482    } else {
3483      char* __extbe = __extbuf_;
3484      codecvt_base::result __r;
3485      do {
3486        const char_type* __e;
3487        __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e, __extbuf_, __extbuf_ + __ebs_, __extbe);
3488        if (__e == this->pbase())
3489          return traits_type::eof();
3490        if (__r == codecvt_base::noconv) {
3491          streamsize __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
3492          if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
3493            return traits_type::eof();
3494        } else if (__r == codecvt_base::ok || __r == codecvt_base::partial) {
3495          streamsize __nmemb = static_cast<size_t>(__extbe - __extbuf_);
3496          if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
3497            return traits_type::eof();
3498          if (__r == codecvt_base::partial) {
3499            this->setp(const_cast<char_type*>(__e), this->pptr());
3500            this->__pbump(this->epptr() - this->pbase());
3501          }
3502        } else
3503          return traits_type::eof();
3504      } while (__r == codecvt_base::partial);
3505    }
3506    this->setp(__pb_save, __epb_save);
3507  }
3508  return traits_type::not_eof(__c);
3509}
3510
3511_LIBCPP_SUPPRESS_DEPRECATED_PUSH
3512template <class _Codecvt, class _Elem, class _Tr>
3513basic_streambuf<_Elem, _Tr>* wbuffer_convert<_Codecvt, _Elem, _Tr>::setbuf(char_type* __s, streamsize __n) {
3514  _LIBCPP_SUPPRESS_DEPRECATED_POP
3515  this->setg(0, 0, 0);
3516  this->setp(0, 0);
3517  if (__owns_eb_)
3518    delete[] __extbuf_;
3519  if (__owns_ib_)
3520    delete[] __intbuf_;
3521  __ebs_ = __n;
3522  if (__ebs_ > sizeof(__extbuf_min_)) {
3523    if (__always_noconv_ && __s) {
3524      __extbuf_  = (char*)__s;
3525      __owns_eb_ = false;
3526    } else {
3527      __extbuf_  = new char[__ebs_];
3528      __owns_eb_ = true;
3529    }
3530  } else {
3531    __extbuf_  = __extbuf_min_;
3532    __ebs_     = sizeof(__extbuf_min_);
3533    __owns_eb_ = false;
3534  }
3535  if (!__always_noconv_) {
3536    __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
3537    if (__s && __ibs_ >= sizeof(__extbuf_min_)) {
3538      __intbuf_  = __s;
3539      __owns_ib_ = false;
3540    } else {
3541      __intbuf_  = new char_type[__ibs_];
3542      __owns_ib_ = true;
3543    }
3544  } else {
3545    __ibs_     = 0;
3546    __intbuf_  = 0;
3547    __owns_ib_ = false;
3548  }
3549  return this;
3550}
3551
3552_LIBCPP_SUPPRESS_DEPRECATED_PUSH
3553template <class _Codecvt, class _Elem, class _Tr>
3554typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
3555wbuffer_convert<_Codecvt, _Elem, _Tr>::seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __om) {
3556  int __width = __cv_->encoding();
3557  if (__cv_ == 0 || !__bufptr_ || (__width <= 0 && __off != 0) || sync())
3558    return pos_type(off_type(-1));
3559  // __width > 0 || __off == 0, now check __way
3560  if (__way != ios_base::beg && __way != ios_base::cur && __way != ios_base::end)
3561    return pos_type(off_type(-1));
3562  pos_type __r = __bufptr_->pubseekoff(__width * __off, __way, __om);
3563  __r.state(__st_);
3564  return __r;
3565}
3566
3567template <class _Codecvt, class _Elem, class _Tr>
3568typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
3569wbuffer_convert<_Codecvt, _Elem, _Tr>::seekpos(pos_type __sp, ios_base::openmode __wch) {
3570  if (__cv_ == 0 || !__bufptr_ || sync())
3571    return pos_type(off_type(-1));
3572  if (__bufptr_->pubseekpos(__sp, __wch) == pos_type(off_type(-1)))
3573    return pos_type(off_type(-1));
3574  return __sp;
3575}
3576
3577template <class _Codecvt, class _Elem, class _Tr>
3578int wbuffer_convert<_Codecvt, _Elem, _Tr>::sync() {
3579  _LIBCPP_SUPPRESS_DEPRECATED_POP
3580  if (__cv_ == 0 || !__bufptr_)
3581    return 0;
3582  if (__cm_ & ios_base::out) {
3583    if (this->pptr() != this->pbase())
3584      if (overflow() == traits_type::eof())
3585        return -1;
3586    codecvt_base::result __r;
3587    do {
3588      char* __extbe;
3589      __r                = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
3590      streamsize __nmemb = static_cast<streamsize>(__extbe - __extbuf_);
3591      if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
3592        return -1;
3593    } while (__r == codecvt_base::partial);
3594    if (__r == codecvt_base::error)
3595      return -1;
3596    if (__bufptr_->pubsync())
3597      return -1;
3598  } else if (__cm_ & ios_base::in) {
3599    off_type __c;
3600    if (__always_noconv_)
3601      __c = this->egptr() - this->gptr();
3602    else {
3603      int __width = __cv_->encoding();
3604      __c         = __extbufend_ - __extbufnext_;
3605      if (__width > 0)
3606        __c += __width * (this->egptr() - this->gptr());
3607      else {
3608        if (this->gptr() != this->egptr()) {
3609          std::reverse(this->gptr(), this->egptr());
3610          codecvt_base::result __r;
3611          const char_type* __e = this->gptr();
3612          char* __extbe;
3613          do {
3614            __r = __cv_->out(__st_, __e, this->egptr(), __e, __extbuf_, __extbuf_ + __ebs_, __extbe);
3615            switch (__r) {
3616            case codecvt_base::noconv:
3617              __c += this->egptr() - this->gptr();
3618              break;
3619            case codecvt_base::ok:
3620            case codecvt_base::partial:
3621              __c += __extbe - __extbuf_;
3622              break;
3623            default:
3624              return -1;
3625            }
3626          } while (__r == codecvt_base::partial);
3627        }
3628      }
3629    }
3630    if (__bufptr_->pubseekoff(-__c, ios_base::cur, __cm_) == pos_type(off_type(-1)))
3631      return -1;
3632    this->setg(0, 0, 0);
3633    __cm_ = 0;
3634  }
3635  return 0;
3636}
3637
3638_LIBCPP_SUPPRESS_DEPRECATED_PUSH
3639template <class _Codecvt, class _Elem, class _Tr>
3640bool wbuffer_convert<_Codecvt, _Elem, _Tr>::__read_mode() {
3641  if (!(__cm_ & ios_base::in)) {
3642    this->setp(0, 0);
3643    if (__always_noconv_)
3644      this->setg((char_type*)__extbuf_, (char_type*)__extbuf_ + __ebs_, (char_type*)__extbuf_ + __ebs_);
3645    else
3646      this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
3647    __cm_ = ios_base::in;
3648    return true;
3649  }
3650  return false;
3651}
3652
3653template <class _Codecvt, class _Elem, class _Tr>
3654void wbuffer_convert<_Codecvt, _Elem, _Tr>::__write_mode() {
3655  if (!(__cm_ & ios_base::out)) {
3656    this->setg(0, 0, 0);
3657    if (__ebs_ > sizeof(__extbuf_min_)) {
3658      if (__always_noconv_)
3659        this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__ebs_ - 1));
3660      else
3661        this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
3662    } else
3663      this->setp(0, 0);
3664    __cm_ = ios_base::out;
3665  }
3666}
3667
3668template <class _Codecvt, class _Elem, class _Tr>
3669wbuffer_convert<_Codecvt, _Elem, _Tr>* wbuffer_convert<_Codecvt, _Elem, _Tr>::__close() {
3670  wbuffer_convert* __rt = nullptr;
3671  if (__cv_ != nullptr && __bufptr_ != nullptr) {
3672    __rt = this;
3673    if ((__cm_ & ios_base::out) && sync())
3674      __rt = nullptr;
3675  }
3676  return __rt;
3677}
3678
3679_LIBCPP_SUPPRESS_DEPRECATED_POP
3680
3681#  endif // _LIBCPP_STD_VER < 26 || defined(_LIBCPP_ENABLE_CXX26_REMOVED_WSTRING_CONVERT)
3682
3683_LIBCPP_END_NAMESPACE_STD
3684
3685_LIBCPP_POP_MACROS
3686
3687// NOLINTEND(libcpp-robust-against-adl)
3688
3689#endif // _LIBCPP_HAS_LOCALIZATION
3690
3691#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
3692#  include <atomic>
3693#  include <concepts>
3694#  include <cstdarg>
3695#  include <iterator>
3696#  include <mutex>
3697#  include <stdexcept>
3698#  include <type_traits>
3699#  include <typeinfo>
3700#endif
3701
3702#endif // _LIBCPP_LOCALE
3703