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