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