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