blob: 7be358e4da84115f11d4e566a4b7ffd0adf6063a [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- locale ------------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-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 Hinnantc51e1022010-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 Hinnant7c9e5732011-05-31 15:34:58 +000039 locale() noexcept;
40 locale(const locale& other) noexcept;
Howard Hinnantc51e1022010-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 Hinnant7c9e5732011-05-31 15:34:58 +000048 ~locale(); // not virtual
Howard Hinnantc51e1022010-05-11 19:42:16 +000049
Howard Hinnant7c9e5732011-05-31 15:34:58 +000050 const locale& operator=(const locale& other) noexcept;
Howard Hinnantc51e1022010-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 Hinnant7c9e5732011-05-31 15:34:58 +000068template <class Facet> bool has_facet(const locale&) noexcept;
Howard Hinnantc51e1022010-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 Hinnant9dd7e892010-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 Clowccaa0fb2013-08-27 20:18:59 +000096 explicit wstring_convert(Codecvt* pcvt = new Codecvt); // explicit in C++14
Howard Hinnant9dd7e892010-05-31 20:58:54 +000097 wstring_convert(Codecvt* pcvt, state_type state);
Marshall Clowccaa0fb2013-08-27 20:18:59 +000098 explicit wstring_convert(const byte_string& byte_err, // explicit in C++14
Howard Hinnant9dd7e892010-05-31 20:58:54 +000099 const wide_string& wide_err = wide_string());
Marshall Clowccaa0fb2013-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 Hinnant9dd7e892010-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 Clowccaa0fb2013-08-27 20:18:59 +0000114 size_t converted() const; // noexcept in C++14
Howard Hinnant9dd7e892010-05-31 20:58:54 +0000115 state_type state() const;
116};
117
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118template <class Codecvt, class Elem = wchar_t, class Tr = char_traits<Elem>>
Howard Hinnant9dd7e892010-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 Clowccaa0fb2013-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 Hinnant9dd7e892010-05-31 20:58:54 +0000131 streambuf* rdbuf() const;
132 streambuf* rdbuf(streambuf* bytebuf);
133
134 state_type state() const;
135};
Howard Hinnantc51e1022010-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 Fiselier98e428d2016-06-19 06:58:22 +0000183#include <__debug>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000184#include <algorithm>
185#include <memory>
186#include <ios>
187#include <streambuf>
188#include <iterator>
189#include <limits>
Marshall Clowdde4bfe2013-03-18 17:45:34 +0000190#ifndef __APPLE__
Howard Hinnant155c2af2010-05-24 17:49:41 +0000191#include <cstdarg>
192#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000193#include <cstdlib>
194#include <ctime>
Eric Fiselierbb999f92017-05-31 22:14:05 +0000195#include <cstdio>
Ed Schouten118b6032015-03-11 16:39:36 +0000196#ifdef _LIBCPP_HAS_CATOPEN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000197#include <nl_types.h>
Marshall Clow3477ec92014-07-10 15:20:28 +0000198#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000199
Marshall Clowdde4bfe2013-03-18 17:45:34 +0000200#ifdef __APPLE__
Howard Hinnant48fd5d52012-11-14 21:17:15 +0000201#include <Availability.h>
202#endif
203
Ben Craig3756b922016-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 Fiselierf4433a32017-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 Hinnantc51e1022010-05-11 19:42:16 +0000218_LIBCPP_BEGIN_NAMESPACE_STD
219
Marshall Clow82378c02013-03-18 19:34:07 +0000220#if defined(__APPLE__) || defined(__FreeBSD__)
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000221# define _LIBCPP_GET_C_LOCALE 0
Ed Schoutenaa497c82015-03-10 09:35:22 +0000222#elif defined(__CloudABI__) || defined(__NetBSD__)
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000223# define _LIBCPP_GET_C_LOCALE LC_C_LOCALE
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000224#else
225# define _LIBCPP_GET_C_LOCALE __cloc()
Howard Hinnantf312e3e2011-09-28 23:39:33 +0000226 // Get the C locale object
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000227 _LIBCPP_FUNC_VIS locale_t __cloc();
Howard Hinnantf312e3e2011-09-28 23:39:33 +0000228#define __cloc_defined
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000229#endif
230
Howard Hinnantc51e1022010-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 Tokerb8a95f52014-05-15 11:27:39 +0000243// If on exit __b == __e, eofbit is set in __err. If __case_sensitive is false,
Howard Hinnantc51e1022010-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 Hinnant28b24882011-12-01 20:21:04 +0000258 size_t __nkw = static_cast<size_t>(_VSTD::distance(__kb, __ke));
Howard Hinnantc51e1022010-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 Fiseliera09a3b42014-10-27 19:28:20 +0000276 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
Howard Hinnantc51e1022010-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 Fiseliera09a3b42014-10-27 19:28:20 +0000302 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
Howard Hinnantc51e1022010-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 Fiseliera09a3b42014-10-27 19:28:20 +0000336 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
Howard Hinnantc51e1022010-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 Fiseliera09a3b42014-10-27 19:28:20 +0000351 for (__st = __status; __kb != __ke; ++__kb, (void) ++__st)
Howard Hinnantc51e1022010-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 Hinnanta37d3cf2013-08-12 18:38:34 +0000359struct _LIBCPP_TYPE_VIS __num_get_base
Howard Hinnantc51e1022010-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 Hinnanta37d3cf2013-08-12 18:38:34 +0000367_LIBCPP_FUNC_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000368void __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end,
369 ios_base::iostate& __err);
370
Howard Hinnantc51e1022010-05-11 19:42:16 +0000371template <class _CharT>
372struct __num_get
373 : protected __num_get_base
374{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000375 static string __stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
376 _CharT& __thousands_sep);
Aditya Kumaraa866182017-06-14 23:17:45 +0000377
Howard Hinnantc51e1022010-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 Kumaraa866182017-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 Hinnantc51e1022010-05-11 19:42:16 +0000423};
424
Aditya Kumaraa866182017-06-14 23:17:45 +0000425#ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
Howard Hinnantc51e1022010-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 Kumaraa866182017-06-14 23:17:45 +0000436#endif
Howard Hinnantc51e1022010-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 Kumaraa866182017-06-14 23:17:45 +0000453#ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
Howard Hinnantc51e1022010-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 Kumaraa866182017-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 Hinnantc51e1022010-05-11 19:42:16 +0000463{
Howard Hinnantf57b3fc2011-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 Hinnant28b24882011-12-01 20:21:04 +0000470 if (__grouping.size() != 0 && __ct == __thousands_sep)
Howard Hinnantc51e1022010-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 Hinnantf57b3fc2011-03-09 01:03:19 +0000480 if (__f >= 24)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000481 return -1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000482 switch (__base)
483 {
484 case 8:
485 case 10:
486 if (__f >= __base)
Howard Hinnantf57b3fc2011-03-09 01:03:19 +0000487 return -1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000488 break;
Howard Hinnantf57b3fc2011-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 Hinnantc51e1022010-05-11 19:42:16 +0000496 return 0;
Howard Hinnantf57b3fc2011-03-09 01:03:19 +0000497 }
498 return -1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000499 }
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000500 *__a_end++ = __src[__f];
Howard Hinnantc51e1022010-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 Hinnant5132e192012-02-15 19:19:37 +0000536 if (__x == '-' || __x == '+')
537 {
Howard Hinnant21413152013-03-08 19:06:24 +0000538 if (__a_end == __a || (__a_end[-1] & 0x5F) == (__exp & 0x7F))
Howard Hinnant5132e192012-02-15 19:19:37 +0000539 {
540 *__a_end++ = __x;
541 return 0;
542 }
543 return -1;
544 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000545 if (__x == 'x' || __x == 'X')
546 __exp = 'P';
Howard Hinnant21413152013-03-08 19:06:24 +0000547 else if ((__x & 0x5F) == __exp)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000548 {
Howard Hinnant21413152013-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 Hinnantc51e1022010-05-11 19:42:16 +0000556 }
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000557 *__a_end++ = __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000558 if (__f >= 22)
559 return 0;
560 ++__dc;
561 return 0;
562}
563
Eric Fiselier1b57fa82016-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 Hinnantc51e1022010-05-11 19:42:16 +0000566
567template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000568class _LIBCPP_TEMPLATE_VIS num_get
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +0000576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000577 explicit num_get(size_t __refs = 0)
578 : locale::facet(__refs) {}
579
Louis Dionne16fe2952018-07-11 23:14:33 +0000580 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +0000587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +0000594 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +0000601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +0000608 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +0000615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +0000622 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +0000629 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +0000636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +0000643 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +0000650 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +0000660 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000661 ~num_get() {}
662
Marshall Clowae385382013-11-05 14:28:52 +0000663 template <class _Fp>
Shoaib Meenai69c57412017-03-02 03:02:50 +0000664 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
665 iter_type __do_get_floating_point
Marshall Clowae385382013-11-05 14:28:52 +0000666 (iter_type __b, iter_type __e, ios_base& __iob,
667 ios_base::iostate& __err, _Fp& __v) const;
Marshall Clow96d86d82013-11-07 01:00:50 +0000668
669 template <class _Signed>
Shoaib Meenai69c57412017-03-02 03:02:50 +0000670 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
671 iter_type __do_get_signed
Marshall Clow96d86d82013-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 Meenai69c57412017-03-02 03:02:50 +0000676 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
677 iter_type __do_get_unsigned
Marshall Clow96d86d82013-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 Hinnantc51e1022010-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 Hinnantca8923c2013-01-22 17:26:08 +0000736 typename remove_reference<decltype(errno)>::type __save_errno = errno;
Howard Hinnant05c71342011-02-25 19:52:41 +0000737 errno = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000738 char *__p2;
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000739 long long __ll = strtoll_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
Howard Hinnantca8923c2013-01-22 17:26:08 +0000740 typename remove_reference<decltype(errno)>::type __current_errno = errno;
Howard Hinnant05c71342011-02-25 19:52:41 +0000741 if (__current_errno == 0)
742 errno = __save_errno;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000743 if (__p2 != __a_end)
744 {
745 __err = ios_base::failbit;
746 return 0;
747 }
Howard Hinnant05c71342011-02-25 19:52:41 +0000748 else if (__current_errno == ERANGE ||
749 __ll < numeric_limits<_Tp>::min() ||
750 numeric_limits<_Tp>::max() < __ll)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000751 {
752 __err = ios_base::failbit;
Howard Hinnant05c71342011-02-25 19:52:41 +0000753 if (__ll > 0)
754 return numeric_limits<_Tp>::max();
755 else
756 return numeric_limits<_Tp>::min();
Howard Hinnantc51e1022010-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 Fiselierc03318c2018-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 Hinnant05c71342011-02-25 19:52:41 +0000775 }
Howard Hinnantca8923c2013-01-22 17:26:08 +0000776 typename remove_reference<decltype(errno)>::type __save_errno = errno;
Howard Hinnant05c71342011-02-25 19:52:41 +0000777 errno = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000778 char *__p2;
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000779 unsigned long long __ll = strtoull_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
Howard Hinnantca8923c2013-01-22 17:26:08 +0000780 typename remove_reference<decltype(errno)>::type __current_errno = errno;
Howard Hinnant05c71342011-02-25 19:52:41 +0000781 if (__current_errno == 0)
782 errno = __save_errno;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000783 if (__p2 != __a_end)
784 {
785 __err = ios_base::failbit;
786 return 0;
787 }
Eric Fiselierc03318c2018-03-29 01:18:53 +0000788 else if (__current_errno == ERANGE || numeric_limits<_Tp>::max() < __ll)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000789 {
790 __err = ios_base::failbit;
791 return numeric_limits<_Tp>::max();
792 }
Eric Fiselierc03318c2018-03-29 01:18:53 +0000793 _Tp __res = static_cast<_Tp>(__ll);
794 if (__negate) __res = -__res;
795 return __res;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000796 }
797 __err = ios_base::failbit;
798 return 0;
799}
800
801template <class _Tp>
Eric Fiselier98e428d2016-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 Meenai54c6fd62016-12-24 18:05:32 +0000824_LIBCPP_HIDDEN
Howard Hinnantc51e1022010-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 Hinnantc9567812013-04-13 18:19:25 +0000830 typename remove_reference<decltype(errno)>::type __save_errno = errno;
831 errno = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000832 char *__p2;
Eric Fiselier98e428d2016-06-19 06:58:22 +0000833 _Tp __ld = __do_strtod<_Tp>(__a, &__p2);
Howard Hinnantc9567812013-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 Hinnantc51e1022010-05-11 19:42:16 +0000837 if (__p2 != __a_end)
838 {
839 __err = ios_base::failbit;
840 return 0;
841 }
Howard Hinnantc9567812013-04-13 18:19:25 +0000842 else if (__current_errno == ERANGE)
843 __err = ios_base::failbit;
Eric Fiselier98e428d2016-06-19 06:58:22 +0000844 return __ld;
Howard Hinnantc51e1022010-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 Clow96d86d82013-11-07 01:00:50 +0000886// signed
887
Howard Hinnantc51e1022010-05-11 19:42:16 +0000888template <class _CharT, class _InputIterator>
Marshall Clow96d86d82013-11-07 01:00:50 +0000889template <class _Signed>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000890_InputIterator
Marshall Clow96d86d82013-11-07 01:00:50 +0000891num_get<_CharT, _InputIterator>::__do_get_signed(iter_type __b, iter_type __e,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000892 ios_base& __iob,
893 ios_base::iostate& __err,
Marshall Clow96d86d82013-11-07 01:00:50 +0000894 _Signed& __v) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000895{
896 // Stage 1
897 int __base = this->__get_base(__iob);
898 // Stage 2
Howard Hinnantc51e1022010-05-11 19:42:16 +0000899 char_type __thousands_sep;
Aditya Kumaraa866182017-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 Hinnantc51e1022010-05-11 19:42:16 +0000907 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Aditya Kumaraa866182017-06-14 23:17:45 +0000908#endif
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000909 string __buf;
910 __buf.resize(__buf.capacity());
911 char* __a = &__buf[0];
Howard Hinnantc51e1022010-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 Hinnant5d4013d2013-04-15 20:40:06 +0000917 {
Joerg Sonnenberger7a804f62014-02-07 21:14:29 +0000918 if (__a_end == __a + __buf.size())
Howard Hinnant5d4013d2013-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 Hinnant3b6579a2010-08-22 00:02:43 +0000926 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000927 __thousands_sep, __grouping, __g, __g_end,
928 __atoms))
929 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000930 }
Howard Hinnantc51e1022010-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 Clow96d86d82013-11-07 01:00:50 +0000934 __v = __num_get_signed_integral<_Signed>(__a, __a_end, __err, __base);
Howard Hinnantc51e1022010-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 Clow96d86d82013-11-07 01:00:50 +0000943// unsigned
Howard Hinnantc51e1022010-05-11 19:42:16 +0000944
945template <class _CharT, class _InputIterator>
Marshall Clow96d86d82013-11-07 01:00:50 +0000946template <class _Unsigned>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947_InputIterator
Marshall Clow96d86d82013-11-07 01:00:50 +0000948num_get<_CharT, _InputIterator>::__do_get_unsigned(iter_type __b, iter_type __e,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949 ios_base& __iob,
950 ios_base::iostate& __err,
Marshall Clow96d86d82013-11-07 01:00:50 +0000951 _Unsigned& __v) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000952{
953 // Stage 1
954 int __base = this->__get_base(__iob);
955 // Stage 2
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956 char_type __thousands_sep;
Aditya Kumaraa866182017-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 Hinnantc51e1022010-05-11 19:42:16 +0000964 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Aditya Kumaraa866182017-06-14 23:17:45 +0000965#endif
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000966 string __buf;
967 __buf.resize(__buf.capacity());
968 char* __a = &__buf[0];
Howard Hinnantc51e1022010-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 Hinnant5d4013d2013-04-15 20:40:06 +0000974 {
Joerg Sonnenberger7a804f62014-02-07 21:14:29 +0000975 if (__a_end == __a + __buf.size())
Howard Hinnant5d4013d2013-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 Hinnant3b6579a2010-08-22 00:02:43 +0000983 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000984 __thousands_sep, __grouping, __g, __g_end,
985 __atoms))
986 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000987 }
Howard Hinnantc51e1022010-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 Clow96d86d82013-11-07 01:00:50 +0000991 __v = __num_get_unsigned_integral<_Unsigned>(__a, __a_end, __err, __base);
Howard Hinnantc51e1022010-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 Clowae385382013-11-05 14:28:52 +00001000// floating point
1001
Howard Hinnantc51e1022010-05-11 19:42:16 +00001002template <class _CharT, class _InputIterator>
Marshall Clowae385382013-11-05 14:28:52 +00001003template <class _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004_InputIterator
Marshall Clowae385382013-11-05 14:28:52 +00001005num_get<_CharT, _InputIterator>::__do_get_floating_point(iter_type __b, iter_type __e,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006 ios_base& __iob,
1007 ios_base::iostate& __err,
Marshall Clowae385382013-11-05 14:28:52 +00001008 _Fp& __v) const
Howard Hinnantc51e1022010-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 Hinnant3b6579a2010-08-22 00:02:43 +00001015 string __grouping = this->__stage2_float_prep(__iob, __atoms,
1016 __decimal_point,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017 __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001018 string __buf;
1019 __buf.resize(__buf.capacity());
1020 char* __a = &__buf[0];
Howard Hinnantc51e1022010-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 Hinnant5d4013d2013-04-15 20:40:06 +00001028 {
Joerg Sonnenberger7a804f62014-02-07 21:14:29 +00001029 if (__a_end == __a + __buf.size())
Howard Hinnant5d4013d2013-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 Hinnant3b6579a2010-08-22 00:02:43 +00001037 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1038 __decimal_point, __thousands_sep,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039 __grouping, __g, __g_end,
1040 __dc, __atoms))
1041 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001042 }
Howard Hinnantc51e1022010-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 Clowae385382013-11-05 14:28:52 +00001046 __v = __num_get_float<_Fp>(__a, __a_end, __err);
Howard Hinnantc51e1022010-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 Hinnantc51e1022010-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 Hinnant28b24882011-12-01 20:21:04 +00001066 char_type __thousands_sep = 0;
Howard Hinnantc51e1022010-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 Hinnant5d4013d2013-04-15 20:40:06 +00001070 string __buf;
1071 __buf.resize(__buf.capacity());
1072 char* __a = &__buf[0];
Howard Hinnantc51e1022010-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 Hinnant5d4013d2013-04-15 20:40:06 +00001078 {
Joerg Sonnenberger7a804f62014-02-07 21:14:29 +00001079 if (__a_end == __a + __buf.size())
Howard Hinnant5d4013d2013-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 Hinnant3b6579a2010-08-22 00:02:43 +00001087 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1088 __thousands_sep, __grouping,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089 __g, __g_end, __atoms))
1090 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001091 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092 // Stage 3
Marshall Clow0db963d2014-05-21 16:02:20 +00001093 __buf.resize(__a_end - __a);
Ben Craig3756b922016-03-09 15:39:39 +00001094 if (__libcpp_sscanf_l(__buf.c_str(), _LIBCPP_GET_C_LOCALE, "%p", &__v) != 1)
Howard Hinnantc51e1022010-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 Fiselier1b57fa82016-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 Hinnantc51e1022010-05-11 19:42:16 +00001104
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001105struct _LIBCPP_TYPE_VIS __num_put_base
Howard Hinnantc51e1022010-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 Hinnant1ab52da2011-09-28 21:05:01 +00001199 if (!isxdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200 break;
1201 }
1202 else
1203 {
1204 for (__ns = __nf; __ns < __ne; ++__ns)
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001205 if (!isdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
Howard Hinnantc51e1022010-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 Fiselier1b57fa82016-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 Hinnantc51e1022010-05-11 19:42:16 +00001254
1255template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001256class _LIBCPP_TEMPLATE_VIS num_put
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +00001264 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001265 explicit num_put(size_t __refs = 0)
1266 : locale::facet(__refs) {}
1267
Louis Dionne16fe2952018-07-11 23:14:33 +00001268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +00001275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +00001282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +00001289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +00001296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +00001303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +00001310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +00001317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +00001327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Hinnant48fd5d52012-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 Hinnant97955172012-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 Hinnant48fd5d52012-11-14 21:17:15 +00001425#endif
1426
Howard Hinnantc51e1022010-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 Hinnant8ea98242013-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 Hinnantc51e1022010-05-11 19:42:16 +00001440 string_type __nm = __v ? __np.truename() : __np.falsename();
Howard Hinnant8ea98242013-08-23 17:37:05 +00001441#endif
Howard Hinnantc51e1022010-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 Andriccfb7b5d2017-05-06 20:58:50 +00001458 + ((__iob.flags() & ios_base::showbase) != 0)
Eric Fiselier4cc37352016-04-29 07:23:20 +00001459 + 2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460 char __nar[__nbuf];
Ben Craig3756b922016-03-09 15:39:39 +00001461 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantc51e1022010-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 Andriccfb7b5d2017-05-06 20:58:50 +00001485 + ((__iob.flags() & ios_base::showbase) != 0)
Marshall Clow7ac088f2015-01-26 17:24:52 +00001486 + 2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001487 char __nar[__nbuf];
Ben Craig3756b922016-03-09 15:39:39 +00001488 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantc51e1022010-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 Andriccfb7b5d2017-05-06 20:58:50 +00001512 + ((__iob.flags() & ios_base::showbase) != 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001513 + 1;
1514 char __nar[__nbuf];
Ben Craig3756b922016-03-09 15:39:39 +00001515 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantc51e1022010-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 Andriccfb7b5d2017-05-06 20:58:50 +00001539 + ((__iob.flags() & ios_base::showbase) != 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540 + 1;
1541 char __nar[__nbuf];
Ben Craig3756b922016-03-09 15:39:39 +00001542 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantc51e1022010-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 Craig3756b922016-03-09 15:39:39 +00001569 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
Howard Hinnant155c2af2010-05-24 17:49:41 +00001570 (int)__iob.precision(), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001571 else
Ben Craig3756b922016-03-09 15:39:39 +00001572 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantc51e1022010-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 Craig3756b922016-03-09 15:39:39 +00001577 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578 else
Ben Craig3756b922016-03-09 15:39:39 +00001579 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantc51e1022010-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 Hinnant28b24882011-12-01 20:21:04 +00001592 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
Howard Hinnantc51e1022010-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 Craig3756b922016-03-09 15:39:39 +00001620 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
Howard Hinnant155c2af2010-05-24 17:49:41 +00001621 (int)__iob.precision(), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001622 else
Ben Craig3756b922016-03-09 15:39:39 +00001623 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantc51e1022010-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 Craig3756b922016-03-09 15:39:39 +00001628 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001629 else
Ben Craig3756b922016-03-09 15:39:39 +00001630 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantc51e1022010-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 Hinnant28b24882011-12-01 20:21:04 +00001643 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
Howard Hinnantc51e1022010-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 Craig3756b922016-03-09 15:39:39 +00001666 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantc51e1022010-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 Fiselier1b57fa82016-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 Hinnantc51e1022010-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 Fiseliera09a3b42014-10-27 19:28:20 +00001708 for (++__b, (void) --__n; __b != __e && __n > 0; ++__b, (void) --__n)
Howard Hinnantc51e1022010-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 Hinnant8331b762013-03-06 23:30:19 +00001721class _LIBCPP_TYPE_VIS time_base
Howard Hinnantc51e1022010-05-11 19:42:16 +00001722{
1723public:
1724 enum dateorder {no_order, dmy, mdy, ymd, ydm};
1725};
1726
1727template <class _CharT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001728class _LIBCPP_TEMPLATE_VIS __time_get_c_storage
Howard Hinnantc51e1022010-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 Fiselier5eb6efc2015-08-18 19:39:35 +00001740
Louis Dionne16fe2952018-07-11 23:14:33 +00001741 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5eb6efc2015-08-18 19:39:35 +00001742 ~__time_get_c_storage() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001743};
1744
Eric Fiselierf30c0af2017-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 Hinnantc51e1022010-05-11 19:42:16 +00001761template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001762class _LIBCPP_TEMPLATE_VIS time_get
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +00001773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001774 explicit time_get(size_t __refs = 0)
1775 : locale::facet(__refs) {}
1776
Louis Dionne16fe2952018-07-11 23:14:33 +00001777 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001778 dateorder date_order() const
1779 {
1780 return this->do_date_order();
1781 }
1782
Louis Dionne16fe2952018-07-11 23:14:33 +00001783 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +00001790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +00001797 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +00001804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +00001811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +00001818 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +00001833 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Tokerb8a95f52014-05-15 11:27:39 +00001914// time_get primitives
Howard Hinnantc51e1022010-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 Hinnant28b24882011-12-01 20:21:04 +00001925 ptrdiff_t __i = __scan_keyword(__b, __e, __wk, __wk+14, __ct, __err, false) - __wk;
Howard Hinnantc51e1022010-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 Hinnant28b24882011-12-01 20:21:04 +00001939 ptrdiff_t __i = __scan_keyword(__b, __e, __month, __month+24, __ct, __err, false) - __month;
Howard Hinnantc51e1022010-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 Hinnant28b24882011-12-01 20:21:04 +00002111 ptrdiff_t __i = __scan_keyword(__b, __e, __ap, __ap+2, __ct, __err, false) - __ap;
Howard Hinnantc51e1022010-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 Tokerb8a95f52014-05-15 11:27:39 +00002135// time_get end primitives
Howard Hinnantc51e1022010-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 Hinnantc51e1022010-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 Hinnant28b24882011-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 Hinnantc51e1022010-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 Hinnant28b24882011-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 Hinnantc51e1022010-05-11 19:42:16 +00002294 }
2295 break;
Howard Hinnantf60ff4e2011-04-10 17:54:14 +00002296 case 'F':
2297 {
Howard Hinnant28b24882011-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 Hinnantf60ff4e2011-04-10 17:54:14 +00002300 }
2301 break;
Howard Hinnantc51e1022010-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 Hinnant28b24882011-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 Hinnantc51e1022010-05-11 19:42:16 +00002328 }
2329 break;
2330 case 'R':
2331 {
Howard Hinnant28b24882011-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 Hinnantc51e1022010-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 Hinnant28b24882011-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 Hinnantc51e1022010-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 Hinnant28b24882011-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 Hinnantc51e1022010-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 Fiselier1b57fa82016-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 Hinnantc51e1022010-05-11 19:42:16 +00002373
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002374class _LIBCPP_TYPE_VIS __time_get
Howard Hinnantc51e1022010-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 Fiselierb5eb1bf2017-01-04 23:56:00 +00002385class _LIBCPP_TEMPLATE_VIS __time_get_storage
Howard Hinnantc51e1022010-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 Dionne16fe2952018-07-11 23:14:33 +00002402 _LIBCPP_INLINE_VISIBILITY ~__time_get_storage() {}
Howard Hinnantc51e1022010-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
Louis Dionne5254b372018-10-25 12:13:43 +00002411#define _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(_CharT) \
2412template <> _LIBCPP_FUNC_VIS time_base::dateorder __time_get_storage<_CharT>::__do_date_order() const; \
2413template <> _LIBCPP_FUNC_VIS __time_get_storage<_CharT>::__time_get_storage(const char*); \
2414template <> _LIBCPP_FUNC_VIS __time_get_storage<_CharT>::__time_get_storage(const string&); \
2415template <> _LIBCPP_FUNC_VIS void __time_get_storage<_CharT>::init(const ctype<_CharT>&); \
2416template <> _LIBCPP_FUNC_VIS __time_get_storage<_CharT>::string_type __time_get_storage<_CharT>::__analyze(char, const ctype<_CharT>&); \
2417extern template _LIBCPP_FUNC_VIS time_base::dateorder __time_get_storage<_CharT>::__do_date_order() const; \
2418extern template _LIBCPP_FUNC_VIS __time_get_storage<_CharT>::__time_get_storage(const char*); \
2419extern template _LIBCPP_FUNC_VIS __time_get_storage<_CharT>::__time_get_storage(const string&); \
2420extern template _LIBCPP_FUNC_VIS void __time_get_storage<_CharT>::init(const ctype<_CharT>&); \
2421extern template _LIBCPP_FUNC_VIS __time_get_storage<_CharT>::string_type __time_get_storage<_CharT>::__analyze(char, const ctype<_CharT>&); \
2422/**/
2423
2424_LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(char)
2425_LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(wchar_t)
2426#undef _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION
2427
Howard Hinnantc51e1022010-05-11 19:42:16 +00002428template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002429class _LIBCPP_TEMPLATE_VIS time_get_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00002430 : public time_get<_CharT, _InputIterator>,
2431 private __time_get_storage<_CharT>
2432{
2433public:
2434 typedef time_base::dateorder dateorder;
2435 typedef _InputIterator iter_type;
2436 typedef _CharT char_type;
2437 typedef basic_string<char_type> string_type;
2438
Howard Hinnant756c69b2010-09-22 16:48:34 +00002439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002440 explicit time_get_byname(const char* __nm, size_t __refs = 0)
2441 : time_get<_CharT, _InputIterator>(__refs),
2442 __time_get_storage<_CharT>(__nm) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002443 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002444 explicit time_get_byname(const string& __nm, size_t __refs = 0)
2445 : time_get<_CharT, _InputIterator>(__refs),
2446 __time_get_storage<_CharT>(__nm) {}
2447
2448protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002449 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002450 ~time_get_byname() {}
2451
Howard Hinnant756c69b2010-09-22 16:48:34 +00002452 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002453 virtual dateorder do_date_order() const {return this->__do_date_order();}
2454private:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002455 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002456 virtual const string_type* __weeks() const {return this->__weeks_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002458 virtual const string_type* __months() const {return this->__months_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002459 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002460 virtual const string_type* __am_pm() const {return this->__am_pm_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002461 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002462 virtual const string_type& __c() const {return this->__c_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002463 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002464 virtual const string_type& __r() const {return this->__r_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002465 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002466 virtual const string_type& __x() const {return this->__x_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468 virtual const string_type& __X() const {return this->__X_;}
2469};
2470
Eric Fiselier1b57fa82016-09-15 22:27:07 +00002471_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<char>)
2472_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002473
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002474class _LIBCPP_TYPE_VIS __time_put
Howard Hinnantc51e1022010-05-11 19:42:16 +00002475{
2476 locale_t __loc_;
2477protected:
Louis Dionne16fe2952018-07-11 23:14:33 +00002478 _LIBCPP_INLINE_VISIBILITY __time_put() : __loc_(_LIBCPP_GET_C_LOCALE) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002479 __time_put(const char* __nm);
2480 __time_put(const string& __nm);
2481 ~__time_put();
2482 void __do_put(char* __nb, char*& __ne, const tm* __tm,
2483 char __fmt, char __mod) const;
2484 void __do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm,
2485 char __fmt, char __mod) const;
2486};
2487
2488template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002489class _LIBCPP_TEMPLATE_VIS time_put
Howard Hinnantc51e1022010-05-11 19:42:16 +00002490 : public locale::facet,
2491 private __time_put
2492{
2493public:
2494 typedef _CharT char_type;
2495 typedef _OutputIterator iter_type;
2496
Louis Dionne16fe2952018-07-11 23:14:33 +00002497 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002498 explicit time_put(size_t __refs = 0)
2499 : locale::facet(__refs) {}
2500
2501 iter_type put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm,
2502 const char_type* __pb, const char_type* __pe) const;
2503
Louis Dionne16fe2952018-07-11 23:14:33 +00002504 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002505 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
2506 const tm* __tm, char __fmt, char __mod = 0) const
2507 {
2508 return do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2509 }
2510
2511 static locale::id id;
2512
2513protected:
Louis Dionne16fe2952018-07-11 23:14:33 +00002514 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002515 ~time_put() {}
2516 virtual iter_type do_put(iter_type __s, ios_base&, char_type, const tm* __tm,
2517 char __fmt, char __mod) const;
2518
Louis Dionne16fe2952018-07-11 23:14:33 +00002519 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002520 explicit time_put(const char* __nm, size_t __refs)
2521 : locale::facet(__refs),
2522 __time_put(__nm) {}
Louis Dionne16fe2952018-07-11 23:14:33 +00002523 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002524 explicit time_put(const string& __nm, size_t __refs)
2525 : locale::facet(__refs),
2526 __time_put(__nm) {}
2527};
2528
2529template <class _CharT, class _OutputIterator>
2530locale::id
2531time_put<_CharT, _OutputIterator>::id;
2532
2533template <class _CharT, class _OutputIterator>
2534_OutputIterator
2535time_put<_CharT, _OutputIterator>::put(iter_type __s, ios_base& __iob,
2536 char_type __fl, const tm* __tm,
2537 const char_type* __pb,
2538 const char_type* __pe) const
2539{
2540 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2541 for (; __pb != __pe; ++__pb)
2542 {
2543 if (__ct.narrow(*__pb, 0) == '%')
2544 {
2545 if (++__pb == __pe)
2546 {
2547 *__s++ = __pb[-1];
2548 break;
2549 }
2550 char __mod = 0;
2551 char __fmt = __ct.narrow(*__pb, 0);
2552 if (__fmt == 'E' || __fmt == 'O')
2553 {
2554 if (++__pb == __pe)
2555 {
2556 *__s++ = __pb[-2];
2557 *__s++ = __pb[-1];
2558 break;
2559 }
2560 __mod = __fmt;
2561 __fmt = __ct.narrow(*__pb, 0);
2562 }
2563 __s = do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2564 }
2565 else
2566 *__s++ = *__pb;
2567 }
2568 return __s;
2569}
2570
2571template <class _CharT, class _OutputIterator>
2572_OutputIterator
Howard Hinnant28b24882011-12-01 20:21:04 +00002573time_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002574 char_type, const tm* __tm,
2575 char __fmt, char __mod) const
2576{
2577 char_type __nar[100];
2578 char_type* __nb = __nar;
2579 char_type* __ne = __nb + 100;
2580 __do_put(__nb, __ne, __tm, __fmt, __mod);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002581 return _VSTD::copy(__nb, __ne, __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002582}
2583
Eric Fiselier1b57fa82016-09-15 22:27:07 +00002584_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<char>)
2585_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002586
2587template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002588class _LIBCPP_TEMPLATE_VIS time_put_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00002589 : public time_put<_CharT, _OutputIterator>
2590{
2591public:
Louis Dionne16fe2952018-07-11 23:14:33 +00002592 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002593 explicit time_put_byname(const char* __nm, size_t __refs = 0)
2594 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2595
Louis Dionne16fe2952018-07-11 23:14:33 +00002596 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597 explicit time_put_byname(const string& __nm, size_t __refs = 0)
2598 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2599
2600protected:
Louis Dionne16fe2952018-07-11 23:14:33 +00002601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002602 ~time_put_byname() {}
2603};
2604
Eric Fiselier1b57fa82016-09-15 22:27:07 +00002605_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<char>)
2606_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002607
2608// money_base
2609
Howard Hinnant8331b762013-03-06 23:30:19 +00002610class _LIBCPP_TYPE_VIS money_base
Howard Hinnantc51e1022010-05-11 19:42:16 +00002611{
2612public:
2613 enum part {none, space, symbol, sign, value};
2614 struct pattern {char field[4];};
2615
Louis Dionne16fe2952018-07-11 23:14:33 +00002616 _LIBCPP_INLINE_VISIBILITY money_base() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002617};
2618
2619// moneypunct
2620
2621template <class _CharT, bool _International = false>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002622class _LIBCPP_TEMPLATE_VIS moneypunct
Howard Hinnantc51e1022010-05-11 19:42:16 +00002623 : public locale::facet,
2624 public money_base
2625{
2626public:
2627 typedef _CharT char_type;
2628 typedef basic_string<char_type> string_type;
2629
Louis Dionne16fe2952018-07-11 23:14:33 +00002630 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002631 explicit moneypunct(size_t __refs = 0)
2632 : locale::facet(__refs) {}
2633
Louis Dionne16fe2952018-07-11 23:14:33 +00002634 _LIBCPP_INLINE_VISIBILITY char_type decimal_point() const {return do_decimal_point();}
2635 _LIBCPP_INLINE_VISIBILITY char_type thousands_sep() const {return do_thousands_sep();}
2636 _LIBCPP_INLINE_VISIBILITY string grouping() const {return do_grouping();}
2637 _LIBCPP_INLINE_VISIBILITY string_type curr_symbol() const {return do_curr_symbol();}
2638 _LIBCPP_INLINE_VISIBILITY string_type positive_sign() const {return do_positive_sign();}
2639 _LIBCPP_INLINE_VISIBILITY string_type negative_sign() const {return do_negative_sign();}
2640 _LIBCPP_INLINE_VISIBILITY int frac_digits() const {return do_frac_digits();}
2641 _LIBCPP_INLINE_VISIBILITY pattern pos_format() const {return do_pos_format();}
2642 _LIBCPP_INLINE_VISIBILITY pattern neg_format() const {return do_neg_format();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002643
2644 static locale::id id;
2645 static const bool intl = _International;
2646
2647protected:
Louis Dionne16fe2952018-07-11 23:14:33 +00002648 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002649 ~moneypunct() {}
2650
2651 virtual char_type do_decimal_point() const {return numeric_limits<char_type>::max();}
2652 virtual char_type do_thousands_sep() const {return numeric_limits<char_type>::max();}
2653 virtual string do_grouping() const {return string();}
2654 virtual string_type do_curr_symbol() const {return string_type();}
2655 virtual string_type do_positive_sign() const {return string_type();}
2656 virtual string_type do_negative_sign() const {return string_type(1, '-');}
2657 virtual int do_frac_digits() const {return 0;}
2658 virtual pattern do_pos_format() const
Howard Hinnantb10e6cf2012-11-06 21:48:33 +00002659 {pattern __p = {{symbol, sign, none, value}}; return __p;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002660 virtual pattern do_neg_format() const
Howard Hinnantb10e6cf2012-11-06 21:48:33 +00002661 {pattern __p = {{symbol, sign, none, value}}; return __p;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002662};
2663
2664template <class _CharT, bool _International>
2665locale::id
2666moneypunct<_CharT, _International>::id;
2667
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002668template <class _CharT, bool _International>
2669const bool
2670moneypunct<_CharT, _International>::intl;
2671
Eric Fiselier1b57fa82016-09-15 22:27:07 +00002672_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, false>)
2673_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, true>)
2674_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, false>)
2675_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002676
2677// moneypunct_byname
2678
2679template <class _CharT, bool _International = false>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002680class _LIBCPP_TEMPLATE_VIS moneypunct_byname
Howard Hinnant756c69b2010-09-22 16:48:34 +00002681 : public moneypunct<_CharT, _International>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682{
2683public:
2684 typedef money_base::pattern pattern;
2685 typedef _CharT char_type;
2686 typedef basic_string<char_type> string_type;
2687
Louis Dionne16fe2952018-07-11 23:14:33 +00002688 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002689 explicit moneypunct_byname(const char* __nm, size_t __refs = 0)
2690 : moneypunct<_CharT, _International>(__refs) {init(__nm);}
2691
Louis Dionne16fe2952018-07-11 23:14:33 +00002692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002693 explicit moneypunct_byname(const string& __nm, size_t __refs = 0)
2694 : moneypunct<_CharT, _International>(__refs) {init(__nm.c_str());}
2695
2696protected:
Louis Dionne16fe2952018-07-11 23:14:33 +00002697 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698 ~moneypunct_byname() {}
2699
2700 virtual char_type do_decimal_point() const {return __decimal_point_;}
2701 virtual char_type do_thousands_sep() const {return __thousands_sep_;}
2702 virtual string do_grouping() const {return __grouping_;}
2703 virtual string_type do_curr_symbol() const {return __curr_symbol_;}
2704 virtual string_type do_positive_sign() const {return __positive_sign_;}
2705 virtual string_type do_negative_sign() const {return __negative_sign_;}
2706 virtual int do_frac_digits() const {return __frac_digits_;}
2707 virtual pattern do_pos_format() const {return __pos_format_;}
2708 virtual pattern do_neg_format() const {return __neg_format_;}
2709
2710private:
2711 char_type __decimal_point_;
2712 char_type __thousands_sep_;
2713 string __grouping_;
2714 string_type __curr_symbol_;
2715 string_type __positive_sign_;
2716 string_type __negative_sign_;
2717 int __frac_digits_;
2718 pattern __pos_format_;
2719 pattern __neg_format_;
2720
2721 void init(const char*);
2722};
2723
Shoaib Meenai094c3d22017-04-03 04:04:24 +00002724template<> _LIBCPP_FUNC_VIS void moneypunct_byname<char, false>::init(const char*);
2725template<> _LIBCPP_FUNC_VIS void moneypunct_byname<char, true>::init(const char*);
2726template<> _LIBCPP_FUNC_VIS void moneypunct_byname<wchar_t, false>::init(const char*);
2727template<> _LIBCPP_FUNC_VIS void moneypunct_byname<wchar_t, true>::init(const char*);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002728
Eric Fiselier1b57fa82016-09-15 22:27:07 +00002729_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, false>)
2730_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, true>)
2731_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, false>)
2732_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002733
2734// money_get
2735
2736template <class _CharT>
2737class __money_get
2738{
2739protected:
2740 typedef _CharT char_type;
2741 typedef basic_string<char_type> string_type;
2742
Louis Dionne16fe2952018-07-11 23:14:33 +00002743 _LIBCPP_INLINE_VISIBILITY __money_get() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002744
2745 static void __gather_info(bool __intl, const locale& __loc,
2746 money_base::pattern& __pat, char_type& __dp,
2747 char_type& __ts, string& __grp,
2748 string_type& __sym, string_type& __psn,
2749 string_type& __nsn, int& __fd);
2750};
2751
2752template <class _CharT>
2753void
2754__money_get<_CharT>::__gather_info(bool __intl, const locale& __loc,
2755 money_base::pattern& __pat, char_type& __dp,
2756 char_type& __ts, string& __grp,
2757 string_type& __sym, string_type& __psn,
2758 string_type& __nsn, int& __fd)
2759{
2760 if (__intl)
2761 {
2762 const moneypunct<char_type, true>& __mp =
2763 use_facet<moneypunct<char_type, true> >(__loc);
2764 __pat = __mp.neg_format();
2765 __nsn = __mp.negative_sign();
2766 __psn = __mp.positive_sign();
2767 __dp = __mp.decimal_point();
2768 __ts = __mp.thousands_sep();
2769 __grp = __mp.grouping();
2770 __sym = __mp.curr_symbol();
2771 __fd = __mp.frac_digits();
2772 }
2773 else
2774 {
2775 const moneypunct<char_type, false>& __mp =
2776 use_facet<moneypunct<char_type, false> >(__loc);
2777 __pat = __mp.neg_format();
2778 __nsn = __mp.negative_sign();
2779 __psn = __mp.positive_sign();
2780 __dp = __mp.decimal_point();
2781 __ts = __mp.thousands_sep();
2782 __grp = __mp.grouping();
2783 __sym = __mp.curr_symbol();
2784 __fd = __mp.frac_digits();
2785 }
2786}
2787
Eric Fiselier1b57fa82016-09-15 22:27:07 +00002788_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<char>)
2789_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790
2791template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002792class _LIBCPP_TEMPLATE_VIS money_get
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793 : public locale::facet,
2794 private __money_get<_CharT>
2795{
2796public:
2797 typedef _CharT char_type;
2798 typedef _InputIterator iter_type;
2799 typedef basic_string<char_type> string_type;
2800
Louis Dionne16fe2952018-07-11 23:14:33 +00002801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002802 explicit money_get(size_t __refs = 0)
2803 : locale::facet(__refs) {}
2804
Louis Dionne16fe2952018-07-11 23:14:33 +00002805 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002806 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
2807 ios_base::iostate& __err, long double& __v) const
2808 {
2809 return do_get(__b, __e, __intl, __iob, __err, __v);
2810 }
2811
Louis Dionne16fe2952018-07-11 23:14:33 +00002812 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002813 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
2814 ios_base::iostate& __err, string_type& __v) const
2815 {
2816 return do_get(__b, __e, __intl, __iob, __err, __v);
2817 }
2818
2819 static locale::id id;
2820
2821protected:
2822
Louis Dionne16fe2952018-07-11 23:14:33 +00002823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824 ~money_get() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002825
Howard Hinnantc51e1022010-05-11 19:42:16 +00002826 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
2827 ios_base& __iob, ios_base::iostate& __err,
2828 long double& __v) const;
2829 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
2830 ios_base& __iob, ios_base::iostate& __err,
2831 string_type& __v) const;
2832
2833private:
2834 static bool __do_get(iter_type& __b, iter_type __e,
2835 bool __intl, const locale& __loc,
2836 ios_base::fmtflags __flags, ios_base::iostate& __err,
2837 bool& __neg, const ctype<char_type>& __ct,
2838 unique_ptr<char_type, void(*)(void*)>& __wb,
2839 char_type*& __wn, char_type* __we);
2840};
2841
2842template <class _CharT, class _InputIterator>
2843locale::id
2844money_get<_CharT, _InputIterator>::id;
2845
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002846_LIBCPP_FUNC_VIS void __do_nothing(void*);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002847
2848template <class _Tp>
2849_LIBCPP_HIDDEN
2850void
2851__double_or_nothing(unique_ptr<_Tp, void(*)(void*)>& __b, _Tp*& __n, _Tp*& __e)
2852{
2853 bool __owns = __b.get_deleter() != __do_nothing;
Howard Hinnant28b24882011-12-01 20:21:04 +00002854 size_t __cur_cap = static_cast<size_t>(__e-__b.get()) * sizeof(_Tp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002855 size_t __new_cap = __cur_cap < numeric_limits<size_t>::max() / 2 ?
2856 2 * __cur_cap : numeric_limits<size_t>::max();
Marshall Clow6c9ddc22014-10-27 19:08:10 +00002857 if (__new_cap == 0)
2858 __new_cap = sizeof(_Tp);
Howard Hinnant28b24882011-12-01 20:21:04 +00002859 size_t __n_off = static_cast<size_t>(__n - __b.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002860 _Tp* __t = (_Tp*)realloc(__owns ? __b.get() : 0, __new_cap);
2861 if (__t == 0)
2862 __throw_bad_alloc();
2863 if (__owns)
2864 __b.release();
2865 __b = unique_ptr<_Tp, void(*)(void*)>(__t, free);
2866 __new_cap /= sizeof(_Tp);
2867 __n = __b.get() + __n_off;
2868 __e = __b.get() + __new_cap;
2869}
2870
2871// true == success
2872template <class _CharT, class _InputIterator>
2873bool
2874money_get<_CharT, _InputIterator>::__do_get(iter_type& __b, iter_type __e,
2875 bool __intl, const locale& __loc,
2876 ios_base::fmtflags __flags,
2877 ios_base::iostate& __err,
2878 bool& __neg,
2879 const ctype<char_type>& __ct,
2880 unique_ptr<char_type, void(*)(void*)>& __wb,
2881 char_type*& __wn, char_type* __we)
2882{
2883 const unsigned __bz = 100;
2884 unsigned __gbuf[__bz];
2885 unique_ptr<unsigned, void(*)(void*)> __gb(__gbuf, __do_nothing);
2886 unsigned* __gn = __gb.get();
2887 unsigned* __ge = __gn + __bz;
2888 money_base::pattern __pat;
2889 char_type __dp;
2890 char_type __ts;
2891 string __grp;
2892 string_type __sym;
2893 string_type __psn;
2894 string_type __nsn;
Jeffrey Yasskin6b045fb2012-03-10 18:31:43 +00002895 // Capture the spaces read into money_base::{space,none} so they
2896 // can be compared to initial spaces in __sym.
2897 string_type __spaces;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002898 int __fd;
2899 __money_get<_CharT>::__gather_info(__intl, __loc, __pat, __dp, __ts, __grp,
2900 __sym, __psn, __nsn, __fd);
2901 const string_type* __trailing_sign = 0;
2902 __wn = __wb.get();
2903 for (unsigned __p = 0; __p < 4 && __b != __e; ++__p)
2904 {
2905 switch (__pat.field[__p])
2906 {
2907 case money_base::space:
2908 if (__p != 3)
2909 {
2910 if (__ct.is(ctype_base::space, *__b))
Jeffrey Yasskin6b045fb2012-03-10 18:31:43 +00002911 __spaces.push_back(*__b++);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002912 else
2913 {
2914 __err |= ios_base::failbit;
2915 return false;
2916 }
2917 }
Eric Fiselier4db80032017-05-05 20:32:26 +00002918 _LIBCPP_FALLTHROUGH();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002919 case money_base::none:
2920 if (__p != 3)
2921 {
2922 while (__b != __e && __ct.is(ctype_base::space, *__b))
Jeffrey Yasskin6b045fb2012-03-10 18:31:43 +00002923 __spaces.push_back(*__b++);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002924 }
2925 break;
2926 case money_base::sign:
2927 if (__psn.size() + __nsn.size() > 0)
2928 {
2929 if (__psn.size() == 0 || __nsn.size() == 0)
2930 { // sign is optional
2931 if (__psn.size() > 0)
2932 { // __nsn.size() == 0
2933 if (*__b == __psn[0])
2934 {
2935 ++__b;
2936 if (__psn.size() > 1)
2937 __trailing_sign = &__psn;
2938 }
2939 else
2940 __neg = true;
2941 }
2942 else if (*__b == __nsn[0]) // __nsn.size() > 0 && __psn.size() == 0
2943 {
2944 ++__b;
2945 __neg = true;
2946 if (__nsn.size() > 1)
2947 __trailing_sign = &__nsn;
2948 }
2949 }
2950 else // sign is required
2951 {
2952 if (*__b == __psn[0])
2953 {
2954 ++__b;
2955 if (__psn.size() > 1)
2956 __trailing_sign = &__psn;
2957 }
2958 else if (*__b == __nsn[0])
2959 {
2960 ++__b;
2961 __neg = true;
2962 if (__nsn.size() > 1)
2963 __trailing_sign = &__nsn;
2964 }
2965 else
2966 {
2967 __err |= ios_base::failbit;
2968 return false;
2969 }
2970 }
2971 }
2972 break;
2973 case money_base::symbol:
2974 {
2975 bool __more_needed = __trailing_sign ||
2976 (__p < 2) ||
2977 (__p == 2 && __pat.field[3] != static_cast<char>(money_base::none));
Marshall Clowdfcbb432013-10-13 01:02:45 +00002978 bool __sb = (__flags & ios_base::showbase) != 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002979 if (__sb || __more_needed)
2980 {
Jeffrey Yasskin6b045fb2012-03-10 18:31:43 +00002981 typename string_type::const_iterator __sym_space_end = __sym.begin();
2982 if (__p > 0 && (__pat.field[__p - 1] == money_base::none ||
2983 __pat.field[__p - 1] == money_base::space)) {
2984 // Match spaces we've already read against spaces at
2985 // the beginning of __sym.
2986 while (__sym_space_end != __sym.end() &&
2987 __ct.is(ctype_base::space, *__sym_space_end))
2988 ++__sym_space_end;
2989 const size_t __num_spaces = __sym_space_end - __sym.begin();
2990 if (__num_spaces > __spaces.size() ||
2991 !equal(__spaces.end() - __num_spaces, __spaces.end(),
2992 __sym.begin())) {
2993 // No match. Put __sym_space_end back at the
2994 // beginning of __sym, which will prevent a
2995 // match in the next loop.
2996 __sym_space_end = __sym.begin();
2997 }
2998 }
2999 typename string_type::const_iterator __sym_curr_char = __sym_space_end;
3000 while (__sym_curr_char != __sym.end() && __b != __e &&
3001 *__b == *__sym_curr_char) {
3002 ++__b;
3003 ++__sym_curr_char;
3004 }
3005 if (__sb && __sym_curr_char != __sym.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003006 {
3007 __err |= ios_base::failbit;
3008 return false;
3009 }
3010 }
3011 }
3012 break;
3013 case money_base::value:
3014 {
3015 unsigned __ng = 0;
3016 for (; __b != __e; ++__b)
3017 {
3018 char_type __c = *__b;
3019 if (__ct.is(ctype_base::digit, __c))
3020 {
3021 if (__wn == __we)
3022 __double_or_nothing(__wb, __wn, __we);
3023 *__wn++ = __c;
3024 ++__ng;
3025 }
3026 else if (__grp.size() > 0 && __ng > 0 && __c == __ts)
3027 {
3028 if (__gn == __ge)
3029 __double_or_nothing(__gb, __gn, __ge);
3030 *__gn++ = __ng;
3031 __ng = 0;
3032 }
3033 else
3034 break;
3035 }
3036 if (__gb.get() != __gn && __ng > 0)
3037 {
3038 if (__gn == __ge)
3039 __double_or_nothing(__gb, __gn, __ge);
3040 *__gn++ = __ng;
3041 }
3042 if (__fd > 0)
3043 {
3044 if (__b == __e || *__b != __dp)
3045 {
3046 __err |= ios_base::failbit;
3047 return false;
3048 }
3049 for (++__b; __fd > 0; --__fd, ++__b)
3050 {
3051 if (__b == __e || !__ct.is(ctype_base::digit, *__b))
3052 {
3053 __err |= ios_base::failbit;
3054 return false;
3055 }
3056 if (__wn == __we)
3057 __double_or_nothing(__wb, __wn, __we);
3058 *__wn++ = *__b;
3059 }
3060 }
3061 if (__wn == __wb.get())
3062 {
3063 __err |= ios_base::failbit;
3064 return false;
3065 }
3066 }
3067 break;
3068 }
3069 }
3070 if (__trailing_sign)
3071 {
3072 for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, ++__b)
3073 {
3074 if (__b == __e || *__b != (*__trailing_sign)[__i])
3075 {
3076 __err |= ios_base::failbit;
3077 return false;
3078 }
3079 }
3080 }
3081 if (__gb.get() != __gn)
3082 {
3083 ios_base::iostate __et = ios_base::goodbit;
3084 __check_grouping(__grp, __gb.get(), __gn, __et);
3085 if (__et)
3086 {
3087 __err |= ios_base::failbit;
3088 return false;
3089 }
3090 }
3091 return true;
3092}
3093
3094template <class _CharT, class _InputIterator>
3095_InputIterator
3096money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3097 bool __intl, ios_base& __iob,
3098 ios_base::iostate& __err,
3099 long double& __v) const
3100{
Howard Hinnant28b24882011-12-01 20:21:04 +00003101 const int __bz = 100;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003102 char_type __wbuf[__bz];
3103 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3104 char_type* __wn;
3105 char_type* __we = __wbuf + __bz;
3106 locale __loc = __iob.getloc();
3107 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3108 bool __neg = false;
3109 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3110 __wb, __wn, __we))
3111 {
3112 const char __src[] = "0123456789";
3113 char_type __atoms[sizeof(__src)-1];
3114 __ct.widen(__src, __src + (sizeof(__src)-1), __atoms);
3115 char __nbuf[__bz];
3116 char* __nc = __nbuf;
3117 unique_ptr<char, void(*)(void*)> __h(0, free);
3118 if (__wn - __wb.get() > __bz-2)
3119 {
Howard Hinnant28b24882011-12-01 20:21:04 +00003120 __h.reset((char*)malloc(static_cast<size_t>(__wn - __wb.get() + 2)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003121 if (__h.get() == 0)
3122 __throw_bad_alloc();
3123 __nc = __h.get();
3124 }
3125 if (__neg)
3126 *__nc++ = '-';
3127 for (const char_type* __w = __wb.get(); __w < __wn; ++__w, ++__nc)
Marshall Clow1e68fd42013-03-22 02:14:40 +00003128 *__nc = __src[find(__atoms, _VSTD::end(__atoms), *__w) - __atoms];
Howard Hinnantc51e1022010-05-11 19:42:16 +00003129 *__nc = char();
3130 if (sscanf(__nbuf, "%Lf", &__v) != 1)
3131 __throw_runtime_error("money_get error");
3132 }
3133 if (__b == __e)
3134 __err |= ios_base::eofbit;
3135 return __b;
3136}
3137
3138template <class _CharT, class _InputIterator>
3139_InputIterator
3140money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3141 bool __intl, ios_base& __iob,
3142 ios_base::iostate& __err,
3143 string_type& __v) const
3144{
Howard Hinnant28b24882011-12-01 20:21:04 +00003145 const int __bz = 100;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003146 char_type __wbuf[__bz];
3147 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3148 char_type* __wn;
3149 char_type* __we = __wbuf + __bz;
3150 locale __loc = __iob.getloc();
3151 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3152 bool __neg = false;
3153 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3154 __wb, __wn, __we))
3155 {
3156 __v.clear();
3157 if (__neg)
3158 __v.push_back(__ct.widen('-'));
3159 char_type __z = __ct.widen('0');
3160 char_type* __w;
3161 for (__w = __wb.get(); __w < __wn-1; ++__w)
3162 if (*__w != __z)
3163 break;
3164 __v.append(__w, __wn);
3165 }
3166 if (__b == __e)
3167 __err |= ios_base::eofbit;
3168 return __b;
3169}
3170
Eric Fiselier1b57fa82016-09-15 22:27:07 +00003171_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<char>)
3172_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003173
3174// money_put
3175
3176template <class _CharT>
3177class __money_put
3178{
3179protected:
3180 typedef _CharT char_type;
3181 typedef basic_string<char_type> string_type;
3182
Louis Dionne16fe2952018-07-11 23:14:33 +00003183 _LIBCPP_INLINE_VISIBILITY __money_put() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003184
3185 static void __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 static void __format(char_type* __mb, char_type*& __mi, char_type*& __me,
3191 ios_base::fmtflags __flags,
3192 const char_type* __db, const char_type* __de,
3193 const ctype<char_type>& __ct, bool __neg,
3194 const money_base::pattern& __pat, char_type __dp,
3195 char_type __ts, const string& __grp,
3196 const string_type& __sym, const string_type& __sn,
3197 int __fd);
3198};
3199
3200template <class _CharT>
3201void
3202__money_put<_CharT>::__gather_info(bool __intl, bool __neg, const locale& __loc,
3203 money_base::pattern& __pat, char_type& __dp,
3204 char_type& __ts, string& __grp,
3205 string_type& __sym, string_type& __sn,
3206 int& __fd)
3207{
3208 if (__intl)
3209 {
3210 const moneypunct<char_type, true>& __mp =
3211 use_facet<moneypunct<char_type, true> >(__loc);
3212 if (__neg)
3213 {
3214 __pat = __mp.neg_format();
3215 __sn = __mp.negative_sign();
3216 }
3217 else
3218 {
3219 __pat = __mp.pos_format();
3220 __sn = __mp.positive_sign();
3221 }
3222 __dp = __mp.decimal_point();
3223 __ts = __mp.thousands_sep();
3224 __grp = __mp.grouping();
3225 __sym = __mp.curr_symbol();
3226 __fd = __mp.frac_digits();
3227 }
3228 else
3229 {
3230 const moneypunct<char_type, false>& __mp =
3231 use_facet<moneypunct<char_type, false> >(__loc);
3232 if (__neg)
3233 {
3234 __pat = __mp.neg_format();
3235 __sn = __mp.negative_sign();
3236 }
3237 else
3238 {
3239 __pat = __mp.pos_format();
3240 __sn = __mp.positive_sign();
3241 }
3242 __dp = __mp.decimal_point();
3243 __ts = __mp.thousands_sep();
3244 __grp = __mp.grouping();
3245 __sym = __mp.curr_symbol();
3246 __fd = __mp.frac_digits();
3247 }
3248}
3249
3250template <class _CharT>
3251void
3252__money_put<_CharT>::__format(char_type* __mb, char_type*& __mi, char_type*& __me,
3253 ios_base::fmtflags __flags,
3254 const char_type* __db, const char_type* __de,
3255 const ctype<char_type>& __ct, bool __neg,
3256 const money_base::pattern& __pat, char_type __dp,
3257 char_type __ts, const string& __grp,
3258 const string_type& __sym, const string_type& __sn,
3259 int __fd)
3260{
3261 __me = __mb;
3262 for (unsigned __p = 0; __p < 4; ++__p)
3263 {
3264 switch (__pat.field[__p])
3265 {
3266 case money_base::none:
3267 __mi = __me;
3268 break;
3269 case money_base::space:
3270 __mi = __me;
3271 *__me++ = __ct.widen(' ');
3272 break;
3273 case money_base::sign:
3274 if (!__sn.empty())
3275 *__me++ = __sn[0];
3276 break;
3277 case money_base::symbol:
3278 if (!__sym.empty() && (__flags & ios_base::showbase))
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003279 __me = _VSTD::copy(__sym.begin(), __sym.end(), __me);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003280 break;
3281 case money_base::value:
3282 {
3283 // remember start of value so we can reverse it
3284 char_type* __t = __me;
3285 // find beginning of digits
3286 if (__neg)
3287 ++__db;
3288 // find end of digits
3289 const char_type* __d;
3290 for (__d = __db; __d < __de; ++__d)
3291 if (!__ct.is(ctype_base::digit, *__d))
3292 break;
3293 // print fractional part
3294 if (__fd > 0)
3295 {
3296 int __f;
3297 for (__f = __fd; __d > __db && __f > 0; --__f)
3298 *__me++ = *--__d;
3299 char_type __z = __f > 0 ? __ct.widen('0') : char_type();
3300 for (; __f > 0; --__f)
3301 *__me++ = __z;
3302 *__me++ = __dp;
3303 }
3304 // print units part
3305 if (__d == __db)
3306 {
3307 *__me++ = __ct.widen('0');
3308 }
3309 else
3310 {
3311 unsigned __ng = 0;
3312 unsigned __ig = 0;
3313 unsigned __gl = __grp.empty() ? numeric_limits<unsigned>::max()
3314 : static_cast<unsigned>(__grp[__ig]);
3315 while (__d != __db)
3316 {
3317 if (__ng == __gl)
3318 {
3319 *__me++ = __ts;
3320 __ng = 0;
3321 if (++__ig < __grp.size())
3322 __gl = __grp[__ig] == numeric_limits<char>::max() ?
3323 numeric_limits<unsigned>::max() :
3324 static_cast<unsigned>(__grp[__ig]);
3325 }
3326 *__me++ = *--__d;
3327 ++__ng;
3328 }
3329 }
3330 // reverse it
3331 reverse(__t, __me);
3332 }
3333 break;
3334 }
3335 }
3336 // print rest of sign, if any
3337 if (__sn.size() > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003338 __me = _VSTD::copy(__sn.begin()+1, __sn.end(), __me);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003339 // set alignment
3340 if ((__flags & ios_base::adjustfield) == ios_base::left)
3341 __mi = __me;
3342 else if ((__flags & ios_base::adjustfield) != ios_base::internal)
3343 __mi = __mb;
3344}
3345
Eric Fiselier1b57fa82016-09-15 22:27:07 +00003346_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<char>)
3347_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003348
3349template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003350class _LIBCPP_TEMPLATE_VIS money_put
Howard Hinnantc51e1022010-05-11 19:42:16 +00003351 : public locale::facet,
3352 private __money_put<_CharT>
3353{
3354public:
3355 typedef _CharT char_type;
3356 typedef _OutputIterator iter_type;
3357 typedef basic_string<char_type> string_type;
3358
Louis Dionne16fe2952018-07-11 23:14:33 +00003359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003360 explicit money_put(size_t __refs = 0)
3361 : locale::facet(__refs) {}
3362
Louis Dionne16fe2952018-07-11 23:14:33 +00003363 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003364 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3365 long double __units) const
3366 {
3367 return do_put(__s, __intl, __iob, __fl, __units);
3368 }
3369
Louis Dionne16fe2952018-07-11 23:14:33 +00003370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003371 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3372 const string_type& __digits) const
3373 {
3374 return do_put(__s, __intl, __iob, __fl, __digits);
3375 }
3376
3377 static locale::id id;
3378
3379protected:
Louis Dionne16fe2952018-07-11 23:14:33 +00003380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003381 ~money_put() {}
3382
3383 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3384 char_type __fl, long double __units) const;
3385 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3386 char_type __fl, const string_type& __digits) const;
3387};
3388
3389template <class _CharT, class _OutputIterator>
3390locale::id
3391money_put<_CharT, _OutputIterator>::id;
3392
3393template <class _CharT, class _OutputIterator>
3394_OutputIterator
3395money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3396 ios_base& __iob, char_type __fl,
3397 long double __units) const
3398{
3399 // convert to char
3400 const size_t __bs = 100;
3401 char __buf[__bs];
3402 char* __bb = __buf;
3403 char_type __digits[__bs];
3404 char_type* __db = __digits;
Howard Hinnant28b24882011-12-01 20:21:04 +00003405 size_t __n = static_cast<size_t>(snprintf(__bb, __bs, "%.0Lf", __units));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003406 unique_ptr<char, void(*)(void*)> __hn(0, free);
3407 unique_ptr<char_type, void(*)(void*)> __hd(0, free);
3408 // secure memory for digit storage
3409 if (__n > __bs-1)
3410 {
Ben Craig3756b922016-03-09 15:39:39 +00003411 __n = static_cast<size_t>(__libcpp_asprintf_l(&__bb, _LIBCPP_GET_C_LOCALE, "%.0Lf", __units));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003412 if (__bb == 0)
3413 __throw_bad_alloc();
3414 __hn.reset(__bb);
3415 __hd.reset((char_type*)malloc(__n * sizeof(char_type)));
Howard Hinnant03de6f92012-03-07 20:37:43 +00003416 if (__hd == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003417 __throw_bad_alloc();
3418 __db = __hd.get();
3419 }
3420 // gather info
3421 locale __loc = __iob.getloc();
3422 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3423 __ct.widen(__bb, __bb + __n, __db);
3424 bool __neg = __n > 0 && __bb[0] == '-';
3425 money_base::pattern __pat;
3426 char_type __dp;
3427 char_type __ts;
3428 string __grp;
3429 string_type __sym;
3430 string_type __sn;
3431 int __fd;
3432 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3433 // secure memory for formatting
3434 char_type __mbuf[__bs];
3435 char_type* __mb = __mbuf;
3436 unique_ptr<char_type, void(*)(void*)> __hw(0, free);
3437 size_t __exn = static_cast<int>(__n) > __fd ?
Howard Hinnant28b24882011-12-01 20:21:04 +00003438 (__n - static_cast<size_t>(__fd)) * 2 + __sn.size() +
3439 __sym.size() + static_cast<size_t>(__fd) + 1
3440 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003441 if (__exn > __bs)
3442 {
3443 __hw.reset((char_type*)malloc(__exn * sizeof(char_type)));
3444 __mb = __hw.get();
3445 if (__mb == 0)
3446 __throw_bad_alloc();
3447 }
3448 // format
3449 char_type* __mi;
3450 char_type* __me;
3451 this->__format(__mb, __mi, __me, __iob.flags(),
3452 __db, __db + __n, __ct,
3453 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3454 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3455}
3456
3457template <class _CharT, class _OutputIterator>
3458_OutputIterator
3459money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3460 ios_base& __iob, char_type __fl,
3461 const string_type& __digits) const
3462{
3463 // gather info
3464 locale __loc = __iob.getloc();
3465 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3466 bool __neg = __digits.size() > 0 && __digits[0] == __ct.widen('-');
3467 money_base::pattern __pat;
3468 char_type __dp;
3469 char_type __ts;
3470 string __grp;
3471 string_type __sym;
3472 string_type __sn;
3473 int __fd;
3474 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3475 // secure memory for formatting
3476 char_type __mbuf[100];
3477 char_type* __mb = __mbuf;
3478 unique_ptr<char_type, void(*)(void*)> __h(0, free);
Howard Hinnant28b24882011-12-01 20:21:04 +00003479 size_t __exn = static_cast<int>(__digits.size()) > __fd ?
3480 (__digits.size() - static_cast<size_t>(__fd)) * 2 +
3481 __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 1
3482 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003483 if (__exn > 100)
3484 {
3485 __h.reset((char_type*)malloc(__exn * sizeof(char_type)));
3486 __mb = __h.get();
3487 if (__mb == 0)
3488 __throw_bad_alloc();
3489 }
3490 // format
3491 char_type* __mi;
3492 char_type* __me;
3493 this->__format(__mb, __mi, __me, __iob.flags(),
3494 __digits.data(), __digits.data() + __digits.size(), __ct,
3495 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3496 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3497}
3498
Eric Fiselier1b57fa82016-09-15 22:27:07 +00003499_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<char>)
3500_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501
3502// messages
3503
Howard Hinnant8331b762013-03-06 23:30:19 +00003504class _LIBCPP_TYPE_VIS messages_base
Howard Hinnantc51e1022010-05-11 19:42:16 +00003505{
3506public:
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003507 typedef ptrdiff_t catalog;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003508
Louis Dionne16fe2952018-07-11 23:14:33 +00003509 _LIBCPP_INLINE_VISIBILITY messages_base() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003510};
3511
3512template <class _CharT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003513class _LIBCPP_TEMPLATE_VIS messages
Howard Hinnantc51e1022010-05-11 19:42:16 +00003514 : public locale::facet,
3515 public messages_base
3516{
3517public:
3518 typedef _CharT char_type;
3519 typedef basic_string<_CharT> string_type;
3520
Louis Dionne16fe2952018-07-11 23:14:33 +00003521 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003522 explicit messages(size_t __refs = 0)
3523 : locale::facet(__refs) {}
3524
Louis Dionne16fe2952018-07-11 23:14:33 +00003525 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003526 catalog open(const basic_string<char>& __nm, const locale& __loc) const
3527 {
3528 return do_open(__nm, __loc);
3529 }
3530
Louis Dionne16fe2952018-07-11 23:14:33 +00003531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003532 string_type get(catalog __c, int __set, int __msgid,
3533 const string_type& __dflt) const
3534 {
3535 return do_get(__c, __set, __msgid, __dflt);
3536 }
3537
Louis Dionne16fe2952018-07-11 23:14:33 +00003538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003539 void close(catalog __c) const
3540 {
3541 do_close(__c);
3542 }
3543
3544 static locale::id id;
3545
3546protected:
Louis Dionne16fe2952018-07-11 23:14:33 +00003547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548 ~messages() {}
3549
3550 virtual catalog do_open(const basic_string<char>&, const locale&) const;
3551 virtual string_type do_get(catalog, int __set, int __msgid,
3552 const string_type& __dflt) const;
3553 virtual void do_close(catalog) const;
3554};
3555
3556template <class _CharT>
3557locale::id
3558messages<_CharT>::id;
3559
3560template <class _CharT>
3561typename messages<_CharT>::catalog
3562messages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const
3563{
Ed Schouten118b6032015-03-11 16:39:36 +00003564#ifdef _LIBCPP_HAS_CATOPEN
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +00003565 catalog __cat = (catalog)catopen(__nm.c_str(), NL_CAT_LOCALE);
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003566 if (__cat != -1)
3567 __cat = static_cast<catalog>((static_cast<size_t>(__cat) >> 1));
3568 return __cat;
Ed Schouten118b6032015-03-11 16:39:36 +00003569#else // !_LIBCPP_HAS_CATOPEN
3570 return -1;
3571#endif // _LIBCPP_HAS_CATOPEN
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572}
3573
3574template <class _CharT>
3575typename messages<_CharT>::string_type
3576messages<_CharT>::do_get(catalog __c, int __set, int __msgid,
3577 const string_type& __dflt) const
3578{
Ed Schouten118b6032015-03-11 16:39:36 +00003579#ifdef _LIBCPP_HAS_CATOPEN
Howard Hinnantc51e1022010-05-11 19:42:16 +00003580 string __ndflt;
3581 __narrow_to_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__ndflt),
3582 __dflt.c_str(),
3583 __dflt.c_str() + __dflt.size());
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003584 if (__c != -1)
3585 __c <<= 1;
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +00003586 nl_catd __cat = (nl_catd)__c;
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003587 char* __n = catgets(__cat, __set, __msgid, __ndflt.c_str());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003588 string_type __w;
3589 __widen_from_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__w),
3590 __n, __n + strlen(__n));
3591 return __w;
Ed Schouten118b6032015-03-11 16:39:36 +00003592#else // !_LIBCPP_HAS_CATOPEN
3593 return __dflt;
3594#endif // _LIBCPP_HAS_CATOPEN
Howard Hinnantc51e1022010-05-11 19:42:16 +00003595}
3596
3597template <class _CharT>
3598void
3599messages<_CharT>::do_close(catalog __c) const
3600{
Ed Schouten118b6032015-03-11 16:39:36 +00003601#ifdef _LIBCPP_HAS_CATOPEN
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003602 if (__c != -1)
3603 __c <<= 1;
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +00003604 nl_catd __cat = (nl_catd)__c;
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003605 catclose(__cat);
Ed Schouten118b6032015-03-11 16:39:36 +00003606#endif // _LIBCPP_HAS_CATOPEN
Howard Hinnantc51e1022010-05-11 19:42:16 +00003607}
3608
Eric Fiselier1b57fa82016-09-15 22:27:07 +00003609_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<char>)
3610_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003611
3612template <class _CharT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003613class _LIBCPP_TEMPLATE_VIS messages_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00003614 : public messages<_CharT>
3615{
3616public:
3617 typedef messages_base::catalog catalog;
3618 typedef basic_string<_CharT> string_type;
3619
Louis Dionne16fe2952018-07-11 23:14:33 +00003620 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003621 explicit messages_byname(const char*, size_t __refs = 0)
3622 : messages<_CharT>(__refs) {}
3623
Louis Dionne16fe2952018-07-11 23:14:33 +00003624 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003625 explicit messages_byname(const string&, size_t __refs = 0)
3626 : messages<_CharT>(__refs) {}
3627
3628protected:
Louis Dionne16fe2952018-07-11 23:14:33 +00003629 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003630 ~messages_byname() {}
3631};
3632
Eric Fiselier1b57fa82016-09-15 22:27:07 +00003633_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<char>)
3634_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003636template<class _Codecvt, class _Elem = wchar_t,
3637 class _Wide_alloc = allocator<_Elem>,
3638 class _Byte_alloc = allocator<char> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003639class _LIBCPP_TEMPLATE_VIS wstring_convert
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003640{
3641public:
3642 typedef basic_string<char, char_traits<char>, _Byte_alloc> byte_string;
3643 typedef basic_string<_Elem, char_traits<_Elem>, _Wide_alloc> wide_string;
3644 typedef typename _Codecvt::state_type state_type;
3645 typedef typename wide_string::traits_type::int_type int_type;
3646
3647private:
3648 byte_string __byte_err_string_;
3649 wide_string __wide_err_string_;
3650 _Codecvt* __cvtptr_;
3651 state_type __cvtstate_;
3652 size_t __cvtcount_;
3653
3654 wstring_convert(const wstring_convert& __wc);
3655 wstring_convert& operator=(const wstring_convert& __wc);
3656public:
Louis Dionne16fe2952018-07-11 23:14:33 +00003657 _LIBCPP_INLINE_VISIBILITY
Marshall Clowccaa0fb2013-08-27 20:18:59 +00003658 _LIBCPP_EXPLICIT_AFTER_CXX11 wstring_convert(_Codecvt* __pcvt = new _Codecvt);
Louis Dionne16fe2952018-07-11 23:14:33 +00003659 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003660 wstring_convert(_Codecvt* __pcvt, state_type __state);
Marshall Clowccaa0fb2013-08-27 20:18:59 +00003661 _LIBCPP_EXPLICIT_AFTER_CXX11 wstring_convert(const byte_string& __byte_err,
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003662 const wide_string& __wide_err = wide_string());
Eric Fiselierf07d88c2017-04-19 01:34:08 +00003663#ifndef _LIBCPP_CXX03_LANG
Louis Dionne16fe2952018-07-11 23:14:33 +00003664 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003665 wstring_convert(wstring_convert&& __wc);
3666#endif
3667 ~wstring_convert();
3668
Louis Dionne16fe2952018-07-11 23:14:33 +00003669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003670 wide_string from_bytes(char __byte)
3671 {return from_bytes(&__byte, &__byte+1);}
Louis Dionne16fe2952018-07-11 23:14:33 +00003672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003673 wide_string from_bytes(const char* __ptr)
3674 {return from_bytes(__ptr, __ptr + char_traits<char>::length(__ptr));}
Louis Dionne16fe2952018-07-11 23:14:33 +00003675 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003676 wide_string from_bytes(const byte_string& __str)
3677 {return from_bytes(__str.data(), __str.data() + __str.size());}
3678 wide_string from_bytes(const char* __first, const char* __last);
3679
Louis Dionne16fe2952018-07-11 23:14:33 +00003680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003681 byte_string to_bytes(_Elem __wchar)
3682 {return to_bytes(&__wchar, &__wchar+1);}
Louis Dionne16fe2952018-07-11 23:14:33 +00003683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003684 byte_string to_bytes(const _Elem* __wptr)
3685 {return to_bytes(__wptr, __wptr + char_traits<_Elem>::length(__wptr));}
Louis Dionne16fe2952018-07-11 23:14:33 +00003686 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003687 byte_string to_bytes(const wide_string& __wstr)
3688 {return to_bytes(__wstr.data(), __wstr.data() + __wstr.size());}
3689 byte_string to_bytes(const _Elem* __first, const _Elem* __last);
3690
Louis Dionne16fe2952018-07-11 23:14:33 +00003691 _LIBCPP_INLINE_VISIBILITY
Marshall Clowccaa0fb2013-08-27 20:18:59 +00003692 size_t converted() const _NOEXCEPT {return __cvtcount_;}
Louis Dionne16fe2952018-07-11 23:14:33 +00003693 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003694 state_type state() const {return __cvtstate_;}
3695};
3696
3697template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003698inline
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003699wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3700 wstring_convert(_Codecvt* __pcvt)
3701 : __cvtptr_(__pcvt), __cvtstate_(), __cvtcount_(0)
3702{
3703}
3704
3705template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003706inline
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003707wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3708 wstring_convert(_Codecvt* __pcvt, state_type __state)
3709 : __cvtptr_(__pcvt), __cvtstate_(__state), __cvtcount_(0)
3710{
3711}
3712
3713template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3714wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3715 wstring_convert(const byte_string& __byte_err, const wide_string& __wide_err)
3716 : __byte_err_string_(__byte_err), __wide_err_string_(__wide_err),
3717 __cvtstate_(), __cvtcount_(0)
3718{
3719 __cvtptr_ = new _Codecvt;
3720}
3721
Eric Fiselierf07d88c2017-04-19 01:34:08 +00003722#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003723
3724template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003725inline
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003726wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3727 wstring_convert(wstring_convert&& __wc)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003728 : __byte_err_string_(_VSTD::move(__wc.__byte_err_string_)),
3729 __wide_err_string_(_VSTD::move(__wc.__wide_err_string_)),
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003730 __cvtptr_(__wc.__cvtptr_),
Eric Fiselier35c67232016-06-26 22:56:26 +00003731 __cvtstate_(__wc.__cvtstate_), __cvtcount_(__wc.__cvtcount_)
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003732{
3733 __wc.__cvtptr_ = nullptr;
3734}
3735
Eric Fiselierf07d88c2017-04-19 01:34:08 +00003736#endif // _LIBCPP_CXX03_LANG
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003737
3738template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3739wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::~wstring_convert()
3740{
3741 delete __cvtptr_;
3742}
3743
3744template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3745typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::wide_string
3746wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3747 from_bytes(const char* __frm, const char* __frm_end)
3748{
3749 __cvtcount_ = 0;
3750 if (__cvtptr_ != nullptr)
3751 {
3752 wide_string __ws(2*(__frm_end - __frm), _Elem());
Howard Hinnant16d54f22012-07-12 18:07:41 +00003753 if (__frm != __frm_end)
3754 __ws.resize(__ws.capacity());
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003755 codecvt_base::result __r = codecvt_base::ok;
3756 state_type __st = __cvtstate_;
3757 if (__frm != __frm_end)
3758 {
3759 _Elem* __to = &__ws[0];
3760 _Elem* __to_end = __to + __ws.size();
3761 const char* __frm_nxt;
3762 do
3763 {
3764 _Elem* __to_nxt;
3765 __r = __cvtptr_->in(__st, __frm, __frm_end, __frm_nxt,
3766 __to, __to_end, __to_nxt);
3767 __cvtcount_ += __frm_nxt - __frm;
3768 if (__frm_nxt == __frm)
3769 {
3770 __r = codecvt_base::error;
3771 }
3772 else if (__r == codecvt_base::noconv)
3773 {
3774 __ws.resize(__to - &__ws[0]);
3775 // This only gets executed if _Elem is char
3776 __ws.append((const _Elem*)__frm, (const _Elem*)__frm_end);
3777 __frm = __frm_nxt;
3778 __r = codecvt_base::ok;
3779 }
3780 else if (__r == codecvt_base::ok)
3781 {
3782 __ws.resize(__to_nxt - &__ws[0]);
3783 __frm = __frm_nxt;
3784 }
3785 else if (__r == codecvt_base::partial)
3786 {
3787 ptrdiff_t __s = __to_nxt - &__ws[0];
3788 __ws.resize(2 * __s);
3789 __to = &__ws[0] + __s;
3790 __to_end = &__ws[0] + __ws.size();
3791 __frm = __frm_nxt;
3792 }
3793 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3794 }
3795 if (__r == codecvt_base::ok)
3796 return __ws;
3797 }
Marshall Clow8fea1612016-08-25 15:09:01 +00003798
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003799 if (__wide_err_string_.empty())
Marshall Clow8fea1612016-08-25 15:09:01 +00003800 __throw_range_error("wstring_convert: from_bytes error");
3801
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003802 return __wide_err_string_;
3803}
3804
3805template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3806typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::byte_string
3807wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3808 to_bytes(const _Elem* __frm, const _Elem* __frm_end)
3809{
3810 __cvtcount_ = 0;
3811 if (__cvtptr_ != nullptr)
3812 {
3813 byte_string __bs(2*(__frm_end - __frm), char());
Howard Hinnant16d54f22012-07-12 18:07:41 +00003814 if (__frm != __frm_end)
3815 __bs.resize(__bs.capacity());
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003816 codecvt_base::result __r = codecvt_base::ok;
3817 state_type __st = __cvtstate_;
3818 if (__frm != __frm_end)
3819 {
3820 char* __to = &__bs[0];
3821 char* __to_end = __to + __bs.size();
3822 const _Elem* __frm_nxt;
3823 do
3824 {
3825 char* __to_nxt;
3826 __r = __cvtptr_->out(__st, __frm, __frm_end, __frm_nxt,
3827 __to, __to_end, __to_nxt);
3828 __cvtcount_ += __frm_nxt - __frm;
3829 if (__frm_nxt == __frm)
3830 {
3831 __r = codecvt_base::error;
3832 }
3833 else if (__r == codecvt_base::noconv)
3834 {
3835 __bs.resize(__to - &__bs[0]);
3836 // This only gets executed if _Elem is char
3837 __bs.append((const char*)__frm, (const char*)__frm_end);
3838 __frm = __frm_nxt;
3839 __r = codecvt_base::ok;
3840 }
3841 else if (__r == codecvt_base::ok)
3842 {
3843 __bs.resize(__to_nxt - &__bs[0]);
3844 __frm = __frm_nxt;
3845 }
3846 else if (__r == codecvt_base::partial)
3847 {
3848 ptrdiff_t __s = __to_nxt - &__bs[0];
3849 __bs.resize(2 * __s);
3850 __to = &__bs[0] + __s;
3851 __to_end = &__bs[0] + __bs.size();
3852 __frm = __frm_nxt;
3853 }
3854 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3855 }
3856 if (__r == codecvt_base::ok)
3857 {
3858 size_t __s = __bs.size();
3859 __bs.resize(__bs.capacity());
3860 char* __to = &__bs[0] + __s;
3861 char* __to_end = __to + __bs.size();
3862 do
3863 {
3864 char* __to_nxt;
3865 __r = __cvtptr_->unshift(__st, __to, __to_end, __to_nxt);
3866 if (__r == codecvt_base::noconv)
3867 {
3868 __bs.resize(__to - &__bs[0]);
3869 __r = codecvt_base::ok;
3870 }
3871 else if (__r == codecvt_base::ok)
3872 {
3873 __bs.resize(__to_nxt - &__bs[0]);
3874 }
3875 else if (__r == codecvt_base::partial)
3876 {
Howard Hinnant28b24882011-12-01 20:21:04 +00003877 ptrdiff_t __sp = __to_nxt - &__bs[0];
3878 __bs.resize(2 * __sp);
3879 __to = &__bs[0] + __sp;
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003880 __to_end = &__bs[0] + __bs.size();
3881 }
3882 } while (__r == codecvt_base::partial);
3883 if (__r == codecvt_base::ok)
3884 return __bs;
3885 }
3886 }
Marshall Clow8fea1612016-08-25 15:09:01 +00003887
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003888 if (__byte_err_string_.empty())
Marshall Clow8fea1612016-08-25 15:09:01 +00003889 __throw_range_error("wstring_convert: to_bytes error");
3890
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003891 return __byte_err_string_;
3892}
3893
3894template <class _Codecvt, class _Elem = wchar_t, class _Tr = char_traits<_Elem> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003895class _LIBCPP_TEMPLATE_VIS wbuffer_convert
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003896 : public basic_streambuf<_Elem, _Tr>
3897{
3898public:
3899 // types:
3900 typedef _Elem char_type;
3901 typedef _Tr traits_type;
3902 typedef typename traits_type::int_type int_type;
3903 typedef typename traits_type::pos_type pos_type;
3904 typedef typename traits_type::off_type off_type;
3905 typedef typename _Codecvt::state_type state_type;
3906
3907private:
Howard Hinnant0ac182a2010-06-01 20:09:18 +00003908 char* __extbuf_;
3909 const char* __extbufnext_;
3910 const char* __extbufend_;
3911 char __extbuf_min_[8];
3912 size_t __ebs_;
3913 char_type* __intbuf_;
3914 size_t __ibs_;
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003915 streambuf* __bufptr_;
Howard Hinnant0ac182a2010-06-01 20:09:18 +00003916 _Codecvt* __cv_;
3917 state_type __st_;
3918 ios_base::openmode __cm_;
3919 bool __owns_eb_;
3920 bool __owns_ib_;
3921 bool __always_noconv_;
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003922
Howard Hinnant0ac182a2010-06-01 20:09:18 +00003923 wbuffer_convert(const wbuffer_convert&);
3924 wbuffer_convert& operator=(const wbuffer_convert&);
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003925public:
Marshall Clowccaa0fb2013-08-27 20:18:59 +00003926 _LIBCPP_EXPLICIT_AFTER_CXX11 wbuffer_convert(streambuf* __bytebuf = 0,
3927 _Codecvt* __pcvt = new _Codecvt, state_type __state = state_type());
Howard Hinnant0ac182a2010-06-01 20:09:18 +00003928 ~wbuffer_convert();
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003929
Howard Hinnant756c69b2010-09-22 16:48:34 +00003930 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003931 streambuf* rdbuf() const {return __bufptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003932 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003933 streambuf* rdbuf(streambuf* __bytebuf)
3934 {
3935 streambuf* __r = __bufptr_;
3936 __bufptr_ = __bytebuf;
3937 return __r;
3938 }
3939
Howard Hinnant756c69b2010-09-22 16:48:34 +00003940 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0ac182a2010-06-01 20:09:18 +00003941 state_type state() const {return __st_;}
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003942
3943protected:
Howard Hinnant0ac182a2010-06-01 20:09:18 +00003944 virtual int_type underflow();
3945 virtual int_type pbackfail(int_type __c = traits_type::eof());
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003946 virtual int_type overflow (int_type __c = traits_type::eof());
Howard Hinnant0ac182a2010-06-01 20:09:18 +00003947 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s,
3948 streamsize __n);
3949 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
3950 ios_base::openmode __wch = ios_base::in | ios_base::out);
3951 virtual pos_type seekpos(pos_type __sp,
3952 ios_base::openmode __wch = ios_base::in | ios_base::out);
3953 virtual int sync();
3954
3955private:
3956 bool __read_mode();
3957 void __write_mode();
3958 wbuffer_convert* __close();
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003959};
3960
3961template <class _Codecvt, class _Elem, class _Tr>
Howard Hinnant0ac182a2010-06-01 20:09:18 +00003962wbuffer_convert<_Codecvt, _Elem, _Tr>::
3963 wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt, state_type __state)
3964 : __extbuf_(0),
3965 __extbufnext_(0),
3966 __extbufend_(0),
3967 __ebs_(0),
3968 __intbuf_(0),
3969 __ibs_(0),
3970 __bufptr_(__bytebuf),
3971 __cv_(__pcvt),
3972 __st_(__state),
3973 __cm_(0),
3974 __owns_eb_(false),
3975 __owns_ib_(false),
3976 __always_noconv_(__cv_ ? __cv_->always_noconv() : false)
3977{
3978 setbuf(0, 4096);
3979}
3980
3981template <class _Codecvt, class _Elem, class _Tr>
3982wbuffer_convert<_Codecvt, _Elem, _Tr>::~wbuffer_convert()
3983{
3984 __close();
3985 delete __cv_;
3986 if (__owns_eb_)
3987 delete [] __extbuf_;
3988 if (__owns_ib_)
3989 delete [] __intbuf_;
3990}
3991
3992template <class _Codecvt, class _Elem, class _Tr>
3993typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
3994wbuffer_convert<_Codecvt, _Elem, _Tr>::underflow()
3995{
3996 if (__cv_ == 0 || __bufptr_ == 0)
3997 return traits_type::eof();
3998 bool __initial = __read_mode();
3999 char_type __1buf;
4000 if (this->gptr() == 0)
4001 this->setg(&__1buf, &__1buf+1, &__1buf+1);
4002 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
4003 int_type __c = traits_type::eof();
4004 if (this->gptr() == this->egptr())
4005 {
4006 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
4007 if (__always_noconv_)
4008 {
4009 streamsize __nmemb = static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz);
4010 __nmemb = __bufptr_->sgetn((char*)this->eback() + __unget_sz, __nmemb);
4011 if (__nmemb != 0)
4012 {
4013 this->setg(this->eback(),
4014 this->eback() + __unget_sz,
4015 this->eback() + __unget_sz + __nmemb);
4016 __c = *this->gptr();
4017 }
4018 }
4019 else
4020 {
Eric Fiselier98e428d2016-06-19 06:58:22 +00004021 _LIBCPP_ASSERT(!(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
4022 if (__extbufend_ != __extbufnext_)
4023 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004024 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
4025 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004026 streamsize __nmemb = _VSTD::min(static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz),
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004027 static_cast<streamsize>(__extbufend_ - __extbufnext_));
4028 codecvt_base::result __r;
Eric Fiselier6003c772016-12-23 23:37:52 +00004029 // FIXME: Do we ever need to restore the state here?
4030 //state_type __svs = __st_;
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004031 streamsize __nr = __bufptr_->sgetn(const_cast<char*>(__extbufnext_), __nmemb);
4032 if (__nr != 0)
4033 {
4034 __extbufend_ = __extbufnext_ + __nr;
4035 char_type* __inext;
4036 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
4037 this->eback() + __unget_sz,
4038 this->egptr(), __inext);
4039 if (__r == codecvt_base::noconv)
4040 {
Marshall Clowbeda7142017-06-14 20:00:36 +00004041 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
4042 (char_type*) const_cast<char *>(__extbufend_));
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004043 __c = *this->gptr();
4044 }
4045 else if (__inext != this->eback() + __unget_sz)
4046 {
4047 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
4048 __c = *this->gptr();
4049 }
4050 }
4051 }
4052 }
4053 else
4054 __c = *this->gptr();
4055 if (this->eback() == &__1buf)
4056 this->setg(0, 0, 0);
4057 return __c;
4058}
4059
4060template <class _Codecvt, class _Elem, class _Tr>
4061typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4062wbuffer_convert<_Codecvt, _Elem, _Tr>::pbackfail(int_type __c)
4063{
4064 if (__cv_ != 0 && __bufptr_ != 0 && this->eback() < this->gptr())
4065 {
4066 if (traits_type::eq_int_type(__c, traits_type::eof()))
4067 {
4068 this->gbump(-1);
4069 return traits_type::not_eof(__c);
4070 }
4071 if (traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
4072 {
4073 this->gbump(-1);
4074 *this->gptr() = traits_type::to_char_type(__c);
4075 return __c;
4076 }
4077 }
4078 return traits_type::eof();
4079}
4080
4081template <class _Codecvt, class _Elem, class _Tr>
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004082typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4083wbuffer_convert<_Codecvt, _Elem, _Tr>::overflow(int_type __c)
4084{
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004085 if (__cv_ == 0 || __bufptr_ == 0)
4086 return traits_type::eof();
4087 __write_mode();
4088 char_type __1buf;
4089 char_type* __pb_save = this->pbase();
4090 char_type* __epb_save = this->epptr();
4091 if (!traits_type::eq_int_type(__c, traits_type::eof()))
4092 {
4093 if (this->pptr() == 0)
4094 this->setp(&__1buf, &__1buf+1);
4095 *this->pptr() = traits_type::to_char_type(__c);
4096 this->pbump(1);
4097 }
4098 if (this->pptr() != this->pbase())
4099 {
4100 if (__always_noconv_)
4101 {
4102 streamsize __nmemb = static_cast<streamsize>(this->pptr() - this->pbase());
4103 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4104 return traits_type::eof();
4105 }
4106 else
4107 {
4108 char* __extbe = __extbuf_;
4109 codecvt_base::result __r;
4110 do
4111 {
4112 const char_type* __e;
4113 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
4114 __extbuf_, __extbuf_ + __ebs_, __extbe);
4115 if (__e == this->pbase())
4116 return traits_type::eof();
4117 if (__r == codecvt_base::noconv)
4118 {
4119 streamsize __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
4120 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4121 return traits_type::eof();
4122 }
4123 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
4124 {
4125 streamsize __nmemb = static_cast<size_t>(__extbe - __extbuf_);
4126 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4127 return traits_type::eof();
4128 if (__r == codecvt_base::partial)
4129 {
Marshall Clowbeda7142017-06-14 20:00:36 +00004130 this->setp(const_cast<char_type *>(__e), this->pptr());
Marshall Clow33932622017-09-12 15:00:43 +00004131 this->__pbump(this->epptr() - this->pbase());
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004132 }
4133 }
4134 else
4135 return traits_type::eof();
4136 } while (__r == codecvt_base::partial);
4137 }
4138 this->setp(__pb_save, __epb_save);
4139 }
4140 return traits_type::not_eof(__c);
4141}
4142
4143template <class _Codecvt, class _Elem, class _Tr>
4144basic_streambuf<_Elem, _Tr>*
4145wbuffer_convert<_Codecvt, _Elem, _Tr>::setbuf(char_type* __s, streamsize __n)
4146{
4147 this->setg(0, 0, 0);
4148 this->setp(0, 0);
4149 if (__owns_eb_)
4150 delete [] __extbuf_;
4151 if (__owns_ib_)
4152 delete [] __intbuf_;
4153 __ebs_ = __n;
4154 if (__ebs_ > sizeof(__extbuf_min_))
4155 {
4156 if (__always_noconv_ && __s)
4157 {
4158 __extbuf_ = (char*)__s;
4159 __owns_eb_ = false;
4160 }
4161 else
4162 {
4163 __extbuf_ = new char[__ebs_];
4164 __owns_eb_ = true;
4165 }
4166 }
4167 else
4168 {
4169 __extbuf_ = __extbuf_min_;
4170 __ebs_ = sizeof(__extbuf_min_);
4171 __owns_eb_ = false;
4172 }
4173 if (!__always_noconv_)
4174 {
4175 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
4176 if (__s && __ibs_ >= sizeof(__extbuf_min_))
4177 {
4178 __intbuf_ = __s;
4179 __owns_ib_ = false;
4180 }
4181 else
4182 {
4183 __intbuf_ = new char_type[__ibs_];
4184 __owns_ib_ = true;
4185 }
4186 }
4187 else
4188 {
4189 __ibs_ = 0;
4190 __intbuf_ = 0;
4191 __owns_ib_ = false;
4192 }
4193 return this;
4194}
4195
4196template <class _Codecvt, class _Elem, class _Tr>
4197typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4198wbuffer_convert<_Codecvt, _Elem, _Tr>::seekoff(off_type __off, ios_base::seekdir __way,
4199 ios_base::openmode __om)
4200{
4201 int __width = __cv_->encoding();
4202 if (__cv_ == 0 || __bufptr_ == 0 || (__width <= 0 && __off != 0) || sync())
4203 return pos_type(off_type(-1));
Marshall Clowc2a72762015-08-27 14:37:22 +00004204 // __width > 0 || __off == 0, now check __way
4205 if (__way != ios_base::beg && __way != ios_base::cur && __way != ios_base::end)
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004206 return pos_type(off_type(-1));
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004207 pos_type __r = __bufptr_->pubseekoff(__width * __off, __way, __om);
4208 __r.state(__st_);
4209 return __r;
4210}
4211
4212template <class _Codecvt, class _Elem, class _Tr>
4213typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4214wbuffer_convert<_Codecvt, _Elem, _Tr>::seekpos(pos_type __sp, ios_base::openmode __wch)
4215{
4216 if (__cv_ == 0 || __bufptr_ == 0 || sync())
4217 return pos_type(off_type(-1));
4218 if (__bufptr_->pubseekpos(__sp, __wch) == pos_type(off_type(-1)))
4219 return pos_type(off_type(-1));
4220 return __sp;
4221}
4222
4223template <class _Codecvt, class _Elem, class _Tr>
4224int
4225wbuffer_convert<_Codecvt, _Elem, _Tr>::sync()
4226{
4227 if (__cv_ == 0 || __bufptr_ == 0)
4228 return 0;
4229 if (__cm_ & ios_base::out)
4230 {
4231 if (this->pptr() != this->pbase())
4232 if (overflow() == traits_type::eof())
4233 return -1;
4234 codecvt_base::result __r;
4235 do
4236 {
4237 char* __extbe;
4238 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
4239 streamsize __nmemb = static_cast<streamsize>(__extbe - __extbuf_);
4240 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4241 return -1;
4242 } while (__r == codecvt_base::partial);
4243 if (__r == codecvt_base::error)
4244 return -1;
4245 if (__bufptr_->pubsync())
4246 return -1;
4247 }
4248 else if (__cm_ & ios_base::in)
4249 {
4250 off_type __c;
4251 if (__always_noconv_)
4252 __c = this->egptr() - this->gptr();
4253 else
4254 {
4255 int __width = __cv_->encoding();
4256 __c = __extbufend_ - __extbufnext_;
4257 if (__width > 0)
4258 __c += __width * (this->egptr() - this->gptr());
4259 else
4260 {
4261 if (this->gptr() != this->egptr())
4262 {
4263 reverse(this->gptr(), this->egptr());
4264 codecvt_base::result __r;
4265 const char_type* __e = this->gptr();
4266 char* __extbe;
4267 do
4268 {
4269 __r = __cv_->out(__st_, __e, this->egptr(), __e,
4270 __extbuf_, __extbuf_ + __ebs_, __extbe);
4271 switch (__r)
4272 {
4273 case codecvt_base::noconv:
4274 __c += this->egptr() - this->gptr();
4275 break;
4276 case codecvt_base::ok:
4277 case codecvt_base::partial:
4278 __c += __extbe - __extbuf_;
4279 break;
4280 default:
4281 return -1;
4282 }
4283 } while (__r == codecvt_base::partial);
4284 }
4285 }
4286 }
4287 if (__bufptr_->pubseekoff(-__c, ios_base::cur, __cm_) == pos_type(off_type(-1)))
4288 return -1;
4289 this->setg(0, 0, 0);
4290 __cm_ = 0;
4291 }
4292 return 0;
4293}
4294
4295template <class _Codecvt, class _Elem, class _Tr>
4296bool
4297wbuffer_convert<_Codecvt, _Elem, _Tr>::__read_mode()
4298{
4299 if (!(__cm_ & ios_base::in))
4300 {
4301 this->setp(0, 0);
4302 if (__always_noconv_)
4303 this->setg((char_type*)__extbuf_,
4304 (char_type*)__extbuf_ + __ebs_,
4305 (char_type*)__extbuf_ + __ebs_);
4306 else
4307 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
4308 __cm_ = ios_base::in;
4309 return true;
4310 }
4311 return false;
4312}
4313
4314template <class _Codecvt, class _Elem, class _Tr>
4315void
4316wbuffer_convert<_Codecvt, _Elem, _Tr>::__write_mode()
4317{
4318 if (!(__cm_ & ios_base::out))
4319 {
4320 this->setg(0, 0, 0);
4321 if (__ebs_ > sizeof(__extbuf_min_))
4322 {
4323 if (__always_noconv_)
4324 this->setp((char_type*)__extbuf_,
4325 (char_type*)__extbuf_ + (__ebs_ - 1));
4326 else
4327 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
4328 }
4329 else
4330 this->setp(0, 0);
4331 __cm_ = ios_base::out;
4332 }
4333}
4334
4335template <class _Codecvt, class _Elem, class _Tr>
4336wbuffer_convert<_Codecvt, _Elem, _Tr>*
4337wbuffer_convert<_Codecvt, _Elem, _Tr>::__close()
4338{
4339 wbuffer_convert* __rt = 0;
4340 if (__cv_ != 0 && __bufptr_ != 0)
4341 {
4342 __rt = this;
4343 if ((__cm_ & ios_base::out) && sync())
4344 __rt = 0;
4345 }
4346 return __rt;
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004347}
4348
Howard Hinnantc51e1022010-05-11 19:42:16 +00004349_LIBCPP_END_NAMESPACE_STD
4350
Eric Fiselierf4433a32017-05-31 22:07:49 +00004351_LIBCPP_POP_MACROS
4352
Howard Hinnantc51e1022010-05-11 19:42:16 +00004353#endif // _LIBCPP_LOCALE