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