blob: e240799f3831804b06f479742d46292d4700d4f3 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- locale ------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_LOCALE
12#define _LIBCPP_LOCALE
13
14/*
15 locale synopsis
16
17namespace std
18{
19
20class locale
21{
22public:
23 // types:
24 class facet;
25 class id;
26
27 typedef int category;
28 static const category // values assigned here are for exposition only
29 none = 0x000,
30 collate = 0x010,
31 ctype = 0x020,
32 monetary = 0x040,
33 numeric = 0x080,
34 time = 0x100,
35 messages = 0x200,
36 all = collate | ctype | monetary | numeric | time | messages;
37
38 // construct/copy/destroy:
Howard Hinnantc9834542011-05-31 15:34:58 +000039 locale() noexcept;
40 locale(const locale& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000041 explicit locale(const char* std_name);
42 explicit locale(const string& std_name);
43 locale(const locale& other, const char* std_name, category);
44 locale(const locale& other, const string& std_name, category);
45 template <class Facet> locale(const locale& other, Facet* f);
46 locale(const locale& other, const locale& one, category);
47
Howard Hinnantc9834542011-05-31 15:34:58 +000048 ~locale(); // not virtual
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000049
Howard Hinnantc9834542011-05-31 15:34:58 +000050 const locale& operator=(const locale& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000051
52 template <class Facet> locale combine(const locale& other) const;
53
54 // locale operations:
55 basic_string<char> name() const;
56 bool operator==(const locale& other) const;
57 bool operator!=(const locale& other) const;
58 template <class charT, class Traits, class Allocator>
59 bool operator()(const basic_string<charT,Traits,Allocator>& s1,
60 const basic_string<charT,Traits,Allocator>& s2) const;
61
62 // global locale objects:
63 static locale global(const locale&);
64 static const locale& classic();
65};
66
67template <class Facet> const Facet& use_facet(const locale&);
Howard Hinnantc9834542011-05-31 15:34:58 +000068template <class Facet> bool has_facet(const locale&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000069
70// 22.3.3, convenience interfaces:
71template <class charT> bool isspace (charT c, const locale& loc);
72template <class charT> bool isprint (charT c, const locale& loc);
73template <class charT> bool iscntrl (charT c, const locale& loc);
74template <class charT> bool isupper (charT c, const locale& loc);
75template <class charT> bool islower (charT c, const locale& loc);
76template <class charT> bool isalpha (charT c, const locale& loc);
77template <class charT> bool isdigit (charT c, const locale& loc);
78template <class charT> bool ispunct (charT c, const locale& loc);
79template <class charT> bool isxdigit(charT c, const locale& loc);
80template <class charT> bool isalnum (charT c, const locale& loc);
81template <class charT> bool isgraph (charT c, const locale& loc);
82template <class charT> charT toupper(charT c, const locale& loc);
83template <class charT> charT tolower(charT c, const locale& loc);
Howard Hinnantd23b4642010-05-31 20:58:54 +000084
85template<class Codecvt, class Elem = wchar_t,
86 class Wide_alloc = allocator<Elem>,
87 class Byte_alloc = allocator<char>>
88class wstring_convert
89{
90public:
91 typedef basic_string<char, char_traits<char>, Byte_alloc> byte_string;
92 typedef basic_string<Elem, char_traits<Elem>, Wide_alloc> wide_string;
93 typedef typename Codecvt::state_type state_type;
94 typedef typename wide_string::traits_type::int_type int_type;
95
Marshall Clow83179a72013-08-27 20:18:59 +000096 explicit wstring_convert(Codecvt* pcvt = new Codecvt); // explicit in C++14
Howard Hinnantd23b4642010-05-31 20:58:54 +000097 wstring_convert(Codecvt* pcvt, state_type state);
Marshall Clow83179a72013-08-27 20:18:59 +000098 explicit wstring_convert(const byte_string& byte_err, // explicit in C++14
Howard Hinnantd23b4642010-05-31 20:58:54 +000099 const wide_string& wide_err = wide_string());
Marshall Clow83179a72013-08-27 20:18:59 +0000100 wstring_convert(const wstring_convert&) = delete; // C++14
101 wstring_convert & operator=(const wstring_convert &) = delete; // C++14
Howard Hinnantd23b4642010-05-31 20:58:54 +0000102 ~wstring_convert();
103
104 wide_string from_bytes(char byte);
105 wide_string from_bytes(const char* ptr);
106 wide_string from_bytes(const byte_string& str);
107 wide_string from_bytes(const char* first, const char* last);
108
109 byte_string to_bytes(Elem wchar);
110 byte_string to_bytes(const Elem* wptr);
111 byte_string to_bytes(const wide_string& wstr);
112 byte_string to_bytes(const Elem* first, const Elem* last);
113
Marshall Clow83179a72013-08-27 20:18:59 +0000114 size_t converted() const; // noexcept in C++14
Howard Hinnantd23b4642010-05-31 20:58:54 +0000115 state_type state() const;
116};
117
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000118template <class Codecvt, class Elem = wchar_t, class Tr = char_traits<Elem>>
Howard Hinnantd23b4642010-05-31 20:58:54 +0000119class wbuffer_convert
120 : public basic_streambuf<Elem, Tr>
121{
122public:
123 typedef typename Tr::state_type state_type;
124
Marshall Clow83179a72013-08-27 20:18:59 +0000125 explicit wbuffer_convert(streambuf* bytebuf = 0, Codecvt* pcvt = new Codecvt,
126 state_type state = state_type()); // explicit in C++14
127 wbuffer_convert(const wbuffer_convert&) = delete; // C++14
128 wbuffer_convert & operator=(const wbuffer_convert &) = delete; // C++14
129 ~wbuffer_convert(); // C++14
130
Howard Hinnantd23b4642010-05-31 20:58:54 +0000131 streambuf* rdbuf() const;
132 streambuf* rdbuf(streambuf* bytebuf);
133
134 state_type state() const;
135};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000136
137// 22.4.1 and 22.4.1.3, ctype:
138class ctype_base;
139template <class charT> class ctype;
140template <> class ctype<char>; // specialization
141template <class charT> class ctype_byname;
142template <> class ctype_byname<char>; // specialization
143
144class codecvt_base;
145template <class internT, class externT, class stateT> class codecvt;
146template <class internT, class externT, class stateT> class codecvt_byname;
147
148// 22.4.2 and 22.4.3, numeric:
149template <class charT, class InputIterator> class num_get;
150template <class charT, class OutputIterator> class num_put;
151template <class charT> class numpunct;
152template <class charT> class numpunct_byname;
153
154// 22.4.4, col lation:
155template <class charT> class collate;
156template <class charT> class collate_byname;
157
158// 22.4.5, date and time:
159class time_base;
160template <class charT, class InputIterator> class time_get;
161template <class charT, class InputIterator> class time_get_byname;
162template <class charT, class OutputIterator> class time_put;
163template <class charT, class OutputIterator> class time_put_byname;
164
165// 22.4.6, money:
166class money_base;
167template <class charT, class InputIterator> class money_get;
168template <class charT, class OutputIterator> class money_put;
169template <class charT, bool Intl> class moneypunct;
170template <class charT, bool Intl> class moneypunct_byname;
171
172// 22.4.7, message retrieval:
173class messages_base;
174template <class charT> class messages;
175template <class charT> class messages_byname;
176
177} // std
178
179*/
180
181#include <__config>
182#include <__locale>
Eric Fiselierfe6d50f2016-06-19 06:58:22 +0000183#include <__debug>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000184#include <algorithm>
185#include <memory>
186#include <ios>
187#include <streambuf>
188#include <iterator>
189#include <limits>
Marshall Clowdece7fe2013-03-18 17:45:34 +0000190#ifndef __APPLE__
Howard Hinnantadff4892010-05-24 17:49:41 +0000191#include <cstdarg>
192#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000193#include <cstdlib>
194#include <ctime>
Eric Fiselier7b7ac672017-05-31 22:14:05 +0000195#include <cstdio>
Ed Schouten0251f0f2015-03-11 16:39:36 +0000196#ifdef _LIBCPP_HAS_CATOPEN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000197#include <nl_types.h>
Marshall Clowa6439262014-07-10 15:20:28 +0000198#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000199
Marshall Clowdece7fe2013-03-18 17:45:34 +0000200#ifdef __APPLE__
Howard Hinnant537b2fa2012-11-14 21:17:15 +0000201#include <Availability.h>
202#endif
203
Ben Craigfd556582016-03-09 15:39:39 +0000204#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
205#include <__bsd_locale_defaults.h>
206#else
207#include <__bsd_locale_fallbacks.h>
208#endif
209
Eric Fiselier018a3d52017-05-31 22:07:49 +0000210#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
211#pragma GCC system_header
212#endif
213
214_LIBCPP_PUSH_MACROS
215#include <__undef_macros>
216
217
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000218_LIBCPP_BEGIN_NAMESPACE_STD
219
Marshall Clow53e27632013-03-18 19:34:07 +0000220#if defined(__APPLE__) || defined(__FreeBSD__)
Howard Hinnant2ea1ca92011-09-28 21:05:01 +0000221# define _LIBCPP_GET_C_LOCALE 0
Ed Schouten681abae2015-03-10 09:35:22 +0000222#elif defined(__CloudABI__) || defined(__NetBSD__)
Joerg Sonnenbergera71a9522013-05-17 21:17:34 +0000223# define _LIBCPP_GET_C_LOCALE LC_C_LOCALE
Howard Hinnant2ea1ca92011-09-28 21:05:01 +0000224#else
225# define _LIBCPP_GET_C_LOCALE __cloc()
Howard Hinnant866569b2011-09-28 23:39:33 +0000226 // Get the C locale object
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000227 _LIBCPP_FUNC_VIS locale_t __cloc();
Howard Hinnant866569b2011-09-28 23:39:33 +0000228#define __cloc_defined
Howard Hinnant2ea1ca92011-09-28 21:05:01 +0000229#endif
230
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000231// __scan_keyword
232// Scans [__b, __e) until a match is found in the basic_strings range
233// [__kb, __ke) or until it can be shown that there is no match in [__kb, __ke).
234// __b will be incremented (visibly), consuming CharT until a match is found
235// or proved to not exist. A keyword may be "", in which will match anything.
236// If one keyword is a prefix of another, and the next CharT in the input
237// might match another keyword, the algorithm will attempt to find the longest
238// matching keyword. If the longer matching keyword ends up not matching, then
239// no keyword match is found. If no keyword match is found, __ke is returned
240// and failbit is set in __err.
241// Else an iterator pointing to the matching keyword is found. If more than
242// one keyword matches, an iterator to the first matching keyword is returned.
Alp Tokerec34c482014-05-15 11:27:39 +0000243// If on exit __b == __e, eofbit is set in __err. If __case_sensitive is false,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000244// __ct is used to force to lower case before comparing characters.
245// Examples:
246// Keywords: "a", "abb"
247// If the input is "a", the first keyword matches and eofbit is set.
248// If the input is "abc", no match is found and "ab" are consumed.
249template <class _InputIterator, class _ForwardIterator, class _Ctype>
250_LIBCPP_HIDDEN
251_ForwardIterator
252__scan_keyword(_InputIterator& __b, _InputIterator __e,
253 _ForwardIterator __kb, _ForwardIterator __ke,
254 const _Ctype& __ct, ios_base::iostate& __err,
255 bool __case_sensitive = true)
256{
257 typedef typename iterator_traits<_InputIterator>::value_type _CharT;
Howard Hinnantec3773c2011-12-01 20:21:04 +0000258 size_t __nkw = static_cast<size_t>(_VSTD::distance(__kb, __ke));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000259 const unsigned char __doesnt_match = '\0';
260 const unsigned char __might_match = '\1';
261 const unsigned char __does_match = '\2';
262 unsigned char __statbuf[100];
263 unsigned char* __status = __statbuf;
264 unique_ptr<unsigned char, void(*)(void*)> __stat_hold(0, free);
265 if (__nkw > sizeof(__statbuf))
266 {
267 __status = (unsigned char*)malloc(__nkw);
268 if (__status == 0)
269 __throw_bad_alloc();
270 __stat_hold.reset(__status);
271 }
272 size_t __n_might_match = __nkw; // At this point, any keyword might match
273 size_t __n_does_match = 0; // but none of them definitely do
274 // Initialize all statuses to __might_match, except for "" keywords are __does_match
275 unsigned char* __st = __status;
Eric Fiselierb9919752014-10-27 19:28:20 +0000276 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000277 {
278 if (!__ky->empty())
279 *__st = __might_match;
280 else
281 {
282 *__st = __does_match;
283 --__n_might_match;
284 ++__n_does_match;
285 }
286 }
287 // While there might be a match, test keywords against the next CharT
288 for (size_t __indx = 0; __b != __e && __n_might_match > 0; ++__indx)
289 {
290 // Peek at the next CharT but don't consume it
291 _CharT __c = *__b;
292 if (!__case_sensitive)
293 __c = __ct.toupper(__c);
294 bool __consume = false;
295 // For each keyword which might match, see if the __indx character is __c
296 // If a match if found, consume __c
297 // If a match is found, and that is the last character in the keyword,
298 // then that keyword matches.
299 // If the keyword doesn't match this character, then change the keyword
300 // to doesn't match
301 __st = __status;
Eric Fiselierb9919752014-10-27 19:28:20 +0000302 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000303 {
304 if (*__st == __might_match)
305 {
306 _CharT __kc = (*__ky)[__indx];
307 if (!__case_sensitive)
308 __kc = __ct.toupper(__kc);
309 if (__c == __kc)
310 {
311 __consume = true;
312 if (__ky->size() == __indx+1)
313 {
314 *__st = __does_match;
315 --__n_might_match;
316 ++__n_does_match;
317 }
318 }
319 else
320 {
321 *__st = __doesnt_match;
322 --__n_might_match;
323 }
324 }
325 }
326 // consume if we matched a character
327 if (__consume)
328 {
329 ++__b;
330 // If we consumed a character and there might be a matched keyword that
331 // was marked matched on a previous iteration, then such keywords
332 // which are now marked as not matching.
333 if (__n_might_match + __n_does_match > 1)
334 {
335 __st = __status;
Eric Fiselierb9919752014-10-27 19:28:20 +0000336 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000337 {
338 if (*__st == __does_match && __ky->size() != __indx+1)
339 {
340 *__st = __doesnt_match;
341 --__n_does_match;
342 }
343 }
344 }
345 }
346 }
347 // We've exited the loop because we hit eof and/or we have no more "might matches".
348 if (__b == __e)
349 __err |= ios_base::eofbit;
350 // Return the first matching result
Eric Fiselierb9919752014-10-27 19:28:20 +0000351 for (__st = __status; __kb != __ke; ++__kb, (void) ++__st)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000352 if (*__st == __does_match)
353 break;
354 if (__kb == __ke)
355 __err |= ios_base::failbit;
356 return __kb;
357}
358
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000359struct _LIBCPP_TYPE_VIS __num_get_base
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000360{
361 static const int __num_get_buf_sz = 40;
362
363 static int __get_base(ios_base&);
364 static const char __src[33];
365};
366
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000367_LIBCPP_FUNC_VIS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000368void __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end,
369 ios_base::iostate& __err);
370
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371template <class _CharT>
372struct __num_get
373 : protected __num_get_base
374{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000375 static string __stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
376 _CharT& __thousands_sep);
Aditya Kumaraa356d62017-06-14 23:17:45 +0000377
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000378 static int __stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp,
379 char* __a, char*& __a_end,
380 _CharT __decimal_point, _CharT __thousands_sep,
381 const string& __grouping, unsigned* __g,
382 unsigned*& __g_end, unsigned& __dc, _CharT* __atoms);
Aditya Kumaraa356d62017-06-14 23:17:45 +0000383#ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
384 static string __stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep);
385 static int __stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
386 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
387 unsigned* __g, unsigned*& __g_end, _CharT* __atoms);
388
389#else
390 static string __stage2_int_prep(ios_base& __iob, _CharT& __thousands_sep)
391 {
392 locale __loc = __iob.getloc();
393 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
394 __thousands_sep = __np.thousands_sep();
395 return __np.grouping();
396 }
397
398 const _CharT* __do_widen(ios_base& __iob, _CharT* __atoms) const
399 {
400 return __do_widen_p(__iob, __atoms);
401 }
402
403
404 static int __stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
405 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
406 unsigned* __g, unsigned*& __g_end, const _CharT* __atoms);
407private:
408 template<typename T>
409 const T* __do_widen_p(ios_base& __iob, T* __atoms) const
410 {
411 locale __loc = __iob.getloc();
412 use_facet<ctype<T> >(__loc).widen(__src, __src + 26, __atoms);
413 return __atoms;
414 }
415
416 const char* __do_widen_p(ios_base& __iob, char* __atoms) const
417 {
418 (void)__iob;
419 (void)__atoms;
420 return __src;
421 }
422#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000423};
424
Aditya Kumaraa356d62017-06-14 23:17:45 +0000425#ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000426template <class _CharT>
427string
428__num_get<_CharT>::__stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep)
429{
430 locale __loc = __iob.getloc();
431 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 26, __atoms);
432 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
433 __thousands_sep = __np.thousands_sep();
434 return __np.grouping();
435}
Aditya Kumaraa356d62017-06-14 23:17:45 +0000436#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000437
438template <class _CharT>
439string
440__num_get<_CharT>::__stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
441 _CharT& __thousands_sep)
442{
443 locale __loc = __iob.getloc();
444 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 32, __atoms);
445 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
446 __decimal_point = __np.decimal_point();
447 __thousands_sep = __np.thousands_sep();
448 return __np.grouping();
449}
450
451template <class _CharT>
452int
Aditya Kumaraa356d62017-06-14 23:17:45 +0000453#ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000454__num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
455 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
456 unsigned* __g, unsigned*& __g_end, _CharT* __atoms)
Aditya Kumaraa356d62017-06-14 23:17:45 +0000457#else
458__num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
459 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
460 unsigned* __g, unsigned*& __g_end, const _CharT* __atoms)
461
462#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000463{
Howard Hinnant80586722011-03-09 01:03:19 +0000464 if (__a_end == __a && (__ct == __atoms[24] || __ct == __atoms[25]))
465 {
466 *__a_end++ = __ct == __atoms[24] ? '+' : '-';
467 __dc = 0;
468 return 0;
469 }
Howard Hinnantec3773c2011-12-01 20:21:04 +0000470 if (__grouping.size() != 0 && __ct == __thousands_sep)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000471 {
472 if (__g_end-__g < __num_get_buf_sz)
473 {
474 *__g_end++ = __dc;
475 __dc = 0;
476 }
477 return 0;
478 }
479 ptrdiff_t __f = find(__atoms, __atoms + 26, __ct) - __atoms;
Howard Hinnant80586722011-03-09 01:03:19 +0000480 if (__f >= 24)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000481 return -1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000482 switch (__base)
483 {
484 case 8:
485 case 10:
486 if (__f >= __base)
Howard Hinnant80586722011-03-09 01:03:19 +0000487 return -1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000488 break;
Howard Hinnant80586722011-03-09 01:03:19 +0000489 case 16:
490 if (__f < 22)
491 break;
492 if (__a_end != __a && __a_end - __a <= 2 && __a_end[-1] == '0')
493 {
494 __dc = 0;
495 *__a_end++ = __src[__f];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000496 return 0;
Howard Hinnant80586722011-03-09 01:03:19 +0000497 }
498 return -1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000499 }
Howard Hinnantae57a1a2013-04-15 20:40:06 +0000500 *__a_end++ = __src[__f];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000501 ++__dc;
502 return 0;
503}
504
505template <class _CharT>
506int
507__num_get<_CharT>::__stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp, char* __a, char*& __a_end,
508 _CharT __decimal_point, _CharT __thousands_sep, const string& __grouping,
509 unsigned* __g, unsigned*& __g_end, unsigned& __dc, _CharT* __atoms)
510{
511 if (__ct == __decimal_point)
512 {
513 if (!__in_units)
514 return -1;
515 __in_units = false;
516 *__a_end++ = '.';
517 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
518 *__g_end++ = __dc;
519 return 0;
520 }
521 if (__ct == __thousands_sep && __grouping.size() != 0)
522 {
523 if (!__in_units)
524 return -1;
525 if (__g_end-__g < __num_get_buf_sz)
526 {
527 *__g_end++ = __dc;
528 __dc = 0;
529 }
530 return 0;
531 }
532 ptrdiff_t __f = find(__atoms, __atoms + 32, __ct) - __atoms;
533 if (__f >= 32)
534 return -1;
535 char __x = __src[__f];
Howard Hinnantb04ad412012-02-15 19:19:37 +0000536 if (__x == '-' || __x == '+')
537 {
Howard Hinnant6319f142013-03-08 19:06:24 +0000538 if (__a_end == __a || (__a_end[-1] & 0x5F) == (__exp & 0x7F))
Howard Hinnantb04ad412012-02-15 19:19:37 +0000539 {
540 *__a_end++ = __x;
541 return 0;
542 }
543 return -1;
544 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000545 if (__x == 'x' || __x == 'X')
546 __exp = 'P';
Howard Hinnant6319f142013-03-08 19:06:24 +0000547 else if ((__x & 0x5F) == __exp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000548 {
Howard Hinnant6319f142013-03-08 19:06:24 +0000549 __exp |= 0x80;
550 if (__in_units)
551 {
552 __in_units = false;
553 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
554 *__g_end++ = __dc;
555 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000556 }
Howard Hinnantae57a1a2013-04-15 20:40:06 +0000557 *__a_end++ = __x;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000558 if (__f >= 22)
559 return 0;
560 ++__dc;
561 return 0;
562}
563
Eric Fiselier833d6442016-09-15 22:27:07 +0000564_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<char>)
565_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000566
567template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Eric Fiselierc3589a82017-01-04 23:56:00 +0000568class _LIBCPP_TEMPLATE_VIS num_get
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000569 : public locale::facet,
570 private __num_get<_CharT>
571{
572public:
573 typedef _CharT char_type;
574 typedef _InputIterator iter_type;
575
Louis Dionne54238052018-07-11 23:14:33 +0000576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000577 explicit num_get(size_t __refs = 0)
578 : locale::facet(__refs) {}
579
Louis Dionne54238052018-07-11 23:14:33 +0000580 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000581 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
582 ios_base::iostate& __err, bool& __v) const
583 {
584 return do_get(__b, __e, __iob, __err, __v);
585 }
586
Louis Dionne54238052018-07-11 23:14:33 +0000587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000588 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
589 ios_base::iostate& __err, long& __v) const
590 {
591 return do_get(__b, __e, __iob, __err, __v);
592 }
593
Louis Dionne54238052018-07-11 23:14:33 +0000594 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000595 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
596 ios_base::iostate& __err, long long& __v) const
597 {
598 return do_get(__b, __e, __iob, __err, __v);
599 }
600
Louis Dionne54238052018-07-11 23:14:33 +0000601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000602 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
603 ios_base::iostate& __err, unsigned short& __v) const
604 {
605 return do_get(__b, __e, __iob, __err, __v);
606 }
607
Louis Dionne54238052018-07-11 23:14:33 +0000608 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000609 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
610 ios_base::iostate& __err, unsigned int& __v) const
611 {
612 return do_get(__b, __e, __iob, __err, __v);
613 }
614
Louis Dionne54238052018-07-11 23:14:33 +0000615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000616 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
617 ios_base::iostate& __err, unsigned long& __v) const
618 {
619 return do_get(__b, __e, __iob, __err, __v);
620 }
621
Louis Dionne54238052018-07-11 23:14:33 +0000622 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000623 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
624 ios_base::iostate& __err, unsigned long long& __v) const
625 {
626 return do_get(__b, __e, __iob, __err, __v);
627 }
628
Louis Dionne54238052018-07-11 23:14:33 +0000629 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000630 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
631 ios_base::iostate& __err, float& __v) const
632 {
633 return do_get(__b, __e, __iob, __err, __v);
634 }
635
Louis Dionne54238052018-07-11 23:14:33 +0000636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000637 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
638 ios_base::iostate& __err, double& __v) const
639 {
640 return do_get(__b, __e, __iob, __err, __v);
641 }
642
Louis Dionne54238052018-07-11 23:14:33 +0000643 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000644 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
645 ios_base::iostate& __err, long double& __v) const
646 {
647 return do_get(__b, __e, __iob, __err, __v);
648 }
649
Louis Dionne54238052018-07-11 23:14:33 +0000650 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000651 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
652 ios_base::iostate& __err, void*& __v) const
653 {
654 return do_get(__b, __e, __iob, __err, __v);
655 }
656
657 static locale::id id;
658
659protected:
Louis Dionne54238052018-07-11 23:14:33 +0000660 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000661 ~num_get() {}
662
Marshall Clow5ffe5912013-11-05 14:28:52 +0000663 template <class _Fp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000664 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
665 iter_type __do_get_floating_point
Marshall Clow5ffe5912013-11-05 14:28:52 +0000666 (iter_type __b, iter_type __e, ios_base& __iob,
667 ios_base::iostate& __err, _Fp& __v) const;
Marshall Clowfe2a5612013-11-07 01:00:50 +0000668
669 template <class _Signed>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000670 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
671 iter_type __do_get_signed
Marshall Clowfe2a5612013-11-07 01:00:50 +0000672 (iter_type __b, iter_type __e, ios_base& __iob,
673 ios_base::iostate& __err, _Signed& __v) const;
674
675 template <class _Unsigned>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000676 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
677 iter_type __do_get_unsigned
Marshall Clowfe2a5612013-11-07 01:00:50 +0000678 (iter_type __b, iter_type __e, ios_base& __iob,
679 ios_base::iostate& __err, _Unsigned& __v) const;
680
681
682 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
683 ios_base::iostate& __err, bool& __v) const;
684
685 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
686 ios_base::iostate& __err, long& __v) const
687 { return this->__do_get_signed ( __b, __e, __iob, __err, __v ); }
688
689 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
690 ios_base::iostate& __err, long long& __v) const
691 { return this->__do_get_signed ( __b, __e, __iob, __err, __v ); }
692
693 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
694 ios_base::iostate& __err, unsigned short& __v) const
695 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
696
697 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
698 ios_base::iostate& __err, unsigned int& __v) const
699 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
700
701 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
702 ios_base::iostate& __err, unsigned long& __v) const
703 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
704
705 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
706 ios_base::iostate& __err, unsigned long long& __v) const
707 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
708
709 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
710 ios_base::iostate& __err, float& __v) const
711 { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
712
713 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
714 ios_base::iostate& __err, double& __v) const
715 { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
716
717 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
718 ios_base::iostate& __err, long double& __v) const
719 { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
720
721 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
722 ios_base::iostate& __err, void*& __v) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723};
724
725template <class _CharT, class _InputIterator>
726locale::id
727num_get<_CharT, _InputIterator>::id;
728
729template <class _Tp>
730_Tp
731__num_get_signed_integral(const char* __a, const char* __a_end,
732 ios_base::iostate& __err, int __base)
733{
734 if (__a != __a_end)
735 {
Howard Hinnant54e2fff2013-01-22 17:26:08 +0000736 typename remove_reference<decltype(errno)>::type __save_errno = errno;
Howard Hinnante7c8da62011-02-25 19:52:41 +0000737 errno = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000738 char *__p2;
Howard Hinnant2ea1ca92011-09-28 21:05:01 +0000739 long long __ll = strtoll_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
Howard Hinnant54e2fff2013-01-22 17:26:08 +0000740 typename remove_reference<decltype(errno)>::type __current_errno = errno;
Howard Hinnante7c8da62011-02-25 19:52:41 +0000741 if (__current_errno == 0)
742 errno = __save_errno;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000743 if (__p2 != __a_end)
744 {
745 __err = ios_base::failbit;
746 return 0;
747 }
Howard Hinnante7c8da62011-02-25 19:52:41 +0000748 else if (__current_errno == ERANGE ||
749 __ll < numeric_limits<_Tp>::min() ||
750 numeric_limits<_Tp>::max() < __ll)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000751 {
752 __err = ios_base::failbit;
Howard Hinnante7c8da62011-02-25 19:52:41 +0000753 if (__ll > 0)
754 return numeric_limits<_Tp>::max();
755 else
756 return numeric_limits<_Tp>::min();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000757 }
758 return static_cast<_Tp>(__ll);
759 }
760 __err = ios_base::failbit;
761 return 0;
762}
763
764template <class _Tp>
765_Tp
766__num_get_unsigned_integral(const char* __a, const char* __a_end,
767 ios_base::iostate& __err, int __base)
768{
769 if (__a != __a_end)
770 {
Eric Fiselierf382e532018-03-29 01:18:53 +0000771 const bool __negate = *__a == '-';
772 if (__negate && ++__a == __a_end) {
773 __err = ios_base::failbit;
774 return 0;
Howard Hinnante7c8da62011-02-25 19:52:41 +0000775 }
Howard Hinnant54e2fff2013-01-22 17:26:08 +0000776 typename remove_reference<decltype(errno)>::type __save_errno = errno;
Howard Hinnante7c8da62011-02-25 19:52:41 +0000777 errno = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000778 char *__p2;
Howard Hinnant2ea1ca92011-09-28 21:05:01 +0000779 unsigned long long __ll = strtoull_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
Howard Hinnant54e2fff2013-01-22 17:26:08 +0000780 typename remove_reference<decltype(errno)>::type __current_errno = errno;
Howard Hinnante7c8da62011-02-25 19:52:41 +0000781 if (__current_errno == 0)
782 errno = __save_errno;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000783 if (__p2 != __a_end)
784 {
785 __err = ios_base::failbit;
786 return 0;
787 }
Eric Fiselierf382e532018-03-29 01:18:53 +0000788 else if (__current_errno == ERANGE || numeric_limits<_Tp>::max() < __ll)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000789 {
790 __err = ios_base::failbit;
791 return numeric_limits<_Tp>::max();
792 }
Eric Fiselierf382e532018-03-29 01:18:53 +0000793 _Tp __res = static_cast<_Tp>(__ll);
794 if (__negate) __res = -__res;
795 return __res;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000796 }
797 __err = ios_base::failbit;
798 return 0;
799}
800
801template <class _Tp>
Eric Fiselierfe6d50f2016-06-19 06:58:22 +0000802_LIBCPP_INLINE_VISIBILITY
803_Tp __do_strtod(const char* __a, char** __p2);
804
805template <>
806inline _LIBCPP_INLINE_VISIBILITY
807float __do_strtod<float>(const char* __a, char** __p2) {
808 return strtof_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
809}
810
811template <>
812inline _LIBCPP_INLINE_VISIBILITY
813double __do_strtod<double>(const char* __a, char** __p2) {
814 return strtod_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
815}
816
817template <>
818inline _LIBCPP_INLINE_VISIBILITY
819long double __do_strtod<long double>(const char* __a, char** __p2) {
820 return strtold_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
821}
822
823template <class _Tp>
Shoaib Meenai833ad542016-12-24 18:05:32 +0000824_LIBCPP_HIDDEN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000825_Tp
826__num_get_float(const char* __a, const char* __a_end, ios_base::iostate& __err)
827{
828 if (__a != __a_end)
829 {
Howard Hinnant4f671002013-04-13 18:19:25 +0000830 typename remove_reference<decltype(errno)>::type __save_errno = errno;
831 errno = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000832 char *__p2;
Eric Fiselierfe6d50f2016-06-19 06:58:22 +0000833 _Tp __ld = __do_strtod<_Tp>(__a, &__p2);
Howard Hinnant4f671002013-04-13 18:19:25 +0000834 typename remove_reference<decltype(errno)>::type __current_errno = errno;
835 if (__current_errno == 0)
836 errno = __save_errno;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000837 if (__p2 != __a_end)
838 {
839 __err = ios_base::failbit;
840 return 0;
841 }
Howard Hinnant4f671002013-04-13 18:19:25 +0000842 else if (__current_errno == ERANGE)
843 __err = ios_base::failbit;
Eric Fiselierfe6d50f2016-06-19 06:58:22 +0000844 return __ld;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000845 }
846 __err = ios_base::failbit;
847 return 0;
848}
849
850template <class _CharT, class _InputIterator>
851_InputIterator
852num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
853 ios_base& __iob,
854 ios_base::iostate& __err,
855 bool& __v) const
856{
857 if ((__iob.flags() & ios_base::boolalpha) == 0)
858 {
859 long __lv = -1;
860 __b = do_get(__b, __e, __iob, __err, __lv);
861 switch (__lv)
862 {
863 case 0:
864 __v = false;
865 break;
866 case 1:
867 __v = true;
868 break;
869 default:
870 __v = true;
871 __err = ios_base::failbit;
872 break;
873 }
874 return __b;
875 }
876 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__iob.getloc());
877 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__iob.getloc());
878 typedef typename numpunct<_CharT>::string_type string_type;
879 const string_type __names[2] = {__np.truename(), __np.falsename()};
880 const string_type* __i = __scan_keyword(__b, __e, __names, __names+2,
881 __ct, __err);
882 __v = __i == __names;
883 return __b;
884}
885
Marshall Clowfe2a5612013-11-07 01:00:50 +0000886// signed
887
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000888template <class _CharT, class _InputIterator>
Marshall Clowfe2a5612013-11-07 01:00:50 +0000889template <class _Signed>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000890_InputIterator
Marshall Clowfe2a5612013-11-07 01:00:50 +0000891num_get<_CharT, _InputIterator>::__do_get_signed(iter_type __b, iter_type __e,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000892 ios_base& __iob,
893 ios_base::iostate& __err,
Marshall Clowfe2a5612013-11-07 01:00:50 +0000894 _Signed& __v) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000895{
896 // Stage 1
897 int __base = this->__get_base(__iob);
898 // Stage 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000899 char_type __thousands_sep;
Aditya Kumaraa356d62017-06-14 23:17:45 +0000900 const int __atoms_size = 26;
901#ifdef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
902 char_type __atoms1[__atoms_size];
903 const char_type *__atoms = this->__do_widen(__iob, __atoms1);
904 string __grouping = this->__stage2_int_prep(__iob, __thousands_sep);
905#else
906 char_type __atoms[__atoms_size];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000907 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Aditya Kumaraa356d62017-06-14 23:17:45 +0000908#endif
Howard Hinnantae57a1a2013-04-15 20:40:06 +0000909 string __buf;
910 __buf.resize(__buf.capacity());
911 char* __a = &__buf[0];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000912 char* __a_end = __a;
913 unsigned __g[__num_get_base::__num_get_buf_sz];
914 unsigned* __g_end = __g;
915 unsigned __dc = 0;
916 for (; __b != __e; ++__b)
Howard Hinnantae57a1a2013-04-15 20:40:06 +0000917 {
Joerg Sonnenbergercf7278a2014-02-07 21:14:29 +0000918 if (__a_end == __a + __buf.size())
Howard Hinnantae57a1a2013-04-15 20:40:06 +0000919 {
920 size_t __tmp = __buf.size();
921 __buf.resize(2*__buf.size());
922 __buf.resize(__buf.capacity());
923 __a = &__buf[0];
924 __a_end = __a + __tmp;
925 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000926 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000927 __thousands_sep, __grouping, __g, __g_end,
928 __atoms))
929 break;
Howard Hinnantae57a1a2013-04-15 20:40:06 +0000930 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000931 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
932 *__g_end++ = __dc;
933 // Stage 3
Marshall Clowfe2a5612013-11-07 01:00:50 +0000934 __v = __num_get_signed_integral<_Signed>(__a, __a_end, __err, __base);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000935 // 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
Marshall Clowfe2a5612013-11-07 01:00:50 +0000943// unsigned
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000944
945template <class _CharT, class _InputIterator>
Marshall Clowfe2a5612013-11-07 01:00:50 +0000946template <class _Unsigned>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000947_InputIterator
Marshall Clowfe2a5612013-11-07 01:00:50 +0000948num_get<_CharT, _InputIterator>::__do_get_unsigned(iter_type __b, iter_type __e,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000949 ios_base& __iob,
950 ios_base::iostate& __err,
Marshall Clowfe2a5612013-11-07 01:00:50 +0000951 _Unsigned& __v) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000952{
953 // Stage 1
954 int __base = this->__get_base(__iob);
955 // Stage 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000956 char_type __thousands_sep;
Aditya Kumaraa356d62017-06-14 23:17:45 +0000957 const int __atoms_size = 26;
958#ifdef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
959 char_type __atoms1[__atoms_size];
960 const char_type *__atoms = this->__do_widen(__iob, __atoms1);
961 string __grouping = this->__stage2_int_prep(__iob, __thousands_sep);
962#else
963 char_type __atoms[__atoms_size];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000964 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Aditya Kumaraa356d62017-06-14 23:17:45 +0000965#endif
Howard Hinnantae57a1a2013-04-15 20:40:06 +0000966 string __buf;
967 __buf.resize(__buf.capacity());
968 char* __a = &__buf[0];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000969 char* __a_end = __a;
970 unsigned __g[__num_get_base::__num_get_buf_sz];
971 unsigned* __g_end = __g;
972 unsigned __dc = 0;
973 for (; __b != __e; ++__b)
Howard Hinnantae57a1a2013-04-15 20:40:06 +0000974 {
Joerg Sonnenbergercf7278a2014-02-07 21:14:29 +0000975 if (__a_end == __a + __buf.size())
Howard Hinnantae57a1a2013-04-15 20:40:06 +0000976 {
977 size_t __tmp = __buf.size();
978 __buf.resize(2*__buf.size());
979 __buf.resize(__buf.capacity());
980 __a = &__buf[0];
981 __a_end = __a + __tmp;
982 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000983 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000984 __thousands_sep, __grouping, __g, __g_end,
985 __atoms))
986 break;
Howard Hinnantae57a1a2013-04-15 20:40:06 +0000987 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000988 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
989 *__g_end++ = __dc;
990 // Stage 3
Marshall Clowfe2a5612013-11-07 01:00:50 +0000991 __v = __num_get_unsigned_integral<_Unsigned>(__a, __a_end, __err, __base);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000992 // Digit grouping checked
993 __check_grouping(__grouping, __g, __g_end, __err);
994 // EOF checked
995 if (__b == __e)
996 __err |= ios_base::eofbit;
997 return __b;
998}
999
Marshall Clow5ffe5912013-11-05 14:28:52 +00001000// floating point
1001
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001002template <class _CharT, class _InputIterator>
Marshall Clow5ffe5912013-11-05 14:28:52 +00001003template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001004_InputIterator
Marshall Clow5ffe5912013-11-05 14:28:52 +00001005num_get<_CharT, _InputIterator>::__do_get_floating_point(iter_type __b, iter_type __e,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001006 ios_base& __iob,
1007 ios_base::iostate& __err,
Marshall Clow5ffe5912013-11-05 14:28:52 +00001008 _Fp& __v) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001009{
1010 // Stage 1, nothing to do
1011 // Stage 2
1012 char_type __atoms[32];
1013 char_type __decimal_point;
1014 char_type __thousands_sep;
Howard Hinnant324bb032010-08-22 00:02:43 +00001015 string __grouping = this->__stage2_float_prep(__iob, __atoms,
1016 __decimal_point,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001017 __thousands_sep);
Howard Hinnantae57a1a2013-04-15 20:40:06 +00001018 string __buf;
1019 __buf.resize(__buf.capacity());
1020 char* __a = &__buf[0];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001021 char* __a_end = __a;
1022 unsigned __g[__num_get_base::__num_get_buf_sz];
1023 unsigned* __g_end = __g;
1024 unsigned __dc = 0;
1025 bool __in_units = true;
1026 char __exp = 'E';
1027 for (; __b != __e; ++__b)
Howard Hinnantae57a1a2013-04-15 20:40:06 +00001028 {
Joerg Sonnenbergercf7278a2014-02-07 21:14:29 +00001029 if (__a_end == __a + __buf.size())
Howard Hinnantae57a1a2013-04-15 20:40:06 +00001030 {
1031 size_t __tmp = __buf.size();
1032 __buf.resize(2*__buf.size());
1033 __buf.resize(__buf.capacity());
1034 __a = &__buf[0];
1035 __a_end = __a + __tmp;
1036 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001037 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1038 __decimal_point, __thousands_sep,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001039 __grouping, __g, __g_end,
1040 __dc, __atoms))
1041 break;
Howard Hinnantae57a1a2013-04-15 20:40:06 +00001042 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001043 if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1044 *__g_end++ = __dc;
1045 // Stage 3
Marshall Clow5ffe5912013-11-05 14:28:52 +00001046 __v = __num_get_float<_Fp>(__a, __a_end, __err);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001047 // Digit grouping checked
1048 __check_grouping(__grouping, __g, __g_end, __err);
1049 // EOF checked
1050 if (__b == __e)
1051 __err |= ios_base::eofbit;
1052 return __b;
1053}
1054
1055template <class _CharT, class _InputIterator>
1056_InputIterator
1057num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1058 ios_base& __iob,
1059 ios_base::iostate& __err,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001060 void*& __v) const
1061{
1062 // Stage 1
1063 int __base = 16;
1064 // Stage 2
1065 char_type __atoms[26];
Howard Hinnantec3773c2011-12-01 20:21:04 +00001066 char_type __thousands_sep = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001067 string __grouping;
1068 use_facet<ctype<_CharT> >(__iob.getloc()).widen(__num_get_base::__src,
1069 __num_get_base::__src + 26, __atoms);
Howard Hinnantae57a1a2013-04-15 20:40:06 +00001070 string __buf;
1071 __buf.resize(__buf.capacity());
1072 char* __a = &__buf[0];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001073 char* __a_end = __a;
1074 unsigned __g[__num_get_base::__num_get_buf_sz];
1075 unsigned* __g_end = __g;
1076 unsigned __dc = 0;
1077 for (; __b != __e; ++__b)
Howard Hinnantae57a1a2013-04-15 20:40:06 +00001078 {
Joerg Sonnenbergercf7278a2014-02-07 21:14:29 +00001079 if (__a_end == __a + __buf.size())
Howard Hinnantae57a1a2013-04-15 20:40:06 +00001080 {
1081 size_t __tmp = __buf.size();
1082 __buf.resize(2*__buf.size());
1083 __buf.resize(__buf.capacity());
1084 __a = &__buf[0];
1085 __a_end = __a + __tmp;
1086 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001087 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1088 __thousands_sep, __grouping,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001089 __g, __g_end, __atoms))
1090 break;
Howard Hinnantae57a1a2013-04-15 20:40:06 +00001091 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001092 // Stage 3
Marshall Clow117563c2014-05-21 16:02:20 +00001093 __buf.resize(__a_end - __a);
Ben Craigfd556582016-03-09 15:39:39 +00001094 if (__libcpp_sscanf_l(__buf.c_str(), _LIBCPP_GET_C_LOCALE, "%p", &__v) != 1)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001095 __err = ios_base::failbit;
1096 // EOF checked
1097 if (__b == __e)
1098 __err |= ios_base::eofbit;
1099 return __b;
1100}
1101
Eric Fiselier833d6442016-09-15 22:27:07 +00001102_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<char>)
1103_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001104
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001105struct _LIBCPP_TYPE_VIS __num_put_base
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001106{
1107protected:
1108 static void __format_int(char* __fmt, const char* __len, bool __signd,
1109 ios_base::fmtflags __flags);
1110 static bool __format_float(char* __fmt, const char* __len,
1111 ios_base::fmtflags __flags);
1112 static char* __identify_padding(char* __nb, char* __ne,
1113 const ios_base& __iob);
1114};
1115
1116template <class _CharT>
1117struct __num_put
1118 : protected __num_put_base
1119{
1120 static void __widen_and_group_int(char* __nb, char* __np, char* __ne,
1121 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1122 const locale& __loc);
1123 static void __widen_and_group_float(char* __nb, char* __np, char* __ne,
1124 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1125 const locale& __loc);
1126};
1127
1128template <class _CharT>
1129void
1130__num_put<_CharT>::__widen_and_group_int(char* __nb, char* __np, char* __ne,
1131 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1132 const locale& __loc)
1133{
1134 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1135 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1136 string __grouping = __npt.grouping();
1137 if (__grouping.empty())
1138 {
1139 __ct.widen(__nb, __ne, __ob);
1140 __oe = __ob + (__ne - __nb);
1141 }
1142 else
1143 {
1144 __oe = __ob;
1145 char* __nf = __nb;
1146 if (*__nf == '-' || *__nf == '+')
1147 *__oe++ = __ct.widen(*__nf++);
1148 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1149 __nf[1] == 'X'))
1150 {
1151 *__oe++ = __ct.widen(*__nf++);
1152 *__oe++ = __ct.widen(*__nf++);
1153 }
1154 reverse(__nf, __ne);
1155 _CharT __thousands_sep = __npt.thousands_sep();
1156 unsigned __dc = 0;
1157 unsigned __dg = 0;
1158 for (char* __p = __nf; __p < __ne; ++__p)
1159 {
1160 if (static_cast<unsigned>(__grouping[__dg]) > 0 &&
1161 __dc == static_cast<unsigned>(__grouping[__dg]))
1162 {
1163 *__oe++ = __thousands_sep;
1164 __dc = 0;
1165 if (__dg < __grouping.size()-1)
1166 ++__dg;
1167 }
1168 *__oe++ = __ct.widen(*__p);
1169 ++__dc;
1170 }
1171 reverse(__ob + (__nf - __nb), __oe);
1172 }
1173 if (__np == __ne)
1174 __op = __oe;
1175 else
1176 __op = __ob + (__np - __nb);
1177}
1178
1179template <class _CharT>
1180void
1181__num_put<_CharT>::__widen_and_group_float(char* __nb, char* __np, char* __ne,
1182 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1183 const locale& __loc)
1184{
1185 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1186 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1187 string __grouping = __npt.grouping();
1188 __oe = __ob;
1189 char* __nf = __nb;
1190 if (*__nf == '-' || *__nf == '+')
1191 *__oe++ = __ct.widen(*__nf++);
1192 char* __ns;
1193 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1194 __nf[1] == 'X'))
1195 {
1196 *__oe++ = __ct.widen(*__nf++);
1197 *__oe++ = __ct.widen(*__nf++);
1198 for (__ns = __nf; __ns < __ne; ++__ns)
Howard Hinnant2ea1ca92011-09-28 21:05:01 +00001199 if (!isxdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001200 break;
1201 }
1202 else
1203 {
1204 for (__ns = __nf; __ns < __ne; ++__ns)
Howard Hinnant2ea1ca92011-09-28 21:05:01 +00001205 if (!isdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001206 break;
1207 }
1208 if (__grouping.empty())
1209 {
1210 __ct.widen(__nf, __ns, __oe);
1211 __oe += __ns - __nf;
1212 }
1213 else
1214 {
1215 reverse(__nf, __ns);
1216 _CharT __thousands_sep = __npt.thousands_sep();
1217 unsigned __dc = 0;
1218 unsigned __dg = 0;
1219 for (char* __p = __nf; __p < __ns; ++__p)
1220 {
1221 if (__grouping[__dg] > 0 && __dc == static_cast<unsigned>(__grouping[__dg]))
1222 {
1223 *__oe++ = __thousands_sep;
1224 __dc = 0;
1225 if (__dg < __grouping.size()-1)
1226 ++__dg;
1227 }
1228 *__oe++ = __ct.widen(*__p);
1229 ++__dc;
1230 }
1231 reverse(__ob + (__nf - __nb), __oe);
1232 }
1233 for (__nf = __ns; __nf < __ne; ++__nf)
1234 {
1235 if (*__nf == '.')
1236 {
1237 *__oe++ = __npt.decimal_point();
1238 ++__nf;
1239 break;
1240 }
1241 else
1242 *__oe++ = __ct.widen(*__nf);
1243 }
1244 __ct.widen(__nf, __ne, __oe);
1245 __oe += __ne - __nf;
1246 if (__np == __ne)
1247 __op = __oe;
1248 else
1249 __op = __ob + (__np - __nb);
1250}
1251
Eric Fiselier833d6442016-09-15 22:27:07 +00001252_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<char>)
1253_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001254
1255template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Eric Fiselierc3589a82017-01-04 23:56:00 +00001256class _LIBCPP_TEMPLATE_VIS num_put
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001257 : public locale::facet,
1258 private __num_put<_CharT>
1259{
1260public:
1261 typedef _CharT char_type;
1262 typedef _OutputIterator iter_type;
1263
Louis Dionne54238052018-07-11 23:14:33 +00001264 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001265 explicit num_put(size_t __refs = 0)
1266 : locale::facet(__refs) {}
1267
Louis Dionne54238052018-07-11 23:14:33 +00001268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001269 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1270 bool __v) const
1271 {
1272 return do_put(__s, __iob, __fl, __v);
1273 }
1274
Louis Dionne54238052018-07-11 23:14:33 +00001275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001276 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1277 long __v) const
1278 {
1279 return do_put(__s, __iob, __fl, __v);
1280 }
1281
Louis Dionne54238052018-07-11 23:14:33 +00001282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001283 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1284 long long __v) const
1285 {
1286 return do_put(__s, __iob, __fl, __v);
1287 }
1288
Louis Dionne54238052018-07-11 23:14:33 +00001289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001290 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1291 unsigned long __v) const
1292 {
1293 return do_put(__s, __iob, __fl, __v);
1294 }
1295
Louis Dionne54238052018-07-11 23:14:33 +00001296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001297 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1298 unsigned long long __v) const
1299 {
1300 return do_put(__s, __iob, __fl, __v);
1301 }
1302
Louis Dionne54238052018-07-11 23:14:33 +00001303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001304 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1305 double __v) const
1306 {
1307 return do_put(__s, __iob, __fl, __v);
1308 }
1309
Louis Dionne54238052018-07-11 23:14:33 +00001310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001311 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1312 long double __v) const
1313 {
1314 return do_put(__s, __iob, __fl, __v);
1315 }
1316
Louis Dionne54238052018-07-11 23:14:33 +00001317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001318 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1319 const void* __v) const
1320 {
1321 return do_put(__s, __iob, __fl, __v);
1322 }
1323
1324 static locale::id id;
1325
1326protected:
Louis Dionne54238052018-07-11 23:14:33 +00001327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001328 ~num_put() {}
1329
1330 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1331 bool __v) const;
1332 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1333 long __v) const;
1334 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1335 long long __v) const;
1336 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1337 unsigned long) const;
1338 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1339 unsigned long long) const;
1340 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1341 double __v) const;
1342 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1343 long double __v) const;
1344 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1345 const void* __v) const;
1346};
1347
1348template <class _CharT, class _OutputIterator>
1349locale::id
1350num_put<_CharT, _OutputIterator>::id;
1351
1352template <class _CharT, class _OutputIterator>
1353_LIBCPP_HIDDEN
1354_OutputIterator
1355__pad_and_output(_OutputIterator __s,
1356 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1357 ios_base& __iob, _CharT __fl)
1358{
1359 streamsize __sz = __oe - __ob;
1360 streamsize __ns = __iob.width();
1361 if (__ns > __sz)
1362 __ns -= __sz;
1363 else
1364 __ns = 0;
1365 for (;__ob < __op; ++__ob, ++__s)
1366 *__s = *__ob;
1367 for (; __ns; --__ns, ++__s)
1368 *__s = __fl;
1369 for (; __ob < __oe; ++__ob, ++__s)
1370 *__s = *__ob;
1371 __iob.width(0);
1372 return __s;
1373}
1374
Howard Hinnant537b2fa2012-11-14 21:17:15 +00001375#if !defined(__APPLE__) || \
1376 (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED > __MAC_10_8) || \
1377 (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_6_0)
1378
Howard Hinnanta585de62012-09-19 19:14:15 +00001379template <class _CharT, class _Traits>
1380_LIBCPP_HIDDEN
1381ostreambuf_iterator<_CharT, _Traits>
1382__pad_and_output(ostreambuf_iterator<_CharT, _Traits> __s,
1383 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1384 ios_base& __iob, _CharT __fl)
1385{
1386 if (__s.__sbuf_ == nullptr)
1387 return __s;
1388 streamsize __sz = __oe - __ob;
1389 streamsize __ns = __iob.width();
1390 if (__ns > __sz)
1391 __ns -= __sz;
1392 else
1393 __ns = 0;
1394 streamsize __np = __op - __ob;
1395 if (__np > 0)
1396 {
1397 if (__s.__sbuf_->sputn(__ob, __np) != __np)
1398 {
1399 __s.__sbuf_ = nullptr;
1400 return __s;
1401 }
1402 }
1403 if (__ns > 0)
1404 {
1405 basic_string<_CharT, _Traits> __sp(__ns, __fl);
1406 if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns)
1407 {
1408 __s.__sbuf_ = nullptr;
1409 return __s;
1410 }
1411 }
1412 __np = __oe - __op;
1413 if (__np > 0)
1414 {
1415 if (__s.__sbuf_->sputn(__op, __np) != __np)
1416 {
1417 __s.__sbuf_ = nullptr;
1418 return __s;
1419 }
1420 }
1421 __iob.width(0);
1422 return __s;
1423}
1424
Howard Hinnant537b2fa2012-11-14 21:17:15 +00001425#endif
1426
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001427template <class _CharT, class _OutputIterator>
1428_OutputIterator
1429num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1430 char_type __fl, bool __v) const
1431{
1432 if ((__iob.flags() & ios_base::boolalpha) == 0)
1433 return do_put(__s, __iob, __fl, (unsigned long)__v);
1434 const numpunct<char_type>& __np = use_facet<numpunct<char_type> >(__iob.getloc());
1435 typedef typename numpunct<char_type>::string_type string_type;
Howard Hinnant499cea12013-08-23 17:37:05 +00001436#if _LIBCPP_DEBUG_LEVEL >= 2
1437 string_type __tmp(__v ? __np.truename() : __np.falsename());
1438 string_type __nm = _VSTD::move(__tmp);
1439#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001440 string_type __nm = __v ? __np.truename() : __np.falsename();
Howard Hinnant499cea12013-08-23 17:37:05 +00001441#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001442 for (typename string_type::iterator __i = __nm.begin(); __i != __nm.end(); ++__i, ++__s)
1443 *__s = *__i;
1444 return __s;
1445}
1446
1447template <class _CharT, class _OutputIterator>
1448_OutputIterator
1449num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1450 char_type __fl, long __v) const
1451{
1452 // Stage 1 - Get number in narrow char
1453 char __fmt[6] = {'%', 0};
1454 const char* __len = "l";
1455 this->__format_int(__fmt+1, __len, true, __iob.flags());
1456 const unsigned __nbuf = (numeric_limits<long>::digits / 3)
1457 + ((numeric_limits<long>::digits % 3) != 0)
Dimitry Andric2f214592017-05-06 20:58:50 +00001458 + ((__iob.flags() & ios_base::showbase) != 0)
Eric Fiselier59af3f82016-04-29 07:23:20 +00001459 + 2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001460 char __nar[__nbuf];
Ben Craigfd556582016-03-09 15:39:39 +00001461 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001462 char* __ne = __nar + __nc;
1463 char* __np = this->__identify_padding(__nar, __ne, __iob);
1464 // Stage 2 - Widen __nar while adding thousands separators
1465 char_type __o[2*(__nbuf-1) - 1];
1466 char_type* __op; // pad here
1467 char_type* __oe; // end of output
1468 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1469 // [__o, __oe) contains thousands_sep'd wide number
1470 // Stage 3 & 4
1471 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1472}
1473
1474template <class _CharT, class _OutputIterator>
1475_OutputIterator
1476num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1477 char_type __fl, long long __v) const
1478{
1479 // Stage 1 - Get number in narrow char
1480 char __fmt[8] = {'%', 0};
1481 const char* __len = "ll";
1482 this->__format_int(__fmt+1, __len, true, __iob.flags());
1483 const unsigned __nbuf = (numeric_limits<long long>::digits / 3)
1484 + ((numeric_limits<long long>::digits % 3) != 0)
Dimitry Andric2f214592017-05-06 20:58:50 +00001485 + ((__iob.flags() & ios_base::showbase) != 0)
Marshall Clowb9bf4a22015-01-26 17:24:52 +00001486 + 2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001487 char __nar[__nbuf];
Ben Craigfd556582016-03-09 15:39:39 +00001488 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001489 char* __ne = __nar + __nc;
1490 char* __np = this->__identify_padding(__nar, __ne, __iob);
1491 // Stage 2 - Widen __nar while adding thousands separators
1492 char_type __o[2*(__nbuf-1) - 1];
1493 char_type* __op; // pad here
1494 char_type* __oe; // end of output
1495 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1496 // [__o, __oe) contains thousands_sep'd wide number
1497 // Stage 3 & 4
1498 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1499}
1500
1501template <class _CharT, class _OutputIterator>
1502_OutputIterator
1503num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1504 char_type __fl, unsigned long __v) const
1505{
1506 // Stage 1 - Get number in narrow char
1507 char __fmt[6] = {'%', 0};
1508 const char* __len = "l";
1509 this->__format_int(__fmt+1, __len, false, __iob.flags());
1510 const unsigned __nbuf = (numeric_limits<unsigned long>::digits / 3)
1511 + ((numeric_limits<unsigned long>::digits % 3) != 0)
Dimitry Andric2f214592017-05-06 20:58:50 +00001512 + ((__iob.flags() & ios_base::showbase) != 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001513 + 1;
1514 char __nar[__nbuf];
Ben Craigfd556582016-03-09 15:39:39 +00001515 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001516 char* __ne = __nar + __nc;
1517 char* __np = this->__identify_padding(__nar, __ne, __iob);
1518 // Stage 2 - Widen __nar while adding thousands separators
1519 char_type __o[2*(__nbuf-1) - 1];
1520 char_type* __op; // pad here
1521 char_type* __oe; // end of output
1522 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1523 // [__o, __oe) contains thousands_sep'd wide number
1524 // Stage 3 & 4
1525 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1526}
1527
1528template <class _CharT, class _OutputIterator>
1529_OutputIterator
1530num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1531 char_type __fl, unsigned long long __v) const
1532{
1533 // Stage 1 - Get number in narrow char
1534 char __fmt[8] = {'%', 0};
1535 const char* __len = "ll";
1536 this->__format_int(__fmt+1, __len, false, __iob.flags());
1537 const unsigned __nbuf = (numeric_limits<unsigned long long>::digits / 3)
1538 + ((numeric_limits<unsigned long long>::digits % 3) != 0)
Dimitry Andric2f214592017-05-06 20:58:50 +00001539 + ((__iob.flags() & ios_base::showbase) != 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001540 + 1;
1541 char __nar[__nbuf];
Ben Craigfd556582016-03-09 15:39:39 +00001542 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001543 char* __ne = __nar + __nc;
1544 char* __np = this->__identify_padding(__nar, __ne, __iob);
1545 // Stage 2 - Widen __nar while adding thousands separators
1546 char_type __o[2*(__nbuf-1) - 1];
1547 char_type* __op; // pad here
1548 char_type* __oe; // end of output
1549 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1550 // [__o, __oe) contains thousands_sep'd wide number
1551 // Stage 3 & 4
1552 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1553}
1554
1555template <class _CharT, class _OutputIterator>
1556_OutputIterator
1557num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1558 char_type __fl, double __v) const
1559{
1560 // Stage 1 - Get number in narrow char
1561 char __fmt[8] = {'%', 0};
1562 const char* __len = "";
1563 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1564 const unsigned __nbuf = 30;
1565 char __nar[__nbuf];
1566 char* __nb = __nar;
1567 int __nc;
1568 if (__specify_precision)
Ben Craigfd556582016-03-09 15:39:39 +00001569 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
Howard Hinnantadff4892010-05-24 17:49:41 +00001570 (int)__iob.precision(), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001571 else
Ben Craigfd556582016-03-09 15:39:39 +00001572 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001573 unique_ptr<char, void(*)(void*)> __nbh(0, free);
1574 if (__nc > static_cast<int>(__nbuf-1))
1575 {
1576 if (__specify_precision)
Ben Craigfd556582016-03-09 15:39:39 +00001577 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001578 else
Ben Craigfd556582016-03-09 15:39:39 +00001579 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001580 if (__nb == 0)
1581 __throw_bad_alloc();
1582 __nbh.reset(__nb);
1583 }
1584 char* __ne = __nb + __nc;
1585 char* __np = this->__identify_padding(__nb, __ne, __iob);
1586 // Stage 2 - Widen __nar while adding thousands separators
1587 char_type __o[2*(__nbuf-1) - 1];
1588 char_type* __ob = __o;
1589 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1590 if (__nb != __nar)
1591 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00001592 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001593 if (__ob == 0)
1594 __throw_bad_alloc();
1595 __obh.reset(__ob);
1596 }
1597 char_type* __op; // pad here
1598 char_type* __oe; // end of output
1599 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1600 // [__o, __oe) contains thousands_sep'd wide number
1601 // Stage 3 & 4
1602 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1603 return __s;
1604}
1605
1606template <class _CharT, class _OutputIterator>
1607_OutputIterator
1608num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1609 char_type __fl, long double __v) const
1610{
1611 // Stage 1 - Get number in narrow char
1612 char __fmt[8] = {'%', 0};
1613 const char* __len = "L";
1614 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1615 const unsigned __nbuf = 30;
1616 char __nar[__nbuf];
1617 char* __nb = __nar;
1618 int __nc;
1619 if (__specify_precision)
Ben Craigfd556582016-03-09 15:39:39 +00001620 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
Howard Hinnantadff4892010-05-24 17:49:41 +00001621 (int)__iob.precision(), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001622 else
Ben Craigfd556582016-03-09 15:39:39 +00001623 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001624 unique_ptr<char, void(*)(void*)> __nbh(0, free);
1625 if (__nc > static_cast<int>(__nbuf-1))
1626 {
1627 if (__specify_precision)
Ben Craigfd556582016-03-09 15:39:39 +00001628 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001629 else
Ben Craigfd556582016-03-09 15:39:39 +00001630 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001631 if (__nb == 0)
1632 __throw_bad_alloc();
1633 __nbh.reset(__nb);
1634 }
1635 char* __ne = __nb + __nc;
1636 char* __np = this->__identify_padding(__nb, __ne, __iob);
1637 // Stage 2 - Widen __nar while adding thousands separators
1638 char_type __o[2*(__nbuf-1) - 1];
1639 char_type* __ob = __o;
1640 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1641 if (__nb != __nar)
1642 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00001643 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001644 if (__ob == 0)
1645 __throw_bad_alloc();
1646 __obh.reset(__ob);
1647 }
1648 char_type* __op; // pad here
1649 char_type* __oe; // end of output
1650 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1651 // [__o, __oe) contains thousands_sep'd wide number
1652 // Stage 3 & 4
1653 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1654 return __s;
1655}
1656
1657template <class _CharT, class _OutputIterator>
1658_OutputIterator
1659num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1660 char_type __fl, const void* __v) const
1661{
1662 // Stage 1 - Get pointer in narrow char
1663 char __fmt[6] = "%p";
1664 const unsigned __nbuf = 20;
1665 char __nar[__nbuf];
Ben Craigfd556582016-03-09 15:39:39 +00001666 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001667 char* __ne = __nar + __nc;
1668 char* __np = this->__identify_padding(__nar, __ne, __iob);
1669 // Stage 2 - Widen __nar
1670 char_type __o[2*(__nbuf-1) - 1];
1671 char_type* __op; // pad here
1672 char_type* __oe; // end of output
1673 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
1674 __ct.widen(__nar, __ne, __o);
1675 __oe = __o + (__ne - __nar);
1676 if (__np == __ne)
1677 __op = __oe;
1678 else
1679 __op = __o + (__np - __nar);
1680 // [__o, __oe) contains wide number
1681 // Stage 3 & 4
1682 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1683}
1684
Eric Fiselier833d6442016-09-15 22:27:07 +00001685_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<char>)
1686_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001687
1688template <class _CharT, class _InputIterator>
1689_LIBCPP_HIDDEN
1690int
1691__get_up_to_n_digits(_InputIterator& __b, _InputIterator __e,
1692 ios_base::iostate& __err, const ctype<_CharT>& __ct, int __n)
1693{
1694 // Precondition: __n >= 1
1695 if (__b == __e)
1696 {
1697 __err |= ios_base::eofbit | ios_base::failbit;
1698 return 0;
1699 }
1700 // get first digit
1701 _CharT __c = *__b;
1702 if (!__ct.is(ctype_base::digit, __c))
1703 {
1704 __err |= ios_base::failbit;
1705 return 0;
1706 }
1707 int __r = __ct.narrow(__c, 0) - '0';
Eric Fiselierb9919752014-10-27 19:28:20 +00001708 for (++__b, (void) --__n; __b != __e && __n > 0; ++__b, (void) --__n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001709 {
1710 // get next digit
1711 __c = *__b;
1712 if (!__ct.is(ctype_base::digit, __c))
1713 return __r;
1714 __r = __r * 10 + __ct.narrow(__c, 0) - '0';
1715 }
1716 if (__b == __e)
1717 __err |= ios_base::eofbit;
1718 return __r;
1719}
1720
Howard Hinnant83eade62013-03-06 23:30:19 +00001721class _LIBCPP_TYPE_VIS time_base
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001722{
1723public:
1724 enum dateorder {no_order, dmy, mdy, ymd, ydm};
1725};
1726
1727template <class _CharT>
Eric Fiselierc3589a82017-01-04 23:56:00 +00001728class _LIBCPP_TEMPLATE_VIS __time_get_c_storage
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001729{
1730protected:
1731 typedef basic_string<_CharT> string_type;
1732
1733 virtual const string_type* __weeks() const;
1734 virtual const string_type* __months() const;
1735 virtual const string_type* __am_pm() const;
1736 virtual const string_type& __c() const;
1737 virtual const string_type& __r() const;
1738 virtual const string_type& __x() const;
1739 virtual const string_type& __X() const;
Eric Fiselier85d45f62015-08-18 19:39:35 +00001740
Louis Dionne54238052018-07-11 23:14:33 +00001741 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier85d45f62015-08-18 19:39:35 +00001742 ~__time_get_c_storage() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001743};
1744
Eric Fiselierc5a600a2017-05-08 00:29:32 +00001745template <> _LIBCPP_FUNC_VIS const string* __time_get_c_storage<char>::__weeks() const;
1746template <> _LIBCPP_FUNC_VIS const string* __time_get_c_storage<char>::__months() const;
1747template <> _LIBCPP_FUNC_VIS const string* __time_get_c_storage<char>::__am_pm() const;
1748template <> _LIBCPP_FUNC_VIS const string& __time_get_c_storage<char>::__c() const;
1749template <> _LIBCPP_FUNC_VIS const string& __time_get_c_storage<char>::__r() const;
1750template <> _LIBCPP_FUNC_VIS const string& __time_get_c_storage<char>::__x() const;
1751template <> _LIBCPP_FUNC_VIS const string& __time_get_c_storage<char>::__X() const;
1752
1753template <> _LIBCPP_FUNC_VIS const wstring* __time_get_c_storage<wchar_t>::__weeks() const;
1754template <> _LIBCPP_FUNC_VIS const wstring* __time_get_c_storage<wchar_t>::__months() const;
1755template <> _LIBCPP_FUNC_VIS const wstring* __time_get_c_storage<wchar_t>::__am_pm() const;
1756template <> _LIBCPP_FUNC_VIS const wstring& __time_get_c_storage<wchar_t>::__c() const;
1757template <> _LIBCPP_FUNC_VIS const wstring& __time_get_c_storage<wchar_t>::__r() const;
1758template <> _LIBCPP_FUNC_VIS const wstring& __time_get_c_storage<wchar_t>::__x() const;
1759template <> _LIBCPP_FUNC_VIS const wstring& __time_get_c_storage<wchar_t>::__X() const;
1760
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001761template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Eric Fiselierc3589a82017-01-04 23:56:00 +00001762class _LIBCPP_TEMPLATE_VIS time_get
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001763 : public locale::facet,
1764 public time_base,
1765 private __time_get_c_storage<_CharT>
1766{
1767public:
1768 typedef _CharT char_type;
1769 typedef _InputIterator iter_type;
1770 typedef time_base::dateorder dateorder;
1771 typedef basic_string<char_type> string_type;
1772
Louis Dionne54238052018-07-11 23:14:33 +00001773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001774 explicit time_get(size_t __refs = 0)
1775 : locale::facet(__refs) {}
1776
Louis Dionne54238052018-07-11 23:14:33 +00001777 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001778 dateorder date_order() const
1779 {
1780 return this->do_date_order();
1781 }
1782
Louis Dionne54238052018-07-11 23:14:33 +00001783 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001784 iter_type get_time(iter_type __b, iter_type __e, ios_base& __iob,
1785 ios_base::iostate& __err, tm* __tm) const
1786 {
1787 return do_get_time(__b, __e, __iob, __err, __tm);
1788 }
1789
Louis Dionne54238052018-07-11 23:14:33 +00001790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001791 iter_type get_date(iter_type __b, iter_type __e, ios_base& __iob,
1792 ios_base::iostate& __err, tm* __tm) const
1793 {
1794 return do_get_date(__b, __e, __iob, __err, __tm);
1795 }
1796
Louis Dionne54238052018-07-11 23:14:33 +00001797 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001798 iter_type get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
1799 ios_base::iostate& __err, tm* __tm) const
1800 {
1801 return do_get_weekday(__b, __e, __iob, __err, __tm);
1802 }
1803
Louis Dionne54238052018-07-11 23:14:33 +00001804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001805 iter_type get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
1806 ios_base::iostate& __err, tm* __tm) const
1807 {
1808 return do_get_monthname(__b, __e, __iob, __err, __tm);
1809 }
1810
Louis Dionne54238052018-07-11 23:14:33 +00001811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001812 iter_type get_year(iter_type __b, iter_type __e, ios_base& __iob,
1813 ios_base::iostate& __err, tm* __tm) const
1814 {
1815 return do_get_year(__b, __e, __iob, __err, __tm);
1816 }
1817
Louis Dionne54238052018-07-11 23:14:33 +00001818 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001819 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
1820 ios_base::iostate& __err, tm *__tm,
1821 char __fmt, char __mod = 0) const
1822 {
1823 return do_get(__b, __e, __iob, __err, __tm, __fmt, __mod);
1824 }
1825
1826 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
1827 ios_base::iostate& __err, tm* __tm,
1828 const char_type* __fmtb, const char_type* __fmte) const;
1829
1830 static locale::id id;
1831
1832protected:
Louis Dionne54238052018-07-11 23:14:33 +00001833 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001834 ~time_get() {}
1835
1836 virtual dateorder do_date_order() const;
1837 virtual iter_type do_get_time(iter_type __b, iter_type __e, ios_base& __iob,
1838 ios_base::iostate& __err, tm* __tm) const;
1839 virtual iter_type do_get_date(iter_type __b, iter_type __e, ios_base& __iob,
1840 ios_base::iostate& __err, tm* __tm) const;
1841 virtual iter_type do_get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
1842 ios_base::iostate& __err, tm* __tm) const;
1843 virtual iter_type do_get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
1844 ios_base::iostate& __err, tm* __tm) const;
1845 virtual iter_type do_get_year(iter_type __b, iter_type __e, ios_base& __iob,
1846 ios_base::iostate& __err, tm* __tm) const;
1847 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
1848 ios_base::iostate& __err, tm* __tm,
1849 char __fmt, char __mod) const;
1850private:
1851 void __get_white_space(iter_type& __b, iter_type __e,
1852 ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1853 void __get_percent(iter_type& __b, iter_type __e, ios_base::iostate& __err,
1854 const ctype<char_type>& __ct) const;
1855
1856 void __get_weekdayname(int& __m,
1857 iter_type& __b, iter_type __e,
1858 ios_base::iostate& __err,
1859 const ctype<char_type>& __ct) const;
1860 void __get_monthname(int& __m,
1861 iter_type& __b, iter_type __e,
1862 ios_base::iostate& __err,
1863 const ctype<char_type>& __ct) const;
1864 void __get_day(int& __d,
1865 iter_type& __b, iter_type __e,
1866 ios_base::iostate& __err,
1867 const ctype<char_type>& __ct) const;
1868 void __get_month(int& __m,
1869 iter_type& __b, iter_type __e,
1870 ios_base::iostate& __err,
1871 const ctype<char_type>& __ct) const;
1872 void __get_year(int& __y,
1873 iter_type& __b, iter_type __e,
1874 ios_base::iostate& __err,
1875 const ctype<char_type>& __ct) const;
1876 void __get_year4(int& __y,
1877 iter_type& __b, iter_type __e,
1878 ios_base::iostate& __err,
1879 const ctype<char_type>& __ct) const;
1880 void __get_hour(int& __d,
1881 iter_type& __b, iter_type __e,
1882 ios_base::iostate& __err,
1883 const ctype<char_type>& __ct) const;
1884 void __get_12_hour(int& __h,
1885 iter_type& __b, iter_type __e,
1886 ios_base::iostate& __err,
1887 const ctype<char_type>& __ct) const;
1888 void __get_am_pm(int& __h,
1889 iter_type& __b, iter_type __e,
1890 ios_base::iostate& __err,
1891 const ctype<char_type>& __ct) const;
1892 void __get_minute(int& __m,
1893 iter_type& __b, iter_type __e,
1894 ios_base::iostate& __err,
1895 const ctype<char_type>& __ct) const;
1896 void __get_second(int& __s,
1897 iter_type& __b, iter_type __e,
1898 ios_base::iostate& __err,
1899 const ctype<char_type>& __ct) const;
1900 void __get_weekday(int& __w,
1901 iter_type& __b, iter_type __e,
1902 ios_base::iostate& __err,
1903 const ctype<char_type>& __ct) const;
1904 void __get_day_year_num(int& __w,
1905 iter_type& __b, iter_type __e,
1906 ios_base::iostate& __err,
1907 const ctype<char_type>& __ct) const;
1908};
1909
1910template <class _CharT, class _InputIterator>
1911locale::id
1912time_get<_CharT, _InputIterator>::id;
1913
Alp Tokerec34c482014-05-15 11:27:39 +00001914// time_get primitives
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001915
1916template <class _CharT, class _InputIterator>
1917void
1918time_get<_CharT, _InputIterator>::__get_weekdayname(int& __w,
1919 iter_type& __b, iter_type __e,
1920 ios_base::iostate& __err,
1921 const ctype<char_type>& __ct) const
1922{
1923 // Note: ignoring case comes from the POSIX strptime spec
1924 const string_type* __wk = this->__weeks();
Howard Hinnantec3773c2011-12-01 20:21:04 +00001925 ptrdiff_t __i = __scan_keyword(__b, __e, __wk, __wk+14, __ct, __err, false) - __wk;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001926 if (__i < 14)
1927 __w = __i % 7;
1928}
1929
1930template <class _CharT, class _InputIterator>
1931void
1932time_get<_CharT, _InputIterator>::__get_monthname(int& __m,
1933 iter_type& __b, iter_type __e,
1934 ios_base::iostate& __err,
1935 const ctype<char_type>& __ct) const
1936{
1937 // Note: ignoring case comes from the POSIX strptime spec
1938 const string_type* __month = this->__months();
Howard Hinnantec3773c2011-12-01 20:21:04 +00001939 ptrdiff_t __i = __scan_keyword(__b, __e, __month, __month+24, __ct, __err, false) - __month;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001940 if (__i < 24)
1941 __m = __i % 12;
1942}
1943
1944template <class _CharT, class _InputIterator>
1945void
1946time_get<_CharT, _InputIterator>::__get_day(int& __d,
1947 iter_type& __b, iter_type __e,
1948 ios_base::iostate& __err,
1949 const ctype<char_type>& __ct) const
1950{
1951 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
1952 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 31)
1953 __d = __t;
1954 else
1955 __err |= ios_base::failbit;
1956}
1957
1958template <class _CharT, class _InputIterator>
1959void
1960time_get<_CharT, _InputIterator>::__get_month(int& __m,
1961 iter_type& __b, iter_type __e,
1962 ios_base::iostate& __err,
1963 const ctype<char_type>& __ct) const
1964{
1965 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2) - 1;
1966 if (!(__err & ios_base::failbit) && __t <= 11)
1967 __m = __t;
1968 else
1969 __err |= ios_base::failbit;
1970}
1971
1972template <class _CharT, class _InputIterator>
1973void
1974time_get<_CharT, _InputIterator>::__get_year(int& __y,
1975 iter_type& __b, iter_type __e,
1976 ios_base::iostate& __err,
1977 const ctype<char_type>& __ct) const
1978{
1979 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
1980 if (!(__err & ios_base::failbit))
1981 {
1982 if (__t < 69)
1983 __t += 2000;
1984 else if (69 <= __t && __t <= 99)
1985 __t += 1900;
1986 __y = __t - 1900;
1987 }
1988}
1989
1990template <class _CharT, class _InputIterator>
1991void
1992time_get<_CharT, _InputIterator>::__get_year4(int& __y,
1993 iter_type& __b, iter_type __e,
1994 ios_base::iostate& __err,
1995 const ctype<char_type>& __ct) const
1996{
1997 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
1998 if (!(__err & ios_base::failbit))
1999 __y = __t - 1900;
2000}
2001
2002template <class _CharT, class _InputIterator>
2003void
2004time_get<_CharT, _InputIterator>::__get_hour(int& __h,
2005 iter_type& __b, iter_type __e,
2006 ios_base::iostate& __err,
2007 const ctype<char_type>& __ct) const
2008{
2009 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2010 if (!(__err & ios_base::failbit) && __t <= 23)
2011 __h = __t;
2012 else
2013 __err |= ios_base::failbit;
2014}
2015
2016template <class _CharT, class _InputIterator>
2017void
2018time_get<_CharT, _InputIterator>::__get_12_hour(int& __h,
2019 iter_type& __b, iter_type __e,
2020 ios_base::iostate& __err,
2021 const ctype<char_type>& __ct) const
2022{
2023 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2024 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 12)
2025 __h = __t;
2026 else
2027 __err |= ios_base::failbit;
2028}
2029
2030template <class _CharT, class _InputIterator>
2031void
2032time_get<_CharT, _InputIterator>::__get_minute(int& __m,
2033 iter_type& __b, iter_type __e,
2034 ios_base::iostate& __err,
2035 const ctype<char_type>& __ct) const
2036{
2037 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2038 if (!(__err & ios_base::failbit) && __t <= 59)
2039 __m = __t;
2040 else
2041 __err |= ios_base::failbit;
2042}
2043
2044template <class _CharT, class _InputIterator>
2045void
2046time_get<_CharT, _InputIterator>::__get_second(int& __s,
2047 iter_type& __b, iter_type __e,
2048 ios_base::iostate& __err,
2049 const ctype<char_type>& __ct) const
2050{
2051 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2052 if (!(__err & ios_base::failbit) && __t <= 60)
2053 __s = __t;
2054 else
2055 __err |= ios_base::failbit;
2056}
2057
2058template <class _CharT, class _InputIterator>
2059void
2060time_get<_CharT, _InputIterator>::__get_weekday(int& __w,
2061 iter_type& __b, iter_type __e,
2062 ios_base::iostate& __err,
2063 const ctype<char_type>& __ct) const
2064{
2065 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 1);
2066 if (!(__err & ios_base::failbit) && __t <= 6)
2067 __w = __t;
2068 else
2069 __err |= ios_base::failbit;
2070}
2071
2072template <class _CharT, class _InputIterator>
2073void
2074time_get<_CharT, _InputIterator>::__get_day_year_num(int& __d,
2075 iter_type& __b, iter_type __e,
2076 ios_base::iostate& __err,
2077 const ctype<char_type>& __ct) const
2078{
2079 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 3);
2080 if (!(__err & ios_base::failbit) && __t <= 365)
2081 __d = __t;
2082 else
2083 __err |= ios_base::failbit;
2084}
2085
2086template <class _CharT, class _InputIterator>
2087void
2088time_get<_CharT, _InputIterator>::__get_white_space(iter_type& __b, iter_type __e,
2089 ios_base::iostate& __err,
2090 const ctype<char_type>& __ct) const
2091{
2092 for (; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2093 ;
2094 if (__b == __e)
2095 __err |= ios_base::eofbit;
2096}
2097
2098template <class _CharT, class _InputIterator>
2099void
2100time_get<_CharT, _InputIterator>::__get_am_pm(int& __h,
2101 iter_type& __b, iter_type __e,
2102 ios_base::iostate& __err,
2103 const ctype<char_type>& __ct) const
2104{
2105 const string_type* __ap = this->__am_pm();
2106 if (__ap[0].size() + __ap[1].size() == 0)
2107 {
2108 __err |= ios_base::failbit;
2109 return;
2110 }
Howard Hinnantec3773c2011-12-01 20:21:04 +00002111 ptrdiff_t __i = __scan_keyword(__b, __e, __ap, __ap+2, __ct, __err, false) - __ap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002112 if (__i == 0 && __h == 12)
2113 __h = 0;
2114 else if (__i == 1 && __h < 12)
2115 __h += 12;
2116}
2117
2118template <class _CharT, class _InputIterator>
2119void
2120time_get<_CharT, _InputIterator>::__get_percent(iter_type& __b, iter_type __e,
2121 ios_base::iostate& __err,
2122 const ctype<char_type>& __ct) const
2123{
2124 if (__b == __e)
2125 {
2126 __err |= ios_base::eofbit | ios_base::failbit;
2127 return;
2128 }
2129 if (__ct.narrow(*__b, 0) != '%')
2130 __err |= ios_base::failbit;
2131 else if(++__b == __e)
2132 __err |= ios_base::eofbit;
2133}
2134
Alp Tokerec34c482014-05-15 11:27:39 +00002135// time_get end primitives
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002136
2137template <class _CharT, class _InputIterator>
2138_InputIterator
2139time_get<_CharT, _InputIterator>::get(iter_type __b, iter_type __e,
2140 ios_base& __iob,
2141 ios_base::iostate& __err, tm* __tm,
2142 const char_type* __fmtb, const char_type* __fmte) const
2143{
2144 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2145 __err = ios_base::goodbit;
2146 while (__fmtb != __fmte && __err == ios_base::goodbit)
2147 {
2148 if (__b == __e)
2149 {
2150 __err = ios_base::failbit;
2151 break;
2152 }
2153 if (__ct.narrow(*__fmtb, 0) == '%')
2154 {
2155 if (++__fmtb == __fmte)
2156 {
2157 __err = ios_base::failbit;
2158 break;
2159 }
2160 char __cmd = __ct.narrow(*__fmtb, 0);
2161 char __opt = '\0';
2162 if (__cmd == 'E' || __cmd == '0')
2163 {
2164 if (++__fmtb == __fmte)
2165 {
2166 __err = ios_base::failbit;
2167 break;
2168 }
2169 __opt = __cmd;
2170 __cmd = __ct.narrow(*__fmtb, 0);
2171 }
2172 __b = do_get(__b, __e, __iob, __err, __tm, __cmd, __opt);
2173 ++__fmtb;
2174 }
2175 else if (__ct.is(ctype_base::space, *__fmtb))
2176 {
2177 for (++__fmtb; __fmtb != __fmte && __ct.is(ctype_base::space, *__fmtb); ++__fmtb)
2178 ;
2179 for ( ; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2180 ;
2181 }
2182 else if (__ct.toupper(*__b) == __ct.toupper(*__fmtb))
2183 {
2184 ++__b;
2185 ++__fmtb;
2186 }
2187 else
2188 __err = ios_base::failbit;
2189 }
2190 if (__b == __e)
2191 __err |= ios_base::eofbit;
2192 return __b;
2193}
2194
2195template <class _CharT, class _InputIterator>
2196typename time_get<_CharT, _InputIterator>::dateorder
2197time_get<_CharT, _InputIterator>::do_date_order() const
2198{
2199 return mdy;
2200}
2201
2202template <class _CharT, class _InputIterator>
2203_InputIterator
2204time_get<_CharT, _InputIterator>::do_get_time(iter_type __b, iter_type __e,
2205 ios_base& __iob,
2206 ios_base::iostate& __err,
2207 tm* __tm) const
2208{
2209 const char_type __fmt[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2210 return get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt)/sizeof(__fmt[0]));
2211}
2212
2213template <class _CharT, class _InputIterator>
2214_InputIterator
2215time_get<_CharT, _InputIterator>::do_get_date(iter_type __b, iter_type __e,
2216 ios_base& __iob,
2217 ios_base::iostate& __err,
2218 tm* __tm) const
2219{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002220 const string_type& __fmt = this->__x();
2221 return get(__b, __e, __iob, __err, __tm, __fmt.data(), __fmt.data() + __fmt.size());
2222}
2223
2224template <class _CharT, class _InputIterator>
2225_InputIterator
2226time_get<_CharT, _InputIterator>::do_get_weekday(iter_type __b, iter_type __e,
2227 ios_base& __iob,
2228 ios_base::iostate& __err,
2229 tm* __tm) const
2230{
2231 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2232 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2233 return __b;
2234}
2235
2236template <class _CharT, class _InputIterator>
2237_InputIterator
2238time_get<_CharT, _InputIterator>::do_get_monthname(iter_type __b, iter_type __e,
2239 ios_base& __iob,
2240 ios_base::iostate& __err,
2241 tm* __tm) const
2242{
2243 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2244 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2245 return __b;
2246}
2247
2248template <class _CharT, class _InputIterator>
2249_InputIterator
2250time_get<_CharT, _InputIterator>::do_get_year(iter_type __b, iter_type __e,
2251 ios_base& __iob,
2252 ios_base::iostate& __err,
2253 tm* __tm) const
2254{
2255 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2256 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2257 return __b;
2258}
2259
2260template <class _CharT, class _InputIterator>
2261_InputIterator
2262time_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
2263 ios_base& __iob,
2264 ios_base::iostate& __err, tm* __tm,
2265 char __fmt, char) const
2266{
2267 __err = ios_base::goodbit;
2268 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2269 switch (__fmt)
2270 {
2271 case 'a':
2272 case 'A':
2273 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2274 break;
2275 case 'b':
2276 case 'B':
2277 case 'h':
2278 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2279 break;
2280 case 'c':
2281 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00002282 const string_type& __fm = this->__c();
2283 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002284 }
2285 break;
2286 case 'd':
2287 case 'e':
2288 __get_day(__tm->tm_mday, __b, __e, __err, __ct);
2289 break;
2290 case 'D':
2291 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00002292 const char_type __fm[] = {'%', 'm', '/', '%', 'd', '/', '%', 'y'};
2293 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002294 }
2295 break;
Howard Hinnant506b3642011-04-10 17:54:14 +00002296 case 'F':
2297 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00002298 const char_type __fm[] = {'%', 'Y', '-', '%', 'm', '-', '%', 'd'};
2299 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnant506b3642011-04-10 17:54:14 +00002300 }
2301 break;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002302 case 'H':
2303 __get_hour(__tm->tm_hour, __b, __e, __err, __ct);
2304 break;
2305 case 'I':
2306 __get_12_hour(__tm->tm_hour, __b, __e, __err, __ct);
2307 break;
2308 case 'j':
2309 __get_day_year_num(__tm->tm_yday, __b, __e, __err, __ct);
2310 break;
2311 case 'm':
2312 __get_month(__tm->tm_mon, __b, __e, __err, __ct);
2313 break;
2314 case 'M':
2315 __get_minute(__tm->tm_min, __b, __e, __err, __ct);
2316 break;
2317 case 'n':
2318 case 't':
2319 __get_white_space(__b, __e, __err, __ct);
2320 break;
2321 case 'p':
2322 __get_am_pm(__tm->tm_hour, __b, __e, __err, __ct);
2323 break;
2324 case 'r':
2325 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00002326 const char_type __fm[] = {'%', 'I', ':', '%', 'M', ':', '%', 'S', ' ', '%', 'p'};
2327 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002328 }
2329 break;
2330 case 'R':
2331 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00002332 const char_type __fm[] = {'%', 'H', ':', '%', 'M'};
2333 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002334 }
2335 break;
2336 case 'S':
2337 __get_second(__tm->tm_sec, __b, __e, __err, __ct);
2338 break;
2339 case 'T':
2340 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00002341 const char_type __fm[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2342 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002343 }
2344 break;
2345 case 'w':
2346 __get_weekday(__tm->tm_wday, __b, __e, __err, __ct);
2347 break;
2348 case 'x':
2349 return do_get_date(__b, __e, __iob, __err, __tm);
2350 case 'X':
2351 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00002352 const string_type& __fm = this->__X();
2353 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002354 }
2355 break;
2356 case 'y':
2357 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2358 break;
2359 case 'Y':
2360 __get_year4(__tm->tm_year, __b, __e, __err, __ct);
2361 break;
2362 case '%':
2363 __get_percent(__b, __e, __err, __ct);
2364 break;
2365 default:
2366 __err |= ios_base::failbit;
2367 }
2368 return __b;
2369}
2370
Eric Fiselier833d6442016-09-15 22:27:07 +00002371_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get<char>)
2372_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002373
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002374class _LIBCPP_TYPE_VIS __time_get
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002375{
2376protected:
2377 locale_t __loc_;
2378
2379 __time_get(const char* __nm);
2380 __time_get(const string& __nm);
2381 ~__time_get();
2382};
2383
2384template <class _CharT>
Eric Fiselierc3589a82017-01-04 23:56:00 +00002385class _LIBCPP_TEMPLATE_VIS __time_get_storage
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002386 : public __time_get
2387{
2388protected:
2389 typedef basic_string<_CharT> string_type;
2390
2391 string_type __weeks_[14];
2392 string_type __months_[24];
2393 string_type __am_pm_[2];
2394 string_type __c_;
2395 string_type __r_;
2396 string_type __x_;
2397 string_type __X_;
2398
2399 explicit __time_get_storage(const char* __nm);
2400 explicit __time_get_storage(const string& __nm);
2401
Louis Dionne54238052018-07-11 23:14:33 +00002402 _LIBCPP_INLINE_VISIBILITY ~__time_get_storage() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002403
2404 time_base::dateorder __do_date_order() const;
2405
2406private:
2407 void init(const ctype<_CharT>&);
2408 string_type __analyze(char __fmt, const ctype<_CharT>&);
2409};
2410
2411template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Eric Fiselierc3589a82017-01-04 23:56:00 +00002412class _LIBCPP_TEMPLATE_VIS time_get_byname
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002413 : public time_get<_CharT, _InputIterator>,
2414 private __time_get_storage<_CharT>
2415{
2416public:
2417 typedef time_base::dateorder dateorder;
2418 typedef _InputIterator iter_type;
2419 typedef _CharT char_type;
2420 typedef basic_string<char_type> string_type;
2421
Howard Hinnant82894812010-09-22 16:48:34 +00002422 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002423 explicit time_get_byname(const char* __nm, size_t __refs = 0)
2424 : time_get<_CharT, _InputIterator>(__refs),
2425 __time_get_storage<_CharT>(__nm) {}
Howard Hinnant82894812010-09-22 16:48:34 +00002426 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002427 explicit time_get_byname(const string& __nm, size_t __refs = 0)
2428 : time_get<_CharT, _InputIterator>(__refs),
2429 __time_get_storage<_CharT>(__nm) {}
2430
2431protected:
Howard Hinnant82894812010-09-22 16:48:34 +00002432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002433 ~time_get_byname() {}
2434
Howard Hinnant82894812010-09-22 16:48:34 +00002435 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002436 virtual dateorder do_date_order() const {return this->__do_date_order();}
2437private:
Howard Hinnant82894812010-09-22 16:48:34 +00002438 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002439 virtual const string_type* __weeks() const {return this->__weeks_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002440 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002441 virtual const string_type* __months() const {return this->__months_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002442 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002443 virtual const string_type* __am_pm() const {return this->__am_pm_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002445 virtual const string_type& __c() const {return this->__c_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002447 virtual const string_type& __r() const {return this->__r_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002448 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002449 virtual const string_type& __x() const {return this->__x_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002450 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002451 virtual const string_type& __X() const {return this->__X_;}
2452};
2453
Eric Fiselier833d6442016-09-15 22:27:07 +00002454_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<char>)
2455_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002456
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002457class _LIBCPP_TYPE_VIS __time_put
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002458{
2459 locale_t __loc_;
2460protected:
Louis Dionne54238052018-07-11 23:14:33 +00002461 _LIBCPP_INLINE_VISIBILITY __time_put() : __loc_(_LIBCPP_GET_C_LOCALE) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002462 __time_put(const char* __nm);
2463 __time_put(const string& __nm);
2464 ~__time_put();
2465 void __do_put(char* __nb, char*& __ne, const tm* __tm,
2466 char __fmt, char __mod) const;
2467 void __do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm,
2468 char __fmt, char __mod) const;
2469};
2470
2471template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Eric Fiselierc3589a82017-01-04 23:56:00 +00002472class _LIBCPP_TEMPLATE_VIS time_put
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002473 : public locale::facet,
2474 private __time_put
2475{
2476public:
2477 typedef _CharT char_type;
2478 typedef _OutputIterator iter_type;
2479
Louis Dionne54238052018-07-11 23:14:33 +00002480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002481 explicit time_put(size_t __refs = 0)
2482 : locale::facet(__refs) {}
2483
2484 iter_type put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm,
2485 const char_type* __pb, const char_type* __pe) const;
2486
Louis Dionne54238052018-07-11 23:14:33 +00002487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002488 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
2489 const tm* __tm, char __fmt, char __mod = 0) const
2490 {
2491 return do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2492 }
2493
2494 static locale::id id;
2495
2496protected:
Louis Dionne54238052018-07-11 23:14:33 +00002497 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002498 ~time_put() {}
2499 virtual iter_type do_put(iter_type __s, ios_base&, char_type, const tm* __tm,
2500 char __fmt, char __mod) const;
2501
Louis Dionne54238052018-07-11 23:14:33 +00002502 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002503 explicit time_put(const char* __nm, size_t __refs)
2504 : locale::facet(__refs),
2505 __time_put(__nm) {}
Louis Dionne54238052018-07-11 23:14:33 +00002506 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002507 explicit time_put(const string& __nm, size_t __refs)
2508 : locale::facet(__refs),
2509 __time_put(__nm) {}
2510};
2511
2512template <class _CharT, class _OutputIterator>
2513locale::id
2514time_put<_CharT, _OutputIterator>::id;
2515
2516template <class _CharT, class _OutputIterator>
2517_OutputIterator
2518time_put<_CharT, _OutputIterator>::put(iter_type __s, ios_base& __iob,
2519 char_type __fl, const tm* __tm,
2520 const char_type* __pb,
2521 const char_type* __pe) const
2522{
2523 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2524 for (; __pb != __pe; ++__pb)
2525 {
2526 if (__ct.narrow(*__pb, 0) == '%')
2527 {
2528 if (++__pb == __pe)
2529 {
2530 *__s++ = __pb[-1];
2531 break;
2532 }
2533 char __mod = 0;
2534 char __fmt = __ct.narrow(*__pb, 0);
2535 if (__fmt == 'E' || __fmt == 'O')
2536 {
2537 if (++__pb == __pe)
2538 {
2539 *__s++ = __pb[-2];
2540 *__s++ = __pb[-1];
2541 break;
2542 }
2543 __mod = __fmt;
2544 __fmt = __ct.narrow(*__pb, 0);
2545 }
2546 __s = do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2547 }
2548 else
2549 *__s++ = *__pb;
2550 }
2551 return __s;
2552}
2553
2554template <class _CharT, class _OutputIterator>
2555_OutputIterator
Howard Hinnantec3773c2011-12-01 20:21:04 +00002556time_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002557 char_type, const tm* __tm,
2558 char __fmt, char __mod) const
2559{
2560 char_type __nar[100];
2561 char_type* __nb = __nar;
2562 char_type* __ne = __nb + 100;
2563 __do_put(__nb, __ne, __tm, __fmt, __mod);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002564 return _VSTD::copy(__nb, __ne, __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002565}
2566
Eric Fiselier833d6442016-09-15 22:27:07 +00002567_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<char>)
2568_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002569
2570template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Eric Fiselierc3589a82017-01-04 23:56:00 +00002571class _LIBCPP_TEMPLATE_VIS time_put_byname
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002572 : public time_put<_CharT, _OutputIterator>
2573{
2574public:
Louis Dionne54238052018-07-11 23:14:33 +00002575 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002576 explicit time_put_byname(const char* __nm, size_t __refs = 0)
2577 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2578
Louis Dionne54238052018-07-11 23:14:33 +00002579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002580 explicit time_put_byname(const string& __nm, size_t __refs = 0)
2581 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2582
2583protected:
Louis Dionne54238052018-07-11 23:14:33 +00002584 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002585 ~time_put_byname() {}
2586};
2587
Eric Fiselier833d6442016-09-15 22:27:07 +00002588_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<char>)
2589_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002590
2591// money_base
2592
Howard Hinnant83eade62013-03-06 23:30:19 +00002593class _LIBCPP_TYPE_VIS money_base
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002594{
2595public:
2596 enum part {none, space, symbol, sign, value};
2597 struct pattern {char field[4];};
2598
Louis Dionne54238052018-07-11 23:14:33 +00002599 _LIBCPP_INLINE_VISIBILITY money_base() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002600};
2601
2602// moneypunct
2603
2604template <class _CharT, bool _International = false>
Eric Fiselierc3589a82017-01-04 23:56:00 +00002605class _LIBCPP_TEMPLATE_VIS moneypunct
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002606 : public locale::facet,
2607 public money_base
2608{
2609public:
2610 typedef _CharT char_type;
2611 typedef basic_string<char_type> string_type;
2612
Louis Dionne54238052018-07-11 23:14:33 +00002613 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002614 explicit moneypunct(size_t __refs = 0)
2615 : locale::facet(__refs) {}
2616
Louis Dionne54238052018-07-11 23:14:33 +00002617 _LIBCPP_INLINE_VISIBILITY char_type decimal_point() const {return do_decimal_point();}
2618 _LIBCPP_INLINE_VISIBILITY char_type thousands_sep() const {return do_thousands_sep();}
2619 _LIBCPP_INLINE_VISIBILITY string grouping() const {return do_grouping();}
2620 _LIBCPP_INLINE_VISIBILITY string_type curr_symbol() const {return do_curr_symbol();}
2621 _LIBCPP_INLINE_VISIBILITY string_type positive_sign() const {return do_positive_sign();}
2622 _LIBCPP_INLINE_VISIBILITY string_type negative_sign() const {return do_negative_sign();}
2623 _LIBCPP_INLINE_VISIBILITY int frac_digits() const {return do_frac_digits();}
2624 _LIBCPP_INLINE_VISIBILITY pattern pos_format() const {return do_pos_format();}
2625 _LIBCPP_INLINE_VISIBILITY pattern neg_format() const {return do_neg_format();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002626
2627 static locale::id id;
2628 static const bool intl = _International;
2629
2630protected:
Louis Dionne54238052018-07-11 23:14:33 +00002631 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002632 ~moneypunct() {}
2633
2634 virtual char_type do_decimal_point() const {return numeric_limits<char_type>::max();}
2635 virtual char_type do_thousands_sep() const {return numeric_limits<char_type>::max();}
2636 virtual string do_grouping() const {return string();}
2637 virtual string_type do_curr_symbol() const {return string_type();}
2638 virtual string_type do_positive_sign() const {return string_type();}
2639 virtual string_type do_negative_sign() const {return string_type(1, '-');}
2640 virtual int do_frac_digits() const {return 0;}
2641 virtual pattern do_pos_format() const
Howard Hinnant9bae2a92012-11-06 21:48:33 +00002642 {pattern __p = {{symbol, sign, none, value}}; return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002643 virtual pattern do_neg_format() const
Howard Hinnant9bae2a92012-11-06 21:48:33 +00002644 {pattern __p = {{symbol, sign, none, value}}; return __p;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002645};
2646
2647template <class _CharT, bool _International>
2648locale::id
2649moneypunct<_CharT, _International>::id;
2650
Howard Hinnant0a69fa12012-12-12 21:14:28 +00002651template <class _CharT, bool _International>
2652const bool
2653moneypunct<_CharT, _International>::intl;
2654
Eric Fiselier833d6442016-09-15 22:27:07 +00002655_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, false>)
2656_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, true>)
2657_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, false>)
2658_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, true>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002659
2660// moneypunct_byname
2661
2662template <class _CharT, bool _International = false>
Eric Fiselierc3589a82017-01-04 23:56:00 +00002663class _LIBCPP_TEMPLATE_VIS moneypunct_byname
Howard Hinnant82894812010-09-22 16:48:34 +00002664 : public moneypunct<_CharT, _International>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002665{
2666public:
2667 typedef money_base::pattern pattern;
2668 typedef _CharT char_type;
2669 typedef basic_string<char_type> string_type;
2670
Louis Dionne54238052018-07-11 23:14:33 +00002671 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002672 explicit moneypunct_byname(const char* __nm, size_t __refs = 0)
2673 : moneypunct<_CharT, _International>(__refs) {init(__nm);}
2674
Louis Dionne54238052018-07-11 23:14:33 +00002675 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002676 explicit moneypunct_byname(const string& __nm, size_t __refs = 0)
2677 : moneypunct<_CharT, _International>(__refs) {init(__nm.c_str());}
2678
2679protected:
Louis Dionne54238052018-07-11 23:14:33 +00002680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002681 ~moneypunct_byname() {}
2682
2683 virtual char_type do_decimal_point() const {return __decimal_point_;}
2684 virtual char_type do_thousands_sep() const {return __thousands_sep_;}
2685 virtual string do_grouping() const {return __grouping_;}
2686 virtual string_type do_curr_symbol() const {return __curr_symbol_;}
2687 virtual string_type do_positive_sign() const {return __positive_sign_;}
2688 virtual string_type do_negative_sign() const {return __negative_sign_;}
2689 virtual int do_frac_digits() const {return __frac_digits_;}
2690 virtual pattern do_pos_format() const {return __pos_format_;}
2691 virtual pattern do_neg_format() const {return __neg_format_;}
2692
2693private:
2694 char_type __decimal_point_;
2695 char_type __thousands_sep_;
2696 string __grouping_;
2697 string_type __curr_symbol_;
2698 string_type __positive_sign_;
2699 string_type __negative_sign_;
2700 int __frac_digits_;
2701 pattern __pos_format_;
2702 pattern __neg_format_;
2703
2704 void init(const char*);
2705};
2706
Shoaib Meenai9dcbb462017-04-03 04:04:24 +00002707template<> _LIBCPP_FUNC_VIS void moneypunct_byname<char, false>::init(const char*);
2708template<> _LIBCPP_FUNC_VIS void moneypunct_byname<char, true>::init(const char*);
2709template<> _LIBCPP_FUNC_VIS void moneypunct_byname<wchar_t, false>::init(const char*);
2710template<> _LIBCPP_FUNC_VIS void moneypunct_byname<wchar_t, true>::init(const char*);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002711
Eric Fiselier833d6442016-09-15 22:27:07 +00002712_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, false>)
2713_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, true>)
2714_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, false>)
2715_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, true>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002716
2717// money_get
2718
2719template <class _CharT>
2720class __money_get
2721{
2722protected:
2723 typedef _CharT char_type;
2724 typedef basic_string<char_type> string_type;
2725
Louis Dionne54238052018-07-11 23:14:33 +00002726 _LIBCPP_INLINE_VISIBILITY __money_get() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002727
2728 static void __gather_info(bool __intl, const locale& __loc,
2729 money_base::pattern& __pat, char_type& __dp,
2730 char_type& __ts, string& __grp,
2731 string_type& __sym, string_type& __psn,
2732 string_type& __nsn, int& __fd);
2733};
2734
2735template <class _CharT>
2736void
2737__money_get<_CharT>::__gather_info(bool __intl, const locale& __loc,
2738 money_base::pattern& __pat, char_type& __dp,
2739 char_type& __ts, string& __grp,
2740 string_type& __sym, string_type& __psn,
2741 string_type& __nsn, int& __fd)
2742{
2743 if (__intl)
2744 {
2745 const moneypunct<char_type, true>& __mp =
2746 use_facet<moneypunct<char_type, true> >(__loc);
2747 __pat = __mp.neg_format();
2748 __nsn = __mp.negative_sign();
2749 __psn = __mp.positive_sign();
2750 __dp = __mp.decimal_point();
2751 __ts = __mp.thousands_sep();
2752 __grp = __mp.grouping();
2753 __sym = __mp.curr_symbol();
2754 __fd = __mp.frac_digits();
2755 }
2756 else
2757 {
2758 const moneypunct<char_type, false>& __mp =
2759 use_facet<moneypunct<char_type, false> >(__loc);
2760 __pat = __mp.neg_format();
2761 __nsn = __mp.negative_sign();
2762 __psn = __mp.positive_sign();
2763 __dp = __mp.decimal_point();
2764 __ts = __mp.thousands_sep();
2765 __grp = __mp.grouping();
2766 __sym = __mp.curr_symbol();
2767 __fd = __mp.frac_digits();
2768 }
2769}
2770
Eric Fiselier833d6442016-09-15 22:27:07 +00002771_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<char>)
2772_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002773
2774template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Eric Fiselierc3589a82017-01-04 23:56:00 +00002775class _LIBCPP_TEMPLATE_VIS money_get
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002776 : public locale::facet,
2777 private __money_get<_CharT>
2778{
2779public:
2780 typedef _CharT char_type;
2781 typedef _InputIterator iter_type;
2782 typedef basic_string<char_type> string_type;
2783
Louis Dionne54238052018-07-11 23:14:33 +00002784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002785 explicit money_get(size_t __refs = 0)
2786 : locale::facet(__refs) {}
2787
Louis Dionne54238052018-07-11 23:14:33 +00002788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002789 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
2790 ios_base::iostate& __err, long double& __v) const
2791 {
2792 return do_get(__b, __e, __intl, __iob, __err, __v);
2793 }
2794
Louis Dionne54238052018-07-11 23:14:33 +00002795 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002796 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
2797 ios_base::iostate& __err, string_type& __v) const
2798 {
2799 return do_get(__b, __e, __intl, __iob, __err, __v);
2800 }
2801
2802 static locale::id id;
2803
2804protected:
2805
Louis Dionne54238052018-07-11 23:14:33 +00002806 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002807 ~money_get() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00002808
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002809 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
2810 ios_base& __iob, ios_base::iostate& __err,
2811 long double& __v) const;
2812 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
2813 ios_base& __iob, ios_base::iostate& __err,
2814 string_type& __v) const;
2815
2816private:
2817 static bool __do_get(iter_type& __b, iter_type __e,
2818 bool __intl, const locale& __loc,
2819 ios_base::fmtflags __flags, ios_base::iostate& __err,
2820 bool& __neg, const ctype<char_type>& __ct,
2821 unique_ptr<char_type, void(*)(void*)>& __wb,
2822 char_type*& __wn, char_type* __we);
2823};
2824
2825template <class _CharT, class _InputIterator>
2826locale::id
2827money_get<_CharT, _InputIterator>::id;
2828
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002829_LIBCPP_FUNC_VIS void __do_nothing(void*);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002830
2831template <class _Tp>
2832_LIBCPP_HIDDEN
2833void
2834__double_or_nothing(unique_ptr<_Tp, void(*)(void*)>& __b, _Tp*& __n, _Tp*& __e)
2835{
2836 bool __owns = __b.get_deleter() != __do_nothing;
Howard Hinnantec3773c2011-12-01 20:21:04 +00002837 size_t __cur_cap = static_cast<size_t>(__e-__b.get()) * sizeof(_Tp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002838 size_t __new_cap = __cur_cap < numeric_limits<size_t>::max() / 2 ?
2839 2 * __cur_cap : numeric_limits<size_t>::max();
Marshall Clow1d306de2014-10-27 19:08:10 +00002840 if (__new_cap == 0)
2841 __new_cap = sizeof(_Tp);
Howard Hinnantec3773c2011-12-01 20:21:04 +00002842 size_t __n_off = static_cast<size_t>(__n - __b.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002843 _Tp* __t = (_Tp*)realloc(__owns ? __b.get() : 0, __new_cap);
2844 if (__t == 0)
2845 __throw_bad_alloc();
2846 if (__owns)
2847 __b.release();
2848 __b = unique_ptr<_Tp, void(*)(void*)>(__t, free);
2849 __new_cap /= sizeof(_Tp);
2850 __n = __b.get() + __n_off;
2851 __e = __b.get() + __new_cap;
2852}
2853
2854// true == success
2855template <class _CharT, class _InputIterator>
2856bool
2857money_get<_CharT, _InputIterator>::__do_get(iter_type& __b, iter_type __e,
2858 bool __intl, const locale& __loc,
2859 ios_base::fmtflags __flags,
2860 ios_base::iostate& __err,
2861 bool& __neg,
2862 const ctype<char_type>& __ct,
2863 unique_ptr<char_type, void(*)(void*)>& __wb,
2864 char_type*& __wn, char_type* __we)
2865{
2866 const unsigned __bz = 100;
2867 unsigned __gbuf[__bz];
2868 unique_ptr<unsigned, void(*)(void*)> __gb(__gbuf, __do_nothing);
2869 unsigned* __gn = __gb.get();
2870 unsigned* __ge = __gn + __bz;
2871 money_base::pattern __pat;
2872 char_type __dp;
2873 char_type __ts;
2874 string __grp;
2875 string_type __sym;
2876 string_type __psn;
2877 string_type __nsn;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00002878 // Capture the spaces read into money_base::{space,none} so they
2879 // can be compared to initial spaces in __sym.
2880 string_type __spaces;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002881 int __fd;
2882 __money_get<_CharT>::__gather_info(__intl, __loc, __pat, __dp, __ts, __grp,
2883 __sym, __psn, __nsn, __fd);
2884 const string_type* __trailing_sign = 0;
2885 __wn = __wb.get();
2886 for (unsigned __p = 0; __p < 4 && __b != __e; ++__p)
2887 {
2888 switch (__pat.field[__p])
2889 {
2890 case money_base::space:
2891 if (__p != 3)
2892 {
2893 if (__ct.is(ctype_base::space, *__b))
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00002894 __spaces.push_back(*__b++);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002895 else
2896 {
2897 __err |= ios_base::failbit;
2898 return false;
2899 }
2900 }
Eric Fiselier799d02d2017-05-05 20:32:26 +00002901 _LIBCPP_FALLTHROUGH();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002902 case money_base::none:
2903 if (__p != 3)
2904 {
2905 while (__b != __e && __ct.is(ctype_base::space, *__b))
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00002906 __spaces.push_back(*__b++);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002907 }
2908 break;
2909 case money_base::sign:
2910 if (__psn.size() + __nsn.size() > 0)
2911 {
2912 if (__psn.size() == 0 || __nsn.size() == 0)
2913 { // sign is optional
2914 if (__psn.size() > 0)
2915 { // __nsn.size() == 0
2916 if (*__b == __psn[0])
2917 {
2918 ++__b;
2919 if (__psn.size() > 1)
2920 __trailing_sign = &__psn;
2921 }
2922 else
2923 __neg = true;
2924 }
2925 else if (*__b == __nsn[0]) // __nsn.size() > 0 && __psn.size() == 0
2926 {
2927 ++__b;
2928 __neg = true;
2929 if (__nsn.size() > 1)
2930 __trailing_sign = &__nsn;
2931 }
2932 }
2933 else // sign is required
2934 {
2935 if (*__b == __psn[0])
2936 {
2937 ++__b;
2938 if (__psn.size() > 1)
2939 __trailing_sign = &__psn;
2940 }
2941 else if (*__b == __nsn[0])
2942 {
2943 ++__b;
2944 __neg = true;
2945 if (__nsn.size() > 1)
2946 __trailing_sign = &__nsn;
2947 }
2948 else
2949 {
2950 __err |= ios_base::failbit;
2951 return false;
2952 }
2953 }
2954 }
2955 break;
2956 case money_base::symbol:
2957 {
2958 bool __more_needed = __trailing_sign ||
2959 (__p < 2) ||
2960 (__p == 2 && __pat.field[3] != static_cast<char>(money_base::none));
Marshall Clow9de3d4c2013-10-13 01:02:45 +00002961 bool __sb = (__flags & ios_base::showbase) != 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002962 if (__sb || __more_needed)
2963 {
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00002964 typename string_type::const_iterator __sym_space_end = __sym.begin();
2965 if (__p > 0 && (__pat.field[__p - 1] == money_base::none ||
2966 __pat.field[__p - 1] == money_base::space)) {
2967 // Match spaces we've already read against spaces at
2968 // the beginning of __sym.
2969 while (__sym_space_end != __sym.end() &&
2970 __ct.is(ctype_base::space, *__sym_space_end))
2971 ++__sym_space_end;
2972 const size_t __num_spaces = __sym_space_end - __sym.begin();
2973 if (__num_spaces > __spaces.size() ||
2974 !equal(__spaces.end() - __num_spaces, __spaces.end(),
2975 __sym.begin())) {
2976 // No match. Put __sym_space_end back at the
2977 // beginning of __sym, which will prevent a
2978 // match in the next loop.
2979 __sym_space_end = __sym.begin();
2980 }
2981 }
2982 typename string_type::const_iterator __sym_curr_char = __sym_space_end;
2983 while (__sym_curr_char != __sym.end() && __b != __e &&
2984 *__b == *__sym_curr_char) {
2985 ++__b;
2986 ++__sym_curr_char;
2987 }
2988 if (__sb && __sym_curr_char != __sym.end())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002989 {
2990 __err |= ios_base::failbit;
2991 return false;
2992 }
2993 }
2994 }
2995 break;
2996 case money_base::value:
2997 {
2998 unsigned __ng = 0;
2999 for (; __b != __e; ++__b)
3000 {
3001 char_type __c = *__b;
3002 if (__ct.is(ctype_base::digit, __c))
3003 {
3004 if (__wn == __we)
3005 __double_or_nothing(__wb, __wn, __we);
3006 *__wn++ = __c;
3007 ++__ng;
3008 }
3009 else if (__grp.size() > 0 && __ng > 0 && __c == __ts)
3010 {
3011 if (__gn == __ge)
3012 __double_or_nothing(__gb, __gn, __ge);
3013 *__gn++ = __ng;
3014 __ng = 0;
3015 }
3016 else
3017 break;
3018 }
3019 if (__gb.get() != __gn && __ng > 0)
3020 {
3021 if (__gn == __ge)
3022 __double_or_nothing(__gb, __gn, __ge);
3023 *__gn++ = __ng;
3024 }
3025 if (__fd > 0)
3026 {
3027 if (__b == __e || *__b != __dp)
3028 {
3029 __err |= ios_base::failbit;
3030 return false;
3031 }
3032 for (++__b; __fd > 0; --__fd, ++__b)
3033 {
3034 if (__b == __e || !__ct.is(ctype_base::digit, *__b))
3035 {
3036 __err |= ios_base::failbit;
3037 return false;
3038 }
3039 if (__wn == __we)
3040 __double_or_nothing(__wb, __wn, __we);
3041 *__wn++ = *__b;
3042 }
3043 }
3044 if (__wn == __wb.get())
3045 {
3046 __err |= ios_base::failbit;
3047 return false;
3048 }
3049 }
3050 break;
3051 }
3052 }
3053 if (__trailing_sign)
3054 {
3055 for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, ++__b)
3056 {
3057 if (__b == __e || *__b != (*__trailing_sign)[__i])
3058 {
3059 __err |= ios_base::failbit;
3060 return false;
3061 }
3062 }
3063 }
3064 if (__gb.get() != __gn)
3065 {
3066 ios_base::iostate __et = ios_base::goodbit;
3067 __check_grouping(__grp, __gb.get(), __gn, __et);
3068 if (__et)
3069 {
3070 __err |= ios_base::failbit;
3071 return false;
3072 }
3073 }
3074 return true;
3075}
3076
3077template <class _CharT, class _InputIterator>
3078_InputIterator
3079money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3080 bool __intl, ios_base& __iob,
3081 ios_base::iostate& __err,
3082 long double& __v) const
3083{
Howard Hinnantec3773c2011-12-01 20:21:04 +00003084 const int __bz = 100;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003085 char_type __wbuf[__bz];
3086 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3087 char_type* __wn;
3088 char_type* __we = __wbuf + __bz;
3089 locale __loc = __iob.getloc();
3090 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3091 bool __neg = false;
3092 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3093 __wb, __wn, __we))
3094 {
3095 const char __src[] = "0123456789";
3096 char_type __atoms[sizeof(__src)-1];
3097 __ct.widen(__src, __src + (sizeof(__src)-1), __atoms);
3098 char __nbuf[__bz];
3099 char* __nc = __nbuf;
3100 unique_ptr<char, void(*)(void*)> __h(0, free);
3101 if (__wn - __wb.get() > __bz-2)
3102 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00003103 __h.reset((char*)malloc(static_cast<size_t>(__wn - __wb.get() + 2)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003104 if (__h.get() == 0)
3105 __throw_bad_alloc();
3106 __nc = __h.get();
3107 }
3108 if (__neg)
3109 *__nc++ = '-';
3110 for (const char_type* __w = __wb.get(); __w < __wn; ++__w, ++__nc)
Marshall Clow9b145da2013-03-22 02:14:40 +00003111 *__nc = __src[find(__atoms, _VSTD::end(__atoms), *__w) - __atoms];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003112 *__nc = char();
3113 if (sscanf(__nbuf, "%Lf", &__v) != 1)
3114 __throw_runtime_error("money_get error");
3115 }
3116 if (__b == __e)
3117 __err |= ios_base::eofbit;
3118 return __b;
3119}
3120
3121template <class _CharT, class _InputIterator>
3122_InputIterator
3123money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3124 bool __intl, ios_base& __iob,
3125 ios_base::iostate& __err,
3126 string_type& __v) const
3127{
Howard Hinnantec3773c2011-12-01 20:21:04 +00003128 const int __bz = 100;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003129 char_type __wbuf[__bz];
3130 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3131 char_type* __wn;
3132 char_type* __we = __wbuf + __bz;
3133 locale __loc = __iob.getloc();
3134 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3135 bool __neg = false;
3136 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3137 __wb, __wn, __we))
3138 {
3139 __v.clear();
3140 if (__neg)
3141 __v.push_back(__ct.widen('-'));
3142 char_type __z = __ct.widen('0');
3143 char_type* __w;
3144 for (__w = __wb.get(); __w < __wn-1; ++__w)
3145 if (*__w != __z)
3146 break;
3147 __v.append(__w, __wn);
3148 }
3149 if (__b == __e)
3150 __err |= ios_base::eofbit;
3151 return __b;
3152}
3153
Eric Fiselier833d6442016-09-15 22:27:07 +00003154_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<char>)
3155_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003156
3157// money_put
3158
3159template <class _CharT>
3160class __money_put
3161{
3162protected:
3163 typedef _CharT char_type;
3164 typedef basic_string<char_type> string_type;
3165
Louis Dionne54238052018-07-11 23:14:33 +00003166 _LIBCPP_INLINE_VISIBILITY __money_put() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003167
3168 static void __gather_info(bool __intl, bool __neg, const locale& __loc,
3169 money_base::pattern& __pat, char_type& __dp,
3170 char_type& __ts, string& __grp,
3171 string_type& __sym, string_type& __sn,
3172 int& __fd);
3173 static void __format(char_type* __mb, char_type*& __mi, char_type*& __me,
3174 ios_base::fmtflags __flags,
3175 const char_type* __db, const char_type* __de,
3176 const ctype<char_type>& __ct, bool __neg,
3177 const money_base::pattern& __pat, char_type __dp,
3178 char_type __ts, const string& __grp,
3179 const string_type& __sym, const string_type& __sn,
3180 int __fd);
3181};
3182
3183template <class _CharT>
3184void
3185__money_put<_CharT>::__gather_info(bool __intl, bool __neg, const locale& __loc,
3186 money_base::pattern& __pat, char_type& __dp,
3187 char_type& __ts, string& __grp,
3188 string_type& __sym, string_type& __sn,
3189 int& __fd)
3190{
3191 if (__intl)
3192 {
3193 const moneypunct<char_type, true>& __mp =
3194 use_facet<moneypunct<char_type, true> >(__loc);
3195 if (__neg)
3196 {
3197 __pat = __mp.neg_format();
3198 __sn = __mp.negative_sign();
3199 }
3200 else
3201 {
3202 __pat = __mp.pos_format();
3203 __sn = __mp.positive_sign();
3204 }
3205 __dp = __mp.decimal_point();
3206 __ts = __mp.thousands_sep();
3207 __grp = __mp.grouping();
3208 __sym = __mp.curr_symbol();
3209 __fd = __mp.frac_digits();
3210 }
3211 else
3212 {
3213 const moneypunct<char_type, false>& __mp =
3214 use_facet<moneypunct<char_type, false> >(__loc);
3215 if (__neg)
3216 {
3217 __pat = __mp.neg_format();
3218 __sn = __mp.negative_sign();
3219 }
3220 else
3221 {
3222 __pat = __mp.pos_format();
3223 __sn = __mp.positive_sign();
3224 }
3225 __dp = __mp.decimal_point();
3226 __ts = __mp.thousands_sep();
3227 __grp = __mp.grouping();
3228 __sym = __mp.curr_symbol();
3229 __fd = __mp.frac_digits();
3230 }
3231}
3232
3233template <class _CharT>
3234void
3235__money_put<_CharT>::__format(char_type* __mb, char_type*& __mi, char_type*& __me,
3236 ios_base::fmtflags __flags,
3237 const char_type* __db, const char_type* __de,
3238 const ctype<char_type>& __ct, bool __neg,
3239 const money_base::pattern& __pat, char_type __dp,
3240 char_type __ts, const string& __grp,
3241 const string_type& __sym, const string_type& __sn,
3242 int __fd)
3243{
3244 __me = __mb;
3245 for (unsigned __p = 0; __p < 4; ++__p)
3246 {
3247 switch (__pat.field[__p])
3248 {
3249 case money_base::none:
3250 __mi = __me;
3251 break;
3252 case money_base::space:
3253 __mi = __me;
3254 *__me++ = __ct.widen(' ');
3255 break;
3256 case money_base::sign:
3257 if (!__sn.empty())
3258 *__me++ = __sn[0];
3259 break;
3260 case money_base::symbol:
3261 if (!__sym.empty() && (__flags & ios_base::showbase))
Howard Hinnant0949eed2011-06-30 21:18:19 +00003262 __me = _VSTD::copy(__sym.begin(), __sym.end(), __me);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003263 break;
3264 case money_base::value:
3265 {
3266 // remember start of value so we can reverse it
3267 char_type* __t = __me;
3268 // find beginning of digits
3269 if (__neg)
3270 ++__db;
3271 // find end of digits
3272 const char_type* __d;
3273 for (__d = __db; __d < __de; ++__d)
3274 if (!__ct.is(ctype_base::digit, *__d))
3275 break;
3276 // print fractional part
3277 if (__fd > 0)
3278 {
3279 int __f;
3280 for (__f = __fd; __d > __db && __f > 0; --__f)
3281 *__me++ = *--__d;
3282 char_type __z = __f > 0 ? __ct.widen('0') : char_type();
3283 for (; __f > 0; --__f)
3284 *__me++ = __z;
3285 *__me++ = __dp;
3286 }
3287 // print units part
3288 if (__d == __db)
3289 {
3290 *__me++ = __ct.widen('0');
3291 }
3292 else
3293 {
3294 unsigned __ng = 0;
3295 unsigned __ig = 0;
3296 unsigned __gl = __grp.empty() ? numeric_limits<unsigned>::max()
3297 : static_cast<unsigned>(__grp[__ig]);
3298 while (__d != __db)
3299 {
3300 if (__ng == __gl)
3301 {
3302 *__me++ = __ts;
3303 __ng = 0;
3304 if (++__ig < __grp.size())
3305 __gl = __grp[__ig] == numeric_limits<char>::max() ?
3306 numeric_limits<unsigned>::max() :
3307 static_cast<unsigned>(__grp[__ig]);
3308 }
3309 *__me++ = *--__d;
3310 ++__ng;
3311 }
3312 }
3313 // reverse it
3314 reverse(__t, __me);
3315 }
3316 break;
3317 }
3318 }
3319 // print rest of sign, if any
3320 if (__sn.size() > 1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003321 __me = _VSTD::copy(__sn.begin()+1, __sn.end(), __me);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003322 // set alignment
3323 if ((__flags & ios_base::adjustfield) == ios_base::left)
3324 __mi = __me;
3325 else if ((__flags & ios_base::adjustfield) != ios_base::internal)
3326 __mi = __mb;
3327}
3328
Eric Fiselier833d6442016-09-15 22:27:07 +00003329_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<char>)
3330_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003331
3332template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Eric Fiselierc3589a82017-01-04 23:56:00 +00003333class _LIBCPP_TEMPLATE_VIS money_put
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003334 : public locale::facet,
3335 private __money_put<_CharT>
3336{
3337public:
3338 typedef _CharT char_type;
3339 typedef _OutputIterator iter_type;
3340 typedef basic_string<char_type> string_type;
3341
Louis Dionne54238052018-07-11 23:14:33 +00003342 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003343 explicit money_put(size_t __refs = 0)
3344 : locale::facet(__refs) {}
3345
Louis Dionne54238052018-07-11 23:14:33 +00003346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003347 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3348 long double __units) const
3349 {
3350 return do_put(__s, __intl, __iob, __fl, __units);
3351 }
3352
Louis Dionne54238052018-07-11 23:14:33 +00003353 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003354 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3355 const string_type& __digits) const
3356 {
3357 return do_put(__s, __intl, __iob, __fl, __digits);
3358 }
3359
3360 static locale::id id;
3361
3362protected:
Louis Dionne54238052018-07-11 23:14:33 +00003363 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003364 ~money_put() {}
3365
3366 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3367 char_type __fl, long double __units) const;
3368 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3369 char_type __fl, const string_type& __digits) const;
3370};
3371
3372template <class _CharT, class _OutputIterator>
3373locale::id
3374money_put<_CharT, _OutputIterator>::id;
3375
3376template <class _CharT, class _OutputIterator>
3377_OutputIterator
3378money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3379 ios_base& __iob, char_type __fl,
3380 long double __units) const
3381{
3382 // convert to char
3383 const size_t __bs = 100;
3384 char __buf[__bs];
3385 char* __bb = __buf;
3386 char_type __digits[__bs];
3387 char_type* __db = __digits;
Howard Hinnantec3773c2011-12-01 20:21:04 +00003388 size_t __n = static_cast<size_t>(snprintf(__bb, __bs, "%.0Lf", __units));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003389 unique_ptr<char, void(*)(void*)> __hn(0, free);
3390 unique_ptr<char_type, void(*)(void*)> __hd(0, free);
3391 // secure memory for digit storage
3392 if (__n > __bs-1)
3393 {
Ben Craigfd556582016-03-09 15:39:39 +00003394 __n = static_cast<size_t>(__libcpp_asprintf_l(&__bb, _LIBCPP_GET_C_LOCALE, "%.0Lf", __units));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003395 if (__bb == 0)
3396 __throw_bad_alloc();
3397 __hn.reset(__bb);
3398 __hd.reset((char_type*)malloc(__n * sizeof(char_type)));
Howard Hinnant05b57d52012-03-07 20:37:43 +00003399 if (__hd == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003400 __throw_bad_alloc();
3401 __db = __hd.get();
3402 }
3403 // gather info
3404 locale __loc = __iob.getloc();
3405 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3406 __ct.widen(__bb, __bb + __n, __db);
3407 bool __neg = __n > 0 && __bb[0] == '-';
3408 money_base::pattern __pat;
3409 char_type __dp;
3410 char_type __ts;
3411 string __grp;
3412 string_type __sym;
3413 string_type __sn;
3414 int __fd;
3415 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3416 // secure memory for formatting
3417 char_type __mbuf[__bs];
3418 char_type* __mb = __mbuf;
3419 unique_ptr<char_type, void(*)(void*)> __hw(0, free);
3420 size_t __exn = static_cast<int>(__n) > __fd ?
Howard Hinnantec3773c2011-12-01 20:21:04 +00003421 (__n - static_cast<size_t>(__fd)) * 2 + __sn.size() +
3422 __sym.size() + static_cast<size_t>(__fd) + 1
3423 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003424 if (__exn > __bs)
3425 {
3426 __hw.reset((char_type*)malloc(__exn * sizeof(char_type)));
3427 __mb = __hw.get();
3428 if (__mb == 0)
3429 __throw_bad_alloc();
3430 }
3431 // format
3432 char_type* __mi;
3433 char_type* __me;
3434 this->__format(__mb, __mi, __me, __iob.flags(),
3435 __db, __db + __n, __ct,
3436 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3437 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3438}
3439
3440template <class _CharT, class _OutputIterator>
3441_OutputIterator
3442money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3443 ios_base& __iob, char_type __fl,
3444 const string_type& __digits) const
3445{
3446 // gather info
3447 locale __loc = __iob.getloc();
3448 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3449 bool __neg = __digits.size() > 0 && __digits[0] == __ct.widen('-');
3450 money_base::pattern __pat;
3451 char_type __dp;
3452 char_type __ts;
3453 string __grp;
3454 string_type __sym;
3455 string_type __sn;
3456 int __fd;
3457 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3458 // secure memory for formatting
3459 char_type __mbuf[100];
3460 char_type* __mb = __mbuf;
3461 unique_ptr<char_type, void(*)(void*)> __h(0, free);
Howard Hinnantec3773c2011-12-01 20:21:04 +00003462 size_t __exn = static_cast<int>(__digits.size()) > __fd ?
3463 (__digits.size() - static_cast<size_t>(__fd)) * 2 +
3464 __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 1
3465 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003466 if (__exn > 100)
3467 {
3468 __h.reset((char_type*)malloc(__exn * sizeof(char_type)));
3469 __mb = __h.get();
3470 if (__mb == 0)
3471 __throw_bad_alloc();
3472 }
3473 // format
3474 char_type* __mi;
3475 char_type* __me;
3476 this->__format(__mb, __mi, __me, __iob.flags(),
3477 __digits.data(), __digits.data() + __digits.size(), __ct,
3478 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3479 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3480}
3481
Eric Fiselier833d6442016-09-15 22:27:07 +00003482_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<char>)
3483_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003484
3485// messages
3486
Howard Hinnant83eade62013-03-06 23:30:19 +00003487class _LIBCPP_TYPE_VIS messages_base
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003488{
3489public:
Howard Hinnantb2080c72011-02-25 00:51:08 +00003490 typedef ptrdiff_t catalog;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003491
Louis Dionne54238052018-07-11 23:14:33 +00003492 _LIBCPP_INLINE_VISIBILITY messages_base() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003493};
3494
3495template <class _CharT>
Eric Fiselierc3589a82017-01-04 23:56:00 +00003496class _LIBCPP_TEMPLATE_VIS messages
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003497 : public locale::facet,
3498 public messages_base
3499{
3500public:
3501 typedef _CharT char_type;
3502 typedef basic_string<_CharT> string_type;
3503
Louis Dionne54238052018-07-11 23:14:33 +00003504 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003505 explicit messages(size_t __refs = 0)
3506 : locale::facet(__refs) {}
3507
Louis Dionne54238052018-07-11 23:14:33 +00003508 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003509 catalog open(const basic_string<char>& __nm, const locale& __loc) const
3510 {
3511 return do_open(__nm, __loc);
3512 }
3513
Louis Dionne54238052018-07-11 23:14:33 +00003514 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003515 string_type get(catalog __c, int __set, int __msgid,
3516 const string_type& __dflt) const
3517 {
3518 return do_get(__c, __set, __msgid, __dflt);
3519 }
3520
Louis Dionne54238052018-07-11 23:14:33 +00003521 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003522 void close(catalog __c) const
3523 {
3524 do_close(__c);
3525 }
3526
3527 static locale::id id;
3528
3529protected:
Louis Dionne54238052018-07-11 23:14:33 +00003530 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003531 ~messages() {}
3532
3533 virtual catalog do_open(const basic_string<char>&, const locale&) const;
3534 virtual string_type do_get(catalog, int __set, int __msgid,
3535 const string_type& __dflt) const;
3536 virtual void do_close(catalog) const;
3537};
3538
3539template <class _CharT>
3540locale::id
3541messages<_CharT>::id;
3542
3543template <class _CharT>
3544typename messages<_CharT>::catalog
3545messages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const
3546{
Ed Schouten0251f0f2015-03-11 16:39:36 +00003547#ifdef _LIBCPP_HAS_CATOPEN
Howard Hinnant11624452011-10-11 16:00:46 +00003548 catalog __cat = (catalog)catopen(__nm.c_str(), NL_CAT_LOCALE);
Howard Hinnantb2080c72011-02-25 00:51:08 +00003549 if (__cat != -1)
3550 __cat = static_cast<catalog>((static_cast<size_t>(__cat) >> 1));
3551 return __cat;
Ed Schouten0251f0f2015-03-11 16:39:36 +00003552#else // !_LIBCPP_HAS_CATOPEN
3553 return -1;
3554#endif // _LIBCPP_HAS_CATOPEN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003555}
3556
3557template <class _CharT>
3558typename messages<_CharT>::string_type
3559messages<_CharT>::do_get(catalog __c, int __set, int __msgid,
3560 const string_type& __dflt) const
3561{
Ed Schouten0251f0f2015-03-11 16:39:36 +00003562#ifdef _LIBCPP_HAS_CATOPEN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003563 string __ndflt;
3564 __narrow_to_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__ndflt),
3565 __dflt.c_str(),
3566 __dflt.c_str() + __dflt.size());
Howard Hinnantb2080c72011-02-25 00:51:08 +00003567 if (__c != -1)
3568 __c <<= 1;
Howard Hinnant11624452011-10-11 16:00:46 +00003569 nl_catd __cat = (nl_catd)__c;
Howard Hinnantb2080c72011-02-25 00:51:08 +00003570 char* __n = catgets(__cat, __set, __msgid, __ndflt.c_str());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003571 string_type __w;
3572 __widen_from_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__w),
3573 __n, __n + strlen(__n));
3574 return __w;
Ed Schouten0251f0f2015-03-11 16:39:36 +00003575#else // !_LIBCPP_HAS_CATOPEN
3576 return __dflt;
3577#endif // _LIBCPP_HAS_CATOPEN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003578}
3579
3580template <class _CharT>
3581void
3582messages<_CharT>::do_close(catalog __c) const
3583{
Ed Schouten0251f0f2015-03-11 16:39:36 +00003584#ifdef _LIBCPP_HAS_CATOPEN
Howard Hinnantb2080c72011-02-25 00:51:08 +00003585 if (__c != -1)
3586 __c <<= 1;
Howard Hinnant11624452011-10-11 16:00:46 +00003587 nl_catd __cat = (nl_catd)__c;
Howard Hinnantb2080c72011-02-25 00:51:08 +00003588 catclose(__cat);
Ed Schouten0251f0f2015-03-11 16:39:36 +00003589#endif // _LIBCPP_HAS_CATOPEN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003590}
3591
Eric Fiselier833d6442016-09-15 22:27:07 +00003592_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<char>)
3593_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003594
3595template <class _CharT>
Eric Fiselierc3589a82017-01-04 23:56:00 +00003596class _LIBCPP_TEMPLATE_VIS messages_byname
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003597 : public messages<_CharT>
3598{
3599public:
3600 typedef messages_base::catalog catalog;
3601 typedef basic_string<_CharT> string_type;
3602
Louis Dionne54238052018-07-11 23:14:33 +00003603 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003604 explicit messages_byname(const char*, size_t __refs = 0)
3605 : messages<_CharT>(__refs) {}
3606
Louis Dionne54238052018-07-11 23:14:33 +00003607 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003608 explicit messages_byname(const string&, size_t __refs = 0)
3609 : messages<_CharT>(__refs) {}
3610
3611protected:
Louis Dionne54238052018-07-11 23:14:33 +00003612 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003613 ~messages_byname() {}
3614};
3615
Eric Fiselier833d6442016-09-15 22:27:07 +00003616_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<char>)
3617_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003618
Howard Hinnantd23b4642010-05-31 20:58:54 +00003619template<class _Codecvt, class _Elem = wchar_t,
3620 class _Wide_alloc = allocator<_Elem>,
3621 class _Byte_alloc = allocator<char> >
Eric Fiselierc3589a82017-01-04 23:56:00 +00003622class _LIBCPP_TEMPLATE_VIS wstring_convert
Howard Hinnantd23b4642010-05-31 20:58:54 +00003623{
3624public:
3625 typedef basic_string<char, char_traits<char>, _Byte_alloc> byte_string;
3626 typedef basic_string<_Elem, char_traits<_Elem>, _Wide_alloc> wide_string;
3627 typedef typename _Codecvt::state_type state_type;
3628 typedef typename wide_string::traits_type::int_type int_type;
3629
3630private:
3631 byte_string __byte_err_string_;
3632 wide_string __wide_err_string_;
3633 _Codecvt* __cvtptr_;
3634 state_type __cvtstate_;
3635 size_t __cvtcount_;
3636
3637 wstring_convert(const wstring_convert& __wc);
3638 wstring_convert& operator=(const wstring_convert& __wc);
3639public:
Louis Dionne54238052018-07-11 23:14:33 +00003640 _LIBCPP_INLINE_VISIBILITY
Marshall Clow83179a72013-08-27 20:18:59 +00003641 _LIBCPP_EXPLICIT_AFTER_CXX11 wstring_convert(_Codecvt* __pcvt = new _Codecvt);
Louis Dionne54238052018-07-11 23:14:33 +00003642 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd23b4642010-05-31 20:58:54 +00003643 wstring_convert(_Codecvt* __pcvt, state_type __state);
Marshall Clow83179a72013-08-27 20:18:59 +00003644 _LIBCPP_EXPLICIT_AFTER_CXX11 wstring_convert(const byte_string& __byte_err,
Howard Hinnantd23b4642010-05-31 20:58:54 +00003645 const wide_string& __wide_err = wide_string());
Eric Fiselieraa55cef2017-04-19 01:34:08 +00003646#ifndef _LIBCPP_CXX03_LANG
Louis Dionne54238052018-07-11 23:14:33 +00003647 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd23b4642010-05-31 20:58:54 +00003648 wstring_convert(wstring_convert&& __wc);
3649#endif
3650 ~wstring_convert();
3651
Louis Dionne54238052018-07-11 23:14:33 +00003652 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd23b4642010-05-31 20:58:54 +00003653 wide_string from_bytes(char __byte)
3654 {return from_bytes(&__byte, &__byte+1);}
Louis Dionne54238052018-07-11 23:14:33 +00003655 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd23b4642010-05-31 20:58:54 +00003656 wide_string from_bytes(const char* __ptr)
3657 {return from_bytes(__ptr, __ptr + char_traits<char>::length(__ptr));}
Louis Dionne54238052018-07-11 23:14:33 +00003658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd23b4642010-05-31 20:58:54 +00003659 wide_string from_bytes(const byte_string& __str)
3660 {return from_bytes(__str.data(), __str.data() + __str.size());}
3661 wide_string from_bytes(const char* __first, const char* __last);
3662
Louis Dionne54238052018-07-11 23:14:33 +00003663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd23b4642010-05-31 20:58:54 +00003664 byte_string to_bytes(_Elem __wchar)
3665 {return to_bytes(&__wchar, &__wchar+1);}
Louis Dionne54238052018-07-11 23:14:33 +00003666 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd23b4642010-05-31 20:58:54 +00003667 byte_string to_bytes(const _Elem* __wptr)
3668 {return to_bytes(__wptr, __wptr + char_traits<_Elem>::length(__wptr));}
Louis Dionne54238052018-07-11 23:14:33 +00003669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd23b4642010-05-31 20:58:54 +00003670 byte_string to_bytes(const wide_string& __wstr)
3671 {return to_bytes(__wstr.data(), __wstr.data() + __wstr.size());}
3672 byte_string to_bytes(const _Elem* __first, const _Elem* __last);
3673
Louis Dionne54238052018-07-11 23:14:33 +00003674 _LIBCPP_INLINE_VISIBILITY
Marshall Clow83179a72013-08-27 20:18:59 +00003675 size_t converted() const _NOEXCEPT {return __cvtcount_;}
Louis Dionne54238052018-07-11 23:14:33 +00003676 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd23b4642010-05-31 20:58:54 +00003677 state_type state() const {return __cvtstate_;}
3678};
3679
3680template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003681inline
Howard Hinnantd23b4642010-05-31 20:58:54 +00003682wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3683 wstring_convert(_Codecvt* __pcvt)
3684 : __cvtptr_(__pcvt), __cvtstate_(), __cvtcount_(0)
3685{
3686}
3687
3688template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003689inline
Howard Hinnantd23b4642010-05-31 20:58:54 +00003690wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3691 wstring_convert(_Codecvt* __pcvt, state_type __state)
3692 : __cvtptr_(__pcvt), __cvtstate_(__state), __cvtcount_(0)
3693{
3694}
3695
3696template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3697wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3698 wstring_convert(const byte_string& __byte_err, const wide_string& __wide_err)
3699 : __byte_err_string_(__byte_err), __wide_err_string_(__wide_err),
3700 __cvtstate_(), __cvtcount_(0)
3701{
3702 __cvtptr_ = new _Codecvt;
3703}
3704
Eric Fiselieraa55cef2017-04-19 01:34:08 +00003705#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd23b4642010-05-31 20:58:54 +00003706
3707template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003708inline
Howard Hinnantd23b4642010-05-31 20:58:54 +00003709wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3710 wstring_convert(wstring_convert&& __wc)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003711 : __byte_err_string_(_VSTD::move(__wc.__byte_err_string_)),
3712 __wide_err_string_(_VSTD::move(__wc.__wide_err_string_)),
Howard Hinnantd23b4642010-05-31 20:58:54 +00003713 __cvtptr_(__wc.__cvtptr_),
Eric Fiseliere7aabbb2016-06-26 22:56:26 +00003714 __cvtstate_(__wc.__cvtstate_), __cvtcount_(__wc.__cvtcount_)
Howard Hinnantd23b4642010-05-31 20:58:54 +00003715{
3716 __wc.__cvtptr_ = nullptr;
3717}
3718
Eric Fiselieraa55cef2017-04-19 01:34:08 +00003719#endif // _LIBCPP_CXX03_LANG
Howard Hinnantd23b4642010-05-31 20:58:54 +00003720
3721template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3722wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::~wstring_convert()
3723{
3724 delete __cvtptr_;
3725}
3726
3727template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3728typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::wide_string
3729wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3730 from_bytes(const char* __frm, const char* __frm_end)
3731{
3732 __cvtcount_ = 0;
3733 if (__cvtptr_ != nullptr)
3734 {
3735 wide_string __ws(2*(__frm_end - __frm), _Elem());
Howard Hinnant1ca23672012-07-12 18:07:41 +00003736 if (__frm != __frm_end)
3737 __ws.resize(__ws.capacity());
Howard Hinnantd23b4642010-05-31 20:58:54 +00003738 codecvt_base::result __r = codecvt_base::ok;
3739 state_type __st = __cvtstate_;
3740 if (__frm != __frm_end)
3741 {
3742 _Elem* __to = &__ws[0];
3743 _Elem* __to_end = __to + __ws.size();
3744 const char* __frm_nxt;
3745 do
3746 {
3747 _Elem* __to_nxt;
3748 __r = __cvtptr_->in(__st, __frm, __frm_end, __frm_nxt,
3749 __to, __to_end, __to_nxt);
3750 __cvtcount_ += __frm_nxt - __frm;
3751 if (__frm_nxt == __frm)
3752 {
3753 __r = codecvt_base::error;
3754 }
3755 else if (__r == codecvt_base::noconv)
3756 {
3757 __ws.resize(__to - &__ws[0]);
3758 // This only gets executed if _Elem is char
3759 __ws.append((const _Elem*)__frm, (const _Elem*)__frm_end);
3760 __frm = __frm_nxt;
3761 __r = codecvt_base::ok;
3762 }
3763 else if (__r == codecvt_base::ok)
3764 {
3765 __ws.resize(__to_nxt - &__ws[0]);
3766 __frm = __frm_nxt;
3767 }
3768 else if (__r == codecvt_base::partial)
3769 {
3770 ptrdiff_t __s = __to_nxt - &__ws[0];
3771 __ws.resize(2 * __s);
3772 __to = &__ws[0] + __s;
3773 __to_end = &__ws[0] + __ws.size();
3774 __frm = __frm_nxt;
3775 }
3776 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3777 }
3778 if (__r == codecvt_base::ok)
3779 return __ws;
3780 }
Marshall Clow14c09a22016-08-25 15:09:01 +00003781
Howard Hinnantd23b4642010-05-31 20:58:54 +00003782 if (__wide_err_string_.empty())
Marshall Clow14c09a22016-08-25 15:09:01 +00003783 __throw_range_error("wstring_convert: from_bytes error");
3784
Howard Hinnantd23b4642010-05-31 20:58:54 +00003785 return __wide_err_string_;
3786}
3787
3788template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3789typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::byte_string
3790wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3791 to_bytes(const _Elem* __frm, const _Elem* __frm_end)
3792{
3793 __cvtcount_ = 0;
3794 if (__cvtptr_ != nullptr)
3795 {
3796 byte_string __bs(2*(__frm_end - __frm), char());
Howard Hinnant1ca23672012-07-12 18:07:41 +00003797 if (__frm != __frm_end)
3798 __bs.resize(__bs.capacity());
Howard Hinnantd23b4642010-05-31 20:58:54 +00003799 codecvt_base::result __r = codecvt_base::ok;
3800 state_type __st = __cvtstate_;
3801 if (__frm != __frm_end)
3802 {
3803 char* __to = &__bs[0];
3804 char* __to_end = __to + __bs.size();
3805 const _Elem* __frm_nxt;
3806 do
3807 {
3808 char* __to_nxt;
3809 __r = __cvtptr_->out(__st, __frm, __frm_end, __frm_nxt,
3810 __to, __to_end, __to_nxt);
3811 __cvtcount_ += __frm_nxt - __frm;
3812 if (__frm_nxt == __frm)
3813 {
3814 __r = codecvt_base::error;
3815 }
3816 else if (__r == codecvt_base::noconv)
3817 {
3818 __bs.resize(__to - &__bs[0]);
3819 // This only gets executed if _Elem is char
3820 __bs.append((const char*)__frm, (const char*)__frm_end);
3821 __frm = __frm_nxt;
3822 __r = codecvt_base::ok;
3823 }
3824 else if (__r == codecvt_base::ok)
3825 {
3826 __bs.resize(__to_nxt - &__bs[0]);
3827 __frm = __frm_nxt;
3828 }
3829 else if (__r == codecvt_base::partial)
3830 {
3831 ptrdiff_t __s = __to_nxt - &__bs[0];
3832 __bs.resize(2 * __s);
3833 __to = &__bs[0] + __s;
3834 __to_end = &__bs[0] + __bs.size();
3835 __frm = __frm_nxt;
3836 }
3837 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3838 }
3839 if (__r == codecvt_base::ok)
3840 {
3841 size_t __s = __bs.size();
3842 __bs.resize(__bs.capacity());
3843 char* __to = &__bs[0] + __s;
3844 char* __to_end = __to + __bs.size();
3845 do
3846 {
3847 char* __to_nxt;
3848 __r = __cvtptr_->unshift(__st, __to, __to_end, __to_nxt);
3849 if (__r == codecvt_base::noconv)
3850 {
3851 __bs.resize(__to - &__bs[0]);
3852 __r = codecvt_base::ok;
3853 }
3854 else if (__r == codecvt_base::ok)
3855 {
3856 __bs.resize(__to_nxt - &__bs[0]);
3857 }
3858 else if (__r == codecvt_base::partial)
3859 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00003860 ptrdiff_t __sp = __to_nxt - &__bs[0];
3861 __bs.resize(2 * __sp);
3862 __to = &__bs[0] + __sp;
Howard Hinnantd23b4642010-05-31 20:58:54 +00003863 __to_end = &__bs[0] + __bs.size();
3864 }
3865 } while (__r == codecvt_base::partial);
3866 if (__r == codecvt_base::ok)
3867 return __bs;
3868 }
3869 }
Marshall Clow14c09a22016-08-25 15:09:01 +00003870
Howard Hinnantd23b4642010-05-31 20:58:54 +00003871 if (__byte_err_string_.empty())
Marshall Clow14c09a22016-08-25 15:09:01 +00003872 __throw_range_error("wstring_convert: to_bytes error");
3873
Howard Hinnantd23b4642010-05-31 20:58:54 +00003874 return __byte_err_string_;
3875}
3876
3877template <class _Codecvt, class _Elem = wchar_t, class _Tr = char_traits<_Elem> >
Eric Fiselierc3589a82017-01-04 23:56:00 +00003878class _LIBCPP_TEMPLATE_VIS wbuffer_convert
Howard Hinnantd23b4642010-05-31 20:58:54 +00003879 : public basic_streambuf<_Elem, _Tr>
3880{
3881public:
3882 // types:
3883 typedef _Elem char_type;
3884 typedef _Tr traits_type;
3885 typedef typename traits_type::int_type int_type;
3886 typedef typename traits_type::pos_type pos_type;
3887 typedef typename traits_type::off_type off_type;
3888 typedef typename _Codecvt::state_type state_type;
3889
3890private:
Howard Hinnant4b53f502010-06-01 20:09:18 +00003891 char* __extbuf_;
3892 const char* __extbufnext_;
3893 const char* __extbufend_;
3894 char __extbuf_min_[8];
3895 size_t __ebs_;
3896 char_type* __intbuf_;
3897 size_t __ibs_;
Howard Hinnantd23b4642010-05-31 20:58:54 +00003898 streambuf* __bufptr_;
Howard Hinnant4b53f502010-06-01 20:09:18 +00003899 _Codecvt* __cv_;
3900 state_type __st_;
3901 ios_base::openmode __cm_;
3902 bool __owns_eb_;
3903 bool __owns_ib_;
3904 bool __always_noconv_;
Howard Hinnantd23b4642010-05-31 20:58:54 +00003905
Howard Hinnant4b53f502010-06-01 20:09:18 +00003906 wbuffer_convert(const wbuffer_convert&);
3907 wbuffer_convert& operator=(const wbuffer_convert&);
Howard Hinnantd23b4642010-05-31 20:58:54 +00003908public:
Marshall Clow83179a72013-08-27 20:18:59 +00003909 _LIBCPP_EXPLICIT_AFTER_CXX11 wbuffer_convert(streambuf* __bytebuf = 0,
3910 _Codecvt* __pcvt = new _Codecvt, state_type __state = state_type());
Howard Hinnant4b53f502010-06-01 20:09:18 +00003911 ~wbuffer_convert();
Howard Hinnantd23b4642010-05-31 20:58:54 +00003912
Howard Hinnant82894812010-09-22 16:48:34 +00003913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd23b4642010-05-31 20:58:54 +00003914 streambuf* rdbuf() const {return __bufptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003915 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd23b4642010-05-31 20:58:54 +00003916 streambuf* rdbuf(streambuf* __bytebuf)
3917 {
3918 streambuf* __r = __bufptr_;
3919 __bufptr_ = __bytebuf;
3920 return __r;
3921 }
3922
Howard Hinnant82894812010-09-22 16:48:34 +00003923 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4b53f502010-06-01 20:09:18 +00003924 state_type state() const {return __st_;}
Howard Hinnantd23b4642010-05-31 20:58:54 +00003925
3926protected:
Howard Hinnant4b53f502010-06-01 20:09:18 +00003927 virtual int_type underflow();
3928 virtual int_type pbackfail(int_type __c = traits_type::eof());
Howard Hinnantd23b4642010-05-31 20:58:54 +00003929 virtual int_type overflow (int_type __c = traits_type::eof());
Howard Hinnant4b53f502010-06-01 20:09:18 +00003930 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s,
3931 streamsize __n);
3932 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
3933 ios_base::openmode __wch = ios_base::in | ios_base::out);
3934 virtual pos_type seekpos(pos_type __sp,
3935 ios_base::openmode __wch = ios_base::in | ios_base::out);
3936 virtual int sync();
3937
3938private:
3939 bool __read_mode();
3940 void __write_mode();
3941 wbuffer_convert* __close();
Howard Hinnantd23b4642010-05-31 20:58:54 +00003942};
3943
3944template <class _Codecvt, class _Elem, class _Tr>
Howard Hinnant4b53f502010-06-01 20:09:18 +00003945wbuffer_convert<_Codecvt, _Elem, _Tr>::
3946 wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt, state_type __state)
3947 : __extbuf_(0),
3948 __extbufnext_(0),
3949 __extbufend_(0),
3950 __ebs_(0),
3951 __intbuf_(0),
3952 __ibs_(0),
3953 __bufptr_(__bytebuf),
3954 __cv_(__pcvt),
3955 __st_(__state),
3956 __cm_(0),
3957 __owns_eb_(false),
3958 __owns_ib_(false),
3959 __always_noconv_(__cv_ ? __cv_->always_noconv() : false)
3960{
3961 setbuf(0, 4096);
3962}
3963
3964template <class _Codecvt, class _Elem, class _Tr>
3965wbuffer_convert<_Codecvt, _Elem, _Tr>::~wbuffer_convert()
3966{
3967 __close();
3968 delete __cv_;
3969 if (__owns_eb_)
3970 delete [] __extbuf_;
3971 if (__owns_ib_)
3972 delete [] __intbuf_;
3973}
3974
3975template <class _Codecvt, class _Elem, class _Tr>
3976typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
3977wbuffer_convert<_Codecvt, _Elem, _Tr>::underflow()
3978{
3979 if (__cv_ == 0 || __bufptr_ == 0)
3980 return traits_type::eof();
3981 bool __initial = __read_mode();
3982 char_type __1buf;
3983 if (this->gptr() == 0)
3984 this->setg(&__1buf, &__1buf+1, &__1buf+1);
3985 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
3986 int_type __c = traits_type::eof();
3987 if (this->gptr() == this->egptr())
3988 {
3989 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
3990 if (__always_noconv_)
3991 {
3992 streamsize __nmemb = static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz);
3993 __nmemb = __bufptr_->sgetn((char*)this->eback() + __unget_sz, __nmemb);
3994 if (__nmemb != 0)
3995 {
3996 this->setg(this->eback(),
3997 this->eback() + __unget_sz,
3998 this->eback() + __unget_sz + __nmemb);
3999 __c = *this->gptr();
4000 }
4001 }
4002 else
4003 {
Eric Fiselierfe6d50f2016-06-19 06:58:22 +00004004 _LIBCPP_ASSERT(!(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
4005 if (__extbufend_ != __extbufnext_)
4006 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
Howard Hinnant4b53f502010-06-01 20:09:18 +00004007 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
4008 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004009 streamsize __nmemb = _VSTD::min(static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz),
Howard Hinnant4b53f502010-06-01 20:09:18 +00004010 static_cast<streamsize>(__extbufend_ - __extbufnext_));
4011 codecvt_base::result __r;
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00004012 // FIXME: Do we ever need to restore the state here?
4013 //state_type __svs = __st_;
Howard Hinnant4b53f502010-06-01 20:09:18 +00004014 streamsize __nr = __bufptr_->sgetn(const_cast<char*>(__extbufnext_), __nmemb);
4015 if (__nr != 0)
4016 {
4017 __extbufend_ = __extbufnext_ + __nr;
4018 char_type* __inext;
4019 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
4020 this->eback() + __unget_sz,
4021 this->egptr(), __inext);
4022 if (__r == codecvt_base::noconv)
4023 {
Marshall Clowff5f9b22017-06-14 20:00:36 +00004024 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
4025 (char_type*) const_cast<char *>(__extbufend_));
Howard Hinnant4b53f502010-06-01 20:09:18 +00004026 __c = *this->gptr();
4027 }
4028 else if (__inext != this->eback() + __unget_sz)
4029 {
4030 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
4031 __c = *this->gptr();
4032 }
4033 }
4034 }
4035 }
4036 else
4037 __c = *this->gptr();
4038 if (this->eback() == &__1buf)
4039 this->setg(0, 0, 0);
4040 return __c;
4041}
4042
4043template <class _Codecvt, class _Elem, class _Tr>
4044typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4045wbuffer_convert<_Codecvt, _Elem, _Tr>::pbackfail(int_type __c)
4046{
4047 if (__cv_ != 0 && __bufptr_ != 0 && this->eback() < this->gptr())
4048 {
4049 if (traits_type::eq_int_type(__c, traits_type::eof()))
4050 {
4051 this->gbump(-1);
4052 return traits_type::not_eof(__c);
4053 }
4054 if (traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
4055 {
4056 this->gbump(-1);
4057 *this->gptr() = traits_type::to_char_type(__c);
4058 return __c;
4059 }
4060 }
4061 return traits_type::eof();
4062}
4063
4064template <class _Codecvt, class _Elem, class _Tr>
Howard Hinnantd23b4642010-05-31 20:58:54 +00004065typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4066wbuffer_convert<_Codecvt, _Elem, _Tr>::overflow(int_type __c)
4067{
Howard Hinnant4b53f502010-06-01 20:09:18 +00004068 if (__cv_ == 0 || __bufptr_ == 0)
4069 return traits_type::eof();
4070 __write_mode();
4071 char_type __1buf;
4072 char_type* __pb_save = this->pbase();
4073 char_type* __epb_save = this->epptr();
4074 if (!traits_type::eq_int_type(__c, traits_type::eof()))
4075 {
4076 if (this->pptr() == 0)
4077 this->setp(&__1buf, &__1buf+1);
4078 *this->pptr() = traits_type::to_char_type(__c);
4079 this->pbump(1);
4080 }
4081 if (this->pptr() != this->pbase())
4082 {
4083 if (__always_noconv_)
4084 {
4085 streamsize __nmemb = static_cast<streamsize>(this->pptr() - this->pbase());
4086 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4087 return traits_type::eof();
4088 }
4089 else
4090 {
4091 char* __extbe = __extbuf_;
4092 codecvt_base::result __r;
4093 do
4094 {
4095 const char_type* __e;
4096 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
4097 __extbuf_, __extbuf_ + __ebs_, __extbe);
4098 if (__e == this->pbase())
4099 return traits_type::eof();
4100 if (__r == codecvt_base::noconv)
4101 {
4102 streamsize __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
4103 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4104 return traits_type::eof();
4105 }
4106 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
4107 {
4108 streamsize __nmemb = static_cast<size_t>(__extbe - __extbuf_);
4109 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4110 return traits_type::eof();
4111 if (__r == codecvt_base::partial)
4112 {
Marshall Clowff5f9b22017-06-14 20:00:36 +00004113 this->setp(const_cast<char_type *>(__e), this->pptr());
Marshall Clow29149d32017-09-12 15:00:43 +00004114 this->__pbump(this->epptr() - this->pbase());
Howard Hinnant4b53f502010-06-01 20:09:18 +00004115 }
4116 }
4117 else
4118 return traits_type::eof();
4119 } while (__r == codecvt_base::partial);
4120 }
4121 this->setp(__pb_save, __epb_save);
4122 }
4123 return traits_type::not_eof(__c);
4124}
4125
4126template <class _Codecvt, class _Elem, class _Tr>
4127basic_streambuf<_Elem, _Tr>*
4128wbuffer_convert<_Codecvt, _Elem, _Tr>::setbuf(char_type* __s, streamsize __n)
4129{
4130 this->setg(0, 0, 0);
4131 this->setp(0, 0);
4132 if (__owns_eb_)
4133 delete [] __extbuf_;
4134 if (__owns_ib_)
4135 delete [] __intbuf_;
4136 __ebs_ = __n;
4137 if (__ebs_ > sizeof(__extbuf_min_))
4138 {
4139 if (__always_noconv_ && __s)
4140 {
4141 __extbuf_ = (char*)__s;
4142 __owns_eb_ = false;
4143 }
4144 else
4145 {
4146 __extbuf_ = new char[__ebs_];
4147 __owns_eb_ = true;
4148 }
4149 }
4150 else
4151 {
4152 __extbuf_ = __extbuf_min_;
4153 __ebs_ = sizeof(__extbuf_min_);
4154 __owns_eb_ = false;
4155 }
4156 if (!__always_noconv_)
4157 {
4158 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
4159 if (__s && __ibs_ >= sizeof(__extbuf_min_))
4160 {
4161 __intbuf_ = __s;
4162 __owns_ib_ = false;
4163 }
4164 else
4165 {
4166 __intbuf_ = new char_type[__ibs_];
4167 __owns_ib_ = true;
4168 }
4169 }
4170 else
4171 {
4172 __ibs_ = 0;
4173 __intbuf_ = 0;
4174 __owns_ib_ = false;
4175 }
4176 return this;
4177}
4178
4179template <class _Codecvt, class _Elem, class _Tr>
4180typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4181wbuffer_convert<_Codecvt, _Elem, _Tr>::seekoff(off_type __off, ios_base::seekdir __way,
4182 ios_base::openmode __om)
4183{
4184 int __width = __cv_->encoding();
4185 if (__cv_ == 0 || __bufptr_ == 0 || (__width <= 0 && __off != 0) || sync())
4186 return pos_type(off_type(-1));
Marshall Clow5ea44302015-08-27 14:37:22 +00004187 // __width > 0 || __off == 0, now check __way
4188 if (__way != ios_base::beg && __way != ios_base::cur && __way != ios_base::end)
Howard Hinnant4b53f502010-06-01 20:09:18 +00004189 return pos_type(off_type(-1));
Howard Hinnant4b53f502010-06-01 20:09:18 +00004190 pos_type __r = __bufptr_->pubseekoff(__width * __off, __way, __om);
4191 __r.state(__st_);
4192 return __r;
4193}
4194
4195template <class _Codecvt, class _Elem, class _Tr>
4196typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4197wbuffer_convert<_Codecvt, _Elem, _Tr>::seekpos(pos_type __sp, ios_base::openmode __wch)
4198{
4199 if (__cv_ == 0 || __bufptr_ == 0 || sync())
4200 return pos_type(off_type(-1));
4201 if (__bufptr_->pubseekpos(__sp, __wch) == pos_type(off_type(-1)))
4202 return pos_type(off_type(-1));
4203 return __sp;
4204}
4205
4206template <class _Codecvt, class _Elem, class _Tr>
4207int
4208wbuffer_convert<_Codecvt, _Elem, _Tr>::sync()
4209{
4210 if (__cv_ == 0 || __bufptr_ == 0)
4211 return 0;
4212 if (__cm_ & ios_base::out)
4213 {
4214 if (this->pptr() != this->pbase())
4215 if (overflow() == traits_type::eof())
4216 return -1;
4217 codecvt_base::result __r;
4218 do
4219 {
4220 char* __extbe;
4221 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
4222 streamsize __nmemb = static_cast<streamsize>(__extbe - __extbuf_);
4223 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4224 return -1;
4225 } while (__r == codecvt_base::partial);
4226 if (__r == codecvt_base::error)
4227 return -1;
4228 if (__bufptr_->pubsync())
4229 return -1;
4230 }
4231 else if (__cm_ & ios_base::in)
4232 {
4233 off_type __c;
4234 if (__always_noconv_)
4235 __c = this->egptr() - this->gptr();
4236 else
4237 {
4238 int __width = __cv_->encoding();
4239 __c = __extbufend_ - __extbufnext_;
4240 if (__width > 0)
4241 __c += __width * (this->egptr() - this->gptr());
4242 else
4243 {
4244 if (this->gptr() != this->egptr())
4245 {
4246 reverse(this->gptr(), this->egptr());
4247 codecvt_base::result __r;
4248 const char_type* __e = this->gptr();
4249 char* __extbe;
4250 do
4251 {
4252 __r = __cv_->out(__st_, __e, this->egptr(), __e,
4253 __extbuf_, __extbuf_ + __ebs_, __extbe);
4254 switch (__r)
4255 {
4256 case codecvt_base::noconv:
4257 __c += this->egptr() - this->gptr();
4258 break;
4259 case codecvt_base::ok:
4260 case codecvt_base::partial:
4261 __c += __extbe - __extbuf_;
4262 break;
4263 default:
4264 return -1;
4265 }
4266 } while (__r == codecvt_base::partial);
4267 }
4268 }
4269 }
4270 if (__bufptr_->pubseekoff(-__c, ios_base::cur, __cm_) == pos_type(off_type(-1)))
4271 return -1;
4272 this->setg(0, 0, 0);
4273 __cm_ = 0;
4274 }
4275 return 0;
4276}
4277
4278template <class _Codecvt, class _Elem, class _Tr>
4279bool
4280wbuffer_convert<_Codecvt, _Elem, _Tr>::__read_mode()
4281{
4282 if (!(__cm_ & ios_base::in))
4283 {
4284 this->setp(0, 0);
4285 if (__always_noconv_)
4286 this->setg((char_type*)__extbuf_,
4287 (char_type*)__extbuf_ + __ebs_,
4288 (char_type*)__extbuf_ + __ebs_);
4289 else
4290 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
4291 __cm_ = ios_base::in;
4292 return true;
4293 }
4294 return false;
4295}
4296
4297template <class _Codecvt, class _Elem, class _Tr>
4298void
4299wbuffer_convert<_Codecvt, _Elem, _Tr>::__write_mode()
4300{
4301 if (!(__cm_ & ios_base::out))
4302 {
4303 this->setg(0, 0, 0);
4304 if (__ebs_ > sizeof(__extbuf_min_))
4305 {
4306 if (__always_noconv_)
4307 this->setp((char_type*)__extbuf_,
4308 (char_type*)__extbuf_ + (__ebs_ - 1));
4309 else
4310 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
4311 }
4312 else
4313 this->setp(0, 0);
4314 __cm_ = ios_base::out;
4315 }
4316}
4317
4318template <class _Codecvt, class _Elem, class _Tr>
4319wbuffer_convert<_Codecvt, _Elem, _Tr>*
4320wbuffer_convert<_Codecvt, _Elem, _Tr>::__close()
4321{
4322 wbuffer_convert* __rt = 0;
4323 if (__cv_ != 0 && __bufptr_ != 0)
4324 {
4325 __rt = this;
4326 if ((__cm_ & ios_base::out) && sync())
4327 __rt = 0;
4328 }
4329 return __rt;
Howard Hinnantd23b4642010-05-31 20:58:54 +00004330}
4331
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004332_LIBCPP_END_NAMESPACE_STD
4333
Eric Fiselier018a3d52017-05-31 22:07:49 +00004334_LIBCPP_POP_MACROS
4335
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004336#endif // _LIBCPP_LOCALE