blob: ad1c1f0083ecc26101abca7023486b3f4c369d7c [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>
Howard Hinnant8ad70912013-09-17 01:34:47 +0000195#if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
Howard Hinnantae0f80b2011-09-29 20:33:10 +0000196#include <support/win32/locale_win32.h>
Jonathan Roelofs37058612014-09-19 20:09:12 +0000197#elif defined(_NEWLIB_VERSION)
198// FIXME: replace all the uses of _NEWLIB_VERSION with __NEWLIB__ preceded by an
199// include of <sys/cdefs.h> once https://sourceware.org/ml/newlib-cvs/2014-q3/msg00038.html
200// has had a chance to bake for a bit
201#include <support/newlib/xlocale.h>
Ed Schouten118b6032015-03-11 16:39:36 +0000202#endif
203#ifdef _LIBCPP_HAS_CATOPEN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000204#include <nl_types.h>
Marshall Clow3477ec92014-07-10 15:20:28 +0000205#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000206
Marshall Clowdde4bfe2013-03-18 17:45:34 +0000207#ifdef __APPLE__
Howard Hinnant48fd5d52012-11-14 21:17:15 +0000208#include <Availability.h>
209#endif
210
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000211#include <__undef_min_max>
212
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000213#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000214#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000215#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000216
Ben Craig3756b922016-03-09 15:39:39 +0000217#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
218#include <__bsd_locale_defaults.h>
219#else
220#include <__bsd_locale_fallbacks.h>
221#endif
222
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223_LIBCPP_BEGIN_NAMESPACE_STD
224
Marshall Clow82378c02013-03-18 19:34:07 +0000225#if defined(__APPLE__) || defined(__FreeBSD__)
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000226# define _LIBCPP_GET_C_LOCALE 0
Ed Schoutenaa497c82015-03-10 09:35:22 +0000227#elif defined(__CloudABI__) || defined(__NetBSD__)
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000228# define _LIBCPP_GET_C_LOCALE LC_C_LOCALE
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000229#else
230# define _LIBCPP_GET_C_LOCALE __cloc()
Howard Hinnantf312e3e2011-09-28 23:39:33 +0000231 // Get the C locale object
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000232 _LIBCPP_FUNC_VIS locale_t __cloc();
Howard Hinnantf312e3e2011-09-28 23:39:33 +0000233#define __cloc_defined
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000234#endif
235
Alexis Hunt1adf2aa2011-07-15 05:40:33 +0000236typedef _VSTD::remove_pointer<locale_t>::type __locale_struct;
237typedef _VSTD::unique_ptr<__locale_struct, decltype(&freelocale)> __locale_unique_ptr;
Howard Hinnant155c2af2010-05-24 17:49:41 +0000238
Howard Hinnantc51e1022010-05-11 19:42:16 +0000239// __scan_keyword
240// Scans [__b, __e) until a match is found in the basic_strings range
241// [__kb, __ke) or until it can be shown that there is no match in [__kb, __ke).
242// __b will be incremented (visibly), consuming CharT until a match is found
243// or proved to not exist. A keyword may be "", in which will match anything.
244// If one keyword is a prefix of another, and the next CharT in the input
245// might match another keyword, the algorithm will attempt to find the longest
246// matching keyword. If the longer matching keyword ends up not matching, then
247// no keyword match is found. If no keyword match is found, __ke is returned
248// and failbit is set in __err.
249// Else an iterator pointing to the matching keyword is found. If more than
250// one keyword matches, an iterator to the first matching keyword is returned.
Alp Tokerb8a95f52014-05-15 11:27:39 +0000251// If on exit __b == __e, eofbit is set in __err. If __case_sensitive is false,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000252// __ct is used to force to lower case before comparing characters.
253// Examples:
254// Keywords: "a", "abb"
255// If the input is "a", the first keyword matches and eofbit is set.
256// If the input is "abc", no match is found and "ab" are consumed.
257template <class _InputIterator, class _ForwardIterator, class _Ctype>
258_LIBCPP_HIDDEN
259_ForwardIterator
260__scan_keyword(_InputIterator& __b, _InputIterator __e,
261 _ForwardIterator __kb, _ForwardIterator __ke,
262 const _Ctype& __ct, ios_base::iostate& __err,
263 bool __case_sensitive = true)
264{
265 typedef typename iterator_traits<_InputIterator>::value_type _CharT;
Howard Hinnant28b24882011-12-01 20:21:04 +0000266 size_t __nkw = static_cast<size_t>(_VSTD::distance(__kb, __ke));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000267 const unsigned char __doesnt_match = '\0';
268 const unsigned char __might_match = '\1';
269 const unsigned char __does_match = '\2';
270 unsigned char __statbuf[100];
271 unsigned char* __status = __statbuf;
272 unique_ptr<unsigned char, void(*)(void*)> __stat_hold(0, free);
273 if (__nkw > sizeof(__statbuf))
274 {
275 __status = (unsigned char*)malloc(__nkw);
276 if (__status == 0)
277 __throw_bad_alloc();
278 __stat_hold.reset(__status);
279 }
280 size_t __n_might_match = __nkw; // At this point, any keyword might match
281 size_t __n_does_match = 0; // but none of them definitely do
282 // Initialize all statuses to __might_match, except for "" keywords are __does_match
283 unsigned char* __st = __status;
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000284 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000285 {
286 if (!__ky->empty())
287 *__st = __might_match;
288 else
289 {
290 *__st = __does_match;
291 --__n_might_match;
292 ++__n_does_match;
293 }
294 }
295 // While there might be a match, test keywords against the next CharT
296 for (size_t __indx = 0; __b != __e && __n_might_match > 0; ++__indx)
297 {
298 // Peek at the next CharT but don't consume it
299 _CharT __c = *__b;
300 if (!__case_sensitive)
301 __c = __ct.toupper(__c);
302 bool __consume = false;
303 // For each keyword which might match, see if the __indx character is __c
304 // If a match if found, consume __c
305 // If a match is found, and that is the last character in the keyword,
306 // then that keyword matches.
307 // If the keyword doesn't match this character, then change the keyword
308 // to doesn't match
309 __st = __status;
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000310 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000311 {
312 if (*__st == __might_match)
313 {
314 _CharT __kc = (*__ky)[__indx];
315 if (!__case_sensitive)
316 __kc = __ct.toupper(__kc);
317 if (__c == __kc)
318 {
319 __consume = true;
320 if (__ky->size() == __indx+1)
321 {
322 *__st = __does_match;
323 --__n_might_match;
324 ++__n_does_match;
325 }
326 }
327 else
328 {
329 *__st = __doesnt_match;
330 --__n_might_match;
331 }
332 }
333 }
334 // consume if we matched a character
335 if (__consume)
336 {
337 ++__b;
338 // If we consumed a character and there might be a matched keyword that
339 // was marked matched on a previous iteration, then such keywords
340 // which are now marked as not matching.
341 if (__n_might_match + __n_does_match > 1)
342 {
343 __st = __status;
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000344 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000345 {
346 if (*__st == __does_match && __ky->size() != __indx+1)
347 {
348 *__st = __doesnt_match;
349 --__n_does_match;
350 }
351 }
352 }
353 }
354 }
355 // We've exited the loop because we hit eof and/or we have no more "might matches".
356 if (__b == __e)
357 __err |= ios_base::eofbit;
358 // Return the first matching result
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000359 for (__st = __status; __kb != __ke; ++__kb, (void) ++__st)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000360 if (*__st == __does_match)
361 break;
362 if (__kb == __ke)
363 __err |= ios_base::failbit;
364 return __kb;
365}
366
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000367struct _LIBCPP_TYPE_VIS __num_get_base
Howard Hinnantc51e1022010-05-11 19:42:16 +0000368{
369 static const int __num_get_buf_sz = 40;
370
371 static int __get_base(ios_base&);
372 static const char __src[33];
373};
374
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000375_LIBCPP_FUNC_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376void __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end,
377 ios_base::iostate& __err);
378
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379template <class _CharT>
380struct __num_get
381 : protected __num_get_base
382{
383 static string __stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep);
384 static string __stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
385 _CharT& __thousands_sep);
386 static int __stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
387 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
388 unsigned* __g, unsigned*& __g_end, _CharT* __atoms);
389 static int __stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp,
390 char* __a, char*& __a_end,
391 _CharT __decimal_point, _CharT __thousands_sep,
392 const string& __grouping, unsigned* __g,
393 unsigned*& __g_end, unsigned& __dc, _CharT* __atoms);
394};
395
396template <class _CharT>
397string
398__num_get<_CharT>::__stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep)
399{
400 locale __loc = __iob.getloc();
401 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 26, __atoms);
402 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
403 __thousands_sep = __np.thousands_sep();
404 return __np.grouping();
405}
406
407template <class _CharT>
408string
409__num_get<_CharT>::__stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
410 _CharT& __thousands_sep)
411{
412 locale __loc = __iob.getloc();
413 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 32, __atoms);
414 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
415 __decimal_point = __np.decimal_point();
416 __thousands_sep = __np.thousands_sep();
417 return __np.grouping();
418}
419
420template <class _CharT>
421int
422__num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
423 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
424 unsigned* __g, unsigned*& __g_end, _CharT* __atoms)
425{
Howard Hinnantf57b3fc2011-03-09 01:03:19 +0000426 if (__a_end == __a && (__ct == __atoms[24] || __ct == __atoms[25]))
427 {
428 *__a_end++ = __ct == __atoms[24] ? '+' : '-';
429 __dc = 0;
430 return 0;
431 }
Howard Hinnant28b24882011-12-01 20:21:04 +0000432 if (__grouping.size() != 0 && __ct == __thousands_sep)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000433 {
434 if (__g_end-__g < __num_get_buf_sz)
435 {
436 *__g_end++ = __dc;
437 __dc = 0;
438 }
439 return 0;
440 }
441 ptrdiff_t __f = find(__atoms, __atoms + 26, __ct) - __atoms;
Howard Hinnantf57b3fc2011-03-09 01:03:19 +0000442 if (__f >= 24)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000443 return -1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000444 switch (__base)
445 {
446 case 8:
447 case 10:
448 if (__f >= __base)
Howard Hinnantf57b3fc2011-03-09 01:03:19 +0000449 return -1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000450 break;
Howard Hinnantf57b3fc2011-03-09 01:03:19 +0000451 case 16:
452 if (__f < 22)
453 break;
454 if (__a_end != __a && __a_end - __a <= 2 && __a_end[-1] == '0')
455 {
456 __dc = 0;
457 *__a_end++ = __src[__f];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000458 return 0;
Howard Hinnantf57b3fc2011-03-09 01:03:19 +0000459 }
460 return -1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000461 }
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000462 *__a_end++ = __src[__f];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000463 ++__dc;
464 return 0;
465}
466
467template <class _CharT>
468int
469__num_get<_CharT>::__stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp, char* __a, char*& __a_end,
470 _CharT __decimal_point, _CharT __thousands_sep, const string& __grouping,
471 unsigned* __g, unsigned*& __g_end, unsigned& __dc, _CharT* __atoms)
472{
473 if (__ct == __decimal_point)
474 {
475 if (!__in_units)
476 return -1;
477 __in_units = false;
478 *__a_end++ = '.';
479 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
480 *__g_end++ = __dc;
481 return 0;
482 }
483 if (__ct == __thousands_sep && __grouping.size() != 0)
484 {
485 if (!__in_units)
486 return -1;
487 if (__g_end-__g < __num_get_buf_sz)
488 {
489 *__g_end++ = __dc;
490 __dc = 0;
491 }
492 return 0;
493 }
494 ptrdiff_t __f = find(__atoms, __atoms + 32, __ct) - __atoms;
495 if (__f >= 32)
496 return -1;
497 char __x = __src[__f];
Howard Hinnant5132e192012-02-15 19:19:37 +0000498 if (__x == '-' || __x == '+')
499 {
Howard Hinnant21413152013-03-08 19:06:24 +0000500 if (__a_end == __a || (__a_end[-1] & 0x5F) == (__exp & 0x7F))
Howard Hinnant5132e192012-02-15 19:19:37 +0000501 {
502 *__a_end++ = __x;
503 return 0;
504 }
505 return -1;
506 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000507 if (__x == 'x' || __x == 'X')
508 __exp = 'P';
Howard Hinnant21413152013-03-08 19:06:24 +0000509 else if ((__x & 0x5F) == __exp)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000510 {
Howard Hinnant21413152013-03-08 19:06:24 +0000511 __exp |= 0x80;
512 if (__in_units)
513 {
514 __in_units = false;
515 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
516 *__g_end++ = __dc;
517 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000518 }
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000519 *__a_end++ = __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000520 if (__f >= 22)
521 return 0;
522 ++__dc;
523 return 0;
524}
525
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000526_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<char>)
527_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000528
529template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000530class _LIBCPP_TEMPLATE_VIS num_get
Howard Hinnantc51e1022010-05-11 19:42:16 +0000531 : public locale::facet,
532 private __num_get<_CharT>
533{
534public:
535 typedef _CharT char_type;
536 typedef _InputIterator iter_type;
537
538 _LIBCPP_ALWAYS_INLINE
539 explicit num_get(size_t __refs = 0)
540 : locale::facet(__refs) {}
541
542 _LIBCPP_ALWAYS_INLINE
543 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
544 ios_base::iostate& __err, bool& __v) const
545 {
546 return do_get(__b, __e, __iob, __err, __v);
547 }
548
549 _LIBCPP_ALWAYS_INLINE
550 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
551 ios_base::iostate& __err, long& __v) const
552 {
553 return do_get(__b, __e, __iob, __err, __v);
554 }
555
556 _LIBCPP_ALWAYS_INLINE
557 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
558 ios_base::iostate& __err, long long& __v) const
559 {
560 return do_get(__b, __e, __iob, __err, __v);
561 }
562
563 _LIBCPP_ALWAYS_INLINE
564 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
565 ios_base::iostate& __err, unsigned short& __v) const
566 {
567 return do_get(__b, __e, __iob, __err, __v);
568 }
569
570 _LIBCPP_ALWAYS_INLINE
571 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
572 ios_base::iostate& __err, unsigned int& __v) const
573 {
574 return do_get(__b, __e, __iob, __err, __v);
575 }
576
577 _LIBCPP_ALWAYS_INLINE
578 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
579 ios_base::iostate& __err, unsigned long& __v) const
580 {
581 return do_get(__b, __e, __iob, __err, __v);
582 }
583
584 _LIBCPP_ALWAYS_INLINE
585 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
586 ios_base::iostate& __err, unsigned long long& __v) const
587 {
588 return do_get(__b, __e, __iob, __err, __v);
589 }
590
591 _LIBCPP_ALWAYS_INLINE
592 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
593 ios_base::iostate& __err, float& __v) const
594 {
595 return do_get(__b, __e, __iob, __err, __v);
596 }
597
598 _LIBCPP_ALWAYS_INLINE
599 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
600 ios_base::iostate& __err, double& __v) const
601 {
602 return do_get(__b, __e, __iob, __err, __v);
603 }
604
605 _LIBCPP_ALWAYS_INLINE
606 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
607 ios_base::iostate& __err, long double& __v) const
608 {
609 return do_get(__b, __e, __iob, __err, __v);
610 }
611
612 _LIBCPP_ALWAYS_INLINE
613 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
614 ios_base::iostate& __err, void*& __v) const
615 {
616 return do_get(__b, __e, __iob, __err, __v);
617 }
618
619 static locale::id id;
620
621protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000622 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +0000623 ~num_get() {}
624
Marshall Clowae385382013-11-05 14:28:52 +0000625 template <class _Fp>
Shoaib Meenai69c57412017-03-02 03:02:50 +0000626 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
627 iter_type __do_get_floating_point
Marshall Clowae385382013-11-05 14:28:52 +0000628 (iter_type __b, iter_type __e, ios_base& __iob,
629 ios_base::iostate& __err, _Fp& __v) const;
Marshall Clow96d86d82013-11-07 01:00:50 +0000630
631 template <class _Signed>
Shoaib Meenai69c57412017-03-02 03:02:50 +0000632 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
633 iter_type __do_get_signed
Marshall Clow96d86d82013-11-07 01:00:50 +0000634 (iter_type __b, iter_type __e, ios_base& __iob,
635 ios_base::iostate& __err, _Signed& __v) const;
636
637 template <class _Unsigned>
Shoaib Meenai69c57412017-03-02 03:02:50 +0000638 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
639 iter_type __do_get_unsigned
Marshall Clow96d86d82013-11-07 01:00:50 +0000640 (iter_type __b, iter_type __e, ios_base& __iob,
641 ios_base::iostate& __err, _Unsigned& __v) const;
642
643
644 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
645 ios_base::iostate& __err, bool& __v) const;
646
647 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
648 ios_base::iostate& __err, long& __v) const
649 { return this->__do_get_signed ( __b, __e, __iob, __err, __v ); }
650
651 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
652 ios_base::iostate& __err, long long& __v) const
653 { return this->__do_get_signed ( __b, __e, __iob, __err, __v ); }
654
655 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
656 ios_base::iostate& __err, unsigned short& __v) const
657 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
658
659 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
660 ios_base::iostate& __err, unsigned int& __v) const
661 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
662
663 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
664 ios_base::iostate& __err, unsigned long& __v) const
665 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
666
667 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
668 ios_base::iostate& __err, unsigned long long& __v) const
669 { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
670
671 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
672 ios_base::iostate& __err, float& __v) const
673 { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
674
675 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
676 ios_base::iostate& __err, double& __v) const
677 { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
678
679 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
680 ios_base::iostate& __err, long double& __v) const
681 { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
682
683 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
684 ios_base::iostate& __err, void*& __v) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000685};
686
687template <class _CharT, class _InputIterator>
688locale::id
689num_get<_CharT, _InputIterator>::id;
690
691template <class _Tp>
692_Tp
693__num_get_signed_integral(const char* __a, const char* __a_end,
694 ios_base::iostate& __err, int __base)
695{
696 if (__a != __a_end)
697 {
Howard Hinnantca8923c2013-01-22 17:26:08 +0000698 typename remove_reference<decltype(errno)>::type __save_errno = errno;
Howard Hinnant05c71342011-02-25 19:52:41 +0000699 errno = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000700 char *__p2;
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000701 long long __ll = strtoll_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
Howard Hinnantca8923c2013-01-22 17:26:08 +0000702 typename remove_reference<decltype(errno)>::type __current_errno = errno;
Howard Hinnant05c71342011-02-25 19:52:41 +0000703 if (__current_errno == 0)
704 errno = __save_errno;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000705 if (__p2 != __a_end)
706 {
707 __err = ios_base::failbit;
708 return 0;
709 }
Howard Hinnant05c71342011-02-25 19:52:41 +0000710 else if (__current_errno == ERANGE ||
711 __ll < numeric_limits<_Tp>::min() ||
712 numeric_limits<_Tp>::max() < __ll)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000713 {
714 __err = ios_base::failbit;
Howard Hinnant05c71342011-02-25 19:52:41 +0000715 if (__ll > 0)
716 return numeric_limits<_Tp>::max();
717 else
718 return numeric_limits<_Tp>::min();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000719 }
720 return static_cast<_Tp>(__ll);
721 }
722 __err = ios_base::failbit;
723 return 0;
724}
725
726template <class _Tp>
727_Tp
728__num_get_unsigned_integral(const char* __a, const char* __a_end,
729 ios_base::iostate& __err, int __base)
730{
731 if (__a != __a_end)
732 {
Howard Hinnant05c71342011-02-25 19:52:41 +0000733 if (*__a == '-')
734 {
735 __err = ios_base::failbit;
736 return 0;
737 }
Howard Hinnantca8923c2013-01-22 17:26:08 +0000738 typename remove_reference<decltype(errno)>::type __save_errno = errno;
Howard Hinnant05c71342011-02-25 19:52:41 +0000739 errno = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000740 char *__p2;
Howard Hinnant1ab52da2011-09-28 21:05:01 +0000741 unsigned long long __ll = strtoull_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
Howard Hinnantca8923c2013-01-22 17:26:08 +0000742 typename remove_reference<decltype(errno)>::type __current_errno = errno;
Howard Hinnant05c71342011-02-25 19:52:41 +0000743 if (__current_errno == 0)
744 errno = __save_errno;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000745 if (__p2 != __a_end)
746 {
747 __err = ios_base::failbit;
748 return 0;
749 }
Howard Hinnant05c71342011-02-25 19:52:41 +0000750 else if (__current_errno == ERANGE ||
751 numeric_limits<_Tp>::max() < __ll)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752 {
753 __err = ios_base::failbit;
754 return numeric_limits<_Tp>::max();
755 }
756 return static_cast<_Tp>(__ll);
757 }
758 __err = ios_base::failbit;
759 return 0;
760}
761
762template <class _Tp>
Eric Fiselier98e428d2016-06-19 06:58:22 +0000763_LIBCPP_INLINE_VISIBILITY
764_Tp __do_strtod(const char* __a, char** __p2);
765
766template <>
767inline _LIBCPP_INLINE_VISIBILITY
768float __do_strtod<float>(const char* __a, char** __p2) {
769 return strtof_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
770}
771
772template <>
773inline _LIBCPP_INLINE_VISIBILITY
774double __do_strtod<double>(const char* __a, char** __p2) {
775 return strtod_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
776}
777
778template <>
779inline _LIBCPP_INLINE_VISIBILITY
780long double __do_strtod<long double>(const char* __a, char** __p2) {
781 return strtold_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
782}
783
784template <class _Tp>
Shoaib Meenai54c6fd62016-12-24 18:05:32 +0000785_LIBCPP_HIDDEN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000786_Tp
787__num_get_float(const char* __a, const char* __a_end, ios_base::iostate& __err)
788{
789 if (__a != __a_end)
790 {
Howard Hinnantc9567812013-04-13 18:19:25 +0000791 typename remove_reference<decltype(errno)>::type __save_errno = errno;
792 errno = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000793 char *__p2;
Eric Fiselier98e428d2016-06-19 06:58:22 +0000794 _Tp __ld = __do_strtod<_Tp>(__a, &__p2);
Howard Hinnantc9567812013-04-13 18:19:25 +0000795 typename remove_reference<decltype(errno)>::type __current_errno = errno;
796 if (__current_errno == 0)
797 errno = __save_errno;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000798 if (__p2 != __a_end)
799 {
800 __err = ios_base::failbit;
801 return 0;
802 }
Howard Hinnantc9567812013-04-13 18:19:25 +0000803 else if (__current_errno == ERANGE)
804 __err = ios_base::failbit;
Eric Fiselier98e428d2016-06-19 06:58:22 +0000805 return __ld;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000806 }
807 __err = ios_base::failbit;
808 return 0;
809}
810
811template <class _CharT, class _InputIterator>
812_InputIterator
813num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
814 ios_base& __iob,
815 ios_base::iostate& __err,
816 bool& __v) const
817{
818 if ((__iob.flags() & ios_base::boolalpha) == 0)
819 {
820 long __lv = -1;
821 __b = do_get(__b, __e, __iob, __err, __lv);
822 switch (__lv)
823 {
824 case 0:
825 __v = false;
826 break;
827 case 1:
828 __v = true;
829 break;
830 default:
831 __v = true;
832 __err = ios_base::failbit;
833 break;
834 }
835 return __b;
836 }
837 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__iob.getloc());
838 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__iob.getloc());
839 typedef typename numpunct<_CharT>::string_type string_type;
840 const string_type __names[2] = {__np.truename(), __np.falsename()};
841 const string_type* __i = __scan_keyword(__b, __e, __names, __names+2,
842 __ct, __err);
843 __v = __i == __names;
844 return __b;
845}
846
Marshall Clow96d86d82013-11-07 01:00:50 +0000847// signed
848
Howard Hinnantc51e1022010-05-11 19:42:16 +0000849template <class _CharT, class _InputIterator>
Marshall Clow96d86d82013-11-07 01:00:50 +0000850template <class _Signed>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000851_InputIterator
Marshall Clow96d86d82013-11-07 01:00:50 +0000852num_get<_CharT, _InputIterator>::__do_get_signed(iter_type __b, iter_type __e,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000853 ios_base& __iob,
854 ios_base::iostate& __err,
Marshall Clow96d86d82013-11-07 01:00:50 +0000855 _Signed& __v) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000856{
857 // Stage 1
858 int __base = this->__get_base(__iob);
859 // Stage 2
860 char_type __atoms[26];
861 char_type __thousands_sep;
862 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000863 string __buf;
864 __buf.resize(__buf.capacity());
865 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000866 char* __a_end = __a;
867 unsigned __g[__num_get_base::__num_get_buf_sz];
868 unsigned* __g_end = __g;
869 unsigned __dc = 0;
870 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000871 {
Joerg Sonnenberger7a804f62014-02-07 21:14:29 +0000872 if (__a_end == __a + __buf.size())
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000873 {
874 size_t __tmp = __buf.size();
875 __buf.resize(2*__buf.size());
876 __buf.resize(__buf.capacity());
877 __a = &__buf[0];
878 __a_end = __a + __tmp;
879 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000880 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000881 __thousands_sep, __grouping, __g, __g_end,
882 __atoms))
883 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000884 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000885 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
886 *__g_end++ = __dc;
887 // Stage 3
Marshall Clow96d86d82013-11-07 01:00:50 +0000888 __v = __num_get_signed_integral<_Signed>(__a, __a_end, __err, __base);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000889 // Digit grouping checked
890 __check_grouping(__grouping, __g, __g_end, __err);
891 // EOF checked
892 if (__b == __e)
893 __err |= ios_base::eofbit;
894 return __b;
895}
896
Marshall Clow96d86d82013-11-07 01:00:50 +0000897// unsigned
Howard Hinnantc51e1022010-05-11 19:42:16 +0000898
899template <class _CharT, class _InputIterator>
Marshall Clow96d86d82013-11-07 01:00:50 +0000900template <class _Unsigned>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000901_InputIterator
Marshall Clow96d86d82013-11-07 01:00:50 +0000902num_get<_CharT, _InputIterator>::__do_get_unsigned(iter_type __b, iter_type __e,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000903 ios_base& __iob,
904 ios_base::iostate& __err,
Marshall Clow96d86d82013-11-07 01:00:50 +0000905 _Unsigned& __v) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000906{
907 // Stage 1
908 int __base = this->__get_base(__iob);
909 // Stage 2
910 char_type __atoms[26];
911 char_type __thousands_sep;
912 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000913 string __buf;
914 __buf.resize(__buf.capacity());
915 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000916 char* __a_end = __a;
917 unsigned __g[__num_get_base::__num_get_buf_sz];
918 unsigned* __g_end = __g;
919 unsigned __dc = 0;
920 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000921 {
Joerg Sonnenberger7a804f62014-02-07 21:14:29 +0000922 if (__a_end == __a + __buf.size())
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000923 {
924 size_t __tmp = __buf.size();
925 __buf.resize(2*__buf.size());
926 __buf.resize(__buf.capacity());
927 __a = &__buf[0];
928 __a_end = __a + __tmp;
929 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000930 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000931 __thousands_sep, __grouping, __g, __g_end,
932 __atoms))
933 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000934 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000935 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
936 *__g_end++ = __dc;
937 // Stage 3
Marshall Clow96d86d82013-11-07 01:00:50 +0000938 __v = __num_get_unsigned_integral<_Unsigned>(__a, __a_end, __err, __base);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000939 // Digit grouping checked
940 __check_grouping(__grouping, __g, __g_end, __err);
941 // EOF checked
942 if (__b == __e)
943 __err |= ios_base::eofbit;
944 return __b;
945}
946
Marshall Clowae385382013-11-05 14:28:52 +0000947// floating point
948
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949template <class _CharT, class _InputIterator>
Marshall Clowae385382013-11-05 14:28:52 +0000950template <class _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000951_InputIterator
Marshall Clowae385382013-11-05 14:28:52 +0000952num_get<_CharT, _InputIterator>::__do_get_floating_point(iter_type __b, iter_type __e,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000953 ios_base& __iob,
954 ios_base::iostate& __err,
Marshall Clowae385382013-11-05 14:28:52 +0000955 _Fp& __v) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956{
957 // Stage 1, nothing to do
958 // Stage 2
959 char_type __atoms[32];
960 char_type __decimal_point;
961 char_type __thousands_sep;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000962 string __grouping = this->__stage2_float_prep(__iob, __atoms,
963 __decimal_point,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964 __thousands_sep);
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000965 string __buf;
966 __buf.resize(__buf.capacity());
967 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968 char* __a_end = __a;
969 unsigned __g[__num_get_base::__num_get_buf_sz];
970 unsigned* __g_end = __g;
971 unsigned __dc = 0;
972 bool __in_units = true;
973 char __exp = 'E';
974 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000975 {
Joerg Sonnenberger7a804f62014-02-07 21:14:29 +0000976 if (__a_end == __a + __buf.size())
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000977 {
978 size_t __tmp = __buf.size();
979 __buf.resize(2*__buf.size());
980 __buf.resize(__buf.capacity());
981 __a = &__buf[0];
982 __a_end = __a + __tmp;
983 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000984 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
985 __decimal_point, __thousands_sep,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986 __grouping, __g, __g_end,
987 __dc, __atoms))
988 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +0000989 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990 if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
991 *__g_end++ = __dc;
992 // Stage 3
Marshall Clowae385382013-11-05 14:28:52 +0000993 __v = __num_get_float<_Fp>(__a, __a_end, __err);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000994 // Digit grouping checked
995 __check_grouping(__grouping, __g, __g_end, __err);
996 // EOF checked
997 if (__b == __e)
998 __err |= ios_base::eofbit;
999 return __b;
1000}
1001
1002template <class _CharT, class _InputIterator>
1003_InputIterator
1004num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1005 ios_base& __iob,
1006 ios_base::iostate& __err,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001007 void*& __v) const
1008{
1009 // Stage 1
1010 int __base = 16;
1011 // Stage 2
1012 char_type __atoms[26];
Howard Hinnant28b24882011-12-01 20:21:04 +00001013 char_type __thousands_sep = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001014 string __grouping;
1015 use_facet<ctype<_CharT> >(__iob.getloc()).widen(__num_get_base::__src,
1016 __num_get_base::__src + 26, __atoms);
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001017 string __buf;
1018 __buf.resize(__buf.capacity());
1019 char* __a = &__buf[0];
Howard Hinnantc51e1022010-05-11 19:42:16 +00001020 char* __a_end = __a;
1021 unsigned __g[__num_get_base::__num_get_buf_sz];
1022 unsigned* __g_end = __g;
1023 unsigned __dc = 0;
1024 for (; __b != __e; ++__b)
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001025 {
Joerg Sonnenberger7a804f62014-02-07 21:14:29 +00001026 if (__a_end == __a + __buf.size())
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001027 {
1028 size_t __tmp = __buf.size();
1029 __buf.resize(2*__buf.size());
1030 __buf.resize(__buf.capacity());
1031 __a = &__buf[0];
1032 __a_end = __a + __tmp;
1033 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001034 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1035 __thousands_sep, __grouping,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001036 __g, __g_end, __atoms))
1037 break;
Howard Hinnant5d4013d2013-04-15 20:40:06 +00001038 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039 // Stage 3
Marshall Clow0db963d2014-05-21 16:02:20 +00001040 __buf.resize(__a_end - __a);
Ben Craig3756b922016-03-09 15:39:39 +00001041 if (__libcpp_sscanf_l(__buf.c_str(), _LIBCPP_GET_C_LOCALE, "%p", &__v) != 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001042 __err = ios_base::failbit;
1043 // EOF checked
1044 if (__b == __e)
1045 __err |= ios_base::eofbit;
1046 return __b;
1047}
1048
Eric Fiselier1b57fa82016-09-15 22:27:07 +00001049_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<char>)
1050_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001052struct _LIBCPP_TYPE_VIS __num_put_base
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053{
1054protected:
1055 static void __format_int(char* __fmt, const char* __len, bool __signd,
1056 ios_base::fmtflags __flags);
1057 static bool __format_float(char* __fmt, const char* __len,
1058 ios_base::fmtflags __flags);
1059 static char* __identify_padding(char* __nb, char* __ne,
1060 const ios_base& __iob);
1061};
1062
1063template <class _CharT>
1064struct __num_put
1065 : protected __num_put_base
1066{
1067 static void __widen_and_group_int(char* __nb, char* __np, char* __ne,
1068 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1069 const locale& __loc);
1070 static void __widen_and_group_float(char* __nb, char* __np, char* __ne,
1071 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1072 const locale& __loc);
1073};
1074
1075template <class _CharT>
1076void
1077__num_put<_CharT>::__widen_and_group_int(char* __nb, char* __np, char* __ne,
1078 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1079 const locale& __loc)
1080{
1081 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1082 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1083 string __grouping = __npt.grouping();
1084 if (__grouping.empty())
1085 {
1086 __ct.widen(__nb, __ne, __ob);
1087 __oe = __ob + (__ne - __nb);
1088 }
1089 else
1090 {
1091 __oe = __ob;
1092 char* __nf = __nb;
1093 if (*__nf == '-' || *__nf == '+')
1094 *__oe++ = __ct.widen(*__nf++);
1095 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1096 __nf[1] == 'X'))
1097 {
1098 *__oe++ = __ct.widen(*__nf++);
1099 *__oe++ = __ct.widen(*__nf++);
1100 }
1101 reverse(__nf, __ne);
1102 _CharT __thousands_sep = __npt.thousands_sep();
1103 unsigned __dc = 0;
1104 unsigned __dg = 0;
1105 for (char* __p = __nf; __p < __ne; ++__p)
1106 {
1107 if (static_cast<unsigned>(__grouping[__dg]) > 0 &&
1108 __dc == static_cast<unsigned>(__grouping[__dg]))
1109 {
1110 *__oe++ = __thousands_sep;
1111 __dc = 0;
1112 if (__dg < __grouping.size()-1)
1113 ++__dg;
1114 }
1115 *__oe++ = __ct.widen(*__p);
1116 ++__dc;
1117 }
1118 reverse(__ob + (__nf - __nb), __oe);
1119 }
1120 if (__np == __ne)
1121 __op = __oe;
1122 else
1123 __op = __ob + (__np - __nb);
1124}
1125
1126template <class _CharT>
1127void
1128__num_put<_CharT>::__widen_and_group_float(char* __nb, char* __np, char* __ne,
1129 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1130 const locale& __loc)
1131{
1132 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1133 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1134 string __grouping = __npt.grouping();
1135 __oe = __ob;
1136 char* __nf = __nb;
1137 if (*__nf == '-' || *__nf == '+')
1138 *__oe++ = __ct.widen(*__nf++);
1139 char* __ns;
1140 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1141 __nf[1] == 'X'))
1142 {
1143 *__oe++ = __ct.widen(*__nf++);
1144 *__oe++ = __ct.widen(*__nf++);
1145 for (__ns = __nf; __ns < __ne; ++__ns)
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001146 if (!isxdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001147 break;
1148 }
1149 else
1150 {
1151 for (__ns = __nf; __ns < __ne; ++__ns)
Howard Hinnant1ab52da2011-09-28 21:05:01 +00001152 if (!isdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153 break;
1154 }
1155 if (__grouping.empty())
1156 {
1157 __ct.widen(__nf, __ns, __oe);
1158 __oe += __ns - __nf;
1159 }
1160 else
1161 {
1162 reverse(__nf, __ns);
1163 _CharT __thousands_sep = __npt.thousands_sep();
1164 unsigned __dc = 0;
1165 unsigned __dg = 0;
1166 for (char* __p = __nf; __p < __ns; ++__p)
1167 {
1168 if (__grouping[__dg] > 0 && __dc == static_cast<unsigned>(__grouping[__dg]))
1169 {
1170 *__oe++ = __thousands_sep;
1171 __dc = 0;
1172 if (__dg < __grouping.size()-1)
1173 ++__dg;
1174 }
1175 *__oe++ = __ct.widen(*__p);
1176 ++__dc;
1177 }
1178 reverse(__ob + (__nf - __nb), __oe);
1179 }
1180 for (__nf = __ns; __nf < __ne; ++__nf)
1181 {
1182 if (*__nf == '.')
1183 {
1184 *__oe++ = __npt.decimal_point();
1185 ++__nf;
1186 break;
1187 }
1188 else
1189 *__oe++ = __ct.widen(*__nf);
1190 }
1191 __ct.widen(__nf, __ne, __oe);
1192 __oe += __ne - __nf;
1193 if (__np == __ne)
1194 __op = __oe;
1195 else
1196 __op = __ob + (__np - __nb);
1197}
1198
Eric Fiselier1b57fa82016-09-15 22:27:07 +00001199_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<char>)
1200_LIBCPP_EXTERN_TEMPLATE2(struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201
1202template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001203class _LIBCPP_TEMPLATE_VIS num_put
Howard Hinnantc51e1022010-05-11 19:42:16 +00001204 : public locale::facet,
1205 private __num_put<_CharT>
1206{
1207public:
1208 typedef _CharT char_type;
1209 typedef _OutputIterator iter_type;
1210
1211 _LIBCPP_ALWAYS_INLINE
1212 explicit num_put(size_t __refs = 0)
1213 : locale::facet(__refs) {}
1214
1215 _LIBCPP_ALWAYS_INLINE
1216 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1217 bool __v) const
1218 {
1219 return do_put(__s, __iob, __fl, __v);
1220 }
1221
1222 _LIBCPP_ALWAYS_INLINE
1223 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1224 long __v) const
1225 {
1226 return do_put(__s, __iob, __fl, __v);
1227 }
1228
1229 _LIBCPP_ALWAYS_INLINE
1230 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1231 long long __v) const
1232 {
1233 return do_put(__s, __iob, __fl, __v);
1234 }
1235
1236 _LIBCPP_ALWAYS_INLINE
1237 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1238 unsigned long __v) const
1239 {
1240 return do_put(__s, __iob, __fl, __v);
1241 }
1242
1243 _LIBCPP_ALWAYS_INLINE
1244 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1245 unsigned long long __v) const
1246 {
1247 return do_put(__s, __iob, __fl, __v);
1248 }
1249
1250 _LIBCPP_ALWAYS_INLINE
1251 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1252 double __v) const
1253 {
1254 return do_put(__s, __iob, __fl, __v);
1255 }
1256
1257 _LIBCPP_ALWAYS_INLINE
1258 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1259 long double __v) const
1260 {
1261 return do_put(__s, __iob, __fl, __v);
1262 }
1263
1264 _LIBCPP_ALWAYS_INLINE
1265 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1266 const void* __v) const
1267 {
1268 return do_put(__s, __iob, __fl, __v);
1269 }
1270
1271 static locale::id id;
1272
1273protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00001274 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275 ~num_put() {}
1276
1277 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1278 bool __v) const;
1279 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1280 long __v) const;
1281 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1282 long long __v) const;
1283 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1284 unsigned long) const;
1285 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1286 unsigned long long) const;
1287 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1288 double __v) const;
1289 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1290 long double __v) const;
1291 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1292 const void* __v) const;
1293};
1294
1295template <class _CharT, class _OutputIterator>
1296locale::id
1297num_put<_CharT, _OutputIterator>::id;
1298
1299template <class _CharT, class _OutputIterator>
1300_LIBCPP_HIDDEN
1301_OutputIterator
1302__pad_and_output(_OutputIterator __s,
1303 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1304 ios_base& __iob, _CharT __fl)
1305{
1306 streamsize __sz = __oe - __ob;
1307 streamsize __ns = __iob.width();
1308 if (__ns > __sz)
1309 __ns -= __sz;
1310 else
1311 __ns = 0;
1312 for (;__ob < __op; ++__ob, ++__s)
1313 *__s = *__ob;
1314 for (; __ns; --__ns, ++__s)
1315 *__s = __fl;
1316 for (; __ob < __oe; ++__ob, ++__s)
1317 *__s = *__ob;
1318 __iob.width(0);
1319 return __s;
1320}
1321
Howard Hinnant48fd5d52012-11-14 21:17:15 +00001322#if !defined(__APPLE__) || \
1323 (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED > __MAC_10_8) || \
1324 (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_6_0)
1325
Howard Hinnant97955172012-09-19 19:14:15 +00001326template <class _CharT, class _Traits>
1327_LIBCPP_HIDDEN
1328ostreambuf_iterator<_CharT, _Traits>
1329__pad_and_output(ostreambuf_iterator<_CharT, _Traits> __s,
1330 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1331 ios_base& __iob, _CharT __fl)
1332{
1333 if (__s.__sbuf_ == nullptr)
1334 return __s;
1335 streamsize __sz = __oe - __ob;
1336 streamsize __ns = __iob.width();
1337 if (__ns > __sz)
1338 __ns -= __sz;
1339 else
1340 __ns = 0;
1341 streamsize __np = __op - __ob;
1342 if (__np > 0)
1343 {
1344 if (__s.__sbuf_->sputn(__ob, __np) != __np)
1345 {
1346 __s.__sbuf_ = nullptr;
1347 return __s;
1348 }
1349 }
1350 if (__ns > 0)
1351 {
1352 basic_string<_CharT, _Traits> __sp(__ns, __fl);
1353 if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns)
1354 {
1355 __s.__sbuf_ = nullptr;
1356 return __s;
1357 }
1358 }
1359 __np = __oe - __op;
1360 if (__np > 0)
1361 {
1362 if (__s.__sbuf_->sputn(__op, __np) != __np)
1363 {
1364 __s.__sbuf_ = nullptr;
1365 return __s;
1366 }
1367 }
1368 __iob.width(0);
1369 return __s;
1370}
1371
Howard Hinnant48fd5d52012-11-14 21:17:15 +00001372#endif
1373
Howard Hinnantc51e1022010-05-11 19:42:16 +00001374template <class _CharT, class _OutputIterator>
1375_OutputIterator
1376num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1377 char_type __fl, bool __v) const
1378{
1379 if ((__iob.flags() & ios_base::boolalpha) == 0)
1380 return do_put(__s, __iob, __fl, (unsigned long)__v);
1381 const numpunct<char_type>& __np = use_facet<numpunct<char_type> >(__iob.getloc());
1382 typedef typename numpunct<char_type>::string_type string_type;
Howard Hinnant8ea98242013-08-23 17:37:05 +00001383#if _LIBCPP_DEBUG_LEVEL >= 2
1384 string_type __tmp(__v ? __np.truename() : __np.falsename());
1385 string_type __nm = _VSTD::move(__tmp);
1386#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001387 string_type __nm = __v ? __np.truename() : __np.falsename();
Howard Hinnant8ea98242013-08-23 17:37:05 +00001388#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389 for (typename string_type::iterator __i = __nm.begin(); __i != __nm.end(); ++__i, ++__s)
1390 *__s = *__i;
1391 return __s;
1392}
1393
1394template <class _CharT, class _OutputIterator>
1395_OutputIterator
1396num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1397 char_type __fl, long __v) const
1398{
1399 // Stage 1 - Get number in narrow char
1400 char __fmt[6] = {'%', 0};
1401 const char* __len = "l";
1402 this->__format_int(__fmt+1, __len, true, __iob.flags());
1403 const unsigned __nbuf = (numeric_limits<long>::digits / 3)
1404 + ((numeric_limits<long>::digits % 3) != 0)
Dimitry Andriccfb7b5d2017-05-06 20:58:50 +00001405 + ((__iob.flags() & ios_base::showbase) != 0)
Eric Fiselier4cc37352016-04-29 07:23:20 +00001406 + 2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001407 char __nar[__nbuf];
Ben Craig3756b922016-03-09 15:39:39 +00001408 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001409 char* __ne = __nar + __nc;
1410 char* __np = this->__identify_padding(__nar, __ne, __iob);
1411 // Stage 2 - Widen __nar while adding thousands separators
1412 char_type __o[2*(__nbuf-1) - 1];
1413 char_type* __op; // pad here
1414 char_type* __oe; // end of output
1415 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1416 // [__o, __oe) contains thousands_sep'd wide number
1417 // Stage 3 & 4
1418 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1419}
1420
1421template <class _CharT, class _OutputIterator>
1422_OutputIterator
1423num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1424 char_type __fl, long long __v) const
1425{
1426 // Stage 1 - Get number in narrow char
1427 char __fmt[8] = {'%', 0};
1428 const char* __len = "ll";
1429 this->__format_int(__fmt+1, __len, true, __iob.flags());
1430 const unsigned __nbuf = (numeric_limits<long long>::digits / 3)
1431 + ((numeric_limits<long long>::digits % 3) != 0)
Dimitry Andriccfb7b5d2017-05-06 20:58:50 +00001432 + ((__iob.flags() & ios_base::showbase) != 0)
Marshall Clow7ac088f2015-01-26 17:24:52 +00001433 + 2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001434 char __nar[__nbuf];
Ben Craig3756b922016-03-09 15:39:39 +00001435 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001436 char* __ne = __nar + __nc;
1437 char* __np = this->__identify_padding(__nar, __ne, __iob);
1438 // Stage 2 - Widen __nar while adding thousands separators
1439 char_type __o[2*(__nbuf-1) - 1];
1440 char_type* __op; // pad here
1441 char_type* __oe; // end of output
1442 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1443 // [__o, __oe) contains thousands_sep'd wide number
1444 // Stage 3 & 4
1445 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1446}
1447
1448template <class _CharT, class _OutputIterator>
1449_OutputIterator
1450num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1451 char_type __fl, unsigned long __v) const
1452{
1453 // Stage 1 - Get number in narrow char
1454 char __fmt[6] = {'%', 0};
1455 const char* __len = "l";
1456 this->__format_int(__fmt+1, __len, false, __iob.flags());
1457 const unsigned __nbuf = (numeric_limits<unsigned long>::digits / 3)
1458 + ((numeric_limits<unsigned long>::digits % 3) != 0)
Dimitry Andriccfb7b5d2017-05-06 20:58:50 +00001459 + ((__iob.flags() & ios_base::showbase) != 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460 + 1;
1461 char __nar[__nbuf];
Ben Craig3756b922016-03-09 15:39:39 +00001462 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001463 char* __ne = __nar + __nc;
1464 char* __np = this->__identify_padding(__nar, __ne, __iob);
1465 // Stage 2 - Widen __nar while adding thousands separators
1466 char_type __o[2*(__nbuf-1) - 1];
1467 char_type* __op; // pad here
1468 char_type* __oe; // end of output
1469 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1470 // [__o, __oe) contains thousands_sep'd wide number
1471 // Stage 3 & 4
1472 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1473}
1474
1475template <class _CharT, class _OutputIterator>
1476_OutputIterator
1477num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1478 char_type __fl, unsigned long long __v) const
1479{
1480 // Stage 1 - Get number in narrow char
1481 char __fmt[8] = {'%', 0};
1482 const char* __len = "ll";
1483 this->__format_int(__fmt+1, __len, false, __iob.flags());
1484 const unsigned __nbuf = (numeric_limits<unsigned long long>::digits / 3)
1485 + ((numeric_limits<unsigned long long>::digits % 3) != 0)
Dimitry Andriccfb7b5d2017-05-06 20:58:50 +00001486 + ((__iob.flags() & ios_base::showbase) != 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001487 + 1;
1488 char __nar[__nbuf];
Ben Craig3756b922016-03-09 15:39:39 +00001489 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001490 char* __ne = __nar + __nc;
1491 char* __np = this->__identify_padding(__nar, __ne, __iob);
1492 // Stage 2 - Widen __nar while adding thousands separators
1493 char_type __o[2*(__nbuf-1) - 1];
1494 char_type* __op; // pad here
1495 char_type* __oe; // end of output
1496 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1497 // [__o, __oe) contains thousands_sep'd wide number
1498 // Stage 3 & 4
1499 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1500}
1501
1502template <class _CharT, class _OutputIterator>
1503_OutputIterator
1504num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1505 char_type __fl, double __v) const
1506{
1507 // Stage 1 - Get number in narrow char
1508 char __fmt[8] = {'%', 0};
1509 const char* __len = "";
1510 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1511 const unsigned __nbuf = 30;
1512 char __nar[__nbuf];
1513 char* __nb = __nar;
1514 int __nc;
1515 if (__specify_precision)
Ben Craig3756b922016-03-09 15:39:39 +00001516 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
Howard Hinnant155c2af2010-05-24 17:49:41 +00001517 (int)__iob.precision(), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001518 else
Ben Craig3756b922016-03-09 15:39:39 +00001519 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001520 unique_ptr<char, void(*)(void*)> __nbh(0, free);
1521 if (__nc > static_cast<int>(__nbuf-1))
1522 {
1523 if (__specify_precision)
Ben Craig3756b922016-03-09 15:39:39 +00001524 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001525 else
Ben Craig3756b922016-03-09 15:39:39 +00001526 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001527 if (__nb == 0)
1528 __throw_bad_alloc();
1529 __nbh.reset(__nb);
1530 }
1531 char* __ne = __nb + __nc;
1532 char* __np = this->__identify_padding(__nb, __ne, __iob);
1533 // Stage 2 - Widen __nar while adding thousands separators
1534 char_type __o[2*(__nbuf-1) - 1];
1535 char_type* __ob = __o;
1536 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1537 if (__nb != __nar)
1538 {
Howard Hinnant28b24882011-12-01 20:21:04 +00001539 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540 if (__ob == 0)
1541 __throw_bad_alloc();
1542 __obh.reset(__ob);
1543 }
1544 char_type* __op; // pad here
1545 char_type* __oe; // end of output
1546 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1547 // [__o, __oe) contains thousands_sep'd wide number
1548 // Stage 3 & 4
1549 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1550 return __s;
1551}
1552
1553template <class _CharT, class _OutputIterator>
1554_OutputIterator
1555num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1556 char_type __fl, long double __v) const
1557{
1558 // Stage 1 - Get number in narrow char
1559 char __fmt[8] = {'%', 0};
1560 const char* __len = "L";
1561 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1562 const unsigned __nbuf = 30;
1563 char __nar[__nbuf];
1564 char* __nb = __nar;
1565 int __nc;
1566 if (__specify_precision)
Ben Craig3756b922016-03-09 15:39:39 +00001567 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
Howard Hinnant155c2af2010-05-24 17:49:41 +00001568 (int)__iob.precision(), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001569 else
Ben Craig3756b922016-03-09 15:39:39 +00001570 __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001571 unique_ptr<char, void(*)(void*)> __nbh(0, free);
1572 if (__nc > static_cast<int>(__nbuf-1))
1573 {
1574 if (__specify_precision)
Ben Craig3756b922016-03-09 15:39:39 +00001575 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001576 else
Ben Craig3756b922016-03-09 15:39:39 +00001577 __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578 if (__nb == 0)
1579 __throw_bad_alloc();
1580 __nbh.reset(__nb);
1581 }
1582 char* __ne = __nb + __nc;
1583 char* __np = this->__identify_padding(__nb, __ne, __iob);
1584 // Stage 2 - Widen __nar while adding thousands separators
1585 char_type __o[2*(__nbuf-1) - 1];
1586 char_type* __ob = __o;
1587 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1588 if (__nb != __nar)
1589 {
Howard Hinnant28b24882011-12-01 20:21:04 +00001590 __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591 if (__ob == 0)
1592 __throw_bad_alloc();
1593 __obh.reset(__ob);
1594 }
1595 char_type* __op; // pad here
1596 char_type* __oe; // end of output
1597 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1598 // [__o, __oe) contains thousands_sep'd wide number
1599 // Stage 3 & 4
1600 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1601 return __s;
1602}
1603
1604template <class _CharT, class _OutputIterator>
1605_OutputIterator
1606num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1607 char_type __fl, const void* __v) const
1608{
1609 // Stage 1 - Get pointer in narrow char
1610 char __fmt[6] = "%p";
1611 const unsigned __nbuf = 20;
1612 char __nar[__nbuf];
Ben Craig3756b922016-03-09 15:39:39 +00001613 int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001614 char* __ne = __nar + __nc;
1615 char* __np = this->__identify_padding(__nar, __ne, __iob);
1616 // Stage 2 - Widen __nar
1617 char_type __o[2*(__nbuf-1) - 1];
1618 char_type* __op; // pad here
1619 char_type* __oe; // end of output
1620 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
1621 __ct.widen(__nar, __ne, __o);
1622 __oe = __o + (__ne - __nar);
1623 if (__np == __ne)
1624 __op = __oe;
1625 else
1626 __op = __o + (__np - __nar);
1627 // [__o, __oe) contains wide number
1628 // Stage 3 & 4
1629 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1630}
1631
Eric Fiselier1b57fa82016-09-15 22:27:07 +00001632_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<char>)
1633_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001634
1635template <class _CharT, class _InputIterator>
1636_LIBCPP_HIDDEN
1637int
1638__get_up_to_n_digits(_InputIterator& __b, _InputIterator __e,
1639 ios_base::iostate& __err, const ctype<_CharT>& __ct, int __n)
1640{
1641 // Precondition: __n >= 1
1642 if (__b == __e)
1643 {
1644 __err |= ios_base::eofbit | ios_base::failbit;
1645 return 0;
1646 }
1647 // get first digit
1648 _CharT __c = *__b;
1649 if (!__ct.is(ctype_base::digit, __c))
1650 {
1651 __err |= ios_base::failbit;
1652 return 0;
1653 }
1654 int __r = __ct.narrow(__c, 0) - '0';
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001655 for (++__b, (void) --__n; __b != __e && __n > 0; ++__b, (void) --__n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001656 {
1657 // get next digit
1658 __c = *__b;
1659 if (!__ct.is(ctype_base::digit, __c))
1660 return __r;
1661 __r = __r * 10 + __ct.narrow(__c, 0) - '0';
1662 }
1663 if (__b == __e)
1664 __err |= ios_base::eofbit;
1665 return __r;
1666}
1667
Howard Hinnant8331b762013-03-06 23:30:19 +00001668class _LIBCPP_TYPE_VIS time_base
Howard Hinnantc51e1022010-05-11 19:42:16 +00001669{
1670public:
1671 enum dateorder {no_order, dmy, mdy, ymd, ydm};
1672};
1673
1674template <class _CharT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001675class _LIBCPP_TEMPLATE_VIS __time_get_c_storage
Howard Hinnantc51e1022010-05-11 19:42:16 +00001676{
1677protected:
1678 typedef basic_string<_CharT> string_type;
1679
1680 virtual const string_type* __weeks() const;
1681 virtual const string_type* __months() const;
1682 virtual const string_type* __am_pm() const;
1683 virtual const string_type& __c() const;
1684 virtual const string_type& __r() const;
1685 virtual const string_type& __x() const;
1686 virtual const string_type& __X() const;
Eric Fiselier5eb6efc2015-08-18 19:39:35 +00001687
1688 _LIBCPP_ALWAYS_INLINE
1689 ~__time_get_c_storage() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001690};
1691
Eric Fiselierf30c0af2017-05-08 00:29:32 +00001692template <> _LIBCPP_FUNC_VIS const string* __time_get_c_storage<char>::__weeks() const;
1693template <> _LIBCPP_FUNC_VIS const string* __time_get_c_storage<char>::__months() const;
1694template <> _LIBCPP_FUNC_VIS const string* __time_get_c_storage<char>::__am_pm() const;
1695template <> _LIBCPP_FUNC_VIS const string& __time_get_c_storage<char>::__c() const;
1696template <> _LIBCPP_FUNC_VIS const string& __time_get_c_storage<char>::__r() const;
1697template <> _LIBCPP_FUNC_VIS const string& __time_get_c_storage<char>::__x() const;
1698template <> _LIBCPP_FUNC_VIS const string& __time_get_c_storage<char>::__X() const;
1699
1700template <> _LIBCPP_FUNC_VIS const wstring* __time_get_c_storage<wchar_t>::__weeks() const;
1701template <> _LIBCPP_FUNC_VIS const wstring* __time_get_c_storage<wchar_t>::__months() const;
1702template <> _LIBCPP_FUNC_VIS const wstring* __time_get_c_storage<wchar_t>::__am_pm() const;
1703template <> _LIBCPP_FUNC_VIS const wstring& __time_get_c_storage<wchar_t>::__c() const;
1704template <> _LIBCPP_FUNC_VIS const wstring& __time_get_c_storage<wchar_t>::__r() const;
1705template <> _LIBCPP_FUNC_VIS const wstring& __time_get_c_storage<wchar_t>::__x() const;
1706template <> _LIBCPP_FUNC_VIS const wstring& __time_get_c_storage<wchar_t>::__X() const;
1707
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001709class _LIBCPP_TEMPLATE_VIS time_get
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710 : public locale::facet,
1711 public time_base,
1712 private __time_get_c_storage<_CharT>
1713{
1714public:
1715 typedef _CharT char_type;
1716 typedef _InputIterator iter_type;
1717 typedef time_base::dateorder dateorder;
1718 typedef basic_string<char_type> string_type;
1719
1720 _LIBCPP_ALWAYS_INLINE
1721 explicit time_get(size_t __refs = 0)
1722 : locale::facet(__refs) {}
1723
1724 _LIBCPP_ALWAYS_INLINE
1725 dateorder date_order() const
1726 {
1727 return this->do_date_order();
1728 }
1729
1730 _LIBCPP_ALWAYS_INLINE
1731 iter_type get_time(iter_type __b, iter_type __e, ios_base& __iob,
1732 ios_base::iostate& __err, tm* __tm) const
1733 {
1734 return do_get_time(__b, __e, __iob, __err, __tm);
1735 }
1736
1737 _LIBCPP_ALWAYS_INLINE
1738 iter_type get_date(iter_type __b, iter_type __e, ios_base& __iob,
1739 ios_base::iostate& __err, tm* __tm) const
1740 {
1741 return do_get_date(__b, __e, __iob, __err, __tm);
1742 }
1743
1744 _LIBCPP_ALWAYS_INLINE
1745 iter_type get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
1746 ios_base::iostate& __err, tm* __tm) const
1747 {
1748 return do_get_weekday(__b, __e, __iob, __err, __tm);
1749 }
1750
1751 _LIBCPP_ALWAYS_INLINE
1752 iter_type get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
1753 ios_base::iostate& __err, tm* __tm) const
1754 {
1755 return do_get_monthname(__b, __e, __iob, __err, __tm);
1756 }
1757
1758 _LIBCPP_ALWAYS_INLINE
1759 iter_type get_year(iter_type __b, iter_type __e, ios_base& __iob,
1760 ios_base::iostate& __err, tm* __tm) const
1761 {
1762 return do_get_year(__b, __e, __iob, __err, __tm);
1763 }
1764
1765 _LIBCPP_ALWAYS_INLINE
1766 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
1767 ios_base::iostate& __err, tm *__tm,
1768 char __fmt, char __mod = 0) const
1769 {
1770 return do_get(__b, __e, __iob, __err, __tm, __fmt, __mod);
1771 }
1772
1773 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
1774 ios_base::iostate& __err, tm* __tm,
1775 const char_type* __fmtb, const char_type* __fmte) const;
1776
1777 static locale::id id;
1778
1779protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00001780 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781 ~time_get() {}
1782
1783 virtual dateorder do_date_order() const;
1784 virtual iter_type do_get_time(iter_type __b, iter_type __e, ios_base& __iob,
1785 ios_base::iostate& __err, tm* __tm) const;
1786 virtual iter_type do_get_date(iter_type __b, iter_type __e, ios_base& __iob,
1787 ios_base::iostate& __err, tm* __tm) const;
1788 virtual iter_type do_get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
1789 ios_base::iostate& __err, tm* __tm) const;
1790 virtual iter_type do_get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
1791 ios_base::iostate& __err, tm* __tm) const;
1792 virtual iter_type do_get_year(iter_type __b, iter_type __e, ios_base& __iob,
1793 ios_base::iostate& __err, tm* __tm) const;
1794 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
1795 ios_base::iostate& __err, tm* __tm,
1796 char __fmt, char __mod) const;
1797private:
1798 void __get_white_space(iter_type& __b, iter_type __e,
1799 ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1800 void __get_percent(iter_type& __b, iter_type __e, ios_base::iostate& __err,
1801 const ctype<char_type>& __ct) const;
1802
1803 void __get_weekdayname(int& __m,
1804 iter_type& __b, iter_type __e,
1805 ios_base::iostate& __err,
1806 const ctype<char_type>& __ct) const;
1807 void __get_monthname(int& __m,
1808 iter_type& __b, iter_type __e,
1809 ios_base::iostate& __err,
1810 const ctype<char_type>& __ct) const;
1811 void __get_day(int& __d,
1812 iter_type& __b, iter_type __e,
1813 ios_base::iostate& __err,
1814 const ctype<char_type>& __ct) const;
1815 void __get_month(int& __m,
1816 iter_type& __b, iter_type __e,
1817 ios_base::iostate& __err,
1818 const ctype<char_type>& __ct) const;
1819 void __get_year(int& __y,
1820 iter_type& __b, iter_type __e,
1821 ios_base::iostate& __err,
1822 const ctype<char_type>& __ct) const;
1823 void __get_year4(int& __y,
1824 iter_type& __b, iter_type __e,
1825 ios_base::iostate& __err,
1826 const ctype<char_type>& __ct) const;
1827 void __get_hour(int& __d,
1828 iter_type& __b, iter_type __e,
1829 ios_base::iostate& __err,
1830 const ctype<char_type>& __ct) const;
1831 void __get_12_hour(int& __h,
1832 iter_type& __b, iter_type __e,
1833 ios_base::iostate& __err,
1834 const ctype<char_type>& __ct) const;
1835 void __get_am_pm(int& __h,
1836 iter_type& __b, iter_type __e,
1837 ios_base::iostate& __err,
1838 const ctype<char_type>& __ct) const;
1839 void __get_minute(int& __m,
1840 iter_type& __b, iter_type __e,
1841 ios_base::iostate& __err,
1842 const ctype<char_type>& __ct) const;
1843 void __get_second(int& __s,
1844 iter_type& __b, iter_type __e,
1845 ios_base::iostate& __err,
1846 const ctype<char_type>& __ct) const;
1847 void __get_weekday(int& __w,
1848 iter_type& __b, iter_type __e,
1849 ios_base::iostate& __err,
1850 const ctype<char_type>& __ct) const;
1851 void __get_day_year_num(int& __w,
1852 iter_type& __b, iter_type __e,
1853 ios_base::iostate& __err,
1854 const ctype<char_type>& __ct) const;
1855};
1856
1857template <class _CharT, class _InputIterator>
1858locale::id
1859time_get<_CharT, _InputIterator>::id;
1860
Alp Tokerb8a95f52014-05-15 11:27:39 +00001861// time_get primitives
Howard Hinnantc51e1022010-05-11 19:42:16 +00001862
1863template <class _CharT, class _InputIterator>
1864void
1865time_get<_CharT, _InputIterator>::__get_weekdayname(int& __w,
1866 iter_type& __b, iter_type __e,
1867 ios_base::iostate& __err,
1868 const ctype<char_type>& __ct) const
1869{
1870 // Note: ignoring case comes from the POSIX strptime spec
1871 const string_type* __wk = this->__weeks();
Howard Hinnant28b24882011-12-01 20:21:04 +00001872 ptrdiff_t __i = __scan_keyword(__b, __e, __wk, __wk+14, __ct, __err, false) - __wk;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001873 if (__i < 14)
1874 __w = __i % 7;
1875}
1876
1877template <class _CharT, class _InputIterator>
1878void
1879time_get<_CharT, _InputIterator>::__get_monthname(int& __m,
1880 iter_type& __b, iter_type __e,
1881 ios_base::iostate& __err,
1882 const ctype<char_type>& __ct) const
1883{
1884 // Note: ignoring case comes from the POSIX strptime spec
1885 const string_type* __month = this->__months();
Howard Hinnant28b24882011-12-01 20:21:04 +00001886 ptrdiff_t __i = __scan_keyword(__b, __e, __month, __month+24, __ct, __err, false) - __month;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001887 if (__i < 24)
1888 __m = __i % 12;
1889}
1890
1891template <class _CharT, class _InputIterator>
1892void
1893time_get<_CharT, _InputIterator>::__get_day(int& __d,
1894 iter_type& __b, iter_type __e,
1895 ios_base::iostate& __err,
1896 const ctype<char_type>& __ct) const
1897{
1898 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
1899 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 31)
1900 __d = __t;
1901 else
1902 __err |= ios_base::failbit;
1903}
1904
1905template <class _CharT, class _InputIterator>
1906void
1907time_get<_CharT, _InputIterator>::__get_month(int& __m,
1908 iter_type& __b, iter_type __e,
1909 ios_base::iostate& __err,
1910 const ctype<char_type>& __ct) const
1911{
1912 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2) - 1;
1913 if (!(__err & ios_base::failbit) && __t <= 11)
1914 __m = __t;
1915 else
1916 __err |= ios_base::failbit;
1917}
1918
1919template <class _CharT, class _InputIterator>
1920void
1921time_get<_CharT, _InputIterator>::__get_year(int& __y,
1922 iter_type& __b, iter_type __e,
1923 ios_base::iostate& __err,
1924 const ctype<char_type>& __ct) const
1925{
1926 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
1927 if (!(__err & ios_base::failbit))
1928 {
1929 if (__t < 69)
1930 __t += 2000;
1931 else if (69 <= __t && __t <= 99)
1932 __t += 1900;
1933 __y = __t - 1900;
1934 }
1935}
1936
1937template <class _CharT, class _InputIterator>
1938void
1939time_get<_CharT, _InputIterator>::__get_year4(int& __y,
1940 iter_type& __b, iter_type __e,
1941 ios_base::iostate& __err,
1942 const ctype<char_type>& __ct) const
1943{
1944 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
1945 if (!(__err & ios_base::failbit))
1946 __y = __t - 1900;
1947}
1948
1949template <class _CharT, class _InputIterator>
1950void
1951time_get<_CharT, _InputIterator>::__get_hour(int& __h,
1952 iter_type& __b, iter_type __e,
1953 ios_base::iostate& __err,
1954 const ctype<char_type>& __ct) const
1955{
1956 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
1957 if (!(__err & ios_base::failbit) && __t <= 23)
1958 __h = __t;
1959 else
1960 __err |= ios_base::failbit;
1961}
1962
1963template <class _CharT, class _InputIterator>
1964void
1965time_get<_CharT, _InputIterator>::__get_12_hour(int& __h,
1966 iter_type& __b, iter_type __e,
1967 ios_base::iostate& __err,
1968 const ctype<char_type>& __ct) const
1969{
1970 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
1971 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 12)
1972 __h = __t;
1973 else
1974 __err |= ios_base::failbit;
1975}
1976
1977template <class _CharT, class _InputIterator>
1978void
1979time_get<_CharT, _InputIterator>::__get_minute(int& __m,
1980 iter_type& __b, iter_type __e,
1981 ios_base::iostate& __err,
1982 const ctype<char_type>& __ct) const
1983{
1984 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
1985 if (!(__err & ios_base::failbit) && __t <= 59)
1986 __m = __t;
1987 else
1988 __err |= ios_base::failbit;
1989}
1990
1991template <class _CharT, class _InputIterator>
1992void
1993time_get<_CharT, _InputIterator>::__get_second(int& __s,
1994 iter_type& __b, iter_type __e,
1995 ios_base::iostate& __err,
1996 const ctype<char_type>& __ct) const
1997{
1998 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
1999 if (!(__err & ios_base::failbit) && __t <= 60)
2000 __s = __t;
2001 else
2002 __err |= ios_base::failbit;
2003}
2004
2005template <class _CharT, class _InputIterator>
2006void
2007time_get<_CharT, _InputIterator>::__get_weekday(int& __w,
2008 iter_type& __b, iter_type __e,
2009 ios_base::iostate& __err,
2010 const ctype<char_type>& __ct) const
2011{
2012 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 1);
2013 if (!(__err & ios_base::failbit) && __t <= 6)
2014 __w = __t;
2015 else
2016 __err |= ios_base::failbit;
2017}
2018
2019template <class _CharT, class _InputIterator>
2020void
2021time_get<_CharT, _InputIterator>::__get_day_year_num(int& __d,
2022 iter_type& __b, iter_type __e,
2023 ios_base::iostate& __err,
2024 const ctype<char_type>& __ct) const
2025{
2026 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 3);
2027 if (!(__err & ios_base::failbit) && __t <= 365)
2028 __d = __t;
2029 else
2030 __err |= ios_base::failbit;
2031}
2032
2033template <class _CharT, class _InputIterator>
2034void
2035time_get<_CharT, _InputIterator>::__get_white_space(iter_type& __b, iter_type __e,
2036 ios_base::iostate& __err,
2037 const ctype<char_type>& __ct) const
2038{
2039 for (; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2040 ;
2041 if (__b == __e)
2042 __err |= ios_base::eofbit;
2043}
2044
2045template <class _CharT, class _InputIterator>
2046void
2047time_get<_CharT, _InputIterator>::__get_am_pm(int& __h,
2048 iter_type& __b, iter_type __e,
2049 ios_base::iostate& __err,
2050 const ctype<char_type>& __ct) const
2051{
2052 const string_type* __ap = this->__am_pm();
2053 if (__ap[0].size() + __ap[1].size() == 0)
2054 {
2055 __err |= ios_base::failbit;
2056 return;
2057 }
Howard Hinnant28b24882011-12-01 20:21:04 +00002058 ptrdiff_t __i = __scan_keyword(__b, __e, __ap, __ap+2, __ct, __err, false) - __ap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002059 if (__i == 0 && __h == 12)
2060 __h = 0;
2061 else if (__i == 1 && __h < 12)
2062 __h += 12;
2063}
2064
2065template <class _CharT, class _InputIterator>
2066void
2067time_get<_CharT, _InputIterator>::__get_percent(iter_type& __b, iter_type __e,
2068 ios_base::iostate& __err,
2069 const ctype<char_type>& __ct) const
2070{
2071 if (__b == __e)
2072 {
2073 __err |= ios_base::eofbit | ios_base::failbit;
2074 return;
2075 }
2076 if (__ct.narrow(*__b, 0) != '%')
2077 __err |= ios_base::failbit;
2078 else if(++__b == __e)
2079 __err |= ios_base::eofbit;
2080}
2081
Alp Tokerb8a95f52014-05-15 11:27:39 +00002082// time_get end primitives
Howard Hinnantc51e1022010-05-11 19:42:16 +00002083
2084template <class _CharT, class _InputIterator>
2085_InputIterator
2086time_get<_CharT, _InputIterator>::get(iter_type __b, iter_type __e,
2087 ios_base& __iob,
2088 ios_base::iostate& __err, tm* __tm,
2089 const char_type* __fmtb, const char_type* __fmte) const
2090{
2091 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2092 __err = ios_base::goodbit;
2093 while (__fmtb != __fmte && __err == ios_base::goodbit)
2094 {
2095 if (__b == __e)
2096 {
2097 __err = ios_base::failbit;
2098 break;
2099 }
2100 if (__ct.narrow(*__fmtb, 0) == '%')
2101 {
2102 if (++__fmtb == __fmte)
2103 {
2104 __err = ios_base::failbit;
2105 break;
2106 }
2107 char __cmd = __ct.narrow(*__fmtb, 0);
2108 char __opt = '\0';
2109 if (__cmd == 'E' || __cmd == '0')
2110 {
2111 if (++__fmtb == __fmte)
2112 {
2113 __err = ios_base::failbit;
2114 break;
2115 }
2116 __opt = __cmd;
2117 __cmd = __ct.narrow(*__fmtb, 0);
2118 }
2119 __b = do_get(__b, __e, __iob, __err, __tm, __cmd, __opt);
2120 ++__fmtb;
2121 }
2122 else if (__ct.is(ctype_base::space, *__fmtb))
2123 {
2124 for (++__fmtb; __fmtb != __fmte && __ct.is(ctype_base::space, *__fmtb); ++__fmtb)
2125 ;
2126 for ( ; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2127 ;
2128 }
2129 else if (__ct.toupper(*__b) == __ct.toupper(*__fmtb))
2130 {
2131 ++__b;
2132 ++__fmtb;
2133 }
2134 else
2135 __err = ios_base::failbit;
2136 }
2137 if (__b == __e)
2138 __err |= ios_base::eofbit;
2139 return __b;
2140}
2141
2142template <class _CharT, class _InputIterator>
2143typename time_get<_CharT, _InputIterator>::dateorder
2144time_get<_CharT, _InputIterator>::do_date_order() const
2145{
2146 return mdy;
2147}
2148
2149template <class _CharT, class _InputIterator>
2150_InputIterator
2151time_get<_CharT, _InputIterator>::do_get_time(iter_type __b, iter_type __e,
2152 ios_base& __iob,
2153 ios_base::iostate& __err,
2154 tm* __tm) const
2155{
2156 const char_type __fmt[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2157 return get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt)/sizeof(__fmt[0]));
2158}
2159
2160template <class _CharT, class _InputIterator>
2161_InputIterator
2162time_get<_CharT, _InputIterator>::do_get_date(iter_type __b, iter_type __e,
2163 ios_base& __iob,
2164 ios_base::iostate& __err,
2165 tm* __tm) const
2166{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002167 const string_type& __fmt = this->__x();
2168 return get(__b, __e, __iob, __err, __tm, __fmt.data(), __fmt.data() + __fmt.size());
2169}
2170
2171template <class _CharT, class _InputIterator>
2172_InputIterator
2173time_get<_CharT, _InputIterator>::do_get_weekday(iter_type __b, iter_type __e,
2174 ios_base& __iob,
2175 ios_base::iostate& __err,
2176 tm* __tm) const
2177{
2178 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2179 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2180 return __b;
2181}
2182
2183template <class _CharT, class _InputIterator>
2184_InputIterator
2185time_get<_CharT, _InputIterator>::do_get_monthname(iter_type __b, iter_type __e,
2186 ios_base& __iob,
2187 ios_base::iostate& __err,
2188 tm* __tm) const
2189{
2190 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2191 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2192 return __b;
2193}
2194
2195template <class _CharT, class _InputIterator>
2196_InputIterator
2197time_get<_CharT, _InputIterator>::do_get_year(iter_type __b, iter_type __e,
2198 ios_base& __iob,
2199 ios_base::iostate& __err,
2200 tm* __tm) const
2201{
2202 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2203 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2204 return __b;
2205}
2206
2207template <class _CharT, class _InputIterator>
2208_InputIterator
2209time_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
2210 ios_base& __iob,
2211 ios_base::iostate& __err, tm* __tm,
2212 char __fmt, char) const
2213{
2214 __err = ios_base::goodbit;
2215 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2216 switch (__fmt)
2217 {
2218 case 'a':
2219 case 'A':
2220 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2221 break;
2222 case 'b':
2223 case 'B':
2224 case 'h':
2225 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2226 break;
2227 case 'c':
2228 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002229 const string_type& __fm = this->__c();
2230 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002231 }
2232 break;
2233 case 'd':
2234 case 'e':
2235 __get_day(__tm->tm_mday, __b, __e, __err, __ct);
2236 break;
2237 case 'D':
2238 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002239 const char_type __fm[] = {'%', 'm', '/', '%', 'd', '/', '%', 'y'};
2240 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002241 }
2242 break;
Howard Hinnantf60ff4e2011-04-10 17:54:14 +00002243 case 'F':
2244 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002245 const char_type __fm[] = {'%', 'Y', '-', '%', 'm', '-', '%', 'd'};
2246 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantf60ff4e2011-04-10 17:54:14 +00002247 }
2248 break;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002249 case 'H':
2250 __get_hour(__tm->tm_hour, __b, __e, __err, __ct);
2251 break;
2252 case 'I':
2253 __get_12_hour(__tm->tm_hour, __b, __e, __err, __ct);
2254 break;
2255 case 'j':
2256 __get_day_year_num(__tm->tm_yday, __b, __e, __err, __ct);
2257 break;
2258 case 'm':
2259 __get_month(__tm->tm_mon, __b, __e, __err, __ct);
2260 break;
2261 case 'M':
2262 __get_minute(__tm->tm_min, __b, __e, __err, __ct);
2263 break;
2264 case 'n':
2265 case 't':
2266 __get_white_space(__b, __e, __err, __ct);
2267 break;
2268 case 'p':
2269 __get_am_pm(__tm->tm_hour, __b, __e, __err, __ct);
2270 break;
2271 case 'r':
2272 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002273 const char_type __fm[] = {'%', 'I', ':', '%', 'M', ':', '%', 'S', ' ', '%', 'p'};
2274 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002275 }
2276 break;
2277 case 'R':
2278 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002279 const char_type __fm[] = {'%', 'H', ':', '%', 'M'};
2280 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002281 }
2282 break;
2283 case 'S':
2284 __get_second(__tm->tm_sec, __b, __e, __err, __ct);
2285 break;
2286 case 'T':
2287 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002288 const char_type __fm[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2289 __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002290 }
2291 break;
2292 case 'w':
2293 __get_weekday(__tm->tm_wday, __b, __e, __err, __ct);
2294 break;
2295 case 'x':
2296 return do_get_date(__b, __e, __iob, __err, __tm);
2297 case 'X':
2298 {
Howard Hinnant28b24882011-12-01 20:21:04 +00002299 const string_type& __fm = this->__X();
2300 __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002301 }
2302 break;
2303 case 'y':
2304 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2305 break;
2306 case 'Y':
2307 __get_year4(__tm->tm_year, __b, __e, __err, __ct);
2308 break;
2309 case '%':
2310 __get_percent(__b, __e, __err, __ct);
2311 break;
2312 default:
2313 __err |= ios_base::failbit;
2314 }
2315 return __b;
2316}
2317
Eric Fiselier1b57fa82016-09-15 22:27:07 +00002318_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get<char>)
2319_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002320
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002321class _LIBCPP_TYPE_VIS __time_get
Howard Hinnantc51e1022010-05-11 19:42:16 +00002322{
2323protected:
2324 locale_t __loc_;
2325
2326 __time_get(const char* __nm);
2327 __time_get(const string& __nm);
2328 ~__time_get();
2329};
2330
2331template <class _CharT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002332class _LIBCPP_TEMPLATE_VIS __time_get_storage
Howard Hinnantc51e1022010-05-11 19:42:16 +00002333 : public __time_get
2334{
2335protected:
2336 typedef basic_string<_CharT> string_type;
2337
2338 string_type __weeks_[14];
2339 string_type __months_[24];
2340 string_type __am_pm_[2];
2341 string_type __c_;
2342 string_type __r_;
2343 string_type __x_;
2344 string_type __X_;
2345
2346 explicit __time_get_storage(const char* __nm);
2347 explicit __time_get_storage(const string& __nm);
2348
2349 _LIBCPP_ALWAYS_INLINE ~__time_get_storage() {}
2350
2351 time_base::dateorder __do_date_order() const;
2352
2353private:
2354 void init(const ctype<_CharT>&);
2355 string_type __analyze(char __fmt, const ctype<_CharT>&);
2356};
2357
2358template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002359class _LIBCPP_TEMPLATE_VIS time_get_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00002360 : public time_get<_CharT, _InputIterator>,
2361 private __time_get_storage<_CharT>
2362{
2363public:
2364 typedef time_base::dateorder dateorder;
2365 typedef _InputIterator iter_type;
2366 typedef _CharT char_type;
2367 typedef basic_string<char_type> string_type;
2368
Howard Hinnant756c69b2010-09-22 16:48:34 +00002369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002370 explicit time_get_byname(const char* __nm, size_t __refs = 0)
2371 : time_get<_CharT, _InputIterator>(__refs),
2372 __time_get_storage<_CharT>(__nm) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002373 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002374 explicit time_get_byname(const string& __nm, size_t __refs = 0)
2375 : time_get<_CharT, _InputIterator>(__refs),
2376 __time_get_storage<_CharT>(__nm) {}
2377
2378protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002379 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002380 ~time_get_byname() {}
2381
Howard Hinnant756c69b2010-09-22 16:48:34 +00002382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002383 virtual dateorder do_date_order() const {return this->__do_date_order();}
2384private:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002386 virtual const string_type* __weeks() const {return this->__weeks_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002387 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002388 virtual const string_type* __months() const {return this->__months_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002390 virtual const string_type* __am_pm() const {return this->__am_pm_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002392 virtual const string_type& __c() const {return this->__c_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002394 virtual const string_type& __r() const {return this->__r_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002395 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002396 virtual const string_type& __x() const {return this->__x_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002397 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002398 virtual const string_type& __X() const {return this->__X_;}
2399};
2400
Eric Fiselier1b57fa82016-09-15 22:27:07 +00002401_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<char>)
2402_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002403
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002404class _LIBCPP_TYPE_VIS __time_put
Howard Hinnantc51e1022010-05-11 19:42:16 +00002405{
2406 locale_t __loc_;
2407protected:
Howard Hinnant1ab52da2011-09-28 21:05:01 +00002408 _LIBCPP_ALWAYS_INLINE __time_put() : __loc_(_LIBCPP_GET_C_LOCALE) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002409 __time_put(const char* __nm);
2410 __time_put(const string& __nm);
2411 ~__time_put();
2412 void __do_put(char* __nb, char*& __ne, const tm* __tm,
2413 char __fmt, char __mod) const;
2414 void __do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm,
2415 char __fmt, char __mod) const;
2416};
2417
2418template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002419class _LIBCPP_TEMPLATE_VIS time_put
Howard Hinnantc51e1022010-05-11 19:42:16 +00002420 : public locale::facet,
2421 private __time_put
2422{
2423public:
2424 typedef _CharT char_type;
2425 typedef _OutputIterator iter_type;
2426
2427 _LIBCPP_ALWAYS_INLINE
2428 explicit time_put(size_t __refs = 0)
2429 : locale::facet(__refs) {}
2430
2431 iter_type put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm,
2432 const char_type* __pb, const char_type* __pe) const;
2433
2434 _LIBCPP_ALWAYS_INLINE
2435 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
2436 const tm* __tm, char __fmt, char __mod = 0) const
2437 {
2438 return do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2439 }
2440
2441 static locale::id id;
2442
2443protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002444 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002445 ~time_put() {}
2446 virtual iter_type do_put(iter_type __s, ios_base&, char_type, const tm* __tm,
2447 char __fmt, char __mod) const;
2448
Howard Hinnant756c69b2010-09-22 16:48:34 +00002449 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002450 explicit time_put(const char* __nm, size_t __refs)
2451 : locale::facet(__refs),
2452 __time_put(__nm) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002453 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002454 explicit time_put(const string& __nm, size_t __refs)
2455 : locale::facet(__refs),
2456 __time_put(__nm) {}
2457};
2458
2459template <class _CharT, class _OutputIterator>
2460locale::id
2461time_put<_CharT, _OutputIterator>::id;
2462
2463template <class _CharT, class _OutputIterator>
2464_OutputIterator
2465time_put<_CharT, _OutputIterator>::put(iter_type __s, ios_base& __iob,
2466 char_type __fl, const tm* __tm,
2467 const char_type* __pb,
2468 const char_type* __pe) const
2469{
2470 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2471 for (; __pb != __pe; ++__pb)
2472 {
2473 if (__ct.narrow(*__pb, 0) == '%')
2474 {
2475 if (++__pb == __pe)
2476 {
2477 *__s++ = __pb[-1];
2478 break;
2479 }
2480 char __mod = 0;
2481 char __fmt = __ct.narrow(*__pb, 0);
2482 if (__fmt == 'E' || __fmt == 'O')
2483 {
2484 if (++__pb == __pe)
2485 {
2486 *__s++ = __pb[-2];
2487 *__s++ = __pb[-1];
2488 break;
2489 }
2490 __mod = __fmt;
2491 __fmt = __ct.narrow(*__pb, 0);
2492 }
2493 __s = do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2494 }
2495 else
2496 *__s++ = *__pb;
2497 }
2498 return __s;
2499}
2500
2501template <class _CharT, class _OutputIterator>
2502_OutputIterator
Howard Hinnant28b24882011-12-01 20:21:04 +00002503time_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002504 char_type, const tm* __tm,
2505 char __fmt, char __mod) const
2506{
2507 char_type __nar[100];
2508 char_type* __nb = __nar;
2509 char_type* __ne = __nb + 100;
2510 __do_put(__nb, __ne, __tm, __fmt, __mod);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002511 return _VSTD::copy(__nb, __ne, __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002512}
2513
Eric Fiselier1b57fa82016-09-15 22:27:07 +00002514_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<char>)
2515_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002516
2517template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002518class _LIBCPP_TEMPLATE_VIS time_put_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519 : public time_put<_CharT, _OutputIterator>
2520{
2521public:
2522 _LIBCPP_ALWAYS_INLINE
2523 explicit time_put_byname(const char* __nm, size_t __refs = 0)
2524 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2525
2526 _LIBCPP_ALWAYS_INLINE
2527 explicit time_put_byname(const string& __nm, size_t __refs = 0)
2528 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2529
2530protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002531 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002532 ~time_put_byname() {}
2533};
2534
Eric Fiselier1b57fa82016-09-15 22:27:07 +00002535_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<char>)
2536_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002537
2538// money_base
2539
Howard Hinnant8331b762013-03-06 23:30:19 +00002540class _LIBCPP_TYPE_VIS money_base
Howard Hinnantc51e1022010-05-11 19:42:16 +00002541{
2542public:
2543 enum part {none, space, symbol, sign, value};
2544 struct pattern {char field[4];};
2545
2546 _LIBCPP_ALWAYS_INLINE money_base() {}
2547};
2548
2549// moneypunct
2550
2551template <class _CharT, bool _International = false>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002552class _LIBCPP_TEMPLATE_VIS moneypunct
Howard Hinnantc51e1022010-05-11 19:42:16 +00002553 : public locale::facet,
2554 public money_base
2555{
2556public:
2557 typedef _CharT char_type;
2558 typedef basic_string<char_type> string_type;
2559
Howard Hinnant756c69b2010-09-22 16:48:34 +00002560 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002561 explicit moneypunct(size_t __refs = 0)
2562 : locale::facet(__refs) {}
2563
2564 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
2565 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
2566 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
2567 _LIBCPP_ALWAYS_INLINE string_type curr_symbol() const {return do_curr_symbol();}
2568 _LIBCPP_ALWAYS_INLINE string_type positive_sign() const {return do_positive_sign();}
2569 _LIBCPP_ALWAYS_INLINE string_type negative_sign() const {return do_negative_sign();}
2570 _LIBCPP_ALWAYS_INLINE int frac_digits() const {return do_frac_digits();}
2571 _LIBCPP_ALWAYS_INLINE pattern pos_format() const {return do_pos_format();}
2572 _LIBCPP_ALWAYS_INLINE pattern neg_format() const {return do_neg_format();}
2573
2574 static locale::id id;
2575 static const bool intl = _International;
2576
2577protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002578 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002579 ~moneypunct() {}
2580
2581 virtual char_type do_decimal_point() const {return numeric_limits<char_type>::max();}
2582 virtual char_type do_thousands_sep() const {return numeric_limits<char_type>::max();}
2583 virtual string do_grouping() const {return string();}
2584 virtual string_type do_curr_symbol() const {return string_type();}
2585 virtual string_type do_positive_sign() const {return string_type();}
2586 virtual string_type do_negative_sign() const {return string_type(1, '-');}
2587 virtual int do_frac_digits() const {return 0;}
2588 virtual pattern do_pos_format() const
Howard Hinnantb10e6cf2012-11-06 21:48:33 +00002589 {pattern __p = {{symbol, sign, none, value}}; return __p;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590 virtual pattern do_neg_format() const
Howard Hinnantb10e6cf2012-11-06 21:48:33 +00002591 {pattern __p = {{symbol, sign, none, value}}; return __p;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002592};
2593
2594template <class _CharT, bool _International>
2595locale::id
2596moneypunct<_CharT, _International>::id;
2597
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002598template <class _CharT, bool _International>
2599const bool
2600moneypunct<_CharT, _International>::intl;
2601
Eric Fiselier1b57fa82016-09-15 22:27:07 +00002602_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, false>)
2603_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, true>)
2604_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, false>)
2605_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002606
2607// moneypunct_byname
2608
2609template <class _CharT, bool _International = false>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002610class _LIBCPP_TEMPLATE_VIS moneypunct_byname
Howard Hinnant756c69b2010-09-22 16:48:34 +00002611 : public moneypunct<_CharT, _International>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002612{
2613public:
2614 typedef money_base::pattern pattern;
2615 typedef _CharT char_type;
2616 typedef basic_string<char_type> string_type;
2617
2618 _LIBCPP_ALWAYS_INLINE
2619 explicit moneypunct_byname(const char* __nm, size_t __refs = 0)
2620 : moneypunct<_CharT, _International>(__refs) {init(__nm);}
2621
2622 _LIBCPP_ALWAYS_INLINE
2623 explicit moneypunct_byname(const string& __nm, size_t __refs = 0)
2624 : moneypunct<_CharT, _International>(__refs) {init(__nm.c_str());}
2625
2626protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002627 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628 ~moneypunct_byname() {}
2629
2630 virtual char_type do_decimal_point() const {return __decimal_point_;}
2631 virtual char_type do_thousands_sep() const {return __thousands_sep_;}
2632 virtual string do_grouping() const {return __grouping_;}
2633 virtual string_type do_curr_symbol() const {return __curr_symbol_;}
2634 virtual string_type do_positive_sign() const {return __positive_sign_;}
2635 virtual string_type do_negative_sign() const {return __negative_sign_;}
2636 virtual int do_frac_digits() const {return __frac_digits_;}
2637 virtual pattern do_pos_format() const {return __pos_format_;}
2638 virtual pattern do_neg_format() const {return __neg_format_;}
2639
2640private:
2641 char_type __decimal_point_;
2642 char_type __thousands_sep_;
2643 string __grouping_;
2644 string_type __curr_symbol_;
2645 string_type __positive_sign_;
2646 string_type __negative_sign_;
2647 int __frac_digits_;
2648 pattern __pos_format_;
2649 pattern __neg_format_;
2650
2651 void init(const char*);
2652};
2653
Shoaib Meenai094c3d22017-04-03 04:04:24 +00002654template<> _LIBCPP_FUNC_VIS void moneypunct_byname<char, false>::init(const char*);
2655template<> _LIBCPP_FUNC_VIS void moneypunct_byname<char, true>::init(const char*);
2656template<> _LIBCPP_FUNC_VIS void moneypunct_byname<wchar_t, false>::init(const char*);
2657template<> _LIBCPP_FUNC_VIS void moneypunct_byname<wchar_t, true>::init(const char*);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002658
Eric Fiselier1b57fa82016-09-15 22:27:07 +00002659_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, false>)
2660_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, true>)
2661_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, false>)
2662_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002663
2664// money_get
2665
2666template <class _CharT>
2667class __money_get
2668{
2669protected:
2670 typedef _CharT char_type;
2671 typedef basic_string<char_type> string_type;
2672
2673 _LIBCPP_ALWAYS_INLINE __money_get() {}
2674
2675 static void __gather_info(bool __intl, const locale& __loc,
2676 money_base::pattern& __pat, char_type& __dp,
2677 char_type& __ts, string& __grp,
2678 string_type& __sym, string_type& __psn,
2679 string_type& __nsn, int& __fd);
2680};
2681
2682template <class _CharT>
2683void
2684__money_get<_CharT>::__gather_info(bool __intl, const locale& __loc,
2685 money_base::pattern& __pat, char_type& __dp,
2686 char_type& __ts, string& __grp,
2687 string_type& __sym, string_type& __psn,
2688 string_type& __nsn, int& __fd)
2689{
2690 if (__intl)
2691 {
2692 const moneypunct<char_type, true>& __mp =
2693 use_facet<moneypunct<char_type, true> >(__loc);
2694 __pat = __mp.neg_format();
2695 __nsn = __mp.negative_sign();
2696 __psn = __mp.positive_sign();
2697 __dp = __mp.decimal_point();
2698 __ts = __mp.thousands_sep();
2699 __grp = __mp.grouping();
2700 __sym = __mp.curr_symbol();
2701 __fd = __mp.frac_digits();
2702 }
2703 else
2704 {
2705 const moneypunct<char_type, false>& __mp =
2706 use_facet<moneypunct<char_type, false> >(__loc);
2707 __pat = __mp.neg_format();
2708 __nsn = __mp.negative_sign();
2709 __psn = __mp.positive_sign();
2710 __dp = __mp.decimal_point();
2711 __ts = __mp.thousands_sep();
2712 __grp = __mp.grouping();
2713 __sym = __mp.curr_symbol();
2714 __fd = __mp.frac_digits();
2715 }
2716}
2717
Eric Fiselier1b57fa82016-09-15 22:27:07 +00002718_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<char>)
2719_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002720
2721template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002722class _LIBCPP_TEMPLATE_VIS money_get
Howard Hinnantc51e1022010-05-11 19:42:16 +00002723 : public locale::facet,
2724 private __money_get<_CharT>
2725{
2726public:
2727 typedef _CharT char_type;
2728 typedef _InputIterator iter_type;
2729 typedef basic_string<char_type> string_type;
2730
2731 _LIBCPP_ALWAYS_INLINE
2732 explicit money_get(size_t __refs = 0)
2733 : locale::facet(__refs) {}
2734
2735 _LIBCPP_ALWAYS_INLINE
2736 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
2737 ios_base::iostate& __err, long double& __v) const
2738 {
2739 return do_get(__b, __e, __intl, __iob, __err, __v);
2740 }
2741
2742 _LIBCPP_ALWAYS_INLINE
2743 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
2744 ios_base::iostate& __err, string_type& __v) const
2745 {
2746 return do_get(__b, __e, __intl, __iob, __err, __v);
2747 }
2748
2749 static locale::id id;
2750
2751protected:
2752
Howard Hinnant756c69b2010-09-22 16:48:34 +00002753 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754 ~money_get() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002755
Howard Hinnantc51e1022010-05-11 19:42:16 +00002756 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
2757 ios_base& __iob, ios_base::iostate& __err,
2758 long double& __v) const;
2759 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
2760 ios_base& __iob, ios_base::iostate& __err,
2761 string_type& __v) const;
2762
2763private:
2764 static bool __do_get(iter_type& __b, iter_type __e,
2765 bool __intl, const locale& __loc,
2766 ios_base::fmtflags __flags, ios_base::iostate& __err,
2767 bool& __neg, const ctype<char_type>& __ct,
2768 unique_ptr<char_type, void(*)(void*)>& __wb,
2769 char_type*& __wn, char_type* __we);
2770};
2771
2772template <class _CharT, class _InputIterator>
2773locale::id
2774money_get<_CharT, _InputIterator>::id;
2775
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002776_LIBCPP_FUNC_VIS void __do_nothing(void*);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002777
2778template <class _Tp>
2779_LIBCPP_HIDDEN
2780void
2781__double_or_nothing(unique_ptr<_Tp, void(*)(void*)>& __b, _Tp*& __n, _Tp*& __e)
2782{
2783 bool __owns = __b.get_deleter() != __do_nothing;
Howard Hinnant28b24882011-12-01 20:21:04 +00002784 size_t __cur_cap = static_cast<size_t>(__e-__b.get()) * sizeof(_Tp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002785 size_t __new_cap = __cur_cap < numeric_limits<size_t>::max() / 2 ?
2786 2 * __cur_cap : numeric_limits<size_t>::max();
Marshall Clow6c9ddc22014-10-27 19:08:10 +00002787 if (__new_cap == 0)
2788 __new_cap = sizeof(_Tp);
Howard Hinnant28b24882011-12-01 20:21:04 +00002789 size_t __n_off = static_cast<size_t>(__n - __b.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790 _Tp* __t = (_Tp*)realloc(__owns ? __b.get() : 0, __new_cap);
2791 if (__t == 0)
2792 __throw_bad_alloc();
2793 if (__owns)
2794 __b.release();
2795 __b = unique_ptr<_Tp, void(*)(void*)>(__t, free);
2796 __new_cap /= sizeof(_Tp);
2797 __n = __b.get() + __n_off;
2798 __e = __b.get() + __new_cap;
2799}
2800
2801// true == success
2802template <class _CharT, class _InputIterator>
2803bool
2804money_get<_CharT, _InputIterator>::__do_get(iter_type& __b, iter_type __e,
2805 bool __intl, const locale& __loc,
2806 ios_base::fmtflags __flags,
2807 ios_base::iostate& __err,
2808 bool& __neg,
2809 const ctype<char_type>& __ct,
2810 unique_ptr<char_type, void(*)(void*)>& __wb,
2811 char_type*& __wn, char_type* __we)
2812{
2813 const unsigned __bz = 100;
2814 unsigned __gbuf[__bz];
2815 unique_ptr<unsigned, void(*)(void*)> __gb(__gbuf, __do_nothing);
2816 unsigned* __gn = __gb.get();
2817 unsigned* __ge = __gn + __bz;
2818 money_base::pattern __pat;
2819 char_type __dp;
2820 char_type __ts;
2821 string __grp;
2822 string_type __sym;
2823 string_type __psn;
2824 string_type __nsn;
Jeffrey Yasskin6b045fb2012-03-10 18:31:43 +00002825 // Capture the spaces read into money_base::{space,none} so they
2826 // can be compared to initial spaces in __sym.
2827 string_type __spaces;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002828 int __fd;
2829 __money_get<_CharT>::__gather_info(__intl, __loc, __pat, __dp, __ts, __grp,
2830 __sym, __psn, __nsn, __fd);
2831 const string_type* __trailing_sign = 0;
2832 __wn = __wb.get();
2833 for (unsigned __p = 0; __p < 4 && __b != __e; ++__p)
2834 {
2835 switch (__pat.field[__p])
2836 {
2837 case money_base::space:
2838 if (__p != 3)
2839 {
2840 if (__ct.is(ctype_base::space, *__b))
Jeffrey Yasskin6b045fb2012-03-10 18:31:43 +00002841 __spaces.push_back(*__b++);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002842 else
2843 {
2844 __err |= ios_base::failbit;
2845 return false;
2846 }
2847 }
Eric Fiselier4db80032017-05-05 20:32:26 +00002848 _LIBCPP_FALLTHROUGH();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002849 case money_base::none:
2850 if (__p != 3)
2851 {
2852 while (__b != __e && __ct.is(ctype_base::space, *__b))
Jeffrey Yasskin6b045fb2012-03-10 18:31:43 +00002853 __spaces.push_back(*__b++);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002854 }
2855 break;
2856 case money_base::sign:
2857 if (__psn.size() + __nsn.size() > 0)
2858 {
2859 if (__psn.size() == 0 || __nsn.size() == 0)
2860 { // sign is optional
2861 if (__psn.size() > 0)
2862 { // __nsn.size() == 0
2863 if (*__b == __psn[0])
2864 {
2865 ++__b;
2866 if (__psn.size() > 1)
2867 __trailing_sign = &__psn;
2868 }
2869 else
2870 __neg = true;
2871 }
2872 else if (*__b == __nsn[0]) // __nsn.size() > 0 && __psn.size() == 0
2873 {
2874 ++__b;
2875 __neg = true;
2876 if (__nsn.size() > 1)
2877 __trailing_sign = &__nsn;
2878 }
2879 }
2880 else // sign is required
2881 {
2882 if (*__b == __psn[0])
2883 {
2884 ++__b;
2885 if (__psn.size() > 1)
2886 __trailing_sign = &__psn;
2887 }
2888 else if (*__b == __nsn[0])
2889 {
2890 ++__b;
2891 __neg = true;
2892 if (__nsn.size() > 1)
2893 __trailing_sign = &__nsn;
2894 }
2895 else
2896 {
2897 __err |= ios_base::failbit;
2898 return false;
2899 }
2900 }
2901 }
2902 break;
2903 case money_base::symbol:
2904 {
2905 bool __more_needed = __trailing_sign ||
2906 (__p < 2) ||
2907 (__p == 2 && __pat.field[3] != static_cast<char>(money_base::none));
Marshall Clowdfcbb432013-10-13 01:02:45 +00002908 bool __sb = (__flags & ios_base::showbase) != 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909 if (__sb || __more_needed)
2910 {
Jeffrey Yasskin6b045fb2012-03-10 18:31:43 +00002911 typename string_type::const_iterator __sym_space_end = __sym.begin();
2912 if (__p > 0 && (__pat.field[__p - 1] == money_base::none ||
2913 __pat.field[__p - 1] == money_base::space)) {
2914 // Match spaces we've already read against spaces at
2915 // the beginning of __sym.
2916 while (__sym_space_end != __sym.end() &&
2917 __ct.is(ctype_base::space, *__sym_space_end))
2918 ++__sym_space_end;
2919 const size_t __num_spaces = __sym_space_end - __sym.begin();
2920 if (__num_spaces > __spaces.size() ||
2921 !equal(__spaces.end() - __num_spaces, __spaces.end(),
2922 __sym.begin())) {
2923 // No match. Put __sym_space_end back at the
2924 // beginning of __sym, which will prevent a
2925 // match in the next loop.
2926 __sym_space_end = __sym.begin();
2927 }
2928 }
2929 typename string_type::const_iterator __sym_curr_char = __sym_space_end;
2930 while (__sym_curr_char != __sym.end() && __b != __e &&
2931 *__b == *__sym_curr_char) {
2932 ++__b;
2933 ++__sym_curr_char;
2934 }
2935 if (__sb && __sym_curr_char != __sym.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002936 {
2937 __err |= ios_base::failbit;
2938 return false;
2939 }
2940 }
2941 }
2942 break;
2943 case money_base::value:
2944 {
2945 unsigned __ng = 0;
2946 for (; __b != __e; ++__b)
2947 {
2948 char_type __c = *__b;
2949 if (__ct.is(ctype_base::digit, __c))
2950 {
2951 if (__wn == __we)
2952 __double_or_nothing(__wb, __wn, __we);
2953 *__wn++ = __c;
2954 ++__ng;
2955 }
2956 else if (__grp.size() > 0 && __ng > 0 && __c == __ts)
2957 {
2958 if (__gn == __ge)
2959 __double_or_nothing(__gb, __gn, __ge);
2960 *__gn++ = __ng;
2961 __ng = 0;
2962 }
2963 else
2964 break;
2965 }
2966 if (__gb.get() != __gn && __ng > 0)
2967 {
2968 if (__gn == __ge)
2969 __double_or_nothing(__gb, __gn, __ge);
2970 *__gn++ = __ng;
2971 }
2972 if (__fd > 0)
2973 {
2974 if (__b == __e || *__b != __dp)
2975 {
2976 __err |= ios_base::failbit;
2977 return false;
2978 }
2979 for (++__b; __fd > 0; --__fd, ++__b)
2980 {
2981 if (__b == __e || !__ct.is(ctype_base::digit, *__b))
2982 {
2983 __err |= ios_base::failbit;
2984 return false;
2985 }
2986 if (__wn == __we)
2987 __double_or_nothing(__wb, __wn, __we);
2988 *__wn++ = *__b;
2989 }
2990 }
2991 if (__wn == __wb.get())
2992 {
2993 __err |= ios_base::failbit;
2994 return false;
2995 }
2996 }
2997 break;
2998 }
2999 }
3000 if (__trailing_sign)
3001 {
3002 for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, ++__b)
3003 {
3004 if (__b == __e || *__b != (*__trailing_sign)[__i])
3005 {
3006 __err |= ios_base::failbit;
3007 return false;
3008 }
3009 }
3010 }
3011 if (__gb.get() != __gn)
3012 {
3013 ios_base::iostate __et = ios_base::goodbit;
3014 __check_grouping(__grp, __gb.get(), __gn, __et);
3015 if (__et)
3016 {
3017 __err |= ios_base::failbit;
3018 return false;
3019 }
3020 }
3021 return true;
3022}
3023
3024template <class _CharT, class _InputIterator>
3025_InputIterator
3026money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3027 bool __intl, ios_base& __iob,
3028 ios_base::iostate& __err,
3029 long double& __v) const
3030{
Howard Hinnant28b24882011-12-01 20:21:04 +00003031 const int __bz = 100;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003032 char_type __wbuf[__bz];
3033 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3034 char_type* __wn;
3035 char_type* __we = __wbuf + __bz;
3036 locale __loc = __iob.getloc();
3037 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3038 bool __neg = false;
3039 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3040 __wb, __wn, __we))
3041 {
3042 const char __src[] = "0123456789";
3043 char_type __atoms[sizeof(__src)-1];
3044 __ct.widen(__src, __src + (sizeof(__src)-1), __atoms);
3045 char __nbuf[__bz];
3046 char* __nc = __nbuf;
3047 unique_ptr<char, void(*)(void*)> __h(0, free);
3048 if (__wn - __wb.get() > __bz-2)
3049 {
Howard Hinnant28b24882011-12-01 20:21:04 +00003050 __h.reset((char*)malloc(static_cast<size_t>(__wn - __wb.get() + 2)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003051 if (__h.get() == 0)
3052 __throw_bad_alloc();
3053 __nc = __h.get();
3054 }
3055 if (__neg)
3056 *__nc++ = '-';
3057 for (const char_type* __w = __wb.get(); __w < __wn; ++__w, ++__nc)
Marshall Clow1e68fd42013-03-22 02:14:40 +00003058 *__nc = __src[find(__atoms, _VSTD::end(__atoms), *__w) - __atoms];
Howard Hinnantc51e1022010-05-11 19:42:16 +00003059 *__nc = char();
3060 if (sscanf(__nbuf, "%Lf", &__v) != 1)
3061 __throw_runtime_error("money_get error");
3062 }
3063 if (__b == __e)
3064 __err |= ios_base::eofbit;
3065 return __b;
3066}
3067
3068template <class _CharT, class _InputIterator>
3069_InputIterator
3070money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3071 bool __intl, ios_base& __iob,
3072 ios_base::iostate& __err,
3073 string_type& __v) const
3074{
Howard Hinnant28b24882011-12-01 20:21:04 +00003075 const int __bz = 100;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003076 char_type __wbuf[__bz];
3077 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3078 char_type* __wn;
3079 char_type* __we = __wbuf + __bz;
3080 locale __loc = __iob.getloc();
3081 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3082 bool __neg = false;
3083 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3084 __wb, __wn, __we))
3085 {
3086 __v.clear();
3087 if (__neg)
3088 __v.push_back(__ct.widen('-'));
3089 char_type __z = __ct.widen('0');
3090 char_type* __w;
3091 for (__w = __wb.get(); __w < __wn-1; ++__w)
3092 if (*__w != __z)
3093 break;
3094 __v.append(__w, __wn);
3095 }
3096 if (__b == __e)
3097 __err |= ios_base::eofbit;
3098 return __b;
3099}
3100
Eric Fiselier1b57fa82016-09-15 22:27:07 +00003101_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<char>)
3102_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003103
3104// money_put
3105
3106template <class _CharT>
3107class __money_put
3108{
3109protected:
3110 typedef _CharT char_type;
3111 typedef basic_string<char_type> string_type;
3112
3113 _LIBCPP_ALWAYS_INLINE __money_put() {}
3114
3115 static void __gather_info(bool __intl, bool __neg, const locale& __loc,
3116 money_base::pattern& __pat, char_type& __dp,
3117 char_type& __ts, string& __grp,
3118 string_type& __sym, string_type& __sn,
3119 int& __fd);
3120 static void __format(char_type* __mb, char_type*& __mi, char_type*& __me,
3121 ios_base::fmtflags __flags,
3122 const char_type* __db, const char_type* __de,
3123 const ctype<char_type>& __ct, bool __neg,
3124 const money_base::pattern& __pat, char_type __dp,
3125 char_type __ts, const string& __grp,
3126 const string_type& __sym, const string_type& __sn,
3127 int __fd);
3128};
3129
3130template <class _CharT>
3131void
3132__money_put<_CharT>::__gather_info(bool __intl, bool __neg, const locale& __loc,
3133 money_base::pattern& __pat, char_type& __dp,
3134 char_type& __ts, string& __grp,
3135 string_type& __sym, string_type& __sn,
3136 int& __fd)
3137{
3138 if (__intl)
3139 {
3140 const moneypunct<char_type, true>& __mp =
3141 use_facet<moneypunct<char_type, true> >(__loc);
3142 if (__neg)
3143 {
3144 __pat = __mp.neg_format();
3145 __sn = __mp.negative_sign();
3146 }
3147 else
3148 {
3149 __pat = __mp.pos_format();
3150 __sn = __mp.positive_sign();
3151 }
3152 __dp = __mp.decimal_point();
3153 __ts = __mp.thousands_sep();
3154 __grp = __mp.grouping();
3155 __sym = __mp.curr_symbol();
3156 __fd = __mp.frac_digits();
3157 }
3158 else
3159 {
3160 const moneypunct<char_type, false>& __mp =
3161 use_facet<moneypunct<char_type, false> >(__loc);
3162 if (__neg)
3163 {
3164 __pat = __mp.neg_format();
3165 __sn = __mp.negative_sign();
3166 }
3167 else
3168 {
3169 __pat = __mp.pos_format();
3170 __sn = __mp.positive_sign();
3171 }
3172 __dp = __mp.decimal_point();
3173 __ts = __mp.thousands_sep();
3174 __grp = __mp.grouping();
3175 __sym = __mp.curr_symbol();
3176 __fd = __mp.frac_digits();
3177 }
3178}
3179
3180template <class _CharT>
3181void
3182__money_put<_CharT>::__format(char_type* __mb, char_type*& __mi, char_type*& __me,
3183 ios_base::fmtflags __flags,
3184 const char_type* __db, const char_type* __de,
3185 const ctype<char_type>& __ct, bool __neg,
3186 const money_base::pattern& __pat, char_type __dp,
3187 char_type __ts, const string& __grp,
3188 const string_type& __sym, const string_type& __sn,
3189 int __fd)
3190{
3191 __me = __mb;
3192 for (unsigned __p = 0; __p < 4; ++__p)
3193 {
3194 switch (__pat.field[__p])
3195 {
3196 case money_base::none:
3197 __mi = __me;
3198 break;
3199 case money_base::space:
3200 __mi = __me;
3201 *__me++ = __ct.widen(' ');
3202 break;
3203 case money_base::sign:
3204 if (!__sn.empty())
3205 *__me++ = __sn[0];
3206 break;
3207 case money_base::symbol:
3208 if (!__sym.empty() && (__flags & ios_base::showbase))
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003209 __me = _VSTD::copy(__sym.begin(), __sym.end(), __me);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003210 break;
3211 case money_base::value:
3212 {
3213 // remember start of value so we can reverse it
3214 char_type* __t = __me;
3215 // find beginning of digits
3216 if (__neg)
3217 ++__db;
3218 // find end of digits
3219 const char_type* __d;
3220 for (__d = __db; __d < __de; ++__d)
3221 if (!__ct.is(ctype_base::digit, *__d))
3222 break;
3223 // print fractional part
3224 if (__fd > 0)
3225 {
3226 int __f;
3227 for (__f = __fd; __d > __db && __f > 0; --__f)
3228 *__me++ = *--__d;
3229 char_type __z = __f > 0 ? __ct.widen('0') : char_type();
3230 for (; __f > 0; --__f)
3231 *__me++ = __z;
3232 *__me++ = __dp;
3233 }
3234 // print units part
3235 if (__d == __db)
3236 {
3237 *__me++ = __ct.widen('0');
3238 }
3239 else
3240 {
3241 unsigned __ng = 0;
3242 unsigned __ig = 0;
3243 unsigned __gl = __grp.empty() ? numeric_limits<unsigned>::max()
3244 : static_cast<unsigned>(__grp[__ig]);
3245 while (__d != __db)
3246 {
3247 if (__ng == __gl)
3248 {
3249 *__me++ = __ts;
3250 __ng = 0;
3251 if (++__ig < __grp.size())
3252 __gl = __grp[__ig] == numeric_limits<char>::max() ?
3253 numeric_limits<unsigned>::max() :
3254 static_cast<unsigned>(__grp[__ig]);
3255 }
3256 *__me++ = *--__d;
3257 ++__ng;
3258 }
3259 }
3260 // reverse it
3261 reverse(__t, __me);
3262 }
3263 break;
3264 }
3265 }
3266 // print rest of sign, if any
3267 if (__sn.size() > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003268 __me = _VSTD::copy(__sn.begin()+1, __sn.end(), __me);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003269 // set alignment
3270 if ((__flags & ios_base::adjustfield) == ios_base::left)
3271 __mi = __me;
3272 else if ((__flags & ios_base::adjustfield) != ios_base::internal)
3273 __mi = __mb;
3274}
3275
Eric Fiselier1b57fa82016-09-15 22:27:07 +00003276_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<char>)
3277_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003278
3279template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003280class _LIBCPP_TEMPLATE_VIS money_put
Howard Hinnantc51e1022010-05-11 19:42:16 +00003281 : public locale::facet,
3282 private __money_put<_CharT>
3283{
3284public:
3285 typedef _CharT char_type;
3286 typedef _OutputIterator iter_type;
3287 typedef basic_string<char_type> string_type;
3288
3289 _LIBCPP_ALWAYS_INLINE
3290 explicit money_put(size_t __refs = 0)
3291 : locale::facet(__refs) {}
3292
3293 _LIBCPP_ALWAYS_INLINE
3294 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3295 long double __units) const
3296 {
3297 return do_put(__s, __intl, __iob, __fl, __units);
3298 }
3299
3300 _LIBCPP_ALWAYS_INLINE
3301 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3302 const string_type& __digits) const
3303 {
3304 return do_put(__s, __intl, __iob, __fl, __digits);
3305 }
3306
3307 static locale::id id;
3308
3309protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003310 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00003311 ~money_put() {}
3312
3313 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3314 char_type __fl, long double __units) const;
3315 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3316 char_type __fl, const string_type& __digits) const;
3317};
3318
3319template <class _CharT, class _OutputIterator>
3320locale::id
3321money_put<_CharT, _OutputIterator>::id;
3322
3323template <class _CharT, class _OutputIterator>
3324_OutputIterator
3325money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3326 ios_base& __iob, char_type __fl,
3327 long double __units) const
3328{
3329 // convert to char
3330 const size_t __bs = 100;
3331 char __buf[__bs];
3332 char* __bb = __buf;
3333 char_type __digits[__bs];
3334 char_type* __db = __digits;
Howard Hinnant28b24882011-12-01 20:21:04 +00003335 size_t __n = static_cast<size_t>(snprintf(__bb, __bs, "%.0Lf", __units));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003336 unique_ptr<char, void(*)(void*)> __hn(0, free);
3337 unique_ptr<char_type, void(*)(void*)> __hd(0, free);
3338 // secure memory for digit storage
3339 if (__n > __bs-1)
3340 {
Ben Craig3756b922016-03-09 15:39:39 +00003341 __n = static_cast<size_t>(__libcpp_asprintf_l(&__bb, _LIBCPP_GET_C_LOCALE, "%.0Lf", __units));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003342 if (__bb == 0)
3343 __throw_bad_alloc();
3344 __hn.reset(__bb);
3345 __hd.reset((char_type*)malloc(__n * sizeof(char_type)));
Howard Hinnant03de6f92012-03-07 20:37:43 +00003346 if (__hd == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003347 __throw_bad_alloc();
3348 __db = __hd.get();
3349 }
3350 // gather info
3351 locale __loc = __iob.getloc();
3352 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3353 __ct.widen(__bb, __bb + __n, __db);
3354 bool __neg = __n > 0 && __bb[0] == '-';
3355 money_base::pattern __pat;
3356 char_type __dp;
3357 char_type __ts;
3358 string __grp;
3359 string_type __sym;
3360 string_type __sn;
3361 int __fd;
3362 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3363 // secure memory for formatting
3364 char_type __mbuf[__bs];
3365 char_type* __mb = __mbuf;
3366 unique_ptr<char_type, void(*)(void*)> __hw(0, free);
3367 size_t __exn = static_cast<int>(__n) > __fd ?
Howard Hinnant28b24882011-12-01 20:21:04 +00003368 (__n - static_cast<size_t>(__fd)) * 2 + __sn.size() +
3369 __sym.size() + static_cast<size_t>(__fd) + 1
3370 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003371 if (__exn > __bs)
3372 {
3373 __hw.reset((char_type*)malloc(__exn * sizeof(char_type)));
3374 __mb = __hw.get();
3375 if (__mb == 0)
3376 __throw_bad_alloc();
3377 }
3378 // format
3379 char_type* __mi;
3380 char_type* __me;
3381 this->__format(__mb, __mi, __me, __iob.flags(),
3382 __db, __db + __n, __ct,
3383 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3384 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3385}
3386
3387template <class _CharT, class _OutputIterator>
3388_OutputIterator
3389money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3390 ios_base& __iob, char_type __fl,
3391 const string_type& __digits) const
3392{
3393 // gather info
3394 locale __loc = __iob.getloc();
3395 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3396 bool __neg = __digits.size() > 0 && __digits[0] == __ct.widen('-');
3397 money_base::pattern __pat;
3398 char_type __dp;
3399 char_type __ts;
3400 string __grp;
3401 string_type __sym;
3402 string_type __sn;
3403 int __fd;
3404 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3405 // secure memory for formatting
3406 char_type __mbuf[100];
3407 char_type* __mb = __mbuf;
3408 unique_ptr<char_type, void(*)(void*)> __h(0, free);
Howard Hinnant28b24882011-12-01 20:21:04 +00003409 size_t __exn = static_cast<int>(__digits.size()) > __fd ?
3410 (__digits.size() - static_cast<size_t>(__fd)) * 2 +
3411 __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 1
3412 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003413 if (__exn > 100)
3414 {
3415 __h.reset((char_type*)malloc(__exn * sizeof(char_type)));
3416 __mb = __h.get();
3417 if (__mb == 0)
3418 __throw_bad_alloc();
3419 }
3420 // format
3421 char_type* __mi;
3422 char_type* __me;
3423 this->__format(__mb, __mi, __me, __iob.flags(),
3424 __digits.data(), __digits.data() + __digits.size(), __ct,
3425 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3426 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3427}
3428
Eric Fiselier1b57fa82016-09-15 22:27:07 +00003429_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<char>)
3430_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003431
3432// messages
3433
Howard Hinnant8331b762013-03-06 23:30:19 +00003434class _LIBCPP_TYPE_VIS messages_base
Howard Hinnantc51e1022010-05-11 19:42:16 +00003435{
3436public:
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003437 typedef ptrdiff_t catalog;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003438
3439 _LIBCPP_ALWAYS_INLINE messages_base() {}
3440};
3441
3442template <class _CharT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003443class _LIBCPP_TEMPLATE_VIS messages
Howard Hinnantc51e1022010-05-11 19:42:16 +00003444 : public locale::facet,
3445 public messages_base
3446{
3447public:
3448 typedef _CharT char_type;
3449 typedef basic_string<_CharT> string_type;
3450
3451 _LIBCPP_ALWAYS_INLINE
3452 explicit messages(size_t __refs = 0)
3453 : locale::facet(__refs) {}
3454
3455 _LIBCPP_ALWAYS_INLINE
3456 catalog open(const basic_string<char>& __nm, const locale& __loc) const
3457 {
3458 return do_open(__nm, __loc);
3459 }
3460
3461 _LIBCPP_ALWAYS_INLINE
3462 string_type get(catalog __c, int __set, int __msgid,
3463 const string_type& __dflt) const
3464 {
3465 return do_get(__c, __set, __msgid, __dflt);
3466 }
3467
3468 _LIBCPP_ALWAYS_INLINE
3469 void close(catalog __c) const
3470 {
3471 do_close(__c);
3472 }
3473
3474 static locale::id id;
3475
3476protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003477 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00003478 ~messages() {}
3479
3480 virtual catalog do_open(const basic_string<char>&, const locale&) const;
3481 virtual string_type do_get(catalog, int __set, int __msgid,
3482 const string_type& __dflt) const;
3483 virtual void do_close(catalog) const;
3484};
3485
3486template <class _CharT>
3487locale::id
3488messages<_CharT>::id;
3489
3490template <class _CharT>
3491typename messages<_CharT>::catalog
3492messages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const
3493{
Ed Schouten118b6032015-03-11 16:39:36 +00003494#ifdef _LIBCPP_HAS_CATOPEN
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +00003495 catalog __cat = (catalog)catopen(__nm.c_str(), NL_CAT_LOCALE);
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003496 if (__cat != -1)
3497 __cat = static_cast<catalog>((static_cast<size_t>(__cat) >> 1));
3498 return __cat;
Ed Schouten118b6032015-03-11 16:39:36 +00003499#else // !_LIBCPP_HAS_CATOPEN
3500 return -1;
3501#endif // _LIBCPP_HAS_CATOPEN
Howard Hinnantc51e1022010-05-11 19:42:16 +00003502}
3503
3504template <class _CharT>
3505typename messages<_CharT>::string_type
3506messages<_CharT>::do_get(catalog __c, int __set, int __msgid,
3507 const string_type& __dflt) const
3508{
Ed Schouten118b6032015-03-11 16:39:36 +00003509#ifdef _LIBCPP_HAS_CATOPEN
Howard Hinnantc51e1022010-05-11 19:42:16 +00003510 string __ndflt;
3511 __narrow_to_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__ndflt),
3512 __dflt.c_str(),
3513 __dflt.c_str() + __dflt.size());
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003514 if (__c != -1)
3515 __c <<= 1;
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +00003516 nl_catd __cat = (nl_catd)__c;
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003517 char* __n = catgets(__cat, __set, __msgid, __ndflt.c_str());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003518 string_type __w;
3519 __widen_from_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__w),
3520 __n, __n + strlen(__n));
3521 return __w;
Ed Schouten118b6032015-03-11 16:39:36 +00003522#else // !_LIBCPP_HAS_CATOPEN
3523 return __dflt;
3524#endif // _LIBCPP_HAS_CATOPEN
Howard Hinnantc51e1022010-05-11 19:42:16 +00003525}
3526
3527template <class _CharT>
3528void
3529messages<_CharT>::do_close(catalog __c) const
3530{
Ed Schouten118b6032015-03-11 16:39:36 +00003531#ifdef _LIBCPP_HAS_CATOPEN
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003532 if (__c != -1)
3533 __c <<= 1;
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +00003534 nl_catd __cat = (nl_catd)__c;
Howard Hinnant2862aeb2011-02-25 00:51:08 +00003535 catclose(__cat);
Ed Schouten118b6032015-03-11 16:39:36 +00003536#endif // _LIBCPP_HAS_CATOPEN
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537}
3538
Eric Fiselier1b57fa82016-09-15 22:27:07 +00003539_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<char>)
3540_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003541
3542template <class _CharT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003543class _LIBCPP_TEMPLATE_VIS messages_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00003544 : public messages<_CharT>
3545{
3546public:
3547 typedef messages_base::catalog catalog;
3548 typedef basic_string<_CharT> string_type;
3549
3550 _LIBCPP_ALWAYS_INLINE
3551 explicit messages_byname(const char*, size_t __refs = 0)
3552 : messages<_CharT>(__refs) {}
3553
3554 _LIBCPP_ALWAYS_INLINE
3555 explicit messages_byname(const string&, size_t __refs = 0)
3556 : messages<_CharT>(__refs) {}
3557
3558protected:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003559 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00003560 ~messages_byname() {}
3561};
3562
Eric Fiselier1b57fa82016-09-15 22:27:07 +00003563_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<char>)
3564_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003565
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003566template<class _Codecvt, class _Elem = wchar_t,
3567 class _Wide_alloc = allocator<_Elem>,
3568 class _Byte_alloc = allocator<char> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003569class _LIBCPP_TEMPLATE_VIS wstring_convert
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003570{
3571public:
3572 typedef basic_string<char, char_traits<char>, _Byte_alloc> byte_string;
3573 typedef basic_string<_Elem, char_traits<_Elem>, _Wide_alloc> wide_string;
3574 typedef typename _Codecvt::state_type state_type;
3575 typedef typename wide_string::traits_type::int_type int_type;
3576
3577private:
3578 byte_string __byte_err_string_;
3579 wide_string __wide_err_string_;
3580 _Codecvt* __cvtptr_;
3581 state_type __cvtstate_;
3582 size_t __cvtcount_;
3583
3584 wstring_convert(const wstring_convert& __wc);
3585 wstring_convert& operator=(const wstring_convert& __wc);
3586public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003587 _LIBCPP_ALWAYS_INLINE
Marshall Clowccaa0fb2013-08-27 20:18:59 +00003588 _LIBCPP_EXPLICIT_AFTER_CXX11 wstring_convert(_Codecvt* __pcvt = new _Codecvt);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003589 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003590 wstring_convert(_Codecvt* __pcvt, state_type __state);
Marshall Clowccaa0fb2013-08-27 20:18:59 +00003591 _LIBCPP_EXPLICIT_AFTER_CXX11 wstring_convert(const byte_string& __byte_err,
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003592 const wide_string& __wide_err = wide_string());
Eric Fiselierf07d88c2017-04-19 01:34:08 +00003593#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003594 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003595 wstring_convert(wstring_convert&& __wc);
3596#endif
3597 ~wstring_convert();
3598
Howard Hinnant756c69b2010-09-22 16:48:34 +00003599 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003600 wide_string from_bytes(char __byte)
3601 {return from_bytes(&__byte, &__byte+1);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003602 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003603 wide_string from_bytes(const char* __ptr)
3604 {return from_bytes(__ptr, __ptr + char_traits<char>::length(__ptr));}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003605 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003606 wide_string from_bytes(const byte_string& __str)
3607 {return from_bytes(__str.data(), __str.data() + __str.size());}
3608 wide_string from_bytes(const char* __first, const char* __last);
3609
Howard Hinnant756c69b2010-09-22 16:48:34 +00003610 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003611 byte_string to_bytes(_Elem __wchar)
3612 {return to_bytes(&__wchar, &__wchar+1);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003613 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003614 byte_string to_bytes(const _Elem* __wptr)
3615 {return to_bytes(__wptr, __wptr + char_traits<_Elem>::length(__wptr));}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003616 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003617 byte_string to_bytes(const wide_string& __wstr)
3618 {return to_bytes(__wstr.data(), __wstr.data() + __wstr.size());}
3619 byte_string to_bytes(const _Elem* __first, const _Elem* __last);
3620
Howard Hinnant756c69b2010-09-22 16:48:34 +00003621 _LIBCPP_ALWAYS_INLINE
Marshall Clowccaa0fb2013-08-27 20:18:59 +00003622 size_t converted() const _NOEXCEPT {return __cvtcount_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003623 _LIBCPP_ALWAYS_INLINE
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003624 state_type state() const {return __cvtstate_;}
3625};
3626
3627template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003628inline
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003629wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3630 wstring_convert(_Codecvt* __pcvt)
3631 : __cvtptr_(__pcvt), __cvtstate_(), __cvtcount_(0)
3632{
3633}
3634
3635template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003636inline
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003637wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3638 wstring_convert(_Codecvt* __pcvt, state_type __state)
3639 : __cvtptr_(__pcvt), __cvtstate_(__state), __cvtcount_(0)
3640{
3641}
3642
3643template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3644wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3645 wstring_convert(const byte_string& __byte_err, const wide_string& __wide_err)
3646 : __byte_err_string_(__byte_err), __wide_err_string_(__wide_err),
3647 __cvtstate_(), __cvtcount_(0)
3648{
3649 __cvtptr_ = new _Codecvt;
3650}
3651
Eric Fiselierf07d88c2017-04-19 01:34:08 +00003652#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003653
3654template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003655inline
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003656wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3657 wstring_convert(wstring_convert&& __wc)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003658 : __byte_err_string_(_VSTD::move(__wc.__byte_err_string_)),
3659 __wide_err_string_(_VSTD::move(__wc.__wide_err_string_)),
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003660 __cvtptr_(__wc.__cvtptr_),
Eric Fiselier35c67232016-06-26 22:56:26 +00003661 __cvtstate_(__wc.__cvtstate_), __cvtcount_(__wc.__cvtcount_)
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003662{
3663 __wc.__cvtptr_ = nullptr;
3664}
3665
Eric Fiselierf07d88c2017-04-19 01:34:08 +00003666#endif // _LIBCPP_CXX03_LANG
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003667
3668template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3669wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::~wstring_convert()
3670{
3671 delete __cvtptr_;
3672}
3673
3674template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3675typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::wide_string
3676wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3677 from_bytes(const char* __frm, const char* __frm_end)
3678{
3679 __cvtcount_ = 0;
3680 if (__cvtptr_ != nullptr)
3681 {
3682 wide_string __ws(2*(__frm_end - __frm), _Elem());
Howard Hinnant16d54f22012-07-12 18:07:41 +00003683 if (__frm != __frm_end)
3684 __ws.resize(__ws.capacity());
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003685 codecvt_base::result __r = codecvt_base::ok;
3686 state_type __st = __cvtstate_;
3687 if (__frm != __frm_end)
3688 {
3689 _Elem* __to = &__ws[0];
3690 _Elem* __to_end = __to + __ws.size();
3691 const char* __frm_nxt;
3692 do
3693 {
3694 _Elem* __to_nxt;
3695 __r = __cvtptr_->in(__st, __frm, __frm_end, __frm_nxt,
3696 __to, __to_end, __to_nxt);
3697 __cvtcount_ += __frm_nxt - __frm;
3698 if (__frm_nxt == __frm)
3699 {
3700 __r = codecvt_base::error;
3701 }
3702 else if (__r == codecvt_base::noconv)
3703 {
3704 __ws.resize(__to - &__ws[0]);
3705 // This only gets executed if _Elem is char
3706 __ws.append((const _Elem*)__frm, (const _Elem*)__frm_end);
3707 __frm = __frm_nxt;
3708 __r = codecvt_base::ok;
3709 }
3710 else if (__r == codecvt_base::ok)
3711 {
3712 __ws.resize(__to_nxt - &__ws[0]);
3713 __frm = __frm_nxt;
3714 }
3715 else if (__r == codecvt_base::partial)
3716 {
3717 ptrdiff_t __s = __to_nxt - &__ws[0];
3718 __ws.resize(2 * __s);
3719 __to = &__ws[0] + __s;
3720 __to_end = &__ws[0] + __ws.size();
3721 __frm = __frm_nxt;
3722 }
3723 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3724 }
3725 if (__r == codecvt_base::ok)
3726 return __ws;
3727 }
Marshall Clow8fea1612016-08-25 15:09:01 +00003728
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003729 if (__wide_err_string_.empty())
Marshall Clow8fea1612016-08-25 15:09:01 +00003730 __throw_range_error("wstring_convert: from_bytes error");
3731
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003732 return __wide_err_string_;
3733}
3734
3735template<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
3736typename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::byte_string
3737wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
3738 to_bytes(const _Elem* __frm, const _Elem* __frm_end)
3739{
3740 __cvtcount_ = 0;
3741 if (__cvtptr_ != nullptr)
3742 {
3743 byte_string __bs(2*(__frm_end - __frm), char());
Howard Hinnant16d54f22012-07-12 18:07:41 +00003744 if (__frm != __frm_end)
3745 __bs.resize(__bs.capacity());
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003746 codecvt_base::result __r = codecvt_base::ok;
3747 state_type __st = __cvtstate_;
3748 if (__frm != __frm_end)
3749 {
3750 char* __to = &__bs[0];
3751 char* __to_end = __to + __bs.size();
3752 const _Elem* __frm_nxt;
3753 do
3754 {
3755 char* __to_nxt;
3756 __r = __cvtptr_->out(__st, __frm, __frm_end, __frm_nxt,
3757 __to, __to_end, __to_nxt);
3758 __cvtcount_ += __frm_nxt - __frm;
3759 if (__frm_nxt == __frm)
3760 {
3761 __r = codecvt_base::error;
3762 }
3763 else if (__r == codecvt_base::noconv)
3764 {
3765 __bs.resize(__to - &__bs[0]);
3766 // This only gets executed if _Elem is char
3767 __bs.append((const char*)__frm, (const char*)__frm_end);
3768 __frm = __frm_nxt;
3769 __r = codecvt_base::ok;
3770 }
3771 else if (__r == codecvt_base::ok)
3772 {
3773 __bs.resize(__to_nxt - &__bs[0]);
3774 __frm = __frm_nxt;
3775 }
3776 else if (__r == codecvt_base::partial)
3777 {
3778 ptrdiff_t __s = __to_nxt - &__bs[0];
3779 __bs.resize(2 * __s);
3780 __to = &__bs[0] + __s;
3781 __to_end = &__bs[0] + __bs.size();
3782 __frm = __frm_nxt;
3783 }
3784 } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3785 }
3786 if (__r == codecvt_base::ok)
3787 {
3788 size_t __s = __bs.size();
3789 __bs.resize(__bs.capacity());
3790 char* __to = &__bs[0] + __s;
3791 char* __to_end = __to + __bs.size();
3792 do
3793 {
3794 char* __to_nxt;
3795 __r = __cvtptr_->unshift(__st, __to, __to_end, __to_nxt);
3796 if (__r == codecvt_base::noconv)
3797 {
3798 __bs.resize(__to - &__bs[0]);
3799 __r = codecvt_base::ok;
3800 }
3801 else if (__r == codecvt_base::ok)
3802 {
3803 __bs.resize(__to_nxt - &__bs[0]);
3804 }
3805 else if (__r == codecvt_base::partial)
3806 {
Howard Hinnant28b24882011-12-01 20:21:04 +00003807 ptrdiff_t __sp = __to_nxt - &__bs[0];
3808 __bs.resize(2 * __sp);
3809 __to = &__bs[0] + __sp;
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003810 __to_end = &__bs[0] + __bs.size();
3811 }
3812 } while (__r == codecvt_base::partial);
3813 if (__r == codecvt_base::ok)
3814 return __bs;
3815 }
3816 }
Marshall Clow8fea1612016-08-25 15:09:01 +00003817
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003818 if (__byte_err_string_.empty())
Marshall Clow8fea1612016-08-25 15:09:01 +00003819 __throw_range_error("wstring_convert: to_bytes error");
3820
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003821 return __byte_err_string_;
3822}
3823
3824template <class _Codecvt, class _Elem = wchar_t, class _Tr = char_traits<_Elem> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003825class _LIBCPP_TEMPLATE_VIS wbuffer_convert
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003826 : public basic_streambuf<_Elem, _Tr>
3827{
3828public:
3829 // types:
3830 typedef _Elem char_type;
3831 typedef _Tr traits_type;
3832 typedef typename traits_type::int_type int_type;
3833 typedef typename traits_type::pos_type pos_type;
3834 typedef typename traits_type::off_type off_type;
3835 typedef typename _Codecvt::state_type state_type;
3836
3837private:
Howard Hinnant0ac182a2010-06-01 20:09:18 +00003838 char* __extbuf_;
3839 const char* __extbufnext_;
3840 const char* __extbufend_;
3841 char __extbuf_min_[8];
3842 size_t __ebs_;
3843 char_type* __intbuf_;
3844 size_t __ibs_;
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003845 streambuf* __bufptr_;
Howard Hinnant0ac182a2010-06-01 20:09:18 +00003846 _Codecvt* __cv_;
3847 state_type __st_;
3848 ios_base::openmode __cm_;
3849 bool __owns_eb_;
3850 bool __owns_ib_;
3851 bool __always_noconv_;
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003852
Howard Hinnant0ac182a2010-06-01 20:09:18 +00003853 wbuffer_convert(const wbuffer_convert&);
3854 wbuffer_convert& operator=(const wbuffer_convert&);
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003855public:
Marshall Clowccaa0fb2013-08-27 20:18:59 +00003856 _LIBCPP_EXPLICIT_AFTER_CXX11 wbuffer_convert(streambuf* __bytebuf = 0,
3857 _Codecvt* __pcvt = new _Codecvt, state_type __state = state_type());
Howard Hinnant0ac182a2010-06-01 20:09:18 +00003858 ~wbuffer_convert();
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003859
Howard Hinnant756c69b2010-09-22 16:48:34 +00003860 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003861 streambuf* rdbuf() const {return __bufptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003862 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003863 streambuf* rdbuf(streambuf* __bytebuf)
3864 {
3865 streambuf* __r = __bufptr_;
3866 __bufptr_ = __bytebuf;
3867 return __r;
3868 }
3869
Howard Hinnant756c69b2010-09-22 16:48:34 +00003870 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0ac182a2010-06-01 20:09:18 +00003871 state_type state() const {return __st_;}
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003872
3873protected:
Howard Hinnant0ac182a2010-06-01 20:09:18 +00003874 virtual int_type underflow();
3875 virtual int_type pbackfail(int_type __c = traits_type::eof());
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003876 virtual int_type overflow (int_type __c = traits_type::eof());
Howard Hinnant0ac182a2010-06-01 20:09:18 +00003877 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s,
3878 streamsize __n);
3879 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
3880 ios_base::openmode __wch = ios_base::in | ios_base::out);
3881 virtual pos_type seekpos(pos_type __sp,
3882 ios_base::openmode __wch = ios_base::in | ios_base::out);
3883 virtual int sync();
3884
3885private:
3886 bool __read_mode();
3887 void __write_mode();
3888 wbuffer_convert* __close();
Howard Hinnant9dd7e892010-05-31 20:58:54 +00003889};
3890
3891template <class _Codecvt, class _Elem, class _Tr>
Howard Hinnant0ac182a2010-06-01 20:09:18 +00003892wbuffer_convert<_Codecvt, _Elem, _Tr>::
3893 wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt, state_type __state)
3894 : __extbuf_(0),
3895 __extbufnext_(0),
3896 __extbufend_(0),
3897 __ebs_(0),
3898 __intbuf_(0),
3899 __ibs_(0),
3900 __bufptr_(__bytebuf),
3901 __cv_(__pcvt),
3902 __st_(__state),
3903 __cm_(0),
3904 __owns_eb_(false),
3905 __owns_ib_(false),
3906 __always_noconv_(__cv_ ? __cv_->always_noconv() : false)
3907{
3908 setbuf(0, 4096);
3909}
3910
3911template <class _Codecvt, class _Elem, class _Tr>
3912wbuffer_convert<_Codecvt, _Elem, _Tr>::~wbuffer_convert()
3913{
3914 __close();
3915 delete __cv_;
3916 if (__owns_eb_)
3917 delete [] __extbuf_;
3918 if (__owns_ib_)
3919 delete [] __intbuf_;
3920}
3921
3922template <class _Codecvt, class _Elem, class _Tr>
3923typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
3924wbuffer_convert<_Codecvt, _Elem, _Tr>::underflow()
3925{
3926 if (__cv_ == 0 || __bufptr_ == 0)
3927 return traits_type::eof();
3928 bool __initial = __read_mode();
3929 char_type __1buf;
3930 if (this->gptr() == 0)
3931 this->setg(&__1buf, &__1buf+1, &__1buf+1);
3932 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
3933 int_type __c = traits_type::eof();
3934 if (this->gptr() == this->egptr())
3935 {
3936 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
3937 if (__always_noconv_)
3938 {
3939 streamsize __nmemb = static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz);
3940 __nmemb = __bufptr_->sgetn((char*)this->eback() + __unget_sz, __nmemb);
3941 if (__nmemb != 0)
3942 {
3943 this->setg(this->eback(),
3944 this->eback() + __unget_sz,
3945 this->eback() + __unget_sz + __nmemb);
3946 __c = *this->gptr();
3947 }
3948 }
3949 else
3950 {
Eric Fiselier98e428d2016-06-19 06:58:22 +00003951 _LIBCPP_ASSERT(!(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
3952 if (__extbufend_ != __extbufnext_)
3953 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
Howard Hinnant0ac182a2010-06-01 20:09:18 +00003954 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
3955 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003956 streamsize __nmemb = _VSTD::min(static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz),
Howard Hinnant0ac182a2010-06-01 20:09:18 +00003957 static_cast<streamsize>(__extbufend_ - __extbufnext_));
3958 codecvt_base::result __r;
Eric Fiselier6003c772016-12-23 23:37:52 +00003959 // FIXME: Do we ever need to restore the state here?
3960 //state_type __svs = __st_;
Howard Hinnant0ac182a2010-06-01 20:09:18 +00003961 streamsize __nr = __bufptr_->sgetn(const_cast<char*>(__extbufnext_), __nmemb);
3962 if (__nr != 0)
3963 {
3964 __extbufend_ = __extbufnext_ + __nr;
3965 char_type* __inext;
3966 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
3967 this->eback() + __unget_sz,
3968 this->egptr(), __inext);
3969 if (__r == codecvt_base::noconv)
3970 {
3971 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)__extbufend_);
3972 __c = *this->gptr();
3973 }
3974 else if (__inext != this->eback() + __unget_sz)
3975 {
3976 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
3977 __c = *this->gptr();
3978 }
3979 }
3980 }
3981 }
3982 else
3983 __c = *this->gptr();
3984 if (this->eback() == &__1buf)
3985 this->setg(0, 0, 0);
3986 return __c;
3987}
3988
3989template <class _Codecvt, class _Elem, class _Tr>
3990typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
3991wbuffer_convert<_Codecvt, _Elem, _Tr>::pbackfail(int_type __c)
3992{
3993 if (__cv_ != 0 && __bufptr_ != 0 && this->eback() < this->gptr())
3994 {
3995 if (traits_type::eq_int_type(__c, traits_type::eof()))
3996 {
3997 this->gbump(-1);
3998 return traits_type::not_eof(__c);
3999 }
4000 if (traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
4001 {
4002 this->gbump(-1);
4003 *this->gptr() = traits_type::to_char_type(__c);
4004 return __c;
4005 }
4006 }
4007 return traits_type::eof();
4008}
4009
4010template <class _Codecvt, class _Elem, class _Tr>
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004011typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4012wbuffer_convert<_Codecvt, _Elem, _Tr>::overflow(int_type __c)
4013{
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004014 if (__cv_ == 0 || __bufptr_ == 0)
4015 return traits_type::eof();
4016 __write_mode();
4017 char_type __1buf;
4018 char_type* __pb_save = this->pbase();
4019 char_type* __epb_save = this->epptr();
4020 if (!traits_type::eq_int_type(__c, traits_type::eof()))
4021 {
4022 if (this->pptr() == 0)
4023 this->setp(&__1buf, &__1buf+1);
4024 *this->pptr() = traits_type::to_char_type(__c);
4025 this->pbump(1);
4026 }
4027 if (this->pptr() != this->pbase())
4028 {
4029 if (__always_noconv_)
4030 {
4031 streamsize __nmemb = static_cast<streamsize>(this->pptr() - this->pbase());
4032 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4033 return traits_type::eof();
4034 }
4035 else
4036 {
4037 char* __extbe = __extbuf_;
4038 codecvt_base::result __r;
4039 do
4040 {
4041 const char_type* __e;
4042 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
4043 __extbuf_, __extbuf_ + __ebs_, __extbe);
4044 if (__e == this->pbase())
4045 return traits_type::eof();
4046 if (__r == codecvt_base::noconv)
4047 {
4048 streamsize __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
4049 if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4050 return traits_type::eof();
4051 }
4052 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
4053 {
4054 streamsize __nmemb = static_cast<size_t>(__extbe - __extbuf_);
4055 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4056 return traits_type::eof();
4057 if (__r == codecvt_base::partial)
4058 {
4059 this->setp((char_type*)__e, this->pptr());
4060 this->pbump(this->epptr() - this->pbase());
4061 }
4062 }
4063 else
4064 return traits_type::eof();
4065 } while (__r == codecvt_base::partial);
4066 }
4067 this->setp(__pb_save, __epb_save);
4068 }
4069 return traits_type::not_eof(__c);
4070}
4071
4072template <class _Codecvt, class _Elem, class _Tr>
4073basic_streambuf<_Elem, _Tr>*
4074wbuffer_convert<_Codecvt, _Elem, _Tr>::setbuf(char_type* __s, streamsize __n)
4075{
4076 this->setg(0, 0, 0);
4077 this->setp(0, 0);
4078 if (__owns_eb_)
4079 delete [] __extbuf_;
4080 if (__owns_ib_)
4081 delete [] __intbuf_;
4082 __ebs_ = __n;
4083 if (__ebs_ > sizeof(__extbuf_min_))
4084 {
4085 if (__always_noconv_ && __s)
4086 {
4087 __extbuf_ = (char*)__s;
4088 __owns_eb_ = false;
4089 }
4090 else
4091 {
4092 __extbuf_ = new char[__ebs_];
4093 __owns_eb_ = true;
4094 }
4095 }
4096 else
4097 {
4098 __extbuf_ = __extbuf_min_;
4099 __ebs_ = sizeof(__extbuf_min_);
4100 __owns_eb_ = false;
4101 }
4102 if (!__always_noconv_)
4103 {
4104 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
4105 if (__s && __ibs_ >= sizeof(__extbuf_min_))
4106 {
4107 __intbuf_ = __s;
4108 __owns_ib_ = false;
4109 }
4110 else
4111 {
4112 __intbuf_ = new char_type[__ibs_];
4113 __owns_ib_ = true;
4114 }
4115 }
4116 else
4117 {
4118 __ibs_ = 0;
4119 __intbuf_ = 0;
4120 __owns_ib_ = false;
4121 }
4122 return this;
4123}
4124
4125template <class _Codecvt, class _Elem, class _Tr>
4126typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4127wbuffer_convert<_Codecvt, _Elem, _Tr>::seekoff(off_type __off, ios_base::seekdir __way,
4128 ios_base::openmode __om)
4129{
4130 int __width = __cv_->encoding();
4131 if (__cv_ == 0 || __bufptr_ == 0 || (__width <= 0 && __off != 0) || sync())
4132 return pos_type(off_type(-1));
Marshall Clowc2a72762015-08-27 14:37:22 +00004133 // __width > 0 || __off == 0, now check __way
4134 if (__way != ios_base::beg && __way != ios_base::cur && __way != ios_base::end)
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004135 return pos_type(off_type(-1));
Howard Hinnant0ac182a2010-06-01 20:09:18 +00004136 pos_type __r = __bufptr_->pubseekoff(__width * __off, __way, __om);
4137 __r.state(__st_);
4138 return __r;
4139}
4140
4141template <class _Codecvt, class _Elem, class _Tr>
4142typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4143wbuffer_convert<_Codecvt, _Elem, _Tr>::seekpos(pos_type __sp, ios_base::openmode __wch)
4144{
4145 if (__cv_ == 0 || __bufptr_ == 0 || sync())
4146 return pos_type(off_type(-1));
4147 if (__bufptr_->pubseekpos(__sp, __wch) == pos_type(off_type(-1)))
4148 return pos_type(off_type(-1));
4149 return __sp;
4150}
4151
4152template <class _Codecvt, class _Elem, class _Tr>
4153int
4154wbuffer_convert<_Codecvt, _Elem, _Tr>::sync()
4155{
4156 if (__cv_ == 0 || __bufptr_ == 0)
4157 return 0;
4158 if (__cm_ & ios_base::out)
4159 {
4160 if (this->pptr() != this->pbase())
4161 if (overflow() == traits_type::eof())
4162 return -1;
4163 codecvt_base::result __r;
4164 do
4165 {
4166 char* __extbe;
4167 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
4168 streamsize __nmemb = static_cast<streamsize>(__extbe - __extbuf_);
4169 if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4170 return -1;
4171 } while (__r == codecvt_base::partial);
4172 if (__r == codecvt_base::error)
4173 return -1;
4174 if (__bufptr_->pubsync())
4175 return -1;
4176 }
4177 else if (__cm_ & ios_base::in)
4178 {
4179 off_type __c;
4180 if (__always_noconv_)
4181 __c = this->egptr() - this->gptr();
4182 else
4183 {
4184 int __width = __cv_->encoding();
4185 __c = __extbufend_ - __extbufnext_;
4186 if (__width > 0)
4187 __c += __width * (this->egptr() - this->gptr());
4188 else
4189 {
4190 if (this->gptr() != this->egptr())
4191 {
4192 reverse(this->gptr(), this->egptr());
4193 codecvt_base::result __r;
4194 const char_type* __e = this->gptr();
4195 char* __extbe;
4196 do
4197 {
4198 __r = __cv_->out(__st_, __e, this->egptr(), __e,
4199 __extbuf_, __extbuf_ + __ebs_, __extbe);
4200 switch (__r)
4201 {
4202 case codecvt_base::noconv:
4203 __c += this->egptr() - this->gptr();
4204 break;
4205 case codecvt_base::ok:
4206 case codecvt_base::partial:
4207 __c += __extbe - __extbuf_;
4208 break;
4209 default:
4210 return -1;
4211 }
4212 } while (__r == codecvt_base::partial);
4213 }
4214 }
4215 }
4216 if (__bufptr_->pubseekoff(-__c, ios_base::cur, __cm_) == pos_type(off_type(-1)))
4217 return -1;
4218 this->setg(0, 0, 0);
4219 __cm_ = 0;
4220 }
4221 return 0;
4222}
4223
4224template <class _Codecvt, class _Elem, class _Tr>
4225bool
4226wbuffer_convert<_Codecvt, _Elem, _Tr>::__read_mode()
4227{
4228 if (!(__cm_ & ios_base::in))
4229 {
4230 this->setp(0, 0);
4231 if (__always_noconv_)
4232 this->setg((char_type*)__extbuf_,
4233 (char_type*)__extbuf_ + __ebs_,
4234 (char_type*)__extbuf_ + __ebs_);
4235 else
4236 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
4237 __cm_ = ios_base::in;
4238 return true;
4239 }
4240 return false;
4241}
4242
4243template <class _Codecvt, class _Elem, class _Tr>
4244void
4245wbuffer_convert<_Codecvt, _Elem, _Tr>::__write_mode()
4246{
4247 if (!(__cm_ & ios_base::out))
4248 {
4249 this->setg(0, 0, 0);
4250 if (__ebs_ > sizeof(__extbuf_min_))
4251 {
4252 if (__always_noconv_)
4253 this->setp((char_type*)__extbuf_,
4254 (char_type*)__extbuf_ + (__ebs_ - 1));
4255 else
4256 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
4257 }
4258 else
4259 this->setp(0, 0);
4260 __cm_ = ios_base::out;
4261 }
4262}
4263
4264template <class _Codecvt, class _Elem, class _Tr>
4265wbuffer_convert<_Codecvt, _Elem, _Tr>*
4266wbuffer_convert<_Codecvt, _Elem, _Tr>::__close()
4267{
4268 wbuffer_convert* __rt = 0;
4269 if (__cv_ != 0 && __bufptr_ != 0)
4270 {
4271 __rt = this;
4272 if ((__cm_ & ios_base::out) && sync())
4273 __rt = 0;
4274 }
4275 return __rt;
Howard Hinnant9dd7e892010-05-31 20:58:54 +00004276}
4277
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278_LIBCPP_END_NAMESPACE_STD
4279
4280#endif // _LIBCPP_LOCALE