Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===------------------------- locale.cpp ---------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Howard Hinnant | 04a2c71 | 2013-08-29 20:56:53 +0000 | [diff] [blame] | 10 | #define _LIBCPP_EXTERN_TEMPLATE(...) extern template __VA_ARGS__; |
| 11 | |
David Chisnall | 997e454 | 2012-02-29 13:05:08 +0000 | [diff] [blame] | 12 | // On Solaris, we need to define something to make the C99 parts of localeconv |
| 13 | // visible. |
| 14 | #ifdef __sun__ |
| 15 | #define _LCONV_C99 |
| 16 | #endif |
| 17 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 18 | #include "string" |
| 19 | #include "locale" |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 20 | #include "codecvt" |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 21 | #include "vector" |
| 22 | #include "algorithm" |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 23 | #include "typeinfo" |
Howard Hinnant | b87922c | 2013-07-28 18:20:00 +0000 | [diff] [blame] | 24 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 25 | # include "type_traits" |
| 26 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 27 | #include "clocale" |
| 28 | #include "cstring" |
| 29 | #include "cwctype" |
| 30 | #include "__sso_allocator" |
Howard Hinnant | ef5aa93 | 2013-09-17 01:34:47 +0000 | [diff] [blame] | 31 | #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__) |
Howard Hinnant | 14fa9f9 | 2011-09-29 20:33:10 +0000 | [diff] [blame] | 32 | #include <support/win32/locale_win32.h> |
Dan Albert | 2ef012e | 2014-04-08 12:03:21 -0700 | [diff] [blame] | 33 | #elif defined(__ANDROID__) |
| 34 | #include <support/android/langinfo.h> |
Howard Hinnant | e9df0a5 | 2013-08-01 18:17:34 +0000 | [diff] [blame] | 35 | #else // _LIBCPP_MSVCRT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 36 | #include <langinfo.h> |
Howard Hinnant | e9df0a5 | 2013-08-01 18:17:34 +0000 | [diff] [blame] | 37 | #endif // !_LIBCPP_MSVCRT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 38 | #include <stdlib.h> |
Howard Hinnant | ed14a76 | 2013-07-23 16:05:56 +0000 | [diff] [blame] | 39 | #include <stdio.h> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 40 | |
Marshall Clow | 9ae96d0 | 2013-02-07 17:20:56 +0000 | [diff] [blame] | 41 | // On Linux, wint_t and wchar_t have different signed-ness, and this causes |
| 42 | // lots of noise in the build log, but no bugs that I know of. |
Marshall Clow | a364513 | 2013-10-21 15:07:28 +0000 | [diff] [blame] | 43 | #if defined(__clang__) |
Marshall Clow | 9ae96d0 | 2013-02-07 17:20:56 +0000 | [diff] [blame] | 44 | #pragma clang diagnostic ignored "-Wsign-conversion" |
Marshall Clow | a364513 | 2013-10-21 15:07:28 +0000 | [diff] [blame] | 45 | #endif |
Marshall Clow | 9ae96d0 | 2013-02-07 17:20:56 +0000 | [diff] [blame] | 46 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 47 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 48 | |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 49 | #ifdef __cloc_defined |
Sean Hunt | 62a6ac3 | 2011-07-09 00:56:23 +0000 | [diff] [blame] | 50 | locale_t __cloc() { |
| 51 | // In theory this could create a race condition. In practice |
| 52 | // the race condition is non-fatal since it will just create |
| 53 | // a little resource leak. Better approach would be appreciated. |
Sean Hunt | 62a6ac3 | 2011-07-09 00:56:23 +0000 | [diff] [blame] | 54 | static locale_t result = newlocale(LC_ALL_MASK, "C", 0); |
| 55 | return result; |
Sean Hunt | 62a6ac3 | 2011-07-09 00:56:23 +0000 | [diff] [blame] | 56 | } |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 57 | #endif // __cloc_defined |
Sean Hunt | 62a6ac3 | 2011-07-09 00:56:23 +0000 | [diff] [blame] | 58 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 59 | namespace { |
| 60 | |
| 61 | struct release |
| 62 | { |
| 63 | void operator()(locale::facet* p) {p->__release_shared();} |
| 64 | }; |
| 65 | |
| 66 | template <class T, class A0> |
| 67 | inline |
| 68 | T& |
| 69 | make(A0 a0) |
| 70 | { |
| 71 | static typename aligned_storage<sizeof(T)>::type buf; |
| 72 | ::new (&buf) T(a0); |
Joerg Sonnenberger | 4c6acb5 | 2014-01-04 17:43:00 +0000 | [diff] [blame] | 73 | return *reinterpret_cast<T*>(&buf); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | template <class T, class A0, class A1> |
| 77 | inline |
| 78 | T& |
| 79 | make(A0 a0, A1 a1) |
| 80 | { |
| 81 | static typename aligned_storage<sizeof(T)>::type buf; |
| 82 | ::new (&buf) T(a0, a1); |
Joerg Sonnenberger | 4c6acb5 | 2014-01-04 17:43:00 +0000 | [diff] [blame] | 83 | return *reinterpret_cast<T*>(&buf); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | template <class T, class A0, class A1, class A2> |
| 87 | inline |
| 88 | T& |
| 89 | make(A0 a0, A1 a1, A2 a2) |
| 90 | { |
| 91 | static typename aligned_storage<sizeof(T)>::type buf; |
| 92 | ::new (&buf) T(a0, a1, a2); |
Joerg Sonnenberger | 4c6acb5 | 2014-01-04 17:43:00 +0000 | [diff] [blame] | 93 | return *reinterpret_cast<T*>(&buf); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 96 | template <typename T, size_t N> |
Howard Hinnant | 21772ec | 2012-12-28 18:15:01 +0000 | [diff] [blame] | 97 | inline |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 98 | _LIBCPP_CONSTEXPR |
| 99 | size_t |
| 100 | countof(const T (&)[N]) |
| 101 | { |
| 102 | return N; |
| 103 | } |
| 104 | |
| 105 | template <typename T> |
Howard Hinnant | 21772ec | 2012-12-28 18:15:01 +0000 | [diff] [blame] | 106 | inline |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 107 | _LIBCPP_CONSTEXPR |
| 108 | size_t |
| 109 | countof(const T * const begin, const T * const end) |
| 110 | { |
| 111 | return static_cast<size_t>(end - begin); |
| 112 | } |
| 113 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Howard Hinnant | 5143722 | 2013-08-29 23:37:50 +0000 | [diff] [blame] | 116 | #if defined(_AIX) |
| 117 | // Set priority to INT_MIN + 256 + 150 |
| 118 | # pragma priority ( -2147483242 ) |
| 119 | #endif |
| 120 | |
Howard Hinnant | 0a69fa1 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 121 | const locale::category locale::none; |
| 122 | const locale::category locale::collate; |
| 123 | const locale::category locale::ctype; |
| 124 | const locale::category locale::monetary; |
| 125 | const locale::category locale::numeric; |
| 126 | const locale::category locale::time; |
| 127 | const locale::category locale::messages; |
| 128 | const locale::category locale::all; |
| 129 | |
Marshall Clow | a364513 | 2013-10-21 15:07:28 +0000 | [diff] [blame] | 130 | #if defined(__clang__) |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 131 | #pragma clang diagnostic push |
| 132 | #pragma clang diagnostic ignored "-Wpadded" |
Marshall Clow | a364513 | 2013-10-21 15:07:28 +0000 | [diff] [blame] | 133 | #endif |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 134 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 135 | class _LIBCPP_HIDDEN locale::__imp |
| 136 | : public facet |
| 137 | { |
| 138 | enum {N = 28}; |
Marshall Clow | a364513 | 2013-10-21 15:07:28 +0000 | [diff] [blame] | 139 | #if defined(_LIBCPP_MSVC) |
| 140 | // FIXME: MSVC doesn't support aligned parameters by value. |
| 141 | // I can't get the __sso_allocator to work here |
| 142 | // for MSVC I think for this reason. |
| 143 | vector<facet*> facets_; |
| 144 | #else |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 145 | vector<facet*, __sso_allocator<facet*, N> > facets_; |
Marshall Clow | a364513 | 2013-10-21 15:07:28 +0000 | [diff] [blame] | 146 | #endif |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 147 | string name_; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 148 | public: |
| 149 | explicit __imp(size_t refs = 0); |
| 150 | explicit __imp(const string& name, size_t refs = 0); |
| 151 | __imp(const __imp&); |
| 152 | __imp(const __imp&, const string&, locale::category c); |
| 153 | __imp(const __imp& other, const __imp& one, locale::category c); |
| 154 | __imp(const __imp&, facet* f, long id); |
| 155 | ~__imp(); |
| 156 | |
| 157 | const string& name() const {return name_;} |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 158 | bool has_facet(long id) const |
| 159 | {return static_cast<size_t>(id) < facets_.size() && facets_[static_cast<size_t>(id)];} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 160 | const locale::facet* use_facet(long id) const; |
| 161 | |
| 162 | static const locale& make_classic(); |
| 163 | static locale& make_global(); |
| 164 | private: |
| 165 | void install(facet* f, long id); |
| 166 | template <class F> void install(F* f) {install(f, f->id.__get());} |
| 167 | template <class F> void install_from(const __imp& other); |
| 168 | }; |
| 169 | |
Marshall Clow | a364513 | 2013-10-21 15:07:28 +0000 | [diff] [blame] | 170 | #if defined(__clang__) |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 171 | #pragma clang diagnostic pop |
Marshall Clow | a364513 | 2013-10-21 15:07:28 +0000 | [diff] [blame] | 172 | #endif |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 173 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 174 | locale::__imp::__imp(size_t refs) |
| 175 | : facet(refs), |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 176 | facets_(N), |
| 177 | name_("C") |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 178 | { |
| 179 | facets_.clear(); |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 180 | install(&make<_VSTD::collate<char> >(1u)); |
| 181 | install(&make<_VSTD::collate<wchar_t> >(1u)); |
Joerg Sonnenberger | 4c6acb5 | 2014-01-04 17:43:00 +0000 | [diff] [blame] | 182 | install(&make<_VSTD::ctype<char> >(nullptr, false, 1u)); |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 183 | install(&make<_VSTD::ctype<wchar_t> >(1u)); |
| 184 | install(&make<codecvt<char, char, mbstate_t> >(1u)); |
| 185 | install(&make<codecvt<wchar_t, char, mbstate_t> >(1u)); |
| 186 | install(&make<codecvt<char16_t, char, mbstate_t> >(1u)); |
| 187 | install(&make<codecvt<char32_t, char, mbstate_t> >(1u)); |
| 188 | install(&make<numpunct<char> >(1u)); |
| 189 | install(&make<numpunct<wchar_t> >(1u)); |
| 190 | install(&make<num_get<char> >(1u)); |
| 191 | install(&make<num_get<wchar_t> >(1u)); |
| 192 | install(&make<num_put<char> >(1u)); |
| 193 | install(&make<num_put<wchar_t> >(1u)); |
| 194 | install(&make<moneypunct<char, false> >(1u)); |
| 195 | install(&make<moneypunct<char, true> >(1u)); |
| 196 | install(&make<moneypunct<wchar_t, false> >(1u)); |
| 197 | install(&make<moneypunct<wchar_t, true> >(1u)); |
| 198 | install(&make<money_get<char> >(1u)); |
| 199 | install(&make<money_get<wchar_t> >(1u)); |
| 200 | install(&make<money_put<char> >(1u)); |
| 201 | install(&make<money_put<wchar_t> >(1u)); |
| 202 | install(&make<time_get<char> >(1u)); |
| 203 | install(&make<time_get<wchar_t> >(1u)); |
| 204 | install(&make<time_put<char> >(1u)); |
| 205 | install(&make<time_put<wchar_t> >(1u)); |
| 206 | install(&make<_VSTD::messages<char> >(1u)); |
| 207 | install(&make<_VSTD::messages<wchar_t> >(1u)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | locale::__imp::__imp(const string& name, size_t refs) |
| 211 | : facet(refs), |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 212 | facets_(N), |
| 213 | name_(name) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 214 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 215 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 216 | try |
| 217 | { |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 218 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 219 | facets_ = locale::classic().__locale_->facets_; |
| 220 | for (unsigned i = 0; i < facets_.size(); ++i) |
| 221 | if (facets_[i]) |
| 222 | facets_[i]->__add_shared(); |
| 223 | install(new collate_byname<char>(name_)); |
| 224 | install(new collate_byname<wchar_t>(name_)); |
| 225 | install(new ctype_byname<char>(name_)); |
| 226 | install(new ctype_byname<wchar_t>(name_)); |
| 227 | install(new codecvt_byname<char, char, mbstate_t>(name_)); |
| 228 | install(new codecvt_byname<wchar_t, char, mbstate_t>(name_)); |
| 229 | install(new codecvt_byname<char16_t, char, mbstate_t>(name_)); |
| 230 | install(new codecvt_byname<char32_t, char, mbstate_t>(name_)); |
| 231 | install(new numpunct_byname<char>(name_)); |
| 232 | install(new numpunct_byname<wchar_t>(name_)); |
| 233 | install(new moneypunct_byname<char, false>(name_)); |
| 234 | install(new moneypunct_byname<char, true>(name_)); |
| 235 | install(new moneypunct_byname<wchar_t, false>(name_)); |
| 236 | install(new moneypunct_byname<wchar_t, true>(name_)); |
| 237 | install(new time_get_byname<char>(name_)); |
| 238 | install(new time_get_byname<wchar_t>(name_)); |
| 239 | install(new time_put_byname<char>(name_)); |
| 240 | install(new time_put_byname<wchar_t>(name_)); |
| 241 | install(new messages_byname<char>(name_)); |
| 242 | install(new messages_byname<wchar_t>(name_)); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 243 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 244 | } |
| 245 | catch (...) |
| 246 | { |
| 247 | for (unsigned i = 0; i < facets_.size(); ++i) |
| 248 | if (facets_[i]) |
| 249 | facets_[i]->__release_shared(); |
| 250 | throw; |
| 251 | } |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 252 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Howard Hinnant | 21772ec | 2012-12-28 18:15:01 +0000 | [diff] [blame] | 255 | // NOTE avoid the `base class should be explicitly initialized in the |
Howard Hinnant | 5f767b7 | 2012-12-27 23:24:31 +0000 | [diff] [blame] | 256 | // copy constructor` warning emitted by GCC |
Joerg Sonnenberger | e58bc12 | 2013-05-02 19:34:26 +0000 | [diff] [blame] | 257 | #if defined(__clang__) || _GNUC_VER >= 406 |
Howard Hinnant | 5f767b7 | 2012-12-27 23:24:31 +0000 | [diff] [blame] | 258 | #pragma GCC diagnostic push |
Howard Hinnant | 21772ec | 2012-12-28 18:15:01 +0000 | [diff] [blame] | 259 | #pragma GCC diagnostic ignored "-Wextra" |
Joerg Sonnenberger | e58bc12 | 2013-05-02 19:34:26 +0000 | [diff] [blame] | 260 | #endif |
Howard Hinnant | 5f767b7 | 2012-12-27 23:24:31 +0000 | [diff] [blame] | 261 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 262 | locale::__imp::__imp(const __imp& other) |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 263 | : facets_(max<size_t>(N, other.facets_.size())), |
| 264 | name_(other.name_) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 265 | { |
| 266 | facets_ = other.facets_; |
| 267 | for (unsigned i = 0; i < facets_.size(); ++i) |
| 268 | if (facets_[i]) |
| 269 | facets_[i]->__add_shared(); |
| 270 | } |
| 271 | |
Joerg Sonnenberger | e58bc12 | 2013-05-02 19:34:26 +0000 | [diff] [blame] | 272 | #if defined(__clang__) || _GNUC_VER >= 406 |
Howard Hinnant | 5f767b7 | 2012-12-27 23:24:31 +0000 | [diff] [blame] | 273 | #pragma GCC diagnostic pop |
Joerg Sonnenberger | e58bc12 | 2013-05-02 19:34:26 +0000 | [diff] [blame] | 274 | #endif |
Howard Hinnant | 5f767b7 | 2012-12-27 23:24:31 +0000 | [diff] [blame] | 275 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 276 | locale::__imp::__imp(const __imp& other, const string& name, locale::category c) |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 277 | : facets_(N), |
| 278 | name_("*") |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 279 | { |
| 280 | facets_ = other.facets_; |
| 281 | for (unsigned i = 0; i < facets_.size(); ++i) |
| 282 | if (facets_[i]) |
| 283 | facets_[i]->__add_shared(); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 284 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 285 | try |
| 286 | { |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 287 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 288 | if (c & locale::collate) |
| 289 | { |
| 290 | install(new collate_byname<char>(name)); |
| 291 | install(new collate_byname<wchar_t>(name)); |
| 292 | } |
| 293 | if (c & locale::ctype) |
| 294 | { |
| 295 | install(new ctype_byname<char>(name)); |
| 296 | install(new ctype_byname<wchar_t>(name)); |
| 297 | install(new codecvt_byname<char, char, mbstate_t>(name)); |
| 298 | install(new codecvt_byname<wchar_t, char, mbstate_t>(name)); |
| 299 | install(new codecvt_byname<char16_t, char, mbstate_t>(name)); |
| 300 | install(new codecvt_byname<char32_t, char, mbstate_t>(name)); |
| 301 | } |
| 302 | if (c & locale::monetary) |
| 303 | { |
| 304 | install(new moneypunct_byname<char, false>(name)); |
| 305 | install(new moneypunct_byname<char, true>(name)); |
| 306 | install(new moneypunct_byname<wchar_t, false>(name)); |
| 307 | install(new moneypunct_byname<wchar_t, true>(name)); |
| 308 | } |
| 309 | if (c & locale::numeric) |
| 310 | { |
| 311 | install(new numpunct_byname<char>(name)); |
| 312 | install(new numpunct_byname<wchar_t>(name)); |
| 313 | } |
| 314 | if (c & locale::time) |
| 315 | { |
| 316 | install(new time_get_byname<char>(name)); |
| 317 | install(new time_get_byname<wchar_t>(name)); |
| 318 | install(new time_put_byname<char>(name)); |
| 319 | install(new time_put_byname<wchar_t>(name)); |
| 320 | } |
| 321 | if (c & locale::messages) |
| 322 | { |
| 323 | install(new messages_byname<char>(name)); |
| 324 | install(new messages_byname<wchar_t>(name)); |
| 325 | } |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 326 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 327 | } |
| 328 | catch (...) |
| 329 | { |
| 330 | for (unsigned i = 0; i < facets_.size(); ++i) |
| 331 | if (facets_[i]) |
| 332 | facets_[i]->__release_shared(); |
| 333 | throw; |
| 334 | } |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 335 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | template<class F> |
| 339 | inline |
| 340 | void |
| 341 | locale::__imp::install_from(const locale::__imp& one) |
| 342 | { |
| 343 | long id = F::id.__get(); |
| 344 | install(const_cast<F*>(static_cast<const F*>(one.use_facet(id))), id); |
| 345 | } |
| 346 | |
| 347 | locale::__imp::__imp(const __imp& other, const __imp& one, locale::category c) |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 348 | : facets_(N), |
| 349 | name_("*") |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 350 | { |
| 351 | facets_ = other.facets_; |
| 352 | for (unsigned i = 0; i < facets_.size(); ++i) |
| 353 | if (facets_[i]) |
| 354 | facets_[i]->__add_shared(); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 355 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 356 | try |
| 357 | { |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 358 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 359 | if (c & locale::collate) |
| 360 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 361 | install_from<_VSTD::collate<char> >(one); |
| 362 | install_from<_VSTD::collate<wchar_t> >(one); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 363 | } |
| 364 | if (c & locale::ctype) |
| 365 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 366 | install_from<_VSTD::ctype<char> >(one); |
| 367 | install_from<_VSTD::ctype<wchar_t> >(one); |
| 368 | install_from<_VSTD::codecvt<char, char, mbstate_t> >(one); |
| 369 | install_from<_VSTD::codecvt<char16_t, char, mbstate_t> >(one); |
| 370 | install_from<_VSTD::codecvt<char32_t, char, mbstate_t> >(one); |
| 371 | install_from<_VSTD::codecvt<wchar_t, char, mbstate_t> >(one); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 372 | } |
| 373 | if (c & locale::monetary) |
| 374 | { |
| 375 | install_from<moneypunct<char, false> >(one); |
| 376 | install_from<moneypunct<char, true> >(one); |
| 377 | install_from<moneypunct<wchar_t, false> >(one); |
| 378 | install_from<moneypunct<wchar_t, true> >(one); |
| 379 | install_from<money_get<char> >(one); |
| 380 | install_from<money_get<wchar_t> >(one); |
| 381 | install_from<money_put<char> >(one); |
| 382 | install_from<money_put<wchar_t> >(one); |
| 383 | } |
| 384 | if (c & locale::numeric) |
| 385 | { |
| 386 | install_from<numpunct<char> >(one); |
| 387 | install_from<numpunct<wchar_t> >(one); |
| 388 | install_from<num_get<char> >(one); |
| 389 | install_from<num_get<wchar_t> >(one); |
| 390 | install_from<num_put<char> >(one); |
| 391 | install_from<num_put<wchar_t> >(one); |
| 392 | } |
| 393 | if (c & locale::time) |
| 394 | { |
| 395 | install_from<time_get<char> >(one); |
| 396 | install_from<time_get<wchar_t> >(one); |
| 397 | install_from<time_put<char> >(one); |
| 398 | install_from<time_put<wchar_t> >(one); |
| 399 | } |
| 400 | if (c & locale::messages) |
| 401 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 402 | install_from<_VSTD::messages<char> >(one); |
| 403 | install_from<_VSTD::messages<wchar_t> >(one); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 404 | } |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 405 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 406 | } |
| 407 | catch (...) |
| 408 | { |
| 409 | for (unsigned i = 0; i < facets_.size(); ++i) |
| 410 | if (facets_[i]) |
| 411 | facets_[i]->__release_shared(); |
| 412 | throw; |
| 413 | } |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 414 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | locale::__imp::__imp(const __imp& other, facet* f, long id) |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 418 | : facets_(max<size_t>(N, other.facets_.size()+1)), |
| 419 | name_("*") |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 420 | { |
| 421 | f->__add_shared(); |
| 422 | unique_ptr<facet, release> hold(f); |
| 423 | facets_ = other.facets_; |
| 424 | for (unsigned i = 0; i < other.facets_.size(); ++i) |
| 425 | if (facets_[i]) |
| 426 | facets_[i]->__add_shared(); |
| 427 | install(hold.get(), id); |
| 428 | } |
| 429 | |
| 430 | locale::__imp::~__imp() |
| 431 | { |
| 432 | for (unsigned i = 0; i < facets_.size(); ++i) |
| 433 | if (facets_[i]) |
| 434 | facets_[i]->__release_shared(); |
| 435 | } |
| 436 | |
| 437 | void |
| 438 | locale::__imp::install(facet* f, long id) |
| 439 | { |
| 440 | f->__add_shared(); |
| 441 | unique_ptr<facet, release> hold(f); |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 442 | if (static_cast<size_t>(id) >= facets_.size()) |
| 443 | facets_.resize(static_cast<size_t>(id+1)); |
| 444 | if (facets_[static_cast<size_t>(id)]) |
| 445 | facets_[static_cast<size_t>(id)]->__release_shared(); |
| 446 | facets_[static_cast<size_t>(id)] = hold.release(); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | const locale::facet* |
| 450 | locale::__imp::use_facet(long id) const |
| 451 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 452 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 453 | if (!has_facet(id)) |
| 454 | throw bad_cast(); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 455 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 456 | return facets_[static_cast<size_t>(id)]; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | // locale |
| 460 | |
| 461 | const locale& |
| 462 | locale::__imp::make_classic() |
| 463 | { |
| 464 | // only one thread can get in here and it only gets in once |
| 465 | static aligned_storage<sizeof(locale)>::type buf; |
Joerg Sonnenberger | 4c6acb5 | 2014-01-04 17:43:00 +0000 | [diff] [blame] | 466 | locale* c = reinterpret_cast<locale*>(&buf); |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 467 | c->__locale_ = &make<__imp>(1u); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 468 | return *c; |
| 469 | } |
| 470 | |
| 471 | const locale& |
| 472 | locale::classic() |
| 473 | { |
| 474 | static const locale& c = __imp::make_classic(); |
| 475 | return c; |
| 476 | } |
| 477 | |
| 478 | locale& |
| 479 | locale::__imp::make_global() |
| 480 | { |
| 481 | // only one thread can get in here and it only gets in once |
| 482 | static aligned_storage<sizeof(locale)>::type buf; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 483 | ::new (&buf) locale(locale::classic()); |
Joerg Sonnenberger | 4c6acb5 | 2014-01-04 17:43:00 +0000 | [diff] [blame] | 484 | return *reinterpret_cast<locale*>(&buf); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | locale& |
| 488 | locale::__global() |
| 489 | { |
| 490 | static locale& g = __imp::make_global(); |
| 491 | return g; |
| 492 | } |
| 493 | |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 494 | locale::locale() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 495 | : __locale_(__global().__locale_) |
| 496 | { |
| 497 | __locale_->__add_shared(); |
| 498 | } |
| 499 | |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 500 | locale::locale(const locale& l) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 501 | : __locale_(l.__locale_) |
| 502 | { |
| 503 | __locale_->__add_shared(); |
| 504 | } |
| 505 | |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 506 | locale::~locale() |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 507 | { |
| 508 | __locale_->__release_shared(); |
| 509 | } |
| 510 | |
| 511 | const locale& |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 512 | locale::operator=(const locale& other) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 513 | { |
| 514 | other.__locale_->__add_shared(); |
| 515 | __locale_->__release_shared(); |
| 516 | __locale_ = other.__locale_; |
| 517 | return *this; |
| 518 | } |
| 519 | |
| 520 | locale::locale(const char* name) |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 521 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 522 | : __locale_(name ? new __imp(name) |
| 523 | : throw runtime_error("locale constructed with null")) |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 524 | #else // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 525 | : __locale_(new __imp(name)) |
| 526 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 527 | { |
| 528 | __locale_->__add_shared(); |
| 529 | } |
| 530 | |
| 531 | locale::locale(const string& name) |
| 532 | : __locale_(new __imp(name)) |
| 533 | { |
| 534 | __locale_->__add_shared(); |
| 535 | } |
| 536 | |
| 537 | locale::locale(const locale& other, const char* name, category c) |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 538 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 539 | : __locale_(name ? new __imp(*other.__locale_, name, c) |
| 540 | : throw runtime_error("locale constructed with null")) |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 541 | #else // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 542 | : __locale_(new __imp(*other.__locale_, name, c)) |
| 543 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 544 | { |
| 545 | __locale_->__add_shared(); |
| 546 | } |
| 547 | |
| 548 | locale::locale(const locale& other, const string& name, category c) |
| 549 | : __locale_(new __imp(*other.__locale_, name, c)) |
| 550 | { |
| 551 | __locale_->__add_shared(); |
| 552 | } |
| 553 | |
| 554 | locale::locale(const locale& other, const locale& one, category c) |
| 555 | : __locale_(new __imp(*other.__locale_, *one.__locale_, c)) |
| 556 | { |
| 557 | __locale_->__add_shared(); |
| 558 | } |
| 559 | |
| 560 | string |
| 561 | locale::name() const |
| 562 | { |
| 563 | return __locale_->name(); |
| 564 | } |
| 565 | |
| 566 | void |
| 567 | locale::__install_ctor(const locale& other, facet* f, long id) |
| 568 | { |
| 569 | if (f) |
| 570 | __locale_ = new __imp(*other.__locale_, f, id); |
| 571 | else |
| 572 | __locale_ = other.__locale_; |
| 573 | __locale_->__add_shared(); |
| 574 | } |
| 575 | |
| 576 | locale |
| 577 | locale::global(const locale& loc) |
| 578 | { |
| 579 | locale& g = __global(); |
| 580 | locale r = g; |
| 581 | g = loc; |
| 582 | if (g.name() != "*") |
| 583 | setlocale(LC_ALL, g.name().c_str()); |
| 584 | return r; |
| 585 | } |
| 586 | |
| 587 | bool |
| 588 | locale::has_facet(id& x) const |
| 589 | { |
| 590 | return __locale_->has_facet(x.__get()); |
| 591 | } |
| 592 | |
| 593 | const locale::facet* |
| 594 | locale::use_facet(id& x) const |
| 595 | { |
| 596 | return __locale_->use_facet(x.__get()); |
| 597 | } |
| 598 | |
| 599 | bool |
| 600 | locale::operator==(const locale& y) const |
| 601 | { |
| 602 | return (__locale_ == y.__locale_) |
| 603 | || (__locale_->name() != "*" && __locale_->name() == y.__locale_->name()); |
| 604 | } |
| 605 | |
| 606 | // locale::facet |
| 607 | |
| 608 | locale::facet::~facet() |
| 609 | { |
| 610 | } |
| 611 | |
| 612 | void |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 613 | locale::facet::__on_zero_shared() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 614 | { |
| 615 | delete this; |
| 616 | } |
| 617 | |
| 618 | // locale::id |
| 619 | |
| 620 | int32_t locale::id::__next_id = 0; |
| 621 | |
| 622 | namespace |
| 623 | { |
| 624 | |
| 625 | class __fake_bind |
| 626 | { |
| 627 | locale::id* id_; |
| 628 | void (locale::id::* pmf_)(); |
| 629 | public: |
| 630 | __fake_bind(void (locale::id::* pmf)(), locale::id* id) |
| 631 | : id_(id), pmf_(pmf) {} |
| 632 | |
| 633 | void operator()() const |
| 634 | { |
| 635 | (id_->*pmf_)(); |
| 636 | } |
| 637 | }; |
| 638 | |
| 639 | } |
| 640 | |
| 641 | long |
| 642 | locale::id::__get() |
| 643 | { |
| 644 | call_once(__flag_, __fake_bind(&locale::id::__init, this)); |
| 645 | return __id_ - 1; |
| 646 | } |
| 647 | |
| 648 | void |
| 649 | locale::id::__init() |
| 650 | { |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 651 | __id_ = __sync_add_and_fetch(&__next_id, 1); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | // template <> class collate_byname<char> |
| 655 | |
| 656 | collate_byname<char>::collate_byname(const char* n, size_t refs) |
| 657 | : collate<char>(refs), |
| 658 | __l(newlocale(LC_ALL_MASK, n, 0)) |
| 659 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 660 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 661 | if (__l == 0) |
| 662 | throw runtime_error("collate_byname<char>::collate_byname" |
| 663 | " failed to construct for " + string(n)); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 664 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | collate_byname<char>::collate_byname(const string& name, size_t refs) |
| 668 | : collate<char>(refs), |
| 669 | __l(newlocale(LC_ALL_MASK, name.c_str(), 0)) |
| 670 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 671 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 672 | if (__l == 0) |
| 673 | throw runtime_error("collate_byname<char>::collate_byname" |
| 674 | " failed to construct for " + name); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 675 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | collate_byname<char>::~collate_byname() |
| 679 | { |
| 680 | freelocale(__l); |
| 681 | } |
| 682 | |
| 683 | int |
| 684 | collate_byname<char>::do_compare(const char_type* __lo1, const char_type* __hi1, |
| 685 | const char_type* __lo2, const char_type* __hi2) const |
| 686 | { |
| 687 | string_type lhs(__lo1, __hi1); |
| 688 | string_type rhs(__lo2, __hi2); |
| 689 | int r = strcoll_l(lhs.c_str(), rhs.c_str(), __l); |
| 690 | if (r < 0) |
| 691 | return -1; |
| 692 | if (r > 0) |
| 693 | return 1; |
| 694 | return r; |
| 695 | } |
| 696 | |
| 697 | collate_byname<char>::string_type |
| 698 | collate_byname<char>::do_transform(const char_type* lo, const char_type* hi) const |
| 699 | { |
| 700 | const string_type in(lo, hi); |
| 701 | string_type out(strxfrm_l(0, in.c_str(), 0, __l), char()); |
| 702 | strxfrm_l(const_cast<char*>(out.c_str()), in.c_str(), out.size()+1, __l); |
| 703 | return out; |
| 704 | } |
| 705 | |
| 706 | // template <> class collate_byname<wchar_t> |
| 707 | |
| 708 | collate_byname<wchar_t>::collate_byname(const char* n, size_t refs) |
| 709 | : collate<wchar_t>(refs), |
| 710 | __l(newlocale(LC_ALL_MASK, n, 0)) |
| 711 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 712 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 713 | if (__l == 0) |
| 714 | throw runtime_error("collate_byname<wchar_t>::collate_byname(size_t refs)" |
| 715 | " failed to construct for " + string(n)); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 716 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | collate_byname<wchar_t>::collate_byname(const string& name, size_t refs) |
| 720 | : collate<wchar_t>(refs), |
| 721 | __l(newlocale(LC_ALL_MASK, name.c_str(), 0)) |
| 722 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 723 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 724 | if (__l == 0) |
| 725 | throw runtime_error("collate_byname<wchar_t>::collate_byname(size_t refs)" |
| 726 | " failed to construct for " + name); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 727 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | collate_byname<wchar_t>::~collate_byname() |
| 731 | { |
| 732 | freelocale(__l); |
| 733 | } |
| 734 | |
| 735 | int |
| 736 | collate_byname<wchar_t>::do_compare(const char_type* __lo1, const char_type* __hi1, |
| 737 | const char_type* __lo2, const char_type* __hi2) const |
| 738 | { |
| 739 | string_type lhs(__lo1, __hi1); |
| 740 | string_type rhs(__lo2, __hi2); |
| 741 | int r = wcscoll_l(lhs.c_str(), rhs.c_str(), __l); |
| 742 | if (r < 0) |
| 743 | return -1; |
| 744 | if (r > 0) |
| 745 | return 1; |
| 746 | return r; |
| 747 | } |
| 748 | |
| 749 | collate_byname<wchar_t>::string_type |
| 750 | collate_byname<wchar_t>::do_transform(const char_type* lo, const char_type* hi) const |
| 751 | { |
| 752 | const string_type in(lo, hi); |
| 753 | string_type out(wcsxfrm_l(0, in.c_str(), 0, __l), wchar_t()); |
| 754 | wcsxfrm_l(const_cast<wchar_t*>(out.c_str()), in.c_str(), out.size()+1, __l); |
| 755 | return out; |
| 756 | } |
| 757 | |
| 758 | // template <> class ctype<wchar_t>; |
| 759 | |
Howard Hinnant | 0a69fa1 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 760 | const ctype_base::mask ctype_base::space; |
| 761 | const ctype_base::mask ctype_base::print; |
| 762 | const ctype_base::mask ctype_base::cntrl; |
| 763 | const ctype_base::mask ctype_base::upper; |
| 764 | const ctype_base::mask ctype_base::lower; |
| 765 | const ctype_base::mask ctype_base::alpha; |
| 766 | const ctype_base::mask ctype_base::digit; |
| 767 | const ctype_base::mask ctype_base::punct; |
| 768 | const ctype_base::mask ctype_base::xdigit; |
| 769 | const ctype_base::mask ctype_base::blank; |
| 770 | const ctype_base::mask ctype_base::alnum; |
| 771 | const ctype_base::mask ctype_base::graph; |
| 772 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 773 | locale::id ctype<wchar_t>::id; |
| 774 | |
| 775 | ctype<wchar_t>::~ctype() |
| 776 | { |
| 777 | } |
| 778 | |
| 779 | bool |
| 780 | ctype<wchar_t>::do_is(mask m, char_type c) const |
| 781 | { |
Marshall Clow | a364513 | 2013-10-21 15:07:28 +0000 | [diff] [blame] | 782 | return isascii(c) ? (ctype<char>::classic_table()[c] & m) != 0 : false; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | const wchar_t* |
| 786 | ctype<wchar_t>::do_is(const char_type* low, const char_type* high, mask* vec) const |
| 787 | { |
| 788 | for (; low != high; ++low, ++vec) |
Sean Hunt | 62a6ac3 | 2011-07-09 00:56:23 +0000 | [diff] [blame] | 789 | *vec = static_cast<mask>(isascii(*low) ? |
| 790 | ctype<char>::classic_table()[*low] : 0); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 791 | return low; |
| 792 | } |
| 793 | |
| 794 | const wchar_t* |
| 795 | ctype<wchar_t>::do_scan_is(mask m, const char_type* low, const char_type* high) const |
| 796 | { |
| 797 | for (; low != high; ++low) |
Sean Hunt | 62a6ac3 | 2011-07-09 00:56:23 +0000 | [diff] [blame] | 798 | if (isascii(*low) && (ctype<char>::classic_table()[*low] & m)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 799 | break; |
| 800 | return low; |
| 801 | } |
| 802 | |
| 803 | const wchar_t* |
| 804 | ctype<wchar_t>::do_scan_not(mask m, const char_type* low, const char_type* high) const |
| 805 | { |
| 806 | for (; low != high; ++low) |
Sean Hunt | 62a6ac3 | 2011-07-09 00:56:23 +0000 | [diff] [blame] | 807 | if (!(isascii(*low) && (ctype<char>::classic_table()[*low] & m))) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 808 | break; |
| 809 | return low; |
| 810 | } |
| 811 | |
| 812 | wchar_t |
| 813 | ctype<wchar_t>::do_toupper(char_type c) const |
| 814 | { |
Howard Hinnant | 3c466fc | 2011-09-29 13:33:15 +0000 | [diff] [blame] | 815 | #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE |
| 816 | return isascii(c) ? _DefaultRuneLocale.__mapupper[c] : c; |
Marshall Clow | 2ccffef | 2013-11-19 18:05:03 +0000 | [diff] [blame] | 817 | #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__) |
Sean Hunt | 62a6ac3 | 2011-07-09 00:56:23 +0000 | [diff] [blame] | 818 | return isascii(c) ? ctype<char>::__classic_upper_table()[c] : c; |
Sean Hunt | e59f724 | 2011-07-09 01:09:31 +0000 | [diff] [blame] | 819 | #else |
Howard Hinnant | 3c466fc | 2011-09-29 13:33:15 +0000 | [diff] [blame] | 820 | return (isascii(c) && iswlower_l(c, __cloc())) ? c-L'a'+L'A' : c; |
Sean Hunt | e59f724 | 2011-07-09 01:09:31 +0000 | [diff] [blame] | 821 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | const wchar_t* |
| 825 | ctype<wchar_t>::do_toupper(char_type* low, const char_type* high) const |
| 826 | { |
| 827 | for (; low != high; ++low) |
Howard Hinnant | 3c466fc | 2011-09-29 13:33:15 +0000 | [diff] [blame] | 828 | #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE |
| 829 | *low = isascii(*low) ? _DefaultRuneLocale.__mapupper[*low] : *low; |
Marshall Clow | 2ccffef | 2013-11-19 18:05:03 +0000 | [diff] [blame] | 830 | #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__) |
Sean Hunt | 62a6ac3 | 2011-07-09 00:56:23 +0000 | [diff] [blame] | 831 | *low = isascii(*low) ? ctype<char>::__classic_upper_table()[*low] |
| 832 | : *low; |
Sean Hunt | e59f724 | 2011-07-09 01:09:31 +0000 | [diff] [blame] | 833 | #else |
Howard Hinnant | 3c466fc | 2011-09-29 13:33:15 +0000 | [diff] [blame] | 834 | *low = (isascii(*low) && islower_l(*low, __cloc())) ? (*low-L'a'+L'A') : *low; |
Sean Hunt | e59f724 | 2011-07-09 01:09:31 +0000 | [diff] [blame] | 835 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 836 | return low; |
| 837 | } |
| 838 | |
| 839 | wchar_t |
| 840 | ctype<wchar_t>::do_tolower(char_type c) const |
| 841 | { |
Howard Hinnant | 3c466fc | 2011-09-29 13:33:15 +0000 | [diff] [blame] | 842 | #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE |
| 843 | return isascii(c) ? _DefaultRuneLocale.__maplower[c] : c; |
Marshall Clow | 2ccffef | 2013-11-19 18:05:03 +0000 | [diff] [blame] | 844 | #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__) |
Sean Hunt | 62a6ac3 | 2011-07-09 00:56:23 +0000 | [diff] [blame] | 845 | return isascii(c) ? ctype<char>::__classic_lower_table()[c] : c; |
Sean Hunt | e59f724 | 2011-07-09 01:09:31 +0000 | [diff] [blame] | 846 | #else |
Howard Hinnant | 3c466fc | 2011-09-29 13:33:15 +0000 | [diff] [blame] | 847 | return (isascii(c) && isupper_l(c, __cloc())) ? c-L'A'+'a' : c; |
Sean Hunt | e59f724 | 2011-07-09 01:09:31 +0000 | [diff] [blame] | 848 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | const wchar_t* |
| 852 | ctype<wchar_t>::do_tolower(char_type* low, const char_type* high) const |
| 853 | { |
| 854 | for (; low != high; ++low) |
Howard Hinnant | 3c466fc | 2011-09-29 13:33:15 +0000 | [diff] [blame] | 855 | #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE |
| 856 | *low = isascii(*low) ? _DefaultRuneLocale.__maplower[*low] : *low; |
Marshall Clow | 2ccffef | 2013-11-19 18:05:03 +0000 | [diff] [blame] | 857 | #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__) |
Sean Hunt | 62a6ac3 | 2011-07-09 00:56:23 +0000 | [diff] [blame] | 858 | *low = isascii(*low) ? ctype<char>::__classic_lower_table()[*low] |
| 859 | : *low; |
Sean Hunt | e59f724 | 2011-07-09 01:09:31 +0000 | [diff] [blame] | 860 | #else |
Howard Hinnant | 3c466fc | 2011-09-29 13:33:15 +0000 | [diff] [blame] | 861 | *low = (isascii(*low) && isupper_l(*low, __cloc())) ? *low-L'A'+L'a' : *low; |
Sean Hunt | e59f724 | 2011-07-09 01:09:31 +0000 | [diff] [blame] | 862 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 863 | return low; |
| 864 | } |
| 865 | |
| 866 | wchar_t |
| 867 | ctype<wchar_t>::do_widen(char c) const |
| 868 | { |
| 869 | return c; |
| 870 | } |
| 871 | |
| 872 | const char* |
| 873 | ctype<wchar_t>::do_widen(const char* low, const char* high, char_type* dest) const |
| 874 | { |
| 875 | for (; low != high; ++low, ++dest) |
| 876 | *dest = *low; |
| 877 | return low; |
| 878 | } |
| 879 | |
| 880 | char |
| 881 | ctype<wchar_t>::do_narrow(char_type c, char dfault) const |
| 882 | { |
| 883 | if (isascii(c)) |
| 884 | return static_cast<char>(c); |
| 885 | return dfault; |
| 886 | } |
| 887 | |
| 888 | const wchar_t* |
| 889 | ctype<wchar_t>::do_narrow(const char_type* low, const char_type* high, char dfault, char* dest) const |
| 890 | { |
| 891 | for (; low != high; ++low, ++dest) |
| 892 | if (isascii(*low)) |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 893 | *dest = static_cast<char>(*low); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 894 | else |
| 895 | *dest = dfault; |
| 896 | return low; |
| 897 | } |
| 898 | |
| 899 | // template <> class ctype<char>; |
| 900 | |
| 901 | locale::id ctype<char>::id; |
| 902 | |
| 903 | ctype<char>::ctype(const mask* tab, bool del, size_t refs) |
| 904 | : locale::facet(refs), |
| 905 | __tab_(tab), |
| 906 | __del_(del) |
| 907 | { |
Sean Hunt | 62a6ac3 | 2011-07-09 00:56:23 +0000 | [diff] [blame] | 908 | if (__tab_ == 0) |
| 909 | __tab_ = classic_table(); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | ctype<char>::~ctype() |
| 913 | { |
| 914 | if (__tab_ && __del_) |
| 915 | delete [] __tab_; |
| 916 | } |
| 917 | |
| 918 | char |
| 919 | ctype<char>::do_toupper(char_type c) const |
| 920 | { |
Howard Hinnant | 3c466fc | 2011-09-29 13:33:15 +0000 | [diff] [blame] | 921 | #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 922 | return isascii(c) ? |
| 923 | static_cast<char>(_DefaultRuneLocale.__mapupper[static_cast<ptrdiff_t>(c)]) : c; |
Joerg Sonnenberger | a71a952 | 2013-05-17 21:17:34 +0000 | [diff] [blame] | 924 | #elif defined(__NetBSD__) |
| 925 | return static_cast<char>(__classic_upper_table()[static_cast<unsigned char>(c)]); |
Marshall Clow | 2ccffef | 2013-11-19 18:05:03 +0000 | [diff] [blame] | 926 | #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) |
Marshall Clow | 88c3190 | 2013-02-07 14:22:51 +0000 | [diff] [blame] | 927 | return isascii(c) ? |
Joerg Sonnenberger | a71a952 | 2013-05-17 21:17:34 +0000 | [diff] [blame] | 928 | static_cast<char>(__classic_upper_table()[static_cast<unsigned char>(c)]) : c; |
Sean Hunt | e59f724 | 2011-07-09 01:09:31 +0000 | [diff] [blame] | 929 | #else |
Howard Hinnant | 3c466fc | 2011-09-29 13:33:15 +0000 | [diff] [blame] | 930 | return (isascii(c) && islower_l(c, __cloc())) ? c-'a'+'A' : c; |
Sean Hunt | e59f724 | 2011-07-09 01:09:31 +0000 | [diff] [blame] | 931 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | const char* |
| 935 | ctype<char>::do_toupper(char_type* low, const char_type* high) const |
| 936 | { |
| 937 | for (; low != high; ++low) |
Howard Hinnant | 3c466fc | 2011-09-29 13:33:15 +0000 | [diff] [blame] | 938 | #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 939 | *low = isascii(*low) ? |
| 940 | static_cast<char>(_DefaultRuneLocale.__mapupper[static_cast<ptrdiff_t>(*low)]) : *low; |
Joerg Sonnenberger | a71a952 | 2013-05-17 21:17:34 +0000 | [diff] [blame] | 941 | #elif defined(__NetBSD__) |
| 942 | *low = static_cast<char>(__classic_upper_table()[static_cast<unsigned char>(*low)]); |
Marshall Clow | 2ccffef | 2013-11-19 18:05:03 +0000 | [diff] [blame] | 943 | #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) |
Marshall Clow | 88c3190 | 2013-02-07 14:22:51 +0000 | [diff] [blame] | 944 | *low = isascii(*low) ? |
| 945 | static_cast<char>(__classic_upper_table()[static_cast<size_t>(*low)]) : *low; |
Sean Hunt | e59f724 | 2011-07-09 01:09:31 +0000 | [diff] [blame] | 946 | #else |
Howard Hinnant | 3c466fc | 2011-09-29 13:33:15 +0000 | [diff] [blame] | 947 | *low = (isascii(*low) && islower_l(*low, __cloc())) ? *low-'a'+'A' : *low; |
Sean Hunt | e59f724 | 2011-07-09 01:09:31 +0000 | [diff] [blame] | 948 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 949 | return low; |
| 950 | } |
| 951 | |
| 952 | char |
| 953 | ctype<char>::do_tolower(char_type c) const |
| 954 | { |
Howard Hinnant | 3c466fc | 2011-09-29 13:33:15 +0000 | [diff] [blame] | 955 | #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 956 | return isascii(c) ? |
| 957 | static_cast<char>(_DefaultRuneLocale.__maplower[static_cast<ptrdiff_t>(c)]) : c; |
Joerg Sonnenberger | a71a952 | 2013-05-17 21:17:34 +0000 | [diff] [blame] | 958 | #elif defined(__NetBSD__) |
| 959 | return static_cast<char>(__classic_lower_table()[static_cast<unsigned char>(c)]); |
Marshall Clow | 2ccffef | 2013-11-19 18:05:03 +0000 | [diff] [blame] | 960 | #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__) |
Marshall Clow | 88c3190 | 2013-02-07 14:22:51 +0000 | [diff] [blame] | 961 | return isascii(c) ? |
| 962 | static_cast<char>(__classic_lower_table()[static_cast<size_t>(c)]) : c; |
Sean Hunt | e59f724 | 2011-07-09 01:09:31 +0000 | [diff] [blame] | 963 | #else |
Howard Hinnant | 3c466fc | 2011-09-29 13:33:15 +0000 | [diff] [blame] | 964 | return (isascii(c) && isupper_l(c, __cloc())) ? c-'A'+'a' : c; |
Sean Hunt | e59f724 | 2011-07-09 01:09:31 +0000 | [diff] [blame] | 965 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | const char* |
| 969 | ctype<char>::do_tolower(char_type* low, const char_type* high) const |
| 970 | { |
| 971 | for (; low != high; ++low) |
Howard Hinnant | 3c466fc | 2011-09-29 13:33:15 +0000 | [diff] [blame] | 972 | #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 973 | *low = isascii(*low) ? static_cast<char>(_DefaultRuneLocale.__maplower[static_cast<ptrdiff_t>(*low)]) : *low; |
Joerg Sonnenberger | a71a952 | 2013-05-17 21:17:34 +0000 | [diff] [blame] | 974 | #elif defined(__NetBSD__) |
| 975 | *low = static_cast<char>(__classic_lower_table()[static_cast<unsigned char>(*low)]); |
Marshall Clow | 2ccffef | 2013-11-19 18:05:03 +0000 | [diff] [blame] | 976 | #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) |
Marshall Clow | 88c3190 | 2013-02-07 14:22:51 +0000 | [diff] [blame] | 977 | *low = isascii(*low) ? static_cast<char>(__classic_lower_table()[static_cast<size_t>(*low)]) : *low; |
Sean Hunt | e59f724 | 2011-07-09 01:09:31 +0000 | [diff] [blame] | 978 | #else |
Howard Hinnant | 3c466fc | 2011-09-29 13:33:15 +0000 | [diff] [blame] | 979 | *low = (isascii(*low) && isupper_l(*low, __cloc())) ? *low-'A'+'a' : *low; |
Sean Hunt | e59f724 | 2011-07-09 01:09:31 +0000 | [diff] [blame] | 980 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 981 | return low; |
| 982 | } |
| 983 | |
| 984 | char |
| 985 | ctype<char>::do_widen(char c) const |
| 986 | { |
| 987 | return c; |
| 988 | } |
| 989 | |
| 990 | const char* |
| 991 | ctype<char>::do_widen(const char* low, const char* high, char_type* dest) const |
| 992 | { |
| 993 | for (; low != high; ++low, ++dest) |
| 994 | *dest = *low; |
| 995 | return low; |
| 996 | } |
| 997 | |
| 998 | char |
| 999 | ctype<char>::do_narrow(char_type c, char dfault) const |
| 1000 | { |
| 1001 | if (isascii(c)) |
| 1002 | return static_cast<char>(c); |
| 1003 | return dfault; |
| 1004 | } |
| 1005 | |
| 1006 | const char* |
| 1007 | ctype<char>::do_narrow(const char_type* low, const char_type* high, char dfault, char* dest) const |
| 1008 | { |
| 1009 | for (; low != high; ++low, ++dest) |
| 1010 | if (isascii(*low)) |
| 1011 | *dest = *low; |
| 1012 | else |
| 1013 | *dest = dfault; |
| 1014 | return low; |
| 1015 | } |
| 1016 | |
Marshall Clow | 2ccffef | 2013-11-19 18:05:03 +0000 | [diff] [blame] | 1017 | #ifdef __EMSCRIPTEN__ |
Howard Hinnant | fc2f021 | 2013-03-29 18:27:28 +0000 | [diff] [blame] | 1018 | extern "C" const unsigned short ** __ctype_b_loc(); |
| 1019 | extern "C" const int ** __ctype_tolower_loc(); |
| 1020 | extern "C" const int ** __ctype_toupper_loc(); |
| 1021 | #endif |
| 1022 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1023 | const ctype<char>::mask* |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 1024 | ctype<char>::classic_table() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1025 | { |
David Chisnall | c512df1 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 1026 | #if defined(__APPLE__) || defined(__FreeBSD__) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1027 | return _DefaultRuneLocale.__runetype; |
Joerg Sonnenberger | a71a952 | 2013-05-17 21:17:34 +0000 | [diff] [blame] | 1028 | #elif defined(__NetBSD__) |
| 1029 | return _C_ctype_tab_ + 1; |
Sean Hunt | 62a6ac3 | 2011-07-09 00:56:23 +0000 | [diff] [blame] | 1030 | #elif defined(__GLIBC__) |
| 1031 | return __cloc()->__ctype_b; |
David Chisnall | 997e454 | 2012-02-29 13:05:08 +0000 | [diff] [blame] | 1032 | #elif __sun__ |
| 1033 | return __ctype_mask; |
Howard Hinnant | ef5aa93 | 2013-09-17 01:34:47 +0000 | [diff] [blame] | 1034 | #elif defined(_LIBCPP_MSVCRT) || defined(__MINGW32__) |
Howard Hinnant | 3c466fc | 2011-09-29 13:33:15 +0000 | [diff] [blame] | 1035 | return _ctype+1; // internal ctype mask table defined in msvcrt.dll |
David Chisnall | c512df1 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 1036 | // This is assumed to be safe, which is a nonsense assumption because we're |
| 1037 | // going to end up dereferencing it later... |
Marshall Clow | 2ccffef | 2013-11-19 18:05:03 +0000 | [diff] [blame] | 1038 | #elif defined(__EMSCRIPTEN__) |
Howard Hinnant | fc2f021 | 2013-03-29 18:27:28 +0000 | [diff] [blame] | 1039 | return *__ctype_b_loc(); |
Howard Hinnant | 7f76450 | 2013-08-14 18:00:20 +0000 | [diff] [blame] | 1040 | #elif defined(_AIX) |
Marshall Clow | 016d4e8 | 2013-11-19 19:16:03 +0000 | [diff] [blame] | 1041 | return (const unsigned int *)__lc_ctype_ptr->obj->mask; |
Dan Albert | 2517f4f | 2014-04-17 17:42:46 -0700 | [diff] [blame^] | 1042 | #elif defined(__ANDROID__) |
| 1043 | return _ctype_; |
Sean Hunt | 62a6ac3 | 2011-07-09 00:56:23 +0000 | [diff] [blame] | 1044 | #else |
David Chisnall | 997e454 | 2012-02-29 13:05:08 +0000 | [diff] [blame] | 1045 | // Platform not supported: abort so the person doing the port knows what to |
| 1046 | // fix |
Howard Hinnant | fcbaf48 | 2012-02-29 16:08:57 +0000 | [diff] [blame] | 1047 | # warning ctype<char>::classic_table() is not implemented |
Howard Hinnant | ed14a76 | 2013-07-23 16:05:56 +0000 | [diff] [blame] | 1048 | printf("ctype<char>::classic_table() is not implemented\n"); |
David Chisnall | 997e454 | 2012-02-29 13:05:08 +0000 | [diff] [blame] | 1049 | abort(); |
Sean Hunt | 62a6ac3 | 2011-07-09 00:56:23 +0000 | [diff] [blame] | 1050 | return NULL; |
| 1051 | #endif |
| 1052 | } |
| 1053 | |
Howard Hinnant | 3c466fc | 2011-09-29 13:33:15 +0000 | [diff] [blame] | 1054 | #if defined(__GLIBC__) |
Sean Hunt | 62a6ac3 | 2011-07-09 00:56:23 +0000 | [diff] [blame] | 1055 | const int* |
| 1056 | ctype<char>::__classic_lower_table() _NOEXCEPT |
| 1057 | { |
Sean Hunt | 62a6ac3 | 2011-07-09 00:56:23 +0000 | [diff] [blame] | 1058 | return __cloc()->__ctype_tolower; |
Sean Hunt | 62a6ac3 | 2011-07-09 00:56:23 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | const int* |
| 1062 | ctype<char>::__classic_upper_table() _NOEXCEPT |
| 1063 | { |
Sean Hunt | 62a6ac3 | 2011-07-09 00:56:23 +0000 | [diff] [blame] | 1064 | return __cloc()->__ctype_toupper; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1065 | } |
Joerg Sonnenberger | a71a952 | 2013-05-17 21:17:34 +0000 | [diff] [blame] | 1066 | #elif __NetBSD__ |
| 1067 | const short* |
| 1068 | ctype<char>::__classic_lower_table() _NOEXCEPT |
| 1069 | { |
| 1070 | return _C_tolower_tab_ + 1; |
| 1071 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1072 | |
Joerg Sonnenberger | a71a952 | 2013-05-17 21:17:34 +0000 | [diff] [blame] | 1073 | const short* |
| 1074 | ctype<char>::__classic_upper_table() _NOEXCEPT |
| 1075 | { |
| 1076 | return _C_toupper_tab_ + 1; |
| 1077 | } |
| 1078 | |
Marshall Clow | 2ccffef | 2013-11-19 18:05:03 +0000 | [diff] [blame] | 1079 | #elif defined(__EMSCRIPTEN__) |
Howard Hinnant | fc2f021 | 2013-03-29 18:27:28 +0000 | [diff] [blame] | 1080 | const int* |
| 1081 | ctype<char>::__classic_lower_table() _NOEXCEPT |
| 1082 | { |
| 1083 | return *__ctype_tolower_loc(); |
| 1084 | } |
| 1085 | |
| 1086 | const int* |
| 1087 | ctype<char>::__classic_upper_table() _NOEXCEPT |
| 1088 | { |
| 1089 | return *__ctype_toupper_loc(); |
| 1090 | } |
Marshall Clow | 2ccffef | 2013-11-19 18:05:03 +0000 | [diff] [blame] | 1091 | #endif // __GLIBC__ || __EMSCRIPTEN__ || __NETBSD__ |
Howard Hinnant | fc2f021 | 2013-03-29 18:27:28 +0000 | [diff] [blame] | 1092 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1093 | // template <> class ctype_byname<char> |
| 1094 | |
| 1095 | ctype_byname<char>::ctype_byname(const char* name, size_t refs) |
| 1096 | : ctype<char>(0, false, refs), |
| 1097 | __l(newlocale(LC_ALL_MASK, name, 0)) |
| 1098 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1099 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1100 | if (__l == 0) |
| 1101 | throw runtime_error("ctype_byname<char>::ctype_byname" |
| 1102 | " failed to construct for " + string(name)); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 1103 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | ctype_byname<char>::ctype_byname(const string& name, size_t refs) |
| 1107 | : ctype<char>(0, false, refs), |
| 1108 | __l(newlocale(LC_ALL_MASK, name.c_str(), 0)) |
| 1109 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1110 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1111 | if (__l == 0) |
| 1112 | throw runtime_error("ctype_byname<char>::ctype_byname" |
| 1113 | " failed to construct for " + name); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 1114 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | ctype_byname<char>::~ctype_byname() |
| 1118 | { |
| 1119 | freelocale(__l); |
| 1120 | } |
| 1121 | |
| 1122 | char |
| 1123 | ctype_byname<char>::do_toupper(char_type c) const |
| 1124 | { |
Joerg Sonnenberger | 63d8f7e | 2013-05-02 19:17:48 +0000 | [diff] [blame] | 1125 | return static_cast<char>(toupper_l(static_cast<unsigned char>(c), __l)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | const char* |
| 1129 | ctype_byname<char>::do_toupper(char_type* low, const char_type* high) const |
| 1130 | { |
| 1131 | for (; low != high; ++low) |
Joerg Sonnenberger | 63d8f7e | 2013-05-02 19:17:48 +0000 | [diff] [blame] | 1132 | *low = static_cast<char>(toupper_l(static_cast<unsigned char>(*low), __l)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1133 | return low; |
| 1134 | } |
| 1135 | |
| 1136 | char |
| 1137 | ctype_byname<char>::do_tolower(char_type c) const |
| 1138 | { |
Joerg Sonnenberger | 63d8f7e | 2013-05-02 19:17:48 +0000 | [diff] [blame] | 1139 | return static_cast<char>(tolower_l(static_cast<unsigned char>(c), __l)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | const char* |
| 1143 | ctype_byname<char>::do_tolower(char_type* low, const char_type* high) const |
| 1144 | { |
| 1145 | for (; low != high; ++low) |
Joerg Sonnenberger | 63d8f7e | 2013-05-02 19:17:48 +0000 | [diff] [blame] | 1146 | *low = static_cast<char>(tolower_l(static_cast<unsigned char>(*low), __l)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1147 | return low; |
| 1148 | } |
| 1149 | |
| 1150 | // template <> class ctype_byname<wchar_t> |
| 1151 | |
| 1152 | ctype_byname<wchar_t>::ctype_byname(const char* name, size_t refs) |
| 1153 | : ctype<wchar_t>(refs), |
| 1154 | __l(newlocale(LC_ALL_MASK, name, 0)) |
| 1155 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1156 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1157 | if (__l == 0) |
| 1158 | throw runtime_error("ctype_byname<wchar_t>::ctype_byname" |
| 1159 | " failed to construct for " + string(name)); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 1160 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | ctype_byname<wchar_t>::ctype_byname(const string& name, size_t refs) |
| 1164 | : ctype<wchar_t>(refs), |
| 1165 | __l(newlocale(LC_ALL_MASK, name.c_str(), 0)) |
| 1166 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1167 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1168 | if (__l == 0) |
| 1169 | throw runtime_error("ctype_byname<wchar_t>::ctype_byname" |
| 1170 | " failed to construct for " + name); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 1171 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | ctype_byname<wchar_t>::~ctype_byname() |
| 1175 | { |
| 1176 | freelocale(__l); |
| 1177 | } |
| 1178 | |
| 1179 | bool |
| 1180 | ctype_byname<wchar_t>::do_is(mask m, char_type c) const |
| 1181 | { |
Sean Hunt | 6f0342c | 2011-07-09 03:40:04 +0000 | [diff] [blame] | 1182 | #ifdef _LIBCPP_WCTYPE_IS_MASK |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1183 | return static_cast<bool>(iswctype_l(c, m, __l)); |
Sean Hunt | 6f0342c | 2011-07-09 03:40:04 +0000 | [diff] [blame] | 1184 | #else |
Howard Hinnant | ef793f2 | 2012-08-02 18:35:07 +0000 | [diff] [blame] | 1185 | bool result = false; |
Marshall Clow | 88c3190 | 2013-02-07 14:22:51 +0000 | [diff] [blame] | 1186 | wint_t ch = static_cast<wint_t>(c); |
| 1187 | if (m & space) result |= (iswspace_l(ch, __l) != 0); |
| 1188 | if (m & print) result |= (iswprint_l(ch, __l) != 0); |
| 1189 | if (m & cntrl) result |= (iswcntrl_l(ch, __l) != 0); |
| 1190 | if (m & upper) result |= (iswupper_l(ch, __l) != 0); |
| 1191 | if (m & lower) result |= (iswlower_l(ch, __l) != 0); |
| 1192 | if (m & alpha) result |= (iswalpha_l(ch, __l) != 0); |
| 1193 | if (m & digit) result |= (iswdigit_l(ch, __l) != 0); |
| 1194 | if (m & punct) result |= (iswpunct_l(ch, __l) != 0); |
| 1195 | if (m & xdigit) result |= (iswxdigit_l(ch, __l) != 0); |
| 1196 | if (m & blank) result |= (iswblank_l(ch, __l) != 0); |
Howard Hinnant | 3c466fc | 2011-09-29 13:33:15 +0000 | [diff] [blame] | 1197 | return result; |
Sean Hunt | 6f0342c | 2011-07-09 03:40:04 +0000 | [diff] [blame] | 1198 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | const wchar_t* |
| 1202 | ctype_byname<wchar_t>::do_is(const char_type* low, const char_type* high, mask* vec) const |
| 1203 | { |
| 1204 | for (; low != high; ++low, ++vec) |
| 1205 | { |
| 1206 | if (isascii(*low)) |
Sean Hunt | 62a6ac3 | 2011-07-09 00:56:23 +0000 | [diff] [blame] | 1207 | *vec = static_cast<mask>(ctype<char>::classic_table()[*low]); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1208 | else |
| 1209 | { |
| 1210 | *vec = 0; |
Marshall Clow | 88c3190 | 2013-02-07 14:22:51 +0000 | [diff] [blame] | 1211 | wint_t ch = static_cast<wint_t>(*low); |
| 1212 | if (iswspace_l(ch, __l)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1213 | *vec |= space; |
Marshall Clow | 88c3190 | 2013-02-07 14:22:51 +0000 | [diff] [blame] | 1214 | if (iswprint_l(ch, __l)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1215 | *vec |= print; |
Marshall Clow | 88c3190 | 2013-02-07 14:22:51 +0000 | [diff] [blame] | 1216 | if (iswcntrl_l(ch, __l)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1217 | *vec |= cntrl; |
Marshall Clow | 88c3190 | 2013-02-07 14:22:51 +0000 | [diff] [blame] | 1218 | if (iswupper_l(ch, __l)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1219 | *vec |= upper; |
Marshall Clow | 88c3190 | 2013-02-07 14:22:51 +0000 | [diff] [blame] | 1220 | if (iswlower_l(ch, __l)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1221 | *vec |= lower; |
Marshall Clow | 88c3190 | 2013-02-07 14:22:51 +0000 | [diff] [blame] | 1222 | if (iswalpha_l(ch, __l)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1223 | *vec |= alpha; |
Marshall Clow | 88c3190 | 2013-02-07 14:22:51 +0000 | [diff] [blame] | 1224 | if (iswdigit_l(ch, __l)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1225 | *vec |= digit; |
Marshall Clow | 88c3190 | 2013-02-07 14:22:51 +0000 | [diff] [blame] | 1226 | if (iswpunct_l(ch, __l)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1227 | *vec |= punct; |
Marshall Clow | 88c3190 | 2013-02-07 14:22:51 +0000 | [diff] [blame] | 1228 | if (iswxdigit_l(ch, __l)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1229 | *vec |= xdigit; |
| 1230 | } |
| 1231 | } |
| 1232 | return low; |
| 1233 | } |
| 1234 | |
| 1235 | const wchar_t* |
| 1236 | ctype_byname<wchar_t>::do_scan_is(mask m, const char_type* low, const char_type* high) const |
| 1237 | { |
| 1238 | for (; low != high; ++low) |
Sean Hunt | 6f0342c | 2011-07-09 03:40:04 +0000 | [diff] [blame] | 1239 | { |
| 1240 | #ifdef _LIBCPP_WCTYPE_IS_MASK |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1241 | if (iswctype_l(*low, m, __l)) |
| 1242 | break; |
Sean Hunt | 6f0342c | 2011-07-09 03:40:04 +0000 | [diff] [blame] | 1243 | #else |
Marshall Clow | 88c3190 | 2013-02-07 14:22:51 +0000 | [diff] [blame] | 1244 | wint_t ch = static_cast<wint_t>(*low); |
| 1245 | if (m & space && iswspace_l(ch, __l)) break; |
| 1246 | if (m & print && iswprint_l(ch, __l)) break; |
| 1247 | if (m & cntrl && iswcntrl_l(ch, __l)) break; |
| 1248 | if (m & upper && iswupper_l(ch, __l)) break; |
| 1249 | if (m & lower && iswlower_l(ch, __l)) break; |
| 1250 | if (m & alpha && iswalpha_l(ch, __l)) break; |
| 1251 | if (m & digit && iswdigit_l(ch, __l)) break; |
| 1252 | if (m & punct && iswpunct_l(ch, __l)) break; |
| 1253 | if (m & xdigit && iswxdigit_l(ch, __l)) break; |
| 1254 | if (m & blank && iswblank_l(ch, __l)) break; |
Sean Hunt | 6f0342c | 2011-07-09 03:40:04 +0000 | [diff] [blame] | 1255 | #endif |
| 1256 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1257 | return low; |
| 1258 | } |
| 1259 | |
| 1260 | const wchar_t* |
| 1261 | ctype_byname<wchar_t>::do_scan_not(mask m, const char_type* low, const char_type* high) const |
| 1262 | { |
| 1263 | for (; low != high; ++low) |
Sean Hunt | 6f0342c | 2011-07-09 03:40:04 +0000 | [diff] [blame] | 1264 | { |
| 1265 | #ifdef _LIBCPP_WCTYPE_IS_MASK |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1266 | if (!iswctype_l(*low, m, __l)) |
| 1267 | break; |
Sean Hunt | 6f0342c | 2011-07-09 03:40:04 +0000 | [diff] [blame] | 1268 | #else |
Marshall Clow | 88c3190 | 2013-02-07 14:22:51 +0000 | [diff] [blame] | 1269 | wint_t ch = static_cast<wint_t>(*low); |
| 1270 | if (m & space && iswspace_l(ch, __l)) continue; |
| 1271 | if (m & print && iswprint_l(ch, __l)) continue; |
| 1272 | if (m & cntrl && iswcntrl_l(ch, __l)) continue; |
| 1273 | if (m & upper && iswupper_l(ch, __l)) continue; |
| 1274 | if (m & lower && iswlower_l(ch, __l)) continue; |
| 1275 | if (m & alpha && iswalpha_l(ch, __l)) continue; |
| 1276 | if (m & digit && iswdigit_l(ch, __l)) continue; |
| 1277 | if (m & punct && iswpunct_l(ch, __l)) continue; |
| 1278 | if (m & xdigit && iswxdigit_l(ch, __l)) continue; |
| 1279 | if (m & blank && iswblank_l(ch, __l)) continue; |
Sean Hunt | 6f0342c | 2011-07-09 03:40:04 +0000 | [diff] [blame] | 1280 | break; |
| 1281 | #endif |
| 1282 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1283 | return low; |
| 1284 | } |
| 1285 | |
| 1286 | wchar_t |
| 1287 | ctype_byname<wchar_t>::do_toupper(char_type c) const |
| 1288 | { |
| 1289 | return towupper_l(c, __l); |
| 1290 | } |
| 1291 | |
| 1292 | const wchar_t* |
| 1293 | ctype_byname<wchar_t>::do_toupper(char_type* low, const char_type* high) const |
| 1294 | { |
| 1295 | for (; low != high; ++low) |
| 1296 | *low = towupper_l(*low, __l); |
| 1297 | return low; |
| 1298 | } |
| 1299 | |
| 1300 | wchar_t |
| 1301 | ctype_byname<wchar_t>::do_tolower(char_type c) const |
| 1302 | { |
| 1303 | return towlower_l(c, __l); |
| 1304 | } |
| 1305 | |
| 1306 | const wchar_t* |
| 1307 | ctype_byname<wchar_t>::do_tolower(char_type* low, const char_type* high) const |
| 1308 | { |
| 1309 | for (; low != high; ++low) |
| 1310 | *low = towlower_l(*low, __l); |
| 1311 | return low; |
| 1312 | } |
| 1313 | |
| 1314 | wchar_t |
| 1315 | ctype_byname<wchar_t>::do_widen(char c) const |
| 1316 | { |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 1317 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 1318 | return btowc_l(c, __l); |
| 1319 | #else |
| 1320 | return __btowc_l(c, __l); |
| 1321 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | const char* |
| 1325 | ctype_byname<wchar_t>::do_widen(const char* low, const char* high, char_type* dest) const |
| 1326 | { |
| 1327 | for (; low != high; ++low, ++dest) |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 1328 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 1329 | *dest = btowc_l(*low, __l); |
| 1330 | #else |
| 1331 | *dest = __btowc_l(*low, __l); |
| 1332 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1333 | return low; |
| 1334 | } |
| 1335 | |
| 1336 | char |
| 1337 | ctype_byname<wchar_t>::do_narrow(char_type c, char dfault) const |
| 1338 | { |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 1339 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 1340 | int r = wctob_l(c, __l); |
| 1341 | #else |
| 1342 | int r = __wctob_l(c, __l); |
| 1343 | #endif |
Howard Hinnant | c6e54b9 | 2012-12-27 18:46:00 +0000 | [diff] [blame] | 1344 | return r != static_cast<int>(WEOF) ? static_cast<char>(r) : dfault; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | const wchar_t* |
| 1348 | ctype_byname<wchar_t>::do_narrow(const char_type* low, const char_type* high, char dfault, char* dest) const |
| 1349 | { |
| 1350 | for (; low != high; ++low, ++dest) |
| 1351 | { |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 1352 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 1353 | int r = wctob_l(*low, __l); |
| 1354 | #else |
| 1355 | int r = __wctob_l(*low, __l); |
| 1356 | #endif |
Howard Hinnant | c6e54b9 | 2012-12-27 18:46:00 +0000 | [diff] [blame] | 1357 | *dest = r != static_cast<int>(WEOF) ? static_cast<char>(r) : dfault; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1358 | } |
| 1359 | return low; |
| 1360 | } |
| 1361 | |
| 1362 | // template <> class codecvt<char, char, mbstate_t> |
| 1363 | |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 1364 | locale::id codecvt<char, char, mbstate_t>::id; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1365 | |
| 1366 | codecvt<char, char, mbstate_t>::~codecvt() |
| 1367 | { |
| 1368 | } |
| 1369 | |
| 1370 | codecvt<char, char, mbstate_t>::result |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 1371 | codecvt<char, char, mbstate_t>::do_out(state_type&, |
| 1372 | const intern_type* frm, const intern_type*, const intern_type*& frm_nxt, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1373 | extern_type* to, extern_type*, extern_type*& to_nxt) const |
| 1374 | { |
| 1375 | frm_nxt = frm; |
| 1376 | to_nxt = to; |
| 1377 | return noconv; |
| 1378 | } |
| 1379 | |
| 1380 | codecvt<char, char, mbstate_t>::result |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 1381 | codecvt<char, char, mbstate_t>::do_in(state_type&, |
| 1382 | const extern_type* frm, const extern_type*, const extern_type*& frm_nxt, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1383 | intern_type* to, intern_type*, intern_type*& to_nxt) const |
| 1384 | { |
| 1385 | frm_nxt = frm; |
| 1386 | to_nxt = to; |
| 1387 | return noconv; |
| 1388 | } |
| 1389 | |
| 1390 | codecvt<char, char, mbstate_t>::result |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 1391 | codecvt<char, char, mbstate_t>::do_unshift(state_type&, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1392 | extern_type* to, extern_type*, extern_type*& to_nxt) const |
| 1393 | { |
| 1394 | to_nxt = to; |
| 1395 | return noconv; |
| 1396 | } |
| 1397 | |
| 1398 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 1399 | codecvt<char, char, mbstate_t>::do_encoding() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1400 | { |
| 1401 | return 1; |
| 1402 | } |
| 1403 | |
| 1404 | bool |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 1405 | codecvt<char, char, mbstate_t>::do_always_noconv() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1406 | { |
| 1407 | return true; |
| 1408 | } |
| 1409 | |
| 1410 | int |
| 1411 | codecvt<char, char, mbstate_t>::do_length(state_type&, |
| 1412 | const extern_type* frm, const extern_type* end, size_t mx) const |
| 1413 | { |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 1414 | return static_cast<int>(min<size_t>(mx, static_cast<size_t>(end-frm))); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 1418 | codecvt<char, char, mbstate_t>::do_max_length() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1419 | { |
| 1420 | return 1; |
| 1421 | } |
| 1422 | |
| 1423 | // template <> class codecvt<wchar_t, char, mbstate_t> |
| 1424 | |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 1425 | locale::id codecvt<wchar_t, char, mbstate_t>::id; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1426 | |
| 1427 | codecvt<wchar_t, char, mbstate_t>::codecvt(size_t refs) |
| 1428 | : locale::facet(refs), |
Joerg Sonnenberger | 5328cd3 | 2013-05-09 19:00:18 +0000 | [diff] [blame] | 1429 | __l(_LIBCPP_GET_C_LOCALE) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1430 | { |
| 1431 | } |
| 1432 | |
| 1433 | codecvt<wchar_t, char, mbstate_t>::codecvt(const char* nm, size_t refs) |
| 1434 | : locale::facet(refs), |
| 1435 | __l(newlocale(LC_ALL_MASK, nm, 0)) |
| 1436 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1437 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1438 | if (__l == 0) |
| 1439 | throw runtime_error("codecvt_byname<wchar_t, char, mbstate_t>::codecvt_byname" |
| 1440 | " failed to construct for " + string(nm)); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 1441 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | codecvt<wchar_t, char, mbstate_t>::~codecvt() |
| 1445 | { |
Joerg Sonnenberger | be764c9 | 2013-05-09 23:06:35 +0000 | [diff] [blame] | 1446 | if (__l != _LIBCPP_GET_C_LOCALE) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1447 | freelocale(__l); |
| 1448 | } |
| 1449 | |
| 1450 | codecvt<wchar_t, char, mbstate_t>::result |
| 1451 | codecvt<wchar_t, char, mbstate_t>::do_out(state_type& st, |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 1452 | const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1453 | extern_type* to, extern_type* to_end, extern_type*& to_nxt) const |
| 1454 | { |
| 1455 | // look for first internal null in frm |
| 1456 | const intern_type* fend = frm; |
| 1457 | for (; fend != frm_end; ++fend) |
| 1458 | if (*fend == 0) |
| 1459 | break; |
| 1460 | // loop over all null-terminated sequences in frm |
| 1461 | to_nxt = to; |
| 1462 | for (frm_nxt = frm; frm != frm_end && to != to_end; frm = frm_nxt, to = to_nxt) |
| 1463 | { |
Joerg Sonnenberger | df41821 | 2013-04-26 09:40:18 +0000 | [diff] [blame] | 1464 | // save state in case it is needed to recover to_nxt on error |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1465 | mbstate_t save_state = st; |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 1466 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 1467 | size_t n = wcsnrtombs_l(to, &frm_nxt, static_cast<size_t>(fend-frm), |
| 1468 | static_cast<size_t>(to_end-to), &st, __l); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 1469 | #else |
| 1470 | size_t n = __wcsnrtombs_l(to, &frm_nxt, fend-frm, to_end-to, &st, __l); |
| 1471 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1472 | if (n == size_t(-1)) |
| 1473 | { |
| 1474 | // need to recover to_nxt |
| 1475 | for (to_nxt = to; frm != frm_nxt; ++frm) |
| 1476 | { |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 1477 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 1478 | n = wcrtomb_l(to_nxt, *frm, &save_state, __l); |
| 1479 | #else |
| 1480 | n = __wcrtomb_l(to_nxt, *frm, &save_state, __l); |
| 1481 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1482 | if (n == size_t(-1)) |
| 1483 | break; |
| 1484 | to_nxt += n; |
| 1485 | } |
| 1486 | frm_nxt = frm; |
| 1487 | return error; |
| 1488 | } |
| 1489 | if (n == 0) |
| 1490 | return partial; |
| 1491 | to_nxt += n; |
| 1492 | if (to_nxt == to_end) |
| 1493 | break; |
| 1494 | if (fend != frm_end) // set up next null terminated sequence |
| 1495 | { |
| 1496 | // Try to write the terminating null |
| 1497 | extern_type tmp[MB_LEN_MAX]; |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 1498 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 1499 | n = wcrtomb_l(tmp, intern_type(), &st, __l); |
| 1500 | #else |
| 1501 | n = __wcrtomb_l(tmp, intern_type(), &st, __l); |
| 1502 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1503 | if (n == size_t(-1)) // on error |
| 1504 | return error; |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 1505 | if (n > static_cast<size_t>(to_end-to_nxt)) // is there room? |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1506 | return partial; |
| 1507 | for (extern_type* p = tmp; n; --n) // write it |
| 1508 | *to_nxt++ = *p++; |
| 1509 | ++frm_nxt; |
| 1510 | // look for next null in frm |
| 1511 | for (fend = frm_nxt; fend != frm_end; ++fend) |
| 1512 | if (*fend == 0) |
| 1513 | break; |
| 1514 | } |
| 1515 | } |
| 1516 | return frm_nxt == frm_end ? ok : partial; |
| 1517 | } |
| 1518 | |
| 1519 | codecvt<wchar_t, char, mbstate_t>::result |
| 1520 | codecvt<wchar_t, char, mbstate_t>::do_in(state_type& st, |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 1521 | const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1522 | intern_type* to, intern_type* to_end, intern_type*& to_nxt) const |
| 1523 | { |
| 1524 | // look for first internal null in frm |
| 1525 | const extern_type* fend = frm; |
| 1526 | for (; fend != frm_end; ++fend) |
| 1527 | if (*fend == 0) |
| 1528 | break; |
| 1529 | // loop over all null-terminated sequences in frm |
| 1530 | to_nxt = to; |
| 1531 | for (frm_nxt = frm; frm != frm_end && to != to_end; frm = frm_nxt, to = to_nxt) |
| 1532 | { |
Joerg Sonnenberger | df41821 | 2013-04-26 09:40:18 +0000 | [diff] [blame] | 1533 | // save state in case it is needed to recover to_nxt on error |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1534 | mbstate_t save_state = st; |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 1535 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 1536 | size_t n = mbsnrtowcs_l(to, &frm_nxt, static_cast<size_t>(fend-frm), |
| 1537 | static_cast<size_t>(to_end-to), &st, __l); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 1538 | #else |
| 1539 | size_t n = __mbsnrtowcs_l(to, &frm_nxt, fend-frm, to_end-to, &st, __l); |
| 1540 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1541 | if (n == size_t(-1)) |
| 1542 | { |
| 1543 | // need to recover to_nxt |
| 1544 | for (to_nxt = to; frm != frm_nxt; ++to_nxt) |
| 1545 | { |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 1546 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 1547 | n = mbrtowc_l(to_nxt, frm, static_cast<size_t>(fend-frm), |
| 1548 | &save_state, __l); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 1549 | #else |
| 1550 | n = __mbrtowc_l(to_nxt, frm, fend-frm, &save_state, __l); |
| 1551 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1552 | switch (n) |
| 1553 | { |
| 1554 | case 0: |
| 1555 | ++frm; |
| 1556 | break; |
Howard Hinnant | 982331b | 2012-02-08 19:15:06 +0000 | [diff] [blame] | 1557 | case size_t(-1): |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1558 | frm_nxt = frm; |
| 1559 | return error; |
Howard Hinnant | 982331b | 2012-02-08 19:15:06 +0000 | [diff] [blame] | 1560 | case size_t(-2): |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1561 | frm_nxt = frm; |
| 1562 | return partial; |
| 1563 | default: |
| 1564 | frm += n; |
| 1565 | break; |
| 1566 | } |
| 1567 | } |
| 1568 | frm_nxt = frm; |
| 1569 | return frm_nxt == frm_end ? ok : partial; |
| 1570 | } |
| 1571 | if (n == 0) |
| 1572 | return error; |
| 1573 | to_nxt += n; |
| 1574 | if (to_nxt == to_end) |
| 1575 | break; |
| 1576 | if (fend != frm_end) // set up next null terminated sequence |
| 1577 | { |
| 1578 | // Try to write the terminating null |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 1579 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 1580 | n = mbrtowc_l(to_nxt, frm_nxt, 1, &st, __l); |
| 1581 | #else |
| 1582 | n = __mbrtowc_l(to_nxt, frm_nxt, 1, &st, __l); |
| 1583 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1584 | if (n != 0) // on error |
| 1585 | return error; |
| 1586 | ++to_nxt; |
| 1587 | ++frm_nxt; |
| 1588 | // look for next null in frm |
| 1589 | for (fend = frm_nxt; fend != frm_end; ++fend) |
| 1590 | if (*fend == 0) |
| 1591 | break; |
| 1592 | } |
| 1593 | } |
| 1594 | return frm_nxt == frm_end ? ok : partial; |
| 1595 | } |
| 1596 | |
| 1597 | codecvt<wchar_t, char, mbstate_t>::result |
| 1598 | codecvt<wchar_t, char, mbstate_t>::do_unshift(state_type& st, |
| 1599 | extern_type* to, extern_type* to_end, extern_type*& to_nxt) const |
| 1600 | { |
| 1601 | to_nxt = to; |
| 1602 | extern_type tmp[MB_LEN_MAX]; |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 1603 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 1604 | size_t n = wcrtomb_l(tmp, intern_type(), &st, __l); |
| 1605 | #else |
| 1606 | size_t n = __wcrtomb_l(tmp, intern_type(), &st, __l); |
| 1607 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1608 | if (n == size_t(-1) || n == 0) // on error |
| 1609 | return error; |
| 1610 | --n; |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 1611 | if (n > static_cast<size_t>(to_end-to_nxt)) // is there room? |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1612 | return partial; |
| 1613 | for (extern_type* p = tmp; n; --n) // write it |
| 1614 | *to_nxt++ = *p++; |
| 1615 | return ok; |
| 1616 | } |
| 1617 | |
| 1618 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 1619 | codecvt<wchar_t, char, mbstate_t>::do_encoding() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1620 | { |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 1621 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Joerg Sonnenberger | 4c6acb5 | 2014-01-04 17:43:00 +0000 | [diff] [blame] | 1622 | if (mbtowc_l(nullptr, nullptr, MB_LEN_MAX, __l) == 0) |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 1623 | #else |
Joerg Sonnenberger | 4c6acb5 | 2014-01-04 17:43:00 +0000 | [diff] [blame] | 1624 | if (__mbtowc_l(nullptr, nullptr, MB_LEN_MAX, __l) == 0) |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 1625 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1626 | { |
| 1627 | // stateless encoding |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 1628 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 1629 | if (__l == 0 || MB_CUR_MAX_L(__l) == 1) // there are no known constant length encodings |
| 1630 | #else |
| 1631 | if (__l == 0 || __mb_cur_max_l(__l) == 1) // there are no known constant length encodings |
| 1632 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1633 | return 1; // which take more than 1 char to form a wchar_t |
| 1634 | return 0; |
| 1635 | } |
| 1636 | return -1; |
| 1637 | } |
| 1638 | |
| 1639 | bool |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 1640 | codecvt<wchar_t, char, mbstate_t>::do_always_noconv() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1641 | { |
| 1642 | return false; |
| 1643 | } |
| 1644 | |
| 1645 | int |
| 1646 | codecvt<wchar_t, char, mbstate_t>::do_length(state_type& st, |
| 1647 | const extern_type* frm, const extern_type* frm_end, size_t mx) const |
| 1648 | { |
| 1649 | int nbytes = 0; |
| 1650 | for (size_t nwchar_t = 0; nwchar_t < mx && frm != frm_end; ++nwchar_t) |
| 1651 | { |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 1652 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 1653 | size_t n = mbrlen_l(frm, static_cast<size_t>(frm_end-frm), &st, __l); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 1654 | #else |
| 1655 | size_t n = __mbrlen_l(frm, frm_end-frm, &st, __l); |
| 1656 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1657 | switch (n) |
| 1658 | { |
| 1659 | case 0: |
| 1660 | ++nbytes; |
| 1661 | ++frm; |
| 1662 | break; |
Howard Hinnant | 982331b | 2012-02-08 19:15:06 +0000 | [diff] [blame] | 1663 | case size_t(-1): |
| 1664 | case size_t(-2): |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1665 | return nbytes; |
| 1666 | default: |
| 1667 | nbytes += n; |
| 1668 | frm += n; |
| 1669 | break; |
| 1670 | } |
| 1671 | } |
| 1672 | return nbytes; |
| 1673 | } |
| 1674 | |
| 1675 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 1676 | codecvt<wchar_t, char, mbstate_t>::do_max_length() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1677 | { |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 1678 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Marshall Clow | 9ae96d0 | 2013-02-07 17:20:56 +0000 | [diff] [blame] | 1679 | return __l == 0 ? 1 : static_cast<int>( MB_CUR_MAX_L(__l)); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 1680 | #else |
Marshall Clow | 9ae96d0 | 2013-02-07 17:20:56 +0000 | [diff] [blame] | 1681 | return __l == 0 ? 1 : static_cast<int>(__mb_cur_max_l(__l)); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 1682 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | // Valid UTF ranges |
| 1686 | // UTF-32 UTF-16 UTF-8 # of code points |
| 1687 | // first second first second third fourth |
| 1688 | // 000000 - 00007F 0000 - 007F 00 - 7F 127 |
| 1689 | // 000080 - 0007FF 0080 - 07FF C2 - DF, 80 - BF 1920 |
| 1690 | // 000800 - 000FFF 0800 - 0FFF E0 - E0, A0 - BF, 80 - BF 2048 |
| 1691 | // 001000 - 00CFFF 1000 - CFFF E1 - EC, 80 - BF, 80 - BF 49152 |
| 1692 | // 00D000 - 00D7FF D000 - D7FF ED - ED, 80 - 9F, 80 - BF 2048 |
| 1693 | // 00D800 - 00DFFF invalid |
| 1694 | // 00E000 - 00FFFF E000 - FFFF EE - EF, 80 - BF, 80 - BF 8192 |
| 1695 | // 010000 - 03FFFF D800 - D8BF, DC00 - DFFF F0 - F0, 90 - BF, 80 - BF, 80 - BF 196608 |
| 1696 | // 040000 - 0FFFFF D8C0 - DBBF, DC00 - DFFF F1 - F3, 80 - BF, 80 - BF, 80 - BF 786432 |
| 1697 | // 100000 - 10FFFF DBC0 - DBFF, DC00 - DFFF F4 - F4, 80 - 8F, 80 - BF, 80 - BF 65536 |
| 1698 | |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 1699 | static |
| 1700 | codecvt_base::result |
| 1701 | utf16_to_utf8(const uint16_t* frm, const uint16_t* frm_end, const uint16_t*& frm_nxt, |
| 1702 | uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt, |
| 1703 | unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0)) |
| 1704 | { |
| 1705 | frm_nxt = frm; |
| 1706 | to_nxt = to; |
| 1707 | if (mode & generate_header) |
| 1708 | { |
| 1709 | if (to_end-to_nxt < 3) |
| 1710 | return codecvt_base::partial; |
| 1711 | *to_nxt++ = static_cast<uint8_t>(0xEF); |
| 1712 | *to_nxt++ = static_cast<uint8_t>(0xBB); |
| 1713 | *to_nxt++ = static_cast<uint8_t>(0xBF); |
| 1714 | } |
| 1715 | for (; frm_nxt < frm_end; ++frm_nxt) |
| 1716 | { |
| 1717 | uint16_t wc1 = *frm_nxt; |
| 1718 | if (wc1 > Maxcode) |
| 1719 | return codecvt_base::error; |
| 1720 | if (wc1 < 0x0080) |
| 1721 | { |
| 1722 | if (to_end-to_nxt < 1) |
| 1723 | return codecvt_base::partial; |
| 1724 | *to_nxt++ = static_cast<uint8_t>(wc1); |
| 1725 | } |
| 1726 | else if (wc1 < 0x0800) |
| 1727 | { |
| 1728 | if (to_end-to_nxt < 2) |
| 1729 | return codecvt_base::partial; |
| 1730 | *to_nxt++ = static_cast<uint8_t>(0xC0 | (wc1 >> 6)); |
| 1731 | *to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x03F)); |
| 1732 | } |
| 1733 | else if (wc1 < 0xD800) |
| 1734 | { |
| 1735 | if (to_end-to_nxt < 3) |
| 1736 | return codecvt_base::partial; |
| 1737 | *to_nxt++ = static_cast<uint8_t>(0xE0 | (wc1 >> 12)); |
| 1738 | *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0FC0) >> 6)); |
| 1739 | *to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x003F)); |
| 1740 | } |
| 1741 | else if (wc1 < 0xDC00) |
| 1742 | { |
| 1743 | if (frm_end-frm_nxt < 2) |
| 1744 | return codecvt_base::partial; |
| 1745 | uint16_t wc2 = frm_nxt[1]; |
| 1746 | if ((wc2 & 0xFC00) != 0xDC00) |
| 1747 | return codecvt_base::error; |
| 1748 | if (to_end-to_nxt < 4) |
| 1749 | return codecvt_base::partial; |
Joerg Sonnenberger | 4c6acb5 | 2014-01-04 17:43:00 +0000 | [diff] [blame] | 1750 | if (((((wc1 & 0x03C0UL) >> 6) + 1) << 16) + |
| 1751 | ((wc1 & 0x003FUL) << 10) + (wc2 & 0x03FF) > Maxcode) |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 1752 | return codecvt_base::error; |
| 1753 | ++frm_nxt; |
| 1754 | uint8_t z = ((wc1 & 0x03C0) >> 6) + 1; |
| 1755 | *to_nxt++ = static_cast<uint8_t>(0xF0 | (z >> 2)); |
| 1756 | *to_nxt++ = static_cast<uint8_t>(0x80 | ((z & 0x03) << 4) | ((wc1 & 0x003C) >> 2)); |
| 1757 | *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0003) << 4) | ((wc2 & 0x03C0) >> 6)); |
| 1758 | *to_nxt++ = static_cast<uint8_t>(0x80 | (wc2 & 0x003F)); |
| 1759 | } |
| 1760 | else if (wc1 < 0xE000) |
| 1761 | { |
| 1762 | return codecvt_base::error; |
| 1763 | } |
| 1764 | else |
| 1765 | { |
| 1766 | if (to_end-to_nxt < 3) |
| 1767 | return codecvt_base::partial; |
| 1768 | *to_nxt++ = static_cast<uint8_t>(0xE0 | (wc1 >> 12)); |
| 1769 | *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0FC0) >> 6)); |
| 1770 | *to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x003F)); |
| 1771 | } |
| 1772 | } |
| 1773 | return codecvt_base::ok; |
| 1774 | } |
| 1775 | |
| 1776 | static |
| 1777 | codecvt_base::result |
| 1778 | utf16_to_utf8(const uint32_t* frm, const uint32_t* frm_end, const uint32_t*& frm_nxt, |
| 1779 | uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt, |
| 1780 | unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0)) |
| 1781 | { |
| 1782 | frm_nxt = frm; |
| 1783 | to_nxt = to; |
| 1784 | if (mode & generate_header) |
| 1785 | { |
| 1786 | if (to_end-to_nxt < 3) |
| 1787 | return codecvt_base::partial; |
| 1788 | *to_nxt++ = static_cast<uint8_t>(0xEF); |
| 1789 | *to_nxt++ = static_cast<uint8_t>(0xBB); |
| 1790 | *to_nxt++ = static_cast<uint8_t>(0xBF); |
| 1791 | } |
| 1792 | for (; frm_nxt < frm_end; ++frm_nxt) |
| 1793 | { |
| 1794 | uint16_t wc1 = static_cast<uint16_t>(*frm_nxt); |
| 1795 | if (wc1 > Maxcode) |
| 1796 | return codecvt_base::error; |
| 1797 | if (wc1 < 0x0080) |
| 1798 | { |
| 1799 | if (to_end-to_nxt < 1) |
| 1800 | return codecvt_base::partial; |
| 1801 | *to_nxt++ = static_cast<uint8_t>(wc1); |
| 1802 | } |
| 1803 | else if (wc1 < 0x0800) |
| 1804 | { |
| 1805 | if (to_end-to_nxt < 2) |
| 1806 | return codecvt_base::partial; |
| 1807 | *to_nxt++ = static_cast<uint8_t>(0xC0 | (wc1 >> 6)); |
| 1808 | *to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x03F)); |
| 1809 | } |
| 1810 | else if (wc1 < 0xD800) |
| 1811 | { |
| 1812 | if (to_end-to_nxt < 3) |
| 1813 | return codecvt_base::partial; |
| 1814 | *to_nxt++ = static_cast<uint8_t>(0xE0 | (wc1 >> 12)); |
| 1815 | *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0FC0) >> 6)); |
| 1816 | *to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x003F)); |
| 1817 | } |
| 1818 | else if (wc1 < 0xDC00) |
| 1819 | { |
| 1820 | if (frm_end-frm_nxt < 2) |
| 1821 | return codecvt_base::partial; |
| 1822 | uint16_t wc2 = static_cast<uint16_t>(frm_nxt[1]); |
| 1823 | if ((wc2 & 0xFC00) != 0xDC00) |
| 1824 | return codecvt_base::error; |
| 1825 | if (to_end-to_nxt < 4) |
| 1826 | return codecvt_base::partial; |
Joerg Sonnenberger | 4c6acb5 | 2014-01-04 17:43:00 +0000 | [diff] [blame] | 1827 | if (((((wc1 & 0x03C0UL) >> 6) + 1) << 16) + |
| 1828 | ((wc1 & 0x003FUL) << 10) + (wc2 & 0x03FF) > Maxcode) |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 1829 | return codecvt_base::error; |
| 1830 | ++frm_nxt; |
| 1831 | uint8_t z = ((wc1 & 0x03C0) >> 6) + 1; |
| 1832 | *to_nxt++ = static_cast<uint8_t>(0xF0 | (z >> 2)); |
| 1833 | *to_nxt++ = static_cast<uint8_t>(0x80 | ((z & 0x03) << 4) | ((wc1 & 0x003C) >> 2)); |
| 1834 | *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0003) << 4) | ((wc2 & 0x03C0) >> 6)); |
| 1835 | *to_nxt++ = static_cast<uint8_t>(0x80 | (wc2 & 0x003F)); |
| 1836 | } |
| 1837 | else if (wc1 < 0xE000) |
| 1838 | { |
| 1839 | return codecvt_base::error; |
| 1840 | } |
| 1841 | else |
| 1842 | { |
| 1843 | if (to_end-to_nxt < 3) |
| 1844 | return codecvt_base::partial; |
| 1845 | *to_nxt++ = static_cast<uint8_t>(0xE0 | (wc1 >> 12)); |
| 1846 | *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0FC0) >> 6)); |
| 1847 | *to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x003F)); |
| 1848 | } |
| 1849 | } |
| 1850 | return codecvt_base::ok; |
| 1851 | } |
| 1852 | |
| 1853 | static |
| 1854 | codecvt_base::result |
| 1855 | utf8_to_utf16(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt, |
| 1856 | uint16_t* to, uint16_t* to_end, uint16_t*& to_nxt, |
| 1857 | unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0)) |
| 1858 | { |
| 1859 | frm_nxt = frm; |
| 1860 | to_nxt = to; |
| 1861 | if (mode & consume_header) |
| 1862 | { |
| 1863 | if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB && |
| 1864 | frm_nxt[2] == 0xBF) |
| 1865 | frm_nxt += 3; |
| 1866 | } |
| 1867 | for (; frm_nxt < frm_end && to_nxt < to_end; ++to_nxt) |
| 1868 | { |
| 1869 | uint8_t c1 = *frm_nxt; |
| 1870 | if (c1 > Maxcode) |
| 1871 | return codecvt_base::error; |
| 1872 | if (c1 < 0x80) |
| 1873 | { |
| 1874 | *to_nxt = static_cast<uint16_t>(c1); |
| 1875 | ++frm_nxt; |
| 1876 | } |
| 1877 | else if (c1 < 0xC2) |
| 1878 | { |
| 1879 | return codecvt_base::error; |
| 1880 | } |
| 1881 | else if (c1 < 0xE0) |
| 1882 | { |
| 1883 | if (frm_end-frm_nxt < 2) |
| 1884 | return codecvt_base::partial; |
| 1885 | uint8_t c2 = frm_nxt[1]; |
| 1886 | if ((c2 & 0xC0) != 0x80) |
| 1887 | return codecvt_base::error; |
| 1888 | uint16_t t = static_cast<uint16_t>(((c1 & 0x1F) << 6) | (c2 & 0x3F)); |
| 1889 | if (t > Maxcode) |
| 1890 | return codecvt_base::error; |
| 1891 | *to_nxt = t; |
| 1892 | frm_nxt += 2; |
| 1893 | } |
| 1894 | else if (c1 < 0xF0) |
| 1895 | { |
| 1896 | if (frm_end-frm_nxt < 3) |
| 1897 | return codecvt_base::partial; |
| 1898 | uint8_t c2 = frm_nxt[1]; |
| 1899 | uint8_t c3 = frm_nxt[2]; |
| 1900 | switch (c1) |
| 1901 | { |
| 1902 | case 0xE0: |
| 1903 | if ((c2 & 0xE0) != 0xA0) |
| 1904 | return codecvt_base::error; |
| 1905 | break; |
| 1906 | case 0xED: |
| 1907 | if ((c2 & 0xE0) != 0x80) |
| 1908 | return codecvt_base::error; |
| 1909 | break; |
| 1910 | default: |
| 1911 | if ((c2 & 0xC0) != 0x80) |
| 1912 | return codecvt_base::error; |
| 1913 | break; |
| 1914 | } |
| 1915 | if ((c3 & 0xC0) != 0x80) |
| 1916 | return codecvt_base::error; |
| 1917 | uint16_t t = static_cast<uint16_t>(((c1 & 0x0F) << 12) |
| 1918 | | ((c2 & 0x3F) << 6) |
| 1919 | | (c3 & 0x3F)); |
| 1920 | if (t > Maxcode) |
| 1921 | return codecvt_base::error; |
| 1922 | *to_nxt = t; |
| 1923 | frm_nxt += 3; |
| 1924 | } |
| 1925 | else if (c1 < 0xF5) |
| 1926 | { |
| 1927 | if (frm_end-frm_nxt < 4) |
| 1928 | return codecvt_base::partial; |
| 1929 | uint8_t c2 = frm_nxt[1]; |
| 1930 | uint8_t c3 = frm_nxt[2]; |
| 1931 | uint8_t c4 = frm_nxt[3]; |
| 1932 | switch (c1) |
| 1933 | { |
| 1934 | case 0xF0: |
| 1935 | if (!(0x90 <= c2 && c2 <= 0xBF)) |
| 1936 | return codecvt_base::error; |
| 1937 | break; |
| 1938 | case 0xF4: |
| 1939 | if ((c2 & 0xF0) != 0x80) |
| 1940 | return codecvt_base::error; |
| 1941 | break; |
| 1942 | default: |
| 1943 | if ((c2 & 0xC0) != 0x80) |
| 1944 | return codecvt_base::error; |
| 1945 | break; |
| 1946 | } |
| 1947 | if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80) |
| 1948 | return codecvt_base::error; |
| 1949 | if (to_end-to_nxt < 2) |
| 1950 | return codecvt_base::partial; |
Joerg Sonnenberger | 4c6acb5 | 2014-01-04 17:43:00 +0000 | [diff] [blame] | 1951 | if ((((c1 & 7UL) << 18) + |
| 1952 | ((c2 & 0x3FUL) << 12) + |
| 1953 | ((c3 & 0x3FUL) << 6) + (c4 & 0x3F)) > Maxcode) |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 1954 | return codecvt_base::error; |
| 1955 | *to_nxt = static_cast<uint16_t>( |
| 1956 | 0xD800 |
| 1957 | | (((((c1 & 0x07) << 2) | ((c2 & 0x30) >> 4)) - 1) << 6) |
| 1958 | | ((c2 & 0x0F) << 2) |
| 1959 | | ((c3 & 0x30) >> 4)); |
| 1960 | *++to_nxt = static_cast<uint16_t>( |
| 1961 | 0xDC00 |
| 1962 | | ((c3 & 0x0F) << 6) |
| 1963 | | (c4 & 0x3F)); |
| 1964 | frm_nxt += 4; |
| 1965 | } |
| 1966 | else |
| 1967 | { |
| 1968 | return codecvt_base::error; |
| 1969 | } |
| 1970 | } |
| 1971 | return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok; |
| 1972 | } |
| 1973 | |
| 1974 | static |
| 1975 | codecvt_base::result |
| 1976 | utf8_to_utf16(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt, |
| 1977 | uint32_t* to, uint32_t* to_end, uint32_t*& to_nxt, |
| 1978 | unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0)) |
| 1979 | { |
| 1980 | frm_nxt = frm; |
| 1981 | to_nxt = to; |
| 1982 | if (mode & consume_header) |
| 1983 | { |
| 1984 | if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB && |
| 1985 | frm_nxt[2] == 0xBF) |
| 1986 | frm_nxt += 3; |
| 1987 | } |
| 1988 | for (; frm_nxt < frm_end && to_nxt < to_end; ++to_nxt) |
| 1989 | { |
| 1990 | uint8_t c1 = *frm_nxt; |
| 1991 | if (c1 > Maxcode) |
| 1992 | return codecvt_base::error; |
| 1993 | if (c1 < 0x80) |
| 1994 | { |
| 1995 | *to_nxt = static_cast<uint32_t>(c1); |
| 1996 | ++frm_nxt; |
| 1997 | } |
| 1998 | else if (c1 < 0xC2) |
| 1999 | { |
| 2000 | return codecvt_base::error; |
| 2001 | } |
| 2002 | else if (c1 < 0xE0) |
| 2003 | { |
| 2004 | if (frm_end-frm_nxt < 2) |
| 2005 | return codecvt_base::partial; |
| 2006 | uint8_t c2 = frm_nxt[1]; |
| 2007 | if ((c2 & 0xC0) != 0x80) |
| 2008 | return codecvt_base::error; |
| 2009 | uint16_t t = static_cast<uint16_t>(((c1 & 0x1F) << 6) | (c2 & 0x3F)); |
| 2010 | if (t > Maxcode) |
| 2011 | return codecvt_base::error; |
| 2012 | *to_nxt = static_cast<uint32_t>(t); |
| 2013 | frm_nxt += 2; |
| 2014 | } |
| 2015 | else if (c1 < 0xF0) |
| 2016 | { |
| 2017 | if (frm_end-frm_nxt < 3) |
| 2018 | return codecvt_base::partial; |
| 2019 | uint8_t c2 = frm_nxt[1]; |
| 2020 | uint8_t c3 = frm_nxt[2]; |
| 2021 | switch (c1) |
| 2022 | { |
| 2023 | case 0xE0: |
| 2024 | if ((c2 & 0xE0) != 0xA0) |
| 2025 | return codecvt_base::error; |
| 2026 | break; |
| 2027 | case 0xED: |
| 2028 | if ((c2 & 0xE0) != 0x80) |
| 2029 | return codecvt_base::error; |
| 2030 | break; |
| 2031 | default: |
| 2032 | if ((c2 & 0xC0) != 0x80) |
| 2033 | return codecvt_base::error; |
| 2034 | break; |
| 2035 | } |
| 2036 | if ((c3 & 0xC0) != 0x80) |
| 2037 | return codecvt_base::error; |
| 2038 | uint16_t t = static_cast<uint16_t>(((c1 & 0x0F) << 12) |
| 2039 | | ((c2 & 0x3F) << 6) |
| 2040 | | (c3 & 0x3F)); |
| 2041 | if (t > Maxcode) |
| 2042 | return codecvt_base::error; |
| 2043 | *to_nxt = static_cast<uint32_t>(t); |
| 2044 | frm_nxt += 3; |
| 2045 | } |
| 2046 | else if (c1 < 0xF5) |
| 2047 | { |
| 2048 | if (frm_end-frm_nxt < 4) |
| 2049 | return codecvt_base::partial; |
| 2050 | uint8_t c2 = frm_nxt[1]; |
| 2051 | uint8_t c3 = frm_nxt[2]; |
| 2052 | uint8_t c4 = frm_nxt[3]; |
| 2053 | switch (c1) |
| 2054 | { |
| 2055 | case 0xF0: |
| 2056 | if (!(0x90 <= c2 && c2 <= 0xBF)) |
| 2057 | return codecvt_base::error; |
| 2058 | break; |
| 2059 | case 0xF4: |
| 2060 | if ((c2 & 0xF0) != 0x80) |
| 2061 | return codecvt_base::error; |
| 2062 | break; |
| 2063 | default: |
| 2064 | if ((c2 & 0xC0) != 0x80) |
| 2065 | return codecvt_base::error; |
| 2066 | break; |
| 2067 | } |
| 2068 | if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80) |
| 2069 | return codecvt_base::error; |
| 2070 | if (to_end-to_nxt < 2) |
| 2071 | return codecvt_base::partial; |
Joerg Sonnenberger | 4c6acb5 | 2014-01-04 17:43:00 +0000 | [diff] [blame] | 2072 | if ((((c1 & 7UL) << 18) + |
| 2073 | ((c2 & 0x3FUL) << 12) + |
| 2074 | ((c3 & 0x3FUL) << 6) + (c4 & 0x3F)) > Maxcode) |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 2075 | return codecvt_base::error; |
| 2076 | *to_nxt = static_cast<uint32_t>( |
| 2077 | 0xD800 |
| 2078 | | (((((c1 & 0x07) << 2) | ((c2 & 0x30) >> 4)) - 1) << 6) |
| 2079 | | ((c2 & 0x0F) << 2) |
| 2080 | | ((c3 & 0x30) >> 4)); |
| 2081 | *++to_nxt = static_cast<uint32_t>( |
| 2082 | 0xDC00 |
| 2083 | | ((c3 & 0x0F) << 6) |
| 2084 | | (c4 & 0x3F)); |
| 2085 | frm_nxt += 4; |
| 2086 | } |
| 2087 | else |
| 2088 | { |
| 2089 | return codecvt_base::error; |
| 2090 | } |
| 2091 | } |
| 2092 | return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok; |
| 2093 | } |
| 2094 | |
| 2095 | static |
| 2096 | int |
| 2097 | utf8_to_utf16_length(const uint8_t* frm, const uint8_t* frm_end, |
| 2098 | size_t mx, unsigned long Maxcode = 0x10FFFF, |
| 2099 | codecvt_mode mode = codecvt_mode(0)) |
| 2100 | { |
| 2101 | const uint8_t* frm_nxt = frm; |
| 2102 | if (mode & consume_header) |
| 2103 | { |
| 2104 | if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB && |
| 2105 | frm_nxt[2] == 0xBF) |
| 2106 | frm_nxt += 3; |
| 2107 | } |
| 2108 | for (size_t nchar16_t = 0; frm_nxt < frm_end && nchar16_t < mx; ++nchar16_t) |
| 2109 | { |
| 2110 | uint8_t c1 = *frm_nxt; |
| 2111 | if (c1 > Maxcode) |
| 2112 | break; |
| 2113 | if (c1 < 0x80) |
| 2114 | { |
| 2115 | ++frm_nxt; |
| 2116 | } |
| 2117 | else if (c1 < 0xC2) |
| 2118 | { |
| 2119 | break; |
| 2120 | } |
| 2121 | else if (c1 < 0xE0) |
| 2122 | { |
| 2123 | if ((frm_end-frm_nxt < 2) || (frm_nxt[1] & 0xC0) != 0x80) |
| 2124 | break; |
| 2125 | uint16_t t = static_cast<uint16_t>(((c1 & 0x1F) << 6) | (frm_nxt[1] & 0x3F)); |
| 2126 | if (t > Maxcode) |
| 2127 | break; |
| 2128 | frm_nxt += 2; |
| 2129 | } |
| 2130 | else if (c1 < 0xF0) |
| 2131 | { |
| 2132 | if (frm_end-frm_nxt < 3) |
| 2133 | break; |
| 2134 | uint8_t c2 = frm_nxt[1]; |
| 2135 | uint8_t c3 = frm_nxt[2]; |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 2136 | switch (c1) |
| 2137 | { |
| 2138 | case 0xE0: |
| 2139 | if ((c2 & 0xE0) != 0xA0) |
| 2140 | return static_cast<int>(frm_nxt - frm); |
| 2141 | break; |
| 2142 | case 0xED: |
| 2143 | if ((c2 & 0xE0) != 0x80) |
| 2144 | return static_cast<int>(frm_nxt - frm); |
| 2145 | break; |
| 2146 | default: |
| 2147 | if ((c2 & 0xC0) != 0x80) |
| 2148 | return static_cast<int>(frm_nxt - frm); |
| 2149 | break; |
| 2150 | } |
| 2151 | if ((c3 & 0xC0) != 0x80) |
| 2152 | break; |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2153 | if ((((c1 & 0x0Fu) << 12) | ((c2 & 0x3Fu) << 6) | (c3 & 0x3Fu)) > Maxcode) |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 2154 | break; |
| 2155 | frm_nxt += 3; |
| 2156 | } |
| 2157 | else if (c1 < 0xF5) |
| 2158 | { |
| 2159 | if (frm_end-frm_nxt < 4 || mx-nchar16_t < 2) |
| 2160 | break; |
| 2161 | uint8_t c2 = frm_nxt[1]; |
| 2162 | uint8_t c3 = frm_nxt[2]; |
| 2163 | uint8_t c4 = frm_nxt[3]; |
| 2164 | switch (c1) |
| 2165 | { |
| 2166 | case 0xF0: |
| 2167 | if (!(0x90 <= c2 && c2 <= 0xBF)) |
| 2168 | return static_cast<int>(frm_nxt - frm); |
| 2169 | break; |
| 2170 | case 0xF4: |
| 2171 | if ((c2 & 0xF0) != 0x80) |
| 2172 | return static_cast<int>(frm_nxt - frm); |
| 2173 | break; |
| 2174 | default: |
| 2175 | if ((c2 & 0xC0) != 0x80) |
| 2176 | return static_cast<int>(frm_nxt - frm); |
| 2177 | break; |
| 2178 | } |
| 2179 | if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80) |
| 2180 | break; |
Joerg Sonnenberger | 4c6acb5 | 2014-01-04 17:43:00 +0000 | [diff] [blame] | 2181 | if ((((c1 & 7UL) << 18) + |
| 2182 | ((c2 & 0x3FUL) << 12) + |
| 2183 | ((c3 & 0x3FUL) << 6) + (c4 & 0x3F)) > Maxcode) |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 2184 | break; |
| 2185 | ++nchar16_t; |
| 2186 | frm_nxt += 4; |
| 2187 | } |
| 2188 | else |
| 2189 | { |
| 2190 | break; |
| 2191 | } |
| 2192 | } |
| 2193 | return static_cast<int>(frm_nxt - frm); |
| 2194 | } |
| 2195 | |
| 2196 | static |
| 2197 | codecvt_base::result |
| 2198 | ucs4_to_utf8(const uint32_t* frm, const uint32_t* frm_end, const uint32_t*& frm_nxt, |
| 2199 | uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt, |
| 2200 | unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0)) |
| 2201 | { |
| 2202 | frm_nxt = frm; |
| 2203 | to_nxt = to; |
| 2204 | if (mode & generate_header) |
| 2205 | { |
| 2206 | if (to_end-to_nxt < 3) |
| 2207 | return codecvt_base::partial; |
| 2208 | *to_nxt++ = static_cast<uint8_t>(0xEF); |
| 2209 | *to_nxt++ = static_cast<uint8_t>(0xBB); |
| 2210 | *to_nxt++ = static_cast<uint8_t>(0xBF); |
| 2211 | } |
| 2212 | for (; frm_nxt < frm_end; ++frm_nxt) |
| 2213 | { |
| 2214 | uint32_t wc = *frm_nxt; |
| 2215 | if ((wc & 0xFFFFF800) == 0x00D800 || wc > Maxcode) |
| 2216 | return codecvt_base::error; |
| 2217 | if (wc < 0x000080) |
| 2218 | { |
| 2219 | if (to_end-to_nxt < 1) |
| 2220 | return codecvt_base::partial; |
| 2221 | *to_nxt++ = static_cast<uint8_t>(wc); |
| 2222 | } |
| 2223 | else if (wc < 0x000800) |
| 2224 | { |
| 2225 | if (to_end-to_nxt < 2) |
| 2226 | return codecvt_base::partial; |
| 2227 | *to_nxt++ = static_cast<uint8_t>(0xC0 | (wc >> 6)); |
| 2228 | *to_nxt++ = static_cast<uint8_t>(0x80 | (wc & 0x03F)); |
| 2229 | } |
| 2230 | else if (wc < 0x010000) |
| 2231 | { |
| 2232 | if (to_end-to_nxt < 3) |
| 2233 | return codecvt_base::partial; |
| 2234 | *to_nxt++ = static_cast<uint8_t>(0xE0 | (wc >> 12)); |
| 2235 | *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc & 0x0FC0) >> 6)); |
| 2236 | *to_nxt++ = static_cast<uint8_t>(0x80 | (wc & 0x003F)); |
| 2237 | } |
| 2238 | else // if (wc < 0x110000) |
| 2239 | { |
| 2240 | if (to_end-to_nxt < 4) |
| 2241 | return codecvt_base::partial; |
| 2242 | *to_nxt++ = static_cast<uint8_t>(0xF0 | (wc >> 18)); |
| 2243 | *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc & 0x03F000) >> 12)); |
| 2244 | *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc & 0x000FC0) >> 6)); |
| 2245 | *to_nxt++ = static_cast<uint8_t>(0x80 | (wc & 0x00003F)); |
| 2246 | } |
| 2247 | } |
| 2248 | return codecvt_base::ok; |
| 2249 | } |
| 2250 | |
| 2251 | static |
| 2252 | codecvt_base::result |
| 2253 | utf8_to_ucs4(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt, |
| 2254 | uint32_t* to, uint32_t* to_end, uint32_t*& to_nxt, |
| 2255 | unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0)) |
| 2256 | { |
| 2257 | frm_nxt = frm; |
| 2258 | to_nxt = to; |
| 2259 | if (mode & consume_header) |
| 2260 | { |
| 2261 | if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB && |
| 2262 | frm_nxt[2] == 0xBF) |
| 2263 | frm_nxt += 3; |
| 2264 | } |
| 2265 | for (; frm_nxt < frm_end && to_nxt < to_end; ++to_nxt) |
| 2266 | { |
| 2267 | uint8_t c1 = static_cast<uint8_t>(*frm_nxt); |
| 2268 | if (c1 < 0x80) |
| 2269 | { |
| 2270 | if (c1 > Maxcode) |
| 2271 | return codecvt_base::error; |
| 2272 | *to_nxt = static_cast<uint32_t>(c1); |
| 2273 | ++frm_nxt; |
| 2274 | } |
| 2275 | else if (c1 < 0xC2) |
| 2276 | { |
| 2277 | return codecvt_base::error; |
| 2278 | } |
| 2279 | else if (c1 < 0xE0) |
| 2280 | { |
| 2281 | if (frm_end-frm_nxt < 2) |
| 2282 | return codecvt_base::partial; |
| 2283 | uint8_t c2 = frm_nxt[1]; |
| 2284 | if ((c2 & 0xC0) != 0x80) |
| 2285 | return codecvt_base::error; |
| 2286 | uint32_t t = static_cast<uint32_t>(((c1 & 0x1F) << 6) |
| 2287 | | (c2 & 0x3F)); |
| 2288 | if (t > Maxcode) |
| 2289 | return codecvt_base::error; |
| 2290 | *to_nxt = t; |
| 2291 | frm_nxt += 2; |
| 2292 | } |
| 2293 | else if (c1 < 0xF0) |
| 2294 | { |
| 2295 | if (frm_end-frm_nxt < 3) |
| 2296 | return codecvt_base::partial; |
| 2297 | uint8_t c2 = frm_nxt[1]; |
| 2298 | uint8_t c3 = frm_nxt[2]; |
| 2299 | switch (c1) |
| 2300 | { |
| 2301 | case 0xE0: |
| 2302 | if ((c2 & 0xE0) != 0xA0) |
| 2303 | return codecvt_base::error; |
| 2304 | break; |
| 2305 | case 0xED: |
| 2306 | if ((c2 & 0xE0) != 0x80) |
| 2307 | return codecvt_base::error; |
| 2308 | break; |
| 2309 | default: |
| 2310 | if ((c2 & 0xC0) != 0x80) |
| 2311 | return codecvt_base::error; |
| 2312 | break; |
| 2313 | } |
| 2314 | if ((c3 & 0xC0) != 0x80) |
| 2315 | return codecvt_base::error; |
| 2316 | uint32_t t = static_cast<uint32_t>(((c1 & 0x0F) << 12) |
| 2317 | | ((c2 & 0x3F) << 6) |
| 2318 | | (c3 & 0x3F)); |
| 2319 | if (t > Maxcode) |
| 2320 | return codecvt_base::error; |
| 2321 | *to_nxt = t; |
| 2322 | frm_nxt += 3; |
| 2323 | } |
| 2324 | else if (c1 < 0xF5) |
| 2325 | { |
| 2326 | if (frm_end-frm_nxt < 4) |
| 2327 | return codecvt_base::partial; |
| 2328 | uint8_t c2 = frm_nxt[1]; |
| 2329 | uint8_t c3 = frm_nxt[2]; |
| 2330 | uint8_t c4 = frm_nxt[3]; |
| 2331 | switch (c1) |
| 2332 | { |
| 2333 | case 0xF0: |
| 2334 | if (!(0x90 <= c2 && c2 <= 0xBF)) |
| 2335 | return codecvt_base::error; |
| 2336 | break; |
| 2337 | case 0xF4: |
| 2338 | if ((c2 & 0xF0) != 0x80) |
| 2339 | return codecvt_base::error; |
| 2340 | break; |
| 2341 | default: |
| 2342 | if ((c2 & 0xC0) != 0x80) |
| 2343 | return codecvt_base::error; |
| 2344 | break; |
| 2345 | } |
| 2346 | if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80) |
| 2347 | return codecvt_base::error; |
| 2348 | uint32_t t = static_cast<uint32_t>(((c1 & 0x07) << 18) |
| 2349 | | ((c2 & 0x3F) << 12) |
| 2350 | | ((c3 & 0x3F) << 6) |
| 2351 | | (c4 & 0x3F)); |
| 2352 | if (t > Maxcode) |
| 2353 | return codecvt_base::error; |
| 2354 | *to_nxt = t; |
| 2355 | frm_nxt += 4; |
| 2356 | } |
| 2357 | else |
| 2358 | { |
| 2359 | return codecvt_base::error; |
| 2360 | } |
| 2361 | } |
| 2362 | return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok; |
| 2363 | } |
| 2364 | |
| 2365 | static |
| 2366 | int |
| 2367 | utf8_to_ucs4_length(const uint8_t* frm, const uint8_t* frm_end, |
| 2368 | size_t mx, unsigned long Maxcode = 0x10FFFF, |
| 2369 | codecvt_mode mode = codecvt_mode(0)) |
| 2370 | { |
| 2371 | const uint8_t* frm_nxt = frm; |
| 2372 | if (mode & consume_header) |
| 2373 | { |
| 2374 | if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB && |
| 2375 | frm_nxt[2] == 0xBF) |
| 2376 | frm_nxt += 3; |
| 2377 | } |
| 2378 | for (size_t nchar32_t = 0; frm_nxt < frm_end && nchar32_t < mx; ++nchar32_t) |
| 2379 | { |
| 2380 | uint8_t c1 = static_cast<uint8_t>(*frm_nxt); |
| 2381 | if (c1 < 0x80) |
| 2382 | { |
| 2383 | if (c1 > Maxcode) |
| 2384 | break; |
| 2385 | ++frm_nxt; |
| 2386 | } |
| 2387 | else if (c1 < 0xC2) |
| 2388 | { |
| 2389 | break; |
| 2390 | } |
| 2391 | else if (c1 < 0xE0) |
| 2392 | { |
| 2393 | if ((frm_end-frm_nxt < 2) || ((frm_nxt[1] & 0xC0) != 0x80)) |
| 2394 | break; |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2395 | if ((((c1 & 0x1Fu) << 6) | (frm_nxt[1] & 0x3Fu)) > Maxcode) |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 2396 | break; |
| 2397 | frm_nxt += 2; |
| 2398 | } |
| 2399 | else if (c1 < 0xF0) |
| 2400 | { |
| 2401 | if (frm_end-frm_nxt < 3) |
| 2402 | break; |
| 2403 | uint8_t c2 = frm_nxt[1]; |
| 2404 | uint8_t c3 = frm_nxt[2]; |
| 2405 | switch (c1) |
| 2406 | { |
| 2407 | case 0xE0: |
| 2408 | if ((c2 & 0xE0) != 0xA0) |
| 2409 | return static_cast<int>(frm_nxt - frm); |
| 2410 | break; |
| 2411 | case 0xED: |
| 2412 | if ((c2 & 0xE0) != 0x80) |
| 2413 | return static_cast<int>(frm_nxt - frm); |
| 2414 | break; |
| 2415 | default: |
| 2416 | if ((c2 & 0xC0) != 0x80) |
| 2417 | return static_cast<int>(frm_nxt - frm); |
| 2418 | break; |
| 2419 | } |
| 2420 | if ((c3 & 0xC0) != 0x80) |
| 2421 | break; |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2422 | if ((((c1 & 0x0Fu) << 12) | ((c2 & 0x3Fu) << 6) | (c3 & 0x3Fu)) > Maxcode) |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 2423 | break; |
| 2424 | frm_nxt += 3; |
| 2425 | } |
| 2426 | else if (c1 < 0xF5) |
| 2427 | { |
| 2428 | if (frm_end-frm_nxt < 4) |
| 2429 | break; |
| 2430 | uint8_t c2 = frm_nxt[1]; |
| 2431 | uint8_t c3 = frm_nxt[2]; |
| 2432 | uint8_t c4 = frm_nxt[3]; |
| 2433 | switch (c1) |
| 2434 | { |
| 2435 | case 0xF0: |
| 2436 | if (!(0x90 <= c2 && c2 <= 0xBF)) |
| 2437 | return static_cast<int>(frm_nxt - frm); |
| 2438 | break; |
| 2439 | case 0xF4: |
| 2440 | if ((c2 & 0xF0) != 0x80) |
| 2441 | return static_cast<int>(frm_nxt - frm); |
| 2442 | break; |
| 2443 | default: |
| 2444 | if ((c2 & 0xC0) != 0x80) |
| 2445 | return static_cast<int>(frm_nxt - frm); |
| 2446 | break; |
| 2447 | } |
| 2448 | if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80) |
| 2449 | break; |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2450 | if ((((c1 & 0x07u) << 18) | ((c2 & 0x3Fu) << 12) | |
| 2451 | ((c3 & 0x3Fu) << 6) | (c4 & 0x3Fu)) > Maxcode) |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 2452 | break; |
| 2453 | frm_nxt += 4; |
| 2454 | } |
| 2455 | else |
| 2456 | { |
| 2457 | break; |
| 2458 | } |
| 2459 | } |
| 2460 | return static_cast<int>(frm_nxt - frm); |
| 2461 | } |
| 2462 | |
| 2463 | static |
| 2464 | codecvt_base::result |
| 2465 | ucs2_to_utf8(const uint16_t* frm, const uint16_t* frm_end, const uint16_t*& frm_nxt, |
| 2466 | uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt, |
| 2467 | unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0)) |
| 2468 | { |
| 2469 | frm_nxt = frm; |
| 2470 | to_nxt = to; |
| 2471 | if (mode & generate_header) |
| 2472 | { |
| 2473 | if (to_end-to_nxt < 3) |
| 2474 | return codecvt_base::partial; |
| 2475 | *to_nxt++ = static_cast<uint8_t>(0xEF); |
| 2476 | *to_nxt++ = static_cast<uint8_t>(0xBB); |
| 2477 | *to_nxt++ = static_cast<uint8_t>(0xBF); |
| 2478 | } |
| 2479 | for (; frm_nxt < frm_end; ++frm_nxt) |
| 2480 | { |
| 2481 | uint16_t wc = *frm_nxt; |
| 2482 | if ((wc & 0xF800) == 0xD800 || wc > Maxcode) |
| 2483 | return codecvt_base::error; |
| 2484 | if (wc < 0x0080) |
| 2485 | { |
| 2486 | if (to_end-to_nxt < 1) |
| 2487 | return codecvt_base::partial; |
| 2488 | *to_nxt++ = static_cast<uint8_t>(wc); |
| 2489 | } |
| 2490 | else if (wc < 0x0800) |
| 2491 | { |
| 2492 | if (to_end-to_nxt < 2) |
| 2493 | return codecvt_base::partial; |
| 2494 | *to_nxt++ = static_cast<uint8_t>(0xC0 | (wc >> 6)); |
| 2495 | *to_nxt++ = static_cast<uint8_t>(0x80 | (wc & 0x03F)); |
| 2496 | } |
| 2497 | else // if (wc <= 0xFFFF) |
| 2498 | { |
| 2499 | if (to_end-to_nxt < 3) |
| 2500 | return codecvt_base::partial; |
| 2501 | *to_nxt++ = static_cast<uint8_t>(0xE0 | (wc >> 12)); |
| 2502 | *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc & 0x0FC0) >> 6)); |
| 2503 | *to_nxt++ = static_cast<uint8_t>(0x80 | (wc & 0x003F)); |
| 2504 | } |
| 2505 | } |
| 2506 | return codecvt_base::ok; |
| 2507 | } |
| 2508 | |
| 2509 | static |
| 2510 | codecvt_base::result |
| 2511 | utf8_to_ucs2(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt, |
| 2512 | uint16_t* to, uint16_t* to_end, uint16_t*& to_nxt, |
| 2513 | unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0)) |
| 2514 | { |
| 2515 | frm_nxt = frm; |
| 2516 | to_nxt = to; |
| 2517 | if (mode & consume_header) |
| 2518 | { |
| 2519 | if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB && |
| 2520 | frm_nxt[2] == 0xBF) |
| 2521 | frm_nxt += 3; |
| 2522 | } |
| 2523 | for (; frm_nxt < frm_end && to_nxt < to_end; ++to_nxt) |
| 2524 | { |
| 2525 | uint8_t c1 = static_cast<uint8_t>(*frm_nxt); |
| 2526 | if (c1 < 0x80) |
| 2527 | { |
| 2528 | if (c1 > Maxcode) |
| 2529 | return codecvt_base::error; |
| 2530 | *to_nxt = static_cast<uint16_t>(c1); |
| 2531 | ++frm_nxt; |
| 2532 | } |
| 2533 | else if (c1 < 0xC2) |
| 2534 | { |
| 2535 | return codecvt_base::error; |
| 2536 | } |
| 2537 | else if (c1 < 0xE0) |
| 2538 | { |
| 2539 | if (frm_end-frm_nxt < 2) |
| 2540 | return codecvt_base::partial; |
| 2541 | uint8_t c2 = frm_nxt[1]; |
| 2542 | if ((c2 & 0xC0) != 0x80) |
| 2543 | return codecvt_base::error; |
| 2544 | uint16_t t = static_cast<uint16_t>(((c1 & 0x1F) << 6) |
| 2545 | | (c2 & 0x3F)); |
| 2546 | if (t > Maxcode) |
| 2547 | return codecvt_base::error; |
| 2548 | *to_nxt = t; |
| 2549 | frm_nxt += 2; |
| 2550 | } |
| 2551 | else if (c1 < 0xF0) |
| 2552 | { |
| 2553 | if (frm_end-frm_nxt < 3) |
| 2554 | return codecvt_base::partial; |
| 2555 | uint8_t c2 = frm_nxt[1]; |
| 2556 | uint8_t c3 = frm_nxt[2]; |
| 2557 | switch (c1) |
| 2558 | { |
| 2559 | case 0xE0: |
| 2560 | if ((c2 & 0xE0) != 0xA0) |
| 2561 | return codecvt_base::error; |
| 2562 | break; |
| 2563 | case 0xED: |
| 2564 | if ((c2 & 0xE0) != 0x80) |
| 2565 | return codecvt_base::error; |
| 2566 | break; |
| 2567 | default: |
| 2568 | if ((c2 & 0xC0) != 0x80) |
| 2569 | return codecvt_base::error; |
| 2570 | break; |
| 2571 | } |
| 2572 | if ((c3 & 0xC0) != 0x80) |
| 2573 | return codecvt_base::error; |
| 2574 | uint16_t t = static_cast<uint16_t>(((c1 & 0x0F) << 12) |
| 2575 | | ((c2 & 0x3F) << 6) |
| 2576 | | (c3 & 0x3F)); |
| 2577 | if (t > Maxcode) |
| 2578 | return codecvt_base::error; |
| 2579 | *to_nxt = t; |
| 2580 | frm_nxt += 3; |
| 2581 | } |
| 2582 | else |
| 2583 | { |
| 2584 | return codecvt_base::error; |
| 2585 | } |
| 2586 | } |
| 2587 | return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok; |
| 2588 | } |
| 2589 | |
| 2590 | static |
| 2591 | int |
| 2592 | utf8_to_ucs2_length(const uint8_t* frm, const uint8_t* frm_end, |
| 2593 | size_t mx, unsigned long Maxcode = 0x10FFFF, |
| 2594 | codecvt_mode mode = codecvt_mode(0)) |
| 2595 | { |
| 2596 | const uint8_t* frm_nxt = frm; |
| 2597 | if (mode & consume_header) |
| 2598 | { |
| 2599 | if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB && |
| 2600 | frm_nxt[2] == 0xBF) |
| 2601 | frm_nxt += 3; |
| 2602 | } |
| 2603 | for (size_t nchar32_t = 0; frm_nxt < frm_end && nchar32_t < mx; ++nchar32_t) |
| 2604 | { |
| 2605 | uint8_t c1 = static_cast<uint8_t>(*frm_nxt); |
| 2606 | if (c1 < 0x80) |
| 2607 | { |
| 2608 | if (c1 > Maxcode) |
| 2609 | break; |
| 2610 | ++frm_nxt; |
| 2611 | } |
| 2612 | else if (c1 < 0xC2) |
| 2613 | { |
| 2614 | break; |
| 2615 | } |
| 2616 | else if (c1 < 0xE0) |
| 2617 | { |
| 2618 | if ((frm_end-frm_nxt < 2) || ((frm_nxt[1] & 0xC0) != 0x80)) |
| 2619 | break; |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2620 | if ((((c1 & 0x1Fu) << 6) | (frm_nxt[1] & 0x3Fu)) > Maxcode) |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 2621 | break; |
| 2622 | frm_nxt += 2; |
| 2623 | } |
| 2624 | else if (c1 < 0xF0) |
| 2625 | { |
| 2626 | if (frm_end-frm_nxt < 3) |
| 2627 | break; |
| 2628 | uint8_t c2 = frm_nxt[1]; |
| 2629 | uint8_t c3 = frm_nxt[2]; |
| 2630 | switch (c1) |
| 2631 | { |
| 2632 | case 0xE0: |
| 2633 | if ((c2 & 0xE0) != 0xA0) |
| 2634 | return static_cast<int>(frm_nxt - frm); |
| 2635 | break; |
| 2636 | case 0xED: |
| 2637 | if ((c2 & 0xE0) != 0x80) |
| 2638 | return static_cast<int>(frm_nxt - frm); |
| 2639 | break; |
| 2640 | default: |
| 2641 | if ((c2 & 0xC0) != 0x80) |
| 2642 | return static_cast<int>(frm_nxt - frm); |
| 2643 | break; |
| 2644 | } |
| 2645 | if ((c3 & 0xC0) != 0x80) |
| 2646 | break; |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2647 | if ((((c1 & 0x0Fu) << 12) | ((c2 & 0x3Fu) << 6) | (c3 & 0x3Fu)) > Maxcode) |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 2648 | break; |
| 2649 | frm_nxt += 3; |
| 2650 | } |
| 2651 | else |
| 2652 | { |
| 2653 | break; |
| 2654 | } |
| 2655 | } |
| 2656 | return static_cast<int>(frm_nxt - frm); |
| 2657 | } |
| 2658 | |
| 2659 | static |
| 2660 | codecvt_base::result |
| 2661 | ucs4_to_utf16be(const uint32_t* frm, const uint32_t* frm_end, const uint32_t*& frm_nxt, |
| 2662 | uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt, |
| 2663 | unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0)) |
| 2664 | { |
| 2665 | frm_nxt = frm; |
| 2666 | to_nxt = to; |
| 2667 | if (mode & generate_header) |
| 2668 | { |
| 2669 | if (to_end-to_nxt < 2) |
| 2670 | return codecvt_base::partial; |
| 2671 | *to_nxt++ = static_cast<uint8_t>(0xFE); |
| 2672 | *to_nxt++ = static_cast<uint8_t>(0xFF); |
| 2673 | } |
| 2674 | for (; frm_nxt < frm_end; ++frm_nxt) |
| 2675 | { |
| 2676 | uint32_t wc = *frm_nxt; |
| 2677 | if ((wc & 0xFFFFF800) == 0x00D800 || wc > Maxcode) |
| 2678 | return codecvt_base::error; |
| 2679 | if (wc < 0x010000) |
| 2680 | { |
| 2681 | if (to_end-to_nxt < 2) |
| 2682 | return codecvt_base::partial; |
| 2683 | *to_nxt++ = static_cast<uint8_t>(wc >> 8); |
| 2684 | *to_nxt++ = static_cast<uint8_t>(wc); |
| 2685 | } |
| 2686 | else |
| 2687 | { |
| 2688 | if (to_end-to_nxt < 4) |
| 2689 | return codecvt_base::partial; |
| 2690 | uint16_t t = static_cast<uint16_t>( |
| 2691 | 0xD800 |
| 2692 | | ((((wc & 0x1F0000) >> 16) - 1) << 6) |
| 2693 | | ((wc & 0x00FC00) >> 10)); |
| 2694 | *to_nxt++ = static_cast<uint8_t>(t >> 8); |
| 2695 | *to_nxt++ = static_cast<uint8_t>(t); |
| 2696 | t = static_cast<uint16_t>(0xDC00 | (wc & 0x03FF)); |
| 2697 | *to_nxt++ = static_cast<uint8_t>(t >> 8); |
| 2698 | *to_nxt++ = static_cast<uint8_t>(t); |
| 2699 | } |
| 2700 | } |
| 2701 | return codecvt_base::ok; |
| 2702 | } |
| 2703 | |
| 2704 | static |
| 2705 | codecvt_base::result |
| 2706 | utf16be_to_ucs4(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt, |
| 2707 | uint32_t* to, uint32_t* to_end, uint32_t*& to_nxt, |
| 2708 | unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0)) |
| 2709 | { |
| 2710 | frm_nxt = frm; |
| 2711 | to_nxt = to; |
| 2712 | if (mode & consume_header) |
| 2713 | { |
| 2714 | if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFE && frm_nxt[1] == 0xFF) |
| 2715 | frm_nxt += 2; |
| 2716 | } |
| 2717 | for (; frm_nxt < frm_end - 1 && to_nxt < to_end; ++to_nxt) |
| 2718 | { |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2719 | uint16_t c1 = static_cast<uint16_t>(frm_nxt[0] << 8 | frm_nxt[1]); |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 2720 | if ((c1 & 0xFC00) == 0xDC00) |
| 2721 | return codecvt_base::error; |
| 2722 | if ((c1 & 0xFC00) != 0xD800) |
| 2723 | { |
| 2724 | if (c1 > Maxcode) |
| 2725 | return codecvt_base::error; |
| 2726 | *to_nxt = static_cast<uint32_t>(c1); |
| 2727 | frm_nxt += 2; |
| 2728 | } |
| 2729 | else |
| 2730 | { |
| 2731 | if (frm_end-frm_nxt < 4) |
| 2732 | return codecvt_base::partial; |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2733 | uint16_t c2 = static_cast<uint16_t>(frm_nxt[2] << 8 | frm_nxt[3]); |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 2734 | if ((c2 & 0xFC00) != 0xDC00) |
| 2735 | return codecvt_base::error; |
| 2736 | uint32_t t = static_cast<uint32_t>( |
| 2737 | ((((c1 & 0x03C0) >> 6) + 1) << 16) |
| 2738 | | ((c1 & 0x003F) << 10) |
| 2739 | | (c2 & 0x03FF)); |
| 2740 | if (t > Maxcode) |
| 2741 | return codecvt_base::error; |
| 2742 | *to_nxt = t; |
| 2743 | frm_nxt += 4; |
| 2744 | } |
| 2745 | } |
| 2746 | return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok; |
| 2747 | } |
| 2748 | |
| 2749 | static |
| 2750 | int |
| 2751 | utf16be_to_ucs4_length(const uint8_t* frm, const uint8_t* frm_end, |
| 2752 | size_t mx, unsigned long Maxcode = 0x10FFFF, |
| 2753 | codecvt_mode mode = codecvt_mode(0)) |
| 2754 | { |
| 2755 | const uint8_t* frm_nxt = frm; |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 2756 | if (mode & consume_header) |
| 2757 | { |
| 2758 | if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFE && frm_nxt[1] == 0xFF) |
| 2759 | frm_nxt += 2; |
| 2760 | } |
| 2761 | for (size_t nchar32_t = 0; frm_nxt < frm_end - 1 && nchar32_t < mx; ++nchar32_t) |
| 2762 | { |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2763 | uint16_t c1 = static_cast<uint16_t>(frm_nxt[0] << 8 | frm_nxt[1]); |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 2764 | if ((c1 & 0xFC00) == 0xDC00) |
| 2765 | break; |
| 2766 | if ((c1 & 0xFC00) != 0xD800) |
| 2767 | { |
| 2768 | if (c1 > Maxcode) |
| 2769 | break; |
| 2770 | frm_nxt += 2; |
| 2771 | } |
| 2772 | else |
| 2773 | { |
| 2774 | if (frm_end-frm_nxt < 4) |
| 2775 | break; |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2776 | uint16_t c2 = static_cast<uint16_t>(frm_nxt[2] << 8 | frm_nxt[3]); |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 2777 | if ((c2 & 0xFC00) != 0xDC00) |
| 2778 | break; |
| 2779 | uint32_t t = static_cast<uint32_t>( |
| 2780 | ((((c1 & 0x03C0) >> 6) + 1) << 16) |
| 2781 | | ((c1 & 0x003F) << 10) |
| 2782 | | (c2 & 0x03FF)); |
| 2783 | if (t > Maxcode) |
| 2784 | break; |
| 2785 | frm_nxt += 4; |
| 2786 | } |
| 2787 | } |
| 2788 | return static_cast<int>(frm_nxt - frm); |
| 2789 | } |
| 2790 | |
| 2791 | static |
| 2792 | codecvt_base::result |
| 2793 | ucs4_to_utf16le(const uint32_t* frm, const uint32_t* frm_end, const uint32_t*& frm_nxt, |
| 2794 | uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt, |
| 2795 | unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0)) |
| 2796 | { |
| 2797 | frm_nxt = frm; |
| 2798 | to_nxt = to; |
| 2799 | if (mode & generate_header) |
| 2800 | { |
| 2801 | if (to_end-to_nxt < 2) |
| 2802 | return codecvt_base::partial; |
| 2803 | *to_nxt++ = static_cast<uint8_t>(0xFF); |
| 2804 | *to_nxt++ = static_cast<uint8_t>(0xFE); |
| 2805 | } |
| 2806 | for (; frm_nxt < frm_end; ++frm_nxt) |
| 2807 | { |
| 2808 | uint32_t wc = *frm_nxt; |
| 2809 | if ((wc & 0xFFFFF800) == 0x00D800 || wc > Maxcode) |
| 2810 | return codecvt_base::error; |
| 2811 | if (wc < 0x010000) |
| 2812 | { |
| 2813 | if (to_end-to_nxt < 2) |
| 2814 | return codecvt_base::partial; |
| 2815 | *to_nxt++ = static_cast<uint8_t>(wc); |
| 2816 | *to_nxt++ = static_cast<uint8_t>(wc >> 8); |
| 2817 | } |
| 2818 | else |
| 2819 | { |
| 2820 | if (to_end-to_nxt < 4) |
| 2821 | return codecvt_base::partial; |
| 2822 | uint16_t t = static_cast<uint16_t>( |
| 2823 | 0xD800 |
| 2824 | | ((((wc & 0x1F0000) >> 16) - 1) << 6) |
| 2825 | | ((wc & 0x00FC00) >> 10)); |
| 2826 | *to_nxt++ = static_cast<uint8_t>(t); |
| 2827 | *to_nxt++ = static_cast<uint8_t>(t >> 8); |
| 2828 | t = static_cast<uint16_t>(0xDC00 | (wc & 0x03FF)); |
| 2829 | *to_nxt++ = static_cast<uint8_t>(t); |
| 2830 | *to_nxt++ = static_cast<uint8_t>(t >> 8); |
| 2831 | } |
| 2832 | } |
| 2833 | return codecvt_base::ok; |
| 2834 | } |
| 2835 | |
| 2836 | static |
| 2837 | codecvt_base::result |
| 2838 | utf16le_to_ucs4(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt, |
| 2839 | uint32_t* to, uint32_t* to_end, uint32_t*& to_nxt, |
| 2840 | unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0)) |
| 2841 | { |
| 2842 | frm_nxt = frm; |
| 2843 | to_nxt = to; |
| 2844 | if (mode & consume_header) |
| 2845 | { |
| 2846 | if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFF && frm_nxt[1] == 0xFE) |
| 2847 | frm_nxt += 2; |
| 2848 | } |
| 2849 | for (; frm_nxt < frm_end - 1 && to_nxt < to_end; ++to_nxt) |
| 2850 | { |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2851 | uint16_t c1 = static_cast<uint16_t>(frm_nxt[1] << 8 | frm_nxt[0]); |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 2852 | if ((c1 & 0xFC00) == 0xDC00) |
| 2853 | return codecvt_base::error; |
| 2854 | if ((c1 & 0xFC00) != 0xD800) |
| 2855 | { |
| 2856 | if (c1 > Maxcode) |
| 2857 | return codecvt_base::error; |
| 2858 | *to_nxt = static_cast<uint32_t>(c1); |
| 2859 | frm_nxt += 2; |
| 2860 | } |
| 2861 | else |
| 2862 | { |
| 2863 | if (frm_end-frm_nxt < 4) |
| 2864 | return codecvt_base::partial; |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2865 | uint16_t c2 = static_cast<uint16_t>(frm_nxt[3] << 8 | frm_nxt[2]); |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 2866 | if ((c2 & 0xFC00) != 0xDC00) |
| 2867 | return codecvt_base::error; |
| 2868 | uint32_t t = static_cast<uint32_t>( |
| 2869 | ((((c1 & 0x03C0) >> 6) + 1) << 16) |
| 2870 | | ((c1 & 0x003F) << 10) |
| 2871 | | (c2 & 0x03FF)); |
| 2872 | if (t > Maxcode) |
| 2873 | return codecvt_base::error; |
| 2874 | *to_nxt = t; |
| 2875 | frm_nxt += 4; |
| 2876 | } |
| 2877 | } |
| 2878 | return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok; |
| 2879 | } |
| 2880 | |
| 2881 | static |
| 2882 | int |
| 2883 | utf16le_to_ucs4_length(const uint8_t* frm, const uint8_t* frm_end, |
| 2884 | size_t mx, unsigned long Maxcode = 0x10FFFF, |
| 2885 | codecvt_mode mode = codecvt_mode(0)) |
| 2886 | { |
| 2887 | const uint8_t* frm_nxt = frm; |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 2888 | if (mode & consume_header) |
| 2889 | { |
| 2890 | if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFF && frm_nxt[1] == 0xFE) |
| 2891 | frm_nxt += 2; |
| 2892 | } |
| 2893 | for (size_t nchar32_t = 0; frm_nxt < frm_end - 1 && nchar32_t < mx; ++nchar32_t) |
| 2894 | { |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2895 | uint16_t c1 = static_cast<uint16_t>(frm_nxt[1] << 8 | frm_nxt[0]); |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 2896 | if ((c1 & 0xFC00) == 0xDC00) |
| 2897 | break; |
| 2898 | if ((c1 & 0xFC00) != 0xD800) |
| 2899 | { |
| 2900 | if (c1 > Maxcode) |
| 2901 | break; |
| 2902 | frm_nxt += 2; |
| 2903 | } |
| 2904 | else |
| 2905 | { |
| 2906 | if (frm_end-frm_nxt < 4) |
| 2907 | break; |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2908 | uint16_t c2 = static_cast<uint16_t>(frm_nxt[3] << 8 | frm_nxt[2]); |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 2909 | if ((c2 & 0xFC00) != 0xDC00) |
| 2910 | break; |
| 2911 | uint32_t t = static_cast<uint32_t>( |
| 2912 | ((((c1 & 0x03C0) >> 6) + 1) << 16) |
| 2913 | | ((c1 & 0x003F) << 10) |
| 2914 | | (c2 & 0x03FF)); |
| 2915 | if (t > Maxcode) |
| 2916 | break; |
| 2917 | frm_nxt += 4; |
| 2918 | } |
| 2919 | } |
| 2920 | return static_cast<int>(frm_nxt - frm); |
| 2921 | } |
| 2922 | |
| 2923 | static |
| 2924 | codecvt_base::result |
| 2925 | ucs2_to_utf16be(const uint16_t* frm, const uint16_t* frm_end, const uint16_t*& frm_nxt, |
| 2926 | uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt, |
| 2927 | unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0)) |
| 2928 | { |
| 2929 | frm_nxt = frm; |
| 2930 | to_nxt = to; |
| 2931 | if (mode & generate_header) |
| 2932 | { |
| 2933 | if (to_end-to_nxt < 2) |
| 2934 | return codecvt_base::partial; |
| 2935 | *to_nxt++ = static_cast<uint8_t>(0xFE); |
| 2936 | *to_nxt++ = static_cast<uint8_t>(0xFF); |
| 2937 | } |
| 2938 | for (; frm_nxt < frm_end; ++frm_nxt) |
| 2939 | { |
| 2940 | uint16_t wc = *frm_nxt; |
| 2941 | if ((wc & 0xF800) == 0xD800 || wc > Maxcode) |
| 2942 | return codecvt_base::error; |
| 2943 | if (to_end-to_nxt < 2) |
| 2944 | return codecvt_base::partial; |
| 2945 | *to_nxt++ = static_cast<uint8_t>(wc >> 8); |
| 2946 | *to_nxt++ = static_cast<uint8_t>(wc); |
| 2947 | } |
| 2948 | return codecvt_base::ok; |
| 2949 | } |
| 2950 | |
| 2951 | static |
| 2952 | codecvt_base::result |
| 2953 | utf16be_to_ucs2(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt, |
| 2954 | uint16_t* to, uint16_t* to_end, uint16_t*& to_nxt, |
| 2955 | unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0)) |
| 2956 | { |
| 2957 | frm_nxt = frm; |
| 2958 | to_nxt = to; |
| 2959 | if (mode & consume_header) |
| 2960 | { |
| 2961 | if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFE && frm_nxt[1] == 0xFF) |
| 2962 | frm_nxt += 2; |
| 2963 | } |
| 2964 | for (; frm_nxt < frm_end - 1 && to_nxt < to_end; ++to_nxt) |
| 2965 | { |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2966 | uint16_t c1 = static_cast<uint16_t>(frm_nxt[0] << 8 | frm_nxt[1]); |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 2967 | if ((c1 & 0xF800) == 0xD800 || c1 > Maxcode) |
| 2968 | return codecvt_base::error; |
| 2969 | *to_nxt = c1; |
| 2970 | frm_nxt += 2; |
| 2971 | } |
| 2972 | return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok; |
| 2973 | } |
| 2974 | |
| 2975 | static |
| 2976 | int |
| 2977 | utf16be_to_ucs2_length(const uint8_t* frm, const uint8_t* frm_end, |
| 2978 | size_t mx, unsigned long Maxcode = 0x10FFFF, |
| 2979 | codecvt_mode mode = codecvt_mode(0)) |
| 2980 | { |
| 2981 | const uint8_t* frm_nxt = frm; |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 2982 | if (mode & consume_header) |
| 2983 | { |
| 2984 | if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFE && frm_nxt[1] == 0xFF) |
| 2985 | frm_nxt += 2; |
| 2986 | } |
| 2987 | for (size_t nchar16_t = 0; frm_nxt < frm_end - 1 && nchar16_t < mx; ++nchar16_t) |
| 2988 | { |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2989 | uint16_t c1 = static_cast<uint16_t>(frm_nxt[0] << 8 | frm_nxt[1]); |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 2990 | if ((c1 & 0xF800) == 0xD800 || c1 > Maxcode) |
| 2991 | break; |
| 2992 | frm_nxt += 2; |
| 2993 | } |
| 2994 | return static_cast<int>(frm_nxt - frm); |
| 2995 | } |
| 2996 | |
| 2997 | static |
| 2998 | codecvt_base::result |
| 2999 | ucs2_to_utf16le(const uint16_t* frm, const uint16_t* frm_end, const uint16_t*& frm_nxt, |
| 3000 | uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt, |
| 3001 | unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0)) |
| 3002 | { |
| 3003 | frm_nxt = frm; |
| 3004 | to_nxt = to; |
| 3005 | if (mode & generate_header) |
| 3006 | { |
| 3007 | if (to_end-to_nxt < 2) |
| 3008 | return codecvt_base::partial; |
| 3009 | *to_nxt++ = static_cast<uint8_t>(0xFF); |
| 3010 | *to_nxt++ = static_cast<uint8_t>(0xFE); |
| 3011 | } |
| 3012 | for (; frm_nxt < frm_end; ++frm_nxt) |
| 3013 | { |
| 3014 | uint16_t wc = *frm_nxt; |
| 3015 | if ((wc & 0xF800) == 0xD800 || wc > Maxcode) |
| 3016 | return codecvt_base::error; |
| 3017 | if (to_end-to_nxt < 2) |
| 3018 | return codecvt_base::partial; |
| 3019 | *to_nxt++ = static_cast<uint8_t>(wc); |
| 3020 | *to_nxt++ = static_cast<uint8_t>(wc >> 8); |
| 3021 | } |
| 3022 | return codecvt_base::ok; |
| 3023 | } |
| 3024 | |
| 3025 | static |
| 3026 | codecvt_base::result |
| 3027 | utf16le_to_ucs2(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt, |
| 3028 | uint16_t* to, uint16_t* to_end, uint16_t*& to_nxt, |
| 3029 | unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0)) |
| 3030 | { |
| 3031 | frm_nxt = frm; |
| 3032 | to_nxt = to; |
| 3033 | if (mode & consume_header) |
| 3034 | { |
| 3035 | if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFF && frm_nxt[1] == 0xFE) |
| 3036 | frm_nxt += 2; |
| 3037 | } |
| 3038 | for (; frm_nxt < frm_end - 1 && to_nxt < to_end; ++to_nxt) |
| 3039 | { |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 3040 | uint16_t c1 = static_cast<uint16_t>(frm_nxt[1] << 8 | frm_nxt[0]); |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3041 | if ((c1 & 0xF800) == 0xD800 || c1 > Maxcode) |
| 3042 | return codecvt_base::error; |
| 3043 | *to_nxt = c1; |
| 3044 | frm_nxt += 2; |
| 3045 | } |
| 3046 | return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok; |
| 3047 | } |
| 3048 | |
| 3049 | static |
| 3050 | int |
| 3051 | utf16le_to_ucs2_length(const uint8_t* frm, const uint8_t* frm_end, |
| 3052 | size_t mx, unsigned long Maxcode = 0x10FFFF, |
| 3053 | codecvt_mode mode = codecvt_mode(0)) |
| 3054 | { |
| 3055 | const uint8_t* frm_nxt = frm; |
| 3056 | frm_nxt = frm; |
| 3057 | if (mode & consume_header) |
| 3058 | { |
| 3059 | if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFF && frm_nxt[1] == 0xFE) |
| 3060 | frm_nxt += 2; |
| 3061 | } |
| 3062 | for (size_t nchar16_t = 0; frm_nxt < frm_end - 1 && nchar16_t < mx; ++nchar16_t) |
| 3063 | { |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 3064 | uint16_t c1 = static_cast<uint16_t>(frm_nxt[1] << 8 | frm_nxt[0]); |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3065 | if ((c1 & 0xF800) == 0xD800 || c1 > Maxcode) |
| 3066 | break; |
| 3067 | frm_nxt += 2; |
| 3068 | } |
| 3069 | return static_cast<int>(frm_nxt - frm); |
| 3070 | } |
| 3071 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3072 | // template <> class codecvt<char16_t, char, mbstate_t> |
| 3073 | |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 3074 | locale::id codecvt<char16_t, char, mbstate_t>::id; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3075 | |
| 3076 | codecvt<char16_t, char, mbstate_t>::~codecvt() |
| 3077 | { |
| 3078 | } |
| 3079 | |
| 3080 | codecvt<char16_t, char, mbstate_t>::result |
| 3081 | codecvt<char16_t, char, mbstate_t>::do_out(state_type&, |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 3082 | const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3083 | extern_type* to, extern_type* to_end, extern_type*& to_nxt) const |
| 3084 | { |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3085 | const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm); |
| 3086 | const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end); |
| 3087 | const uint16_t* _frm_nxt = _frm; |
| 3088 | uint8_t* _to = reinterpret_cast<uint8_t*>(to); |
| 3089 | uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end); |
| 3090 | uint8_t* _to_nxt = _to; |
| 3091 | result r = utf16_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt); |
| 3092 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3093 | to_nxt = to + (_to_nxt - _to); |
| 3094 | return r; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3095 | } |
| 3096 | |
| 3097 | codecvt<char16_t, char, mbstate_t>::result |
| 3098 | codecvt<char16_t, char, mbstate_t>::do_in(state_type&, |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 3099 | const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3100 | intern_type* to, intern_type* to_end, intern_type*& to_nxt) const |
| 3101 | { |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3102 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3103 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3104 | const uint8_t* _frm_nxt = _frm; |
| 3105 | uint16_t* _to = reinterpret_cast<uint16_t*>(to); |
| 3106 | uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end); |
| 3107 | uint16_t* _to_nxt = _to; |
| 3108 | result r = utf8_to_utf16(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt); |
| 3109 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3110 | to_nxt = to + (_to_nxt - _to); |
| 3111 | return r; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3112 | } |
| 3113 | |
| 3114 | codecvt<char16_t, char, mbstate_t>::result |
| 3115 | codecvt<char16_t, char, mbstate_t>::do_unshift(state_type&, |
| 3116 | extern_type* to, extern_type*, extern_type*& to_nxt) const |
| 3117 | { |
| 3118 | to_nxt = to; |
| 3119 | return noconv; |
| 3120 | } |
| 3121 | |
| 3122 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3123 | codecvt<char16_t, char, mbstate_t>::do_encoding() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3124 | { |
| 3125 | return 0; |
| 3126 | } |
| 3127 | |
| 3128 | bool |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3129 | codecvt<char16_t, char, mbstate_t>::do_always_noconv() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3130 | { |
| 3131 | return false; |
| 3132 | } |
| 3133 | |
| 3134 | int |
| 3135 | codecvt<char16_t, char, mbstate_t>::do_length(state_type&, |
| 3136 | const extern_type* frm, const extern_type* frm_end, size_t mx) const |
| 3137 | { |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3138 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3139 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3140 | return utf8_to_utf16_length(_frm, _frm_end, mx); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3141 | } |
| 3142 | |
| 3143 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3144 | codecvt<char16_t, char, mbstate_t>::do_max_length() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3145 | { |
| 3146 | return 4; |
| 3147 | } |
| 3148 | |
| 3149 | // template <> class codecvt<char32_t, char, mbstate_t> |
| 3150 | |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 3151 | locale::id codecvt<char32_t, char, mbstate_t>::id; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3152 | |
| 3153 | codecvt<char32_t, char, mbstate_t>::~codecvt() |
| 3154 | { |
| 3155 | } |
| 3156 | |
| 3157 | codecvt<char32_t, char, mbstate_t>::result |
| 3158 | codecvt<char32_t, char, mbstate_t>::do_out(state_type&, |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 3159 | const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3160 | extern_type* to, extern_type* to_end, extern_type*& to_nxt) const |
| 3161 | { |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3162 | const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm); |
| 3163 | const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end); |
| 3164 | const uint32_t* _frm_nxt = _frm; |
| 3165 | uint8_t* _to = reinterpret_cast<uint8_t*>(to); |
| 3166 | uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end); |
| 3167 | uint8_t* _to_nxt = _to; |
| 3168 | result r = ucs4_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt); |
| 3169 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3170 | to_nxt = to + (_to_nxt - _to); |
| 3171 | return r; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3172 | } |
| 3173 | |
| 3174 | codecvt<char32_t, char, mbstate_t>::result |
| 3175 | codecvt<char32_t, char, mbstate_t>::do_in(state_type&, |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 3176 | const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3177 | intern_type* to, intern_type* to_end, intern_type*& to_nxt) const |
| 3178 | { |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3179 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3180 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3181 | const uint8_t* _frm_nxt = _frm; |
| 3182 | uint32_t* _to = reinterpret_cast<uint32_t*>(to); |
| 3183 | uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end); |
| 3184 | uint32_t* _to_nxt = _to; |
| 3185 | result r = utf8_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt); |
| 3186 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3187 | to_nxt = to + (_to_nxt - _to); |
| 3188 | return r; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3189 | } |
| 3190 | |
| 3191 | codecvt<char32_t, char, mbstate_t>::result |
| 3192 | codecvt<char32_t, char, mbstate_t>::do_unshift(state_type&, |
| 3193 | extern_type* to, extern_type*, extern_type*& to_nxt) const |
| 3194 | { |
| 3195 | to_nxt = to; |
| 3196 | return noconv; |
| 3197 | } |
| 3198 | |
| 3199 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3200 | codecvt<char32_t, char, mbstate_t>::do_encoding() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3201 | { |
| 3202 | return 0; |
| 3203 | } |
| 3204 | |
| 3205 | bool |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3206 | codecvt<char32_t, char, mbstate_t>::do_always_noconv() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3207 | { |
| 3208 | return false; |
| 3209 | } |
| 3210 | |
| 3211 | int |
| 3212 | codecvt<char32_t, char, mbstate_t>::do_length(state_type&, |
| 3213 | const extern_type* frm, const extern_type* frm_end, size_t mx) const |
| 3214 | { |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3215 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3216 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3217 | return utf8_to_ucs4_length(_frm, _frm_end, mx); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3218 | } |
| 3219 | |
| 3220 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3221 | codecvt<char32_t, char, mbstate_t>::do_max_length() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3222 | { |
| 3223 | return 4; |
| 3224 | } |
| 3225 | |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3226 | // __codecvt_utf8<wchar_t> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3227 | |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3228 | __codecvt_utf8<wchar_t>::result |
| 3229 | __codecvt_utf8<wchar_t>::do_out(state_type&, |
| 3230 | const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3231 | extern_type* to, extern_type* to_end, extern_type*& to_nxt) const |
| 3232 | { |
Howard Hinnant | 0769e6a | 2013-07-08 19:03:07 +0000 | [diff] [blame] | 3233 | #if _WIN32 |
| 3234 | const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm); |
| 3235 | const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end); |
| 3236 | const uint16_t* _frm_nxt = _frm; |
| 3237 | #else |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3238 | const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm); |
| 3239 | const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end); |
| 3240 | const uint32_t* _frm_nxt = _frm; |
Howard Hinnant | 0769e6a | 2013-07-08 19:03:07 +0000 | [diff] [blame] | 3241 | #endif |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3242 | uint8_t* _to = reinterpret_cast<uint8_t*>(to); |
| 3243 | uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end); |
| 3244 | uint8_t* _to_nxt = _to; |
Howard Hinnant | 0769e6a | 2013-07-08 19:03:07 +0000 | [diff] [blame] | 3245 | #if _WIN32 |
| 3246 | result r = ucs2_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 3247 | _Maxcode_, _Mode_); |
| 3248 | #else |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3249 | result r = ucs4_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 3250 | _Maxcode_, _Mode_); |
Howard Hinnant | 0769e6a | 2013-07-08 19:03:07 +0000 | [diff] [blame] | 3251 | #endif |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3252 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3253 | to_nxt = to + (_to_nxt - _to); |
| 3254 | return r; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3255 | } |
| 3256 | |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3257 | __codecvt_utf8<wchar_t>::result |
| 3258 | __codecvt_utf8<wchar_t>::do_in(state_type&, |
| 3259 | const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3260 | intern_type* to, intern_type* to_end, intern_type*& to_nxt) const |
| 3261 | { |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3262 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3263 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3264 | const uint8_t* _frm_nxt = _frm; |
Howard Hinnant | 0769e6a | 2013-07-08 19:03:07 +0000 | [diff] [blame] | 3265 | #if _WIN32 |
| 3266 | uint16_t* _to = reinterpret_cast<uint16_t*>(to); |
| 3267 | uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end); |
| 3268 | uint16_t* _to_nxt = _to; |
| 3269 | result r = utf8_to_ucs2(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 3270 | _Maxcode_, _Mode_); |
| 3271 | #else |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3272 | uint32_t* _to = reinterpret_cast<uint32_t*>(to); |
| 3273 | uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end); |
| 3274 | uint32_t* _to_nxt = _to; |
| 3275 | result r = utf8_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 3276 | _Maxcode_, _Mode_); |
Howard Hinnant | 0769e6a | 2013-07-08 19:03:07 +0000 | [diff] [blame] | 3277 | #endif |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3278 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3279 | to_nxt = to + (_to_nxt - _to); |
| 3280 | return r; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3283 | __codecvt_utf8<wchar_t>::result |
| 3284 | __codecvt_utf8<wchar_t>::do_unshift(state_type&, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3285 | extern_type* to, extern_type*, extern_type*& to_nxt) const |
| 3286 | { |
| 3287 | to_nxt = to; |
| 3288 | return noconv; |
| 3289 | } |
| 3290 | |
| 3291 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3292 | __codecvt_utf8<wchar_t>::do_encoding() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3293 | { |
| 3294 | return 0; |
| 3295 | } |
| 3296 | |
| 3297 | bool |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3298 | __codecvt_utf8<wchar_t>::do_always_noconv() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3299 | { |
| 3300 | return false; |
| 3301 | } |
| 3302 | |
| 3303 | int |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3304 | __codecvt_utf8<wchar_t>::do_length(state_type&, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3305 | const extern_type* frm, const extern_type* frm_end, size_t mx) const |
| 3306 | { |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3307 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3308 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3309 | return utf8_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3310 | } |
| 3311 | |
| 3312 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3313 | __codecvt_utf8<wchar_t>::do_max_length() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3314 | { |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3315 | if (_Mode_ & consume_header) |
| 3316 | return 7; |
| 3317 | return 4; |
| 3318 | } |
| 3319 | |
| 3320 | // __codecvt_utf8<char16_t> |
| 3321 | |
| 3322 | __codecvt_utf8<char16_t>::result |
| 3323 | __codecvt_utf8<char16_t>::do_out(state_type&, |
| 3324 | const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt, |
| 3325 | extern_type* to, extern_type* to_end, extern_type*& to_nxt) const |
| 3326 | { |
| 3327 | const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm); |
| 3328 | const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end); |
| 3329 | const uint16_t* _frm_nxt = _frm; |
| 3330 | uint8_t* _to = reinterpret_cast<uint8_t*>(to); |
| 3331 | uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end); |
| 3332 | uint8_t* _to_nxt = _to; |
| 3333 | result r = ucs2_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 3334 | _Maxcode_, _Mode_); |
| 3335 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3336 | to_nxt = to + (_to_nxt - _to); |
| 3337 | return r; |
| 3338 | } |
| 3339 | |
| 3340 | __codecvt_utf8<char16_t>::result |
| 3341 | __codecvt_utf8<char16_t>::do_in(state_type&, |
| 3342 | const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt, |
| 3343 | intern_type* to, intern_type* to_end, intern_type*& to_nxt) const |
| 3344 | { |
| 3345 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3346 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3347 | const uint8_t* _frm_nxt = _frm; |
| 3348 | uint16_t* _to = reinterpret_cast<uint16_t*>(to); |
| 3349 | uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end); |
| 3350 | uint16_t* _to_nxt = _to; |
| 3351 | result r = utf8_to_ucs2(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 3352 | _Maxcode_, _Mode_); |
| 3353 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3354 | to_nxt = to + (_to_nxt - _to); |
| 3355 | return r; |
| 3356 | } |
| 3357 | |
| 3358 | __codecvt_utf8<char16_t>::result |
| 3359 | __codecvt_utf8<char16_t>::do_unshift(state_type&, |
| 3360 | extern_type* to, extern_type*, extern_type*& to_nxt) const |
| 3361 | { |
| 3362 | to_nxt = to; |
| 3363 | return noconv; |
| 3364 | } |
| 3365 | |
| 3366 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3367 | __codecvt_utf8<char16_t>::do_encoding() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3368 | { |
| 3369 | return 0; |
| 3370 | } |
| 3371 | |
| 3372 | bool |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3373 | __codecvt_utf8<char16_t>::do_always_noconv() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3374 | { |
| 3375 | return false; |
| 3376 | } |
| 3377 | |
| 3378 | int |
| 3379 | __codecvt_utf8<char16_t>::do_length(state_type&, |
| 3380 | const extern_type* frm, const extern_type* frm_end, size_t mx) const |
| 3381 | { |
| 3382 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3383 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3384 | return utf8_to_ucs2_length(_frm, _frm_end, mx, _Maxcode_, _Mode_); |
| 3385 | } |
| 3386 | |
| 3387 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3388 | __codecvt_utf8<char16_t>::do_max_length() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3389 | { |
| 3390 | if (_Mode_ & consume_header) |
| 3391 | return 6; |
| 3392 | return 3; |
| 3393 | } |
| 3394 | |
| 3395 | // __codecvt_utf8<char32_t> |
| 3396 | |
| 3397 | __codecvt_utf8<char32_t>::result |
| 3398 | __codecvt_utf8<char32_t>::do_out(state_type&, |
| 3399 | const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt, |
| 3400 | extern_type* to, extern_type* to_end, extern_type*& to_nxt) const |
| 3401 | { |
| 3402 | const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm); |
| 3403 | const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end); |
| 3404 | const uint32_t* _frm_nxt = _frm; |
| 3405 | uint8_t* _to = reinterpret_cast<uint8_t*>(to); |
| 3406 | uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end); |
| 3407 | uint8_t* _to_nxt = _to; |
| 3408 | result r = ucs4_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 3409 | _Maxcode_, _Mode_); |
| 3410 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3411 | to_nxt = to + (_to_nxt - _to); |
| 3412 | return r; |
| 3413 | } |
| 3414 | |
| 3415 | __codecvt_utf8<char32_t>::result |
| 3416 | __codecvt_utf8<char32_t>::do_in(state_type&, |
| 3417 | const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt, |
| 3418 | intern_type* to, intern_type* to_end, intern_type*& to_nxt) const |
| 3419 | { |
| 3420 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3421 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3422 | const uint8_t* _frm_nxt = _frm; |
| 3423 | uint32_t* _to = reinterpret_cast<uint32_t*>(to); |
| 3424 | uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end); |
| 3425 | uint32_t* _to_nxt = _to; |
| 3426 | result r = utf8_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 3427 | _Maxcode_, _Mode_); |
| 3428 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3429 | to_nxt = to + (_to_nxt - _to); |
| 3430 | return r; |
| 3431 | } |
| 3432 | |
| 3433 | __codecvt_utf8<char32_t>::result |
| 3434 | __codecvt_utf8<char32_t>::do_unshift(state_type&, |
| 3435 | extern_type* to, extern_type*, extern_type*& to_nxt) const |
| 3436 | { |
| 3437 | to_nxt = to; |
| 3438 | return noconv; |
| 3439 | } |
| 3440 | |
| 3441 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3442 | __codecvt_utf8<char32_t>::do_encoding() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3443 | { |
| 3444 | return 0; |
| 3445 | } |
| 3446 | |
| 3447 | bool |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3448 | __codecvt_utf8<char32_t>::do_always_noconv() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3449 | { |
| 3450 | return false; |
| 3451 | } |
| 3452 | |
| 3453 | int |
| 3454 | __codecvt_utf8<char32_t>::do_length(state_type&, |
| 3455 | const extern_type* frm, const extern_type* frm_end, size_t mx) const |
| 3456 | { |
| 3457 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3458 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3459 | return utf8_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_); |
| 3460 | } |
| 3461 | |
| 3462 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3463 | __codecvt_utf8<char32_t>::do_max_length() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3464 | { |
| 3465 | if (_Mode_ & consume_header) |
| 3466 | return 7; |
| 3467 | return 4; |
| 3468 | } |
| 3469 | |
| 3470 | // __codecvt_utf16<wchar_t, false> |
| 3471 | |
| 3472 | __codecvt_utf16<wchar_t, false>::result |
| 3473 | __codecvt_utf16<wchar_t, false>::do_out(state_type&, |
| 3474 | const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt, |
| 3475 | extern_type* to, extern_type* to_end, extern_type*& to_nxt) const |
| 3476 | { |
| 3477 | const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm); |
| 3478 | const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end); |
| 3479 | const uint32_t* _frm_nxt = _frm; |
| 3480 | uint8_t* _to = reinterpret_cast<uint8_t*>(to); |
| 3481 | uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end); |
| 3482 | uint8_t* _to_nxt = _to; |
| 3483 | result r = ucs4_to_utf16be(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 3484 | _Maxcode_, _Mode_); |
| 3485 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3486 | to_nxt = to + (_to_nxt - _to); |
| 3487 | return r; |
| 3488 | } |
| 3489 | |
| 3490 | __codecvt_utf16<wchar_t, false>::result |
| 3491 | __codecvt_utf16<wchar_t, false>::do_in(state_type&, |
| 3492 | const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt, |
| 3493 | intern_type* to, intern_type* to_end, intern_type*& to_nxt) const |
| 3494 | { |
| 3495 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3496 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3497 | const uint8_t* _frm_nxt = _frm; |
| 3498 | uint32_t* _to = reinterpret_cast<uint32_t*>(to); |
| 3499 | uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end); |
| 3500 | uint32_t* _to_nxt = _to; |
| 3501 | result r = utf16be_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 3502 | _Maxcode_, _Mode_); |
| 3503 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3504 | to_nxt = to + (_to_nxt - _to); |
| 3505 | return r; |
| 3506 | } |
| 3507 | |
| 3508 | __codecvt_utf16<wchar_t, false>::result |
| 3509 | __codecvt_utf16<wchar_t, false>::do_unshift(state_type&, |
| 3510 | extern_type* to, extern_type*, extern_type*& to_nxt) const |
| 3511 | { |
| 3512 | to_nxt = to; |
| 3513 | return noconv; |
| 3514 | } |
| 3515 | |
| 3516 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3517 | __codecvt_utf16<wchar_t, false>::do_encoding() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3518 | { |
| 3519 | return 0; |
| 3520 | } |
| 3521 | |
| 3522 | bool |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3523 | __codecvt_utf16<wchar_t, false>::do_always_noconv() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3524 | { |
| 3525 | return false; |
| 3526 | } |
| 3527 | |
| 3528 | int |
| 3529 | __codecvt_utf16<wchar_t, false>::do_length(state_type&, |
| 3530 | const extern_type* frm, const extern_type* frm_end, size_t mx) const |
| 3531 | { |
| 3532 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3533 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3534 | return utf16be_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_); |
| 3535 | } |
| 3536 | |
| 3537 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3538 | __codecvt_utf16<wchar_t, false>::do_max_length() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3539 | { |
| 3540 | if (_Mode_ & consume_header) |
| 3541 | return 6; |
| 3542 | return 4; |
| 3543 | } |
| 3544 | |
| 3545 | // __codecvt_utf16<wchar_t, true> |
| 3546 | |
| 3547 | __codecvt_utf16<wchar_t, true>::result |
| 3548 | __codecvt_utf16<wchar_t, true>::do_out(state_type&, |
| 3549 | const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt, |
| 3550 | extern_type* to, extern_type* to_end, extern_type*& to_nxt) const |
| 3551 | { |
| 3552 | const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm); |
| 3553 | const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end); |
| 3554 | const uint32_t* _frm_nxt = _frm; |
| 3555 | uint8_t* _to = reinterpret_cast<uint8_t*>(to); |
| 3556 | uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end); |
| 3557 | uint8_t* _to_nxt = _to; |
| 3558 | result r = ucs4_to_utf16le(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 3559 | _Maxcode_, _Mode_); |
| 3560 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3561 | to_nxt = to + (_to_nxt - _to); |
| 3562 | return r; |
| 3563 | } |
| 3564 | |
| 3565 | __codecvt_utf16<wchar_t, true>::result |
| 3566 | __codecvt_utf16<wchar_t, true>::do_in(state_type&, |
| 3567 | const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt, |
| 3568 | intern_type* to, intern_type* to_end, intern_type*& to_nxt) const |
| 3569 | { |
| 3570 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3571 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3572 | const uint8_t* _frm_nxt = _frm; |
| 3573 | uint32_t* _to = reinterpret_cast<uint32_t*>(to); |
| 3574 | uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end); |
| 3575 | uint32_t* _to_nxt = _to; |
| 3576 | result r = utf16le_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 3577 | _Maxcode_, _Mode_); |
| 3578 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3579 | to_nxt = to + (_to_nxt - _to); |
| 3580 | return r; |
| 3581 | } |
| 3582 | |
| 3583 | __codecvt_utf16<wchar_t, true>::result |
| 3584 | __codecvt_utf16<wchar_t, true>::do_unshift(state_type&, |
| 3585 | extern_type* to, extern_type*, extern_type*& to_nxt) const |
| 3586 | { |
| 3587 | to_nxt = to; |
| 3588 | return noconv; |
| 3589 | } |
| 3590 | |
| 3591 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3592 | __codecvt_utf16<wchar_t, true>::do_encoding() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3593 | { |
| 3594 | return 0; |
| 3595 | } |
| 3596 | |
| 3597 | bool |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3598 | __codecvt_utf16<wchar_t, true>::do_always_noconv() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3599 | { |
Howard Hinnant | d23b464 | 2010-05-31 20:58:54 +0000 | [diff] [blame] | 3600 | return false; |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3601 | } |
| 3602 | |
| 3603 | int |
| 3604 | __codecvt_utf16<wchar_t, true>::do_length(state_type&, |
| 3605 | const extern_type* frm, const extern_type* frm_end, size_t mx) const |
| 3606 | { |
| 3607 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3608 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3609 | return utf16le_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_); |
| 3610 | } |
| 3611 | |
| 3612 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3613 | __codecvt_utf16<wchar_t, true>::do_max_length() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3614 | { |
| 3615 | if (_Mode_ & consume_header) |
| 3616 | return 6; |
| 3617 | return 4; |
| 3618 | } |
| 3619 | |
| 3620 | // __codecvt_utf16<char16_t, false> |
| 3621 | |
| 3622 | __codecvt_utf16<char16_t, false>::result |
| 3623 | __codecvt_utf16<char16_t, false>::do_out(state_type&, |
| 3624 | const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt, |
| 3625 | extern_type* to, extern_type* to_end, extern_type*& to_nxt) const |
| 3626 | { |
| 3627 | const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm); |
| 3628 | const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end); |
| 3629 | const uint16_t* _frm_nxt = _frm; |
| 3630 | uint8_t* _to = reinterpret_cast<uint8_t*>(to); |
| 3631 | uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end); |
| 3632 | uint8_t* _to_nxt = _to; |
| 3633 | result r = ucs2_to_utf16be(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 3634 | _Maxcode_, _Mode_); |
| 3635 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3636 | to_nxt = to + (_to_nxt - _to); |
| 3637 | return r; |
| 3638 | } |
| 3639 | |
| 3640 | __codecvt_utf16<char16_t, false>::result |
| 3641 | __codecvt_utf16<char16_t, false>::do_in(state_type&, |
| 3642 | const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt, |
| 3643 | intern_type* to, intern_type* to_end, intern_type*& to_nxt) const |
| 3644 | { |
| 3645 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3646 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3647 | const uint8_t* _frm_nxt = _frm; |
| 3648 | uint16_t* _to = reinterpret_cast<uint16_t*>(to); |
| 3649 | uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end); |
| 3650 | uint16_t* _to_nxt = _to; |
| 3651 | result r = utf16be_to_ucs2(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 3652 | _Maxcode_, _Mode_); |
| 3653 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3654 | to_nxt = to + (_to_nxt - _to); |
| 3655 | return r; |
| 3656 | } |
| 3657 | |
| 3658 | __codecvt_utf16<char16_t, false>::result |
| 3659 | __codecvt_utf16<char16_t, false>::do_unshift(state_type&, |
| 3660 | extern_type* to, extern_type*, extern_type*& to_nxt) const |
| 3661 | { |
| 3662 | to_nxt = to; |
| 3663 | return noconv; |
| 3664 | } |
| 3665 | |
| 3666 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3667 | __codecvt_utf16<char16_t, false>::do_encoding() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3668 | { |
| 3669 | return 0; |
| 3670 | } |
| 3671 | |
| 3672 | bool |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3673 | __codecvt_utf16<char16_t, false>::do_always_noconv() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3674 | { |
| 3675 | return false; |
| 3676 | } |
| 3677 | |
| 3678 | int |
| 3679 | __codecvt_utf16<char16_t, false>::do_length(state_type&, |
| 3680 | const extern_type* frm, const extern_type* frm_end, size_t mx) const |
| 3681 | { |
| 3682 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3683 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3684 | return utf16be_to_ucs2_length(_frm, _frm_end, mx, _Maxcode_, _Mode_); |
| 3685 | } |
| 3686 | |
| 3687 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3688 | __codecvt_utf16<char16_t, false>::do_max_length() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3689 | { |
| 3690 | if (_Mode_ & consume_header) |
| 3691 | return 4; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3692 | return 2; |
| 3693 | } |
| 3694 | |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3695 | // __codecvt_utf16<char16_t, true> |
| 3696 | |
| 3697 | __codecvt_utf16<char16_t, true>::result |
| 3698 | __codecvt_utf16<char16_t, true>::do_out(state_type&, |
| 3699 | const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt, |
| 3700 | extern_type* to, extern_type* to_end, extern_type*& to_nxt) const |
| 3701 | { |
| 3702 | const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm); |
| 3703 | const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end); |
| 3704 | const uint16_t* _frm_nxt = _frm; |
| 3705 | uint8_t* _to = reinterpret_cast<uint8_t*>(to); |
| 3706 | uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end); |
| 3707 | uint8_t* _to_nxt = _to; |
| 3708 | result r = ucs2_to_utf16le(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 3709 | _Maxcode_, _Mode_); |
| 3710 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3711 | to_nxt = to + (_to_nxt - _to); |
| 3712 | return r; |
| 3713 | } |
| 3714 | |
| 3715 | __codecvt_utf16<char16_t, true>::result |
| 3716 | __codecvt_utf16<char16_t, true>::do_in(state_type&, |
| 3717 | const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt, |
| 3718 | intern_type* to, intern_type* to_end, intern_type*& to_nxt) const |
| 3719 | { |
| 3720 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3721 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3722 | const uint8_t* _frm_nxt = _frm; |
| 3723 | uint16_t* _to = reinterpret_cast<uint16_t*>(to); |
| 3724 | uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end); |
| 3725 | uint16_t* _to_nxt = _to; |
| 3726 | result r = utf16le_to_ucs2(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 3727 | _Maxcode_, _Mode_); |
| 3728 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3729 | to_nxt = to + (_to_nxt - _to); |
| 3730 | return r; |
| 3731 | } |
| 3732 | |
| 3733 | __codecvt_utf16<char16_t, true>::result |
| 3734 | __codecvt_utf16<char16_t, true>::do_unshift(state_type&, |
| 3735 | extern_type* to, extern_type*, extern_type*& to_nxt) const |
| 3736 | { |
| 3737 | to_nxt = to; |
| 3738 | return noconv; |
| 3739 | } |
| 3740 | |
| 3741 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3742 | __codecvt_utf16<char16_t, true>::do_encoding() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3743 | { |
| 3744 | return 0; |
| 3745 | } |
| 3746 | |
| 3747 | bool |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3748 | __codecvt_utf16<char16_t, true>::do_always_noconv() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3749 | { |
Howard Hinnant | d23b464 | 2010-05-31 20:58:54 +0000 | [diff] [blame] | 3750 | return false; |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3751 | } |
| 3752 | |
| 3753 | int |
| 3754 | __codecvt_utf16<char16_t, true>::do_length(state_type&, |
| 3755 | const extern_type* frm, const extern_type* frm_end, size_t mx) const |
| 3756 | { |
| 3757 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3758 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3759 | return utf16le_to_ucs2_length(_frm, _frm_end, mx, _Maxcode_, _Mode_); |
| 3760 | } |
| 3761 | |
| 3762 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3763 | __codecvt_utf16<char16_t, true>::do_max_length() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3764 | { |
| 3765 | if (_Mode_ & consume_header) |
| 3766 | return 4; |
| 3767 | return 2; |
| 3768 | } |
| 3769 | |
| 3770 | // __codecvt_utf16<char32_t, false> |
| 3771 | |
| 3772 | __codecvt_utf16<char32_t, false>::result |
| 3773 | __codecvt_utf16<char32_t, false>::do_out(state_type&, |
| 3774 | const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt, |
| 3775 | extern_type* to, extern_type* to_end, extern_type*& to_nxt) const |
| 3776 | { |
| 3777 | const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm); |
| 3778 | const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end); |
| 3779 | const uint32_t* _frm_nxt = _frm; |
| 3780 | uint8_t* _to = reinterpret_cast<uint8_t*>(to); |
| 3781 | uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end); |
| 3782 | uint8_t* _to_nxt = _to; |
| 3783 | result r = ucs4_to_utf16be(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 3784 | _Maxcode_, _Mode_); |
| 3785 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3786 | to_nxt = to + (_to_nxt - _to); |
| 3787 | return r; |
| 3788 | } |
| 3789 | |
| 3790 | __codecvt_utf16<char32_t, false>::result |
| 3791 | __codecvt_utf16<char32_t, false>::do_in(state_type&, |
| 3792 | const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt, |
| 3793 | intern_type* to, intern_type* to_end, intern_type*& to_nxt) const |
| 3794 | { |
| 3795 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3796 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3797 | const uint8_t* _frm_nxt = _frm; |
| 3798 | uint32_t* _to = reinterpret_cast<uint32_t*>(to); |
| 3799 | uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end); |
| 3800 | uint32_t* _to_nxt = _to; |
| 3801 | result r = utf16be_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 3802 | _Maxcode_, _Mode_); |
| 3803 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3804 | to_nxt = to + (_to_nxt - _to); |
| 3805 | return r; |
| 3806 | } |
| 3807 | |
| 3808 | __codecvt_utf16<char32_t, false>::result |
| 3809 | __codecvt_utf16<char32_t, false>::do_unshift(state_type&, |
| 3810 | extern_type* to, extern_type*, extern_type*& to_nxt) const |
| 3811 | { |
| 3812 | to_nxt = to; |
| 3813 | return noconv; |
| 3814 | } |
| 3815 | |
| 3816 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3817 | __codecvt_utf16<char32_t, false>::do_encoding() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3818 | { |
| 3819 | return 0; |
| 3820 | } |
| 3821 | |
| 3822 | bool |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3823 | __codecvt_utf16<char32_t, false>::do_always_noconv() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3824 | { |
| 3825 | return false; |
| 3826 | } |
| 3827 | |
| 3828 | int |
| 3829 | __codecvt_utf16<char32_t, false>::do_length(state_type&, |
| 3830 | const extern_type* frm, const extern_type* frm_end, size_t mx) const |
| 3831 | { |
| 3832 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3833 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3834 | return utf16be_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_); |
| 3835 | } |
| 3836 | |
| 3837 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3838 | __codecvt_utf16<char32_t, false>::do_max_length() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3839 | { |
| 3840 | if (_Mode_ & consume_header) |
| 3841 | return 6; |
| 3842 | return 4; |
| 3843 | } |
| 3844 | |
| 3845 | // __codecvt_utf16<char32_t, true> |
| 3846 | |
| 3847 | __codecvt_utf16<char32_t, true>::result |
| 3848 | __codecvt_utf16<char32_t, true>::do_out(state_type&, |
| 3849 | const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt, |
| 3850 | extern_type* to, extern_type* to_end, extern_type*& to_nxt) const |
| 3851 | { |
| 3852 | const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm); |
| 3853 | const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end); |
| 3854 | const uint32_t* _frm_nxt = _frm; |
| 3855 | uint8_t* _to = reinterpret_cast<uint8_t*>(to); |
| 3856 | uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end); |
| 3857 | uint8_t* _to_nxt = _to; |
| 3858 | result r = ucs4_to_utf16le(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 3859 | _Maxcode_, _Mode_); |
| 3860 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3861 | to_nxt = to + (_to_nxt - _to); |
| 3862 | return r; |
| 3863 | } |
| 3864 | |
| 3865 | __codecvt_utf16<char32_t, true>::result |
| 3866 | __codecvt_utf16<char32_t, true>::do_in(state_type&, |
| 3867 | const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt, |
| 3868 | intern_type* to, intern_type* to_end, intern_type*& to_nxt) const |
| 3869 | { |
| 3870 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3871 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3872 | const uint8_t* _frm_nxt = _frm; |
| 3873 | uint32_t* _to = reinterpret_cast<uint32_t*>(to); |
| 3874 | uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end); |
| 3875 | uint32_t* _to_nxt = _to; |
| 3876 | result r = utf16le_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 3877 | _Maxcode_, _Mode_); |
| 3878 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3879 | to_nxt = to + (_to_nxt - _to); |
| 3880 | return r; |
| 3881 | } |
| 3882 | |
| 3883 | __codecvt_utf16<char32_t, true>::result |
| 3884 | __codecvt_utf16<char32_t, true>::do_unshift(state_type&, |
| 3885 | extern_type* to, extern_type*, extern_type*& to_nxt) const |
| 3886 | { |
| 3887 | to_nxt = to; |
| 3888 | return noconv; |
| 3889 | } |
| 3890 | |
| 3891 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3892 | __codecvt_utf16<char32_t, true>::do_encoding() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3893 | { |
| 3894 | return 0; |
| 3895 | } |
| 3896 | |
| 3897 | bool |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3898 | __codecvt_utf16<char32_t, true>::do_always_noconv() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3899 | { |
Howard Hinnant | d23b464 | 2010-05-31 20:58:54 +0000 | [diff] [blame] | 3900 | return false; |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3901 | } |
| 3902 | |
| 3903 | int |
| 3904 | __codecvt_utf16<char32_t, true>::do_length(state_type&, |
| 3905 | const extern_type* frm, const extern_type* frm_end, size_t mx) const |
| 3906 | { |
| 3907 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3908 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3909 | return utf16le_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_); |
| 3910 | } |
| 3911 | |
| 3912 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3913 | __codecvt_utf16<char32_t, true>::do_max_length() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3914 | { |
| 3915 | if (_Mode_ & consume_header) |
| 3916 | return 6; |
| 3917 | return 4; |
| 3918 | } |
| 3919 | |
| 3920 | // __codecvt_utf8_utf16<wchar_t> |
| 3921 | |
| 3922 | __codecvt_utf8_utf16<wchar_t>::result |
| 3923 | __codecvt_utf8_utf16<wchar_t>::do_out(state_type&, |
| 3924 | const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt, |
| 3925 | extern_type* to, extern_type* to_end, extern_type*& to_nxt) const |
| 3926 | { |
| 3927 | const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm); |
| 3928 | const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end); |
| 3929 | const uint32_t* _frm_nxt = _frm; |
| 3930 | uint8_t* _to = reinterpret_cast<uint8_t*>(to); |
| 3931 | uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end); |
| 3932 | uint8_t* _to_nxt = _to; |
| 3933 | result r = utf16_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 3934 | _Maxcode_, _Mode_); |
| 3935 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3936 | to_nxt = to + (_to_nxt - _to); |
| 3937 | return r; |
| 3938 | } |
| 3939 | |
| 3940 | __codecvt_utf8_utf16<wchar_t>::result |
| 3941 | __codecvt_utf8_utf16<wchar_t>::do_in(state_type&, |
| 3942 | const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt, |
| 3943 | intern_type* to, intern_type* to_end, intern_type*& to_nxt) const |
| 3944 | { |
| 3945 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3946 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3947 | const uint8_t* _frm_nxt = _frm; |
| 3948 | uint32_t* _to = reinterpret_cast<uint32_t*>(to); |
| 3949 | uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end); |
| 3950 | uint32_t* _to_nxt = _to; |
| 3951 | result r = utf8_to_utf16(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 3952 | _Maxcode_, _Mode_); |
| 3953 | frm_nxt = frm + (_frm_nxt - _frm); |
| 3954 | to_nxt = to + (_to_nxt - _to); |
| 3955 | return r; |
| 3956 | } |
| 3957 | |
| 3958 | __codecvt_utf8_utf16<wchar_t>::result |
| 3959 | __codecvt_utf8_utf16<wchar_t>::do_unshift(state_type&, |
| 3960 | extern_type* to, extern_type*, extern_type*& to_nxt) const |
| 3961 | { |
| 3962 | to_nxt = to; |
| 3963 | return noconv; |
| 3964 | } |
| 3965 | |
| 3966 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3967 | __codecvt_utf8_utf16<wchar_t>::do_encoding() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3968 | { |
| 3969 | return 0; |
| 3970 | } |
| 3971 | |
| 3972 | bool |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3973 | __codecvt_utf8_utf16<wchar_t>::do_always_noconv() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3974 | { |
| 3975 | return false; |
| 3976 | } |
| 3977 | |
| 3978 | int |
| 3979 | __codecvt_utf8_utf16<wchar_t>::do_length(state_type&, |
| 3980 | const extern_type* frm, const extern_type* frm_end, size_t mx) const |
| 3981 | { |
| 3982 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 3983 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 3984 | return utf8_to_utf16_length(_frm, _frm_end, mx, _Maxcode_, _Mode_); |
| 3985 | } |
| 3986 | |
| 3987 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 3988 | __codecvt_utf8_utf16<wchar_t>::do_max_length() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 3989 | { |
| 3990 | if (_Mode_ & consume_header) |
| 3991 | return 7; |
| 3992 | return 4; |
| 3993 | } |
| 3994 | |
| 3995 | // __codecvt_utf8_utf16<char16_t> |
| 3996 | |
| 3997 | __codecvt_utf8_utf16<char16_t>::result |
| 3998 | __codecvt_utf8_utf16<char16_t>::do_out(state_type&, |
| 3999 | const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt, |
| 4000 | extern_type* to, extern_type* to_end, extern_type*& to_nxt) const |
| 4001 | { |
| 4002 | const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm); |
| 4003 | const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end); |
| 4004 | const uint16_t* _frm_nxt = _frm; |
| 4005 | uint8_t* _to = reinterpret_cast<uint8_t*>(to); |
| 4006 | uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end); |
| 4007 | uint8_t* _to_nxt = _to; |
| 4008 | result r = utf16_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 4009 | _Maxcode_, _Mode_); |
| 4010 | frm_nxt = frm + (_frm_nxt - _frm); |
| 4011 | to_nxt = to + (_to_nxt - _to); |
| 4012 | return r; |
| 4013 | } |
| 4014 | |
| 4015 | __codecvt_utf8_utf16<char16_t>::result |
| 4016 | __codecvt_utf8_utf16<char16_t>::do_in(state_type&, |
| 4017 | const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt, |
| 4018 | intern_type* to, intern_type* to_end, intern_type*& to_nxt) const |
| 4019 | { |
| 4020 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 4021 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 4022 | const uint8_t* _frm_nxt = _frm; |
| 4023 | uint16_t* _to = reinterpret_cast<uint16_t*>(to); |
| 4024 | uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end); |
| 4025 | uint16_t* _to_nxt = _to; |
| 4026 | result r = utf8_to_utf16(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 4027 | _Maxcode_, _Mode_); |
| 4028 | frm_nxt = frm + (_frm_nxt - _frm); |
| 4029 | to_nxt = to + (_to_nxt - _to); |
| 4030 | return r; |
| 4031 | } |
| 4032 | |
| 4033 | __codecvt_utf8_utf16<char16_t>::result |
| 4034 | __codecvt_utf8_utf16<char16_t>::do_unshift(state_type&, |
| 4035 | extern_type* to, extern_type*, extern_type*& to_nxt) const |
| 4036 | { |
| 4037 | to_nxt = to; |
| 4038 | return noconv; |
| 4039 | } |
| 4040 | |
| 4041 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 4042 | __codecvt_utf8_utf16<char16_t>::do_encoding() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 4043 | { |
| 4044 | return 0; |
| 4045 | } |
| 4046 | |
| 4047 | bool |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 4048 | __codecvt_utf8_utf16<char16_t>::do_always_noconv() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 4049 | { |
| 4050 | return false; |
| 4051 | } |
| 4052 | |
| 4053 | int |
| 4054 | __codecvt_utf8_utf16<char16_t>::do_length(state_type&, |
| 4055 | const extern_type* frm, const extern_type* frm_end, size_t mx) const |
| 4056 | { |
| 4057 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 4058 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 4059 | return utf8_to_utf16_length(_frm, _frm_end, mx, _Maxcode_, _Mode_); |
| 4060 | } |
| 4061 | |
| 4062 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 4063 | __codecvt_utf8_utf16<char16_t>::do_max_length() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 4064 | { |
| 4065 | if (_Mode_ & consume_header) |
| 4066 | return 7; |
| 4067 | return 4; |
| 4068 | } |
| 4069 | |
| 4070 | // __codecvt_utf8_utf16<char32_t> |
| 4071 | |
| 4072 | __codecvt_utf8_utf16<char32_t>::result |
| 4073 | __codecvt_utf8_utf16<char32_t>::do_out(state_type&, |
| 4074 | const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt, |
| 4075 | extern_type* to, extern_type* to_end, extern_type*& to_nxt) const |
| 4076 | { |
| 4077 | const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm); |
| 4078 | const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end); |
| 4079 | const uint32_t* _frm_nxt = _frm; |
| 4080 | uint8_t* _to = reinterpret_cast<uint8_t*>(to); |
| 4081 | uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end); |
| 4082 | uint8_t* _to_nxt = _to; |
| 4083 | result r = utf16_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 4084 | _Maxcode_, _Mode_); |
| 4085 | frm_nxt = frm + (_frm_nxt - _frm); |
| 4086 | to_nxt = to + (_to_nxt - _to); |
| 4087 | return r; |
| 4088 | } |
| 4089 | |
| 4090 | __codecvt_utf8_utf16<char32_t>::result |
| 4091 | __codecvt_utf8_utf16<char32_t>::do_in(state_type&, |
| 4092 | const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt, |
| 4093 | intern_type* to, intern_type* to_end, intern_type*& to_nxt) const |
| 4094 | { |
| 4095 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 4096 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 4097 | const uint8_t* _frm_nxt = _frm; |
| 4098 | uint32_t* _to = reinterpret_cast<uint32_t*>(to); |
| 4099 | uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end); |
| 4100 | uint32_t* _to_nxt = _to; |
| 4101 | result r = utf8_to_utf16(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, |
| 4102 | _Maxcode_, _Mode_); |
| 4103 | frm_nxt = frm + (_frm_nxt - _frm); |
| 4104 | to_nxt = to + (_to_nxt - _to); |
| 4105 | return r; |
| 4106 | } |
| 4107 | |
| 4108 | __codecvt_utf8_utf16<char32_t>::result |
| 4109 | __codecvt_utf8_utf16<char32_t>::do_unshift(state_type&, |
| 4110 | extern_type* to, extern_type*, extern_type*& to_nxt) const |
| 4111 | { |
| 4112 | to_nxt = to; |
| 4113 | return noconv; |
| 4114 | } |
| 4115 | |
| 4116 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 4117 | __codecvt_utf8_utf16<char32_t>::do_encoding() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 4118 | { |
| 4119 | return 0; |
| 4120 | } |
| 4121 | |
| 4122 | bool |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 4123 | __codecvt_utf8_utf16<char32_t>::do_always_noconv() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 4124 | { |
| 4125 | return false; |
| 4126 | } |
| 4127 | |
| 4128 | int |
| 4129 | __codecvt_utf8_utf16<char32_t>::do_length(state_type&, |
| 4130 | const extern_type* frm, const extern_type* frm_end, size_t mx) const |
| 4131 | { |
| 4132 | const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm); |
| 4133 | const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end); |
| 4134 | return utf8_to_utf16_length(_frm, _frm_end, mx, _Maxcode_, _Mode_); |
| 4135 | } |
| 4136 | |
| 4137 | int |
Howard Hinnant | c983454 | 2011-05-31 15:34:58 +0000 | [diff] [blame] | 4138 | __codecvt_utf8_utf16<char32_t>::do_max_length() const _NOEXCEPT |
Howard Hinnant | 87d1a8a | 2010-05-30 21:39:41 +0000 | [diff] [blame] | 4139 | { |
| 4140 | if (_Mode_ & consume_header) |
| 4141 | return 7; |
| 4142 | return 4; |
| 4143 | } |
| 4144 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4145 | // __narrow_to_utf8<16> |
| 4146 | |
| 4147 | __narrow_to_utf8<16>::~__narrow_to_utf8() |
| 4148 | { |
| 4149 | } |
| 4150 | |
| 4151 | // __narrow_to_utf8<32> |
| 4152 | |
| 4153 | __narrow_to_utf8<32>::~__narrow_to_utf8() |
| 4154 | { |
| 4155 | } |
| 4156 | |
| 4157 | // __widen_from_utf8<16> |
| 4158 | |
| 4159 | __widen_from_utf8<16>::~__widen_from_utf8() |
| 4160 | { |
| 4161 | } |
| 4162 | |
| 4163 | // __widen_from_utf8<32> |
| 4164 | |
| 4165 | __widen_from_utf8<32>::~__widen_from_utf8() |
| 4166 | { |
| 4167 | } |
| 4168 | |
| 4169 | // numpunct<char> && numpunct<wchar_t> |
| 4170 | |
| 4171 | locale::id numpunct< char >::id; |
| 4172 | locale::id numpunct<wchar_t>::id; |
| 4173 | |
| 4174 | numpunct<char>::numpunct(size_t refs) |
| 4175 | : locale::facet(refs), |
| 4176 | __decimal_point_('.'), |
| 4177 | __thousands_sep_(',') |
| 4178 | { |
| 4179 | } |
| 4180 | |
| 4181 | numpunct<wchar_t>::numpunct(size_t refs) |
| 4182 | : locale::facet(refs), |
| 4183 | __decimal_point_(L'.'), |
| 4184 | __thousands_sep_(L',') |
| 4185 | { |
| 4186 | } |
| 4187 | |
| 4188 | numpunct<char>::~numpunct() |
| 4189 | { |
| 4190 | } |
| 4191 | |
| 4192 | numpunct<wchar_t>::~numpunct() |
| 4193 | { |
| 4194 | } |
| 4195 | |
| 4196 | char numpunct< char >::do_decimal_point() const {return __decimal_point_;} |
| 4197 | wchar_t numpunct<wchar_t>::do_decimal_point() const {return __decimal_point_;} |
| 4198 | |
| 4199 | char numpunct< char >::do_thousands_sep() const {return __thousands_sep_;} |
| 4200 | wchar_t numpunct<wchar_t>::do_thousands_sep() const {return __thousands_sep_;} |
| 4201 | |
| 4202 | string numpunct< char >::do_grouping() const {return __grouping_;} |
| 4203 | string numpunct<wchar_t>::do_grouping() const {return __grouping_;} |
| 4204 | |
| 4205 | string numpunct< char >::do_truename() const {return "true";} |
| 4206 | wstring numpunct<wchar_t>::do_truename() const {return L"true";} |
| 4207 | |
| 4208 | string numpunct< char >::do_falsename() const {return "false";} |
| 4209 | wstring numpunct<wchar_t>::do_falsename() const {return L"false";} |
| 4210 | |
| 4211 | // numpunct_byname<char> |
| 4212 | |
| 4213 | numpunct_byname<char>::numpunct_byname(const char* nm, size_t refs) |
| 4214 | : numpunct<char>(refs) |
| 4215 | { |
| 4216 | __init(nm); |
| 4217 | } |
| 4218 | |
| 4219 | numpunct_byname<char>::numpunct_byname(const string& nm, size_t refs) |
| 4220 | : numpunct<char>(refs) |
| 4221 | { |
| 4222 | __init(nm.c_str()); |
| 4223 | } |
| 4224 | |
| 4225 | numpunct_byname<char>::~numpunct_byname() |
| 4226 | { |
| 4227 | } |
| 4228 | |
| 4229 | void |
| 4230 | numpunct_byname<char>::__init(const char* nm) |
| 4231 | { |
| 4232 | if (strcmp(nm, "C") != 0) |
| 4233 | { |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 4234 | __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 4235 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 05b57d5 | 2012-03-07 20:37:43 +0000 | [diff] [blame] | 4236 | if (loc == nullptr) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4237 | throw runtime_error("numpunct_byname<char>::numpunct_byname" |
| 4238 | " failed to construct for " + string(nm)); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 4239 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 4240 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 4241 | lconv* lc = localeconv_l(loc.get()); |
| 4242 | #else |
| 4243 | lconv* lc = __localeconv_l(loc.get()); |
| 4244 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4245 | if (*lc->decimal_point) |
| 4246 | __decimal_point_ = *lc->decimal_point; |
| 4247 | if (*lc->thousands_sep) |
| 4248 | __thousands_sep_ = *lc->thousands_sep; |
| 4249 | __grouping_ = lc->grouping; |
Sean Hunt | cb05a08 | 2011-07-07 22:45:07 +0000 | [diff] [blame] | 4250 | // localization for truename and falsename is not available |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4251 | } |
| 4252 | } |
| 4253 | |
| 4254 | // numpunct_byname<wchar_t> |
| 4255 | |
| 4256 | numpunct_byname<wchar_t>::numpunct_byname(const char* nm, size_t refs) |
| 4257 | : numpunct<wchar_t>(refs) |
| 4258 | { |
| 4259 | __init(nm); |
| 4260 | } |
| 4261 | |
| 4262 | numpunct_byname<wchar_t>::numpunct_byname(const string& nm, size_t refs) |
| 4263 | : numpunct<wchar_t>(refs) |
| 4264 | { |
| 4265 | __init(nm.c_str()); |
| 4266 | } |
| 4267 | |
| 4268 | numpunct_byname<wchar_t>::~numpunct_byname() |
| 4269 | { |
| 4270 | } |
| 4271 | |
| 4272 | void |
| 4273 | numpunct_byname<wchar_t>::__init(const char* nm) |
| 4274 | { |
| 4275 | if (strcmp(nm, "C") != 0) |
| 4276 | { |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 4277 | __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 4278 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 05b57d5 | 2012-03-07 20:37:43 +0000 | [diff] [blame] | 4279 | if (loc == nullptr) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4280 | throw runtime_error("numpunct_byname<char>::numpunct_byname" |
| 4281 | " failed to construct for " + string(nm)); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 4282 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 4283 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 4284 | lconv* lc = localeconv_l(loc.get()); |
| 4285 | #else |
| 4286 | lconv* lc = __localeconv_l(loc.get()); |
| 4287 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4288 | if (*lc->decimal_point) |
| 4289 | __decimal_point_ = *lc->decimal_point; |
| 4290 | if (*lc->thousands_sep) |
| 4291 | __thousands_sep_ = *lc->thousands_sep; |
| 4292 | __grouping_ = lc->grouping; |
| 4293 | // locallization for truename and falsename is not available |
| 4294 | } |
| 4295 | } |
| 4296 | |
| 4297 | // num_get helpers |
| 4298 | |
| 4299 | int |
| 4300 | __num_get_base::__get_base(ios_base& iob) |
| 4301 | { |
| 4302 | ios_base::fmtflags __basefield = iob.flags() & ios_base::basefield; |
| 4303 | if (__basefield == ios_base::oct) |
| 4304 | return 8; |
| 4305 | else if (__basefield == ios_base::hex) |
| 4306 | return 16; |
| 4307 | else if (__basefield == 0) |
| 4308 | return 0; |
| 4309 | return 10; |
| 4310 | } |
| 4311 | |
| 4312 | const char __num_get_base::__src[33] = "0123456789abcdefABCDEFxX+-pPiInN"; |
| 4313 | |
| 4314 | void |
| 4315 | __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end, |
| 4316 | ios_base::iostate& __err) |
| 4317 | { |
| 4318 | if (__grouping.size() != 0) |
| 4319 | { |
| 4320 | reverse(__g, __g_end); |
| 4321 | const char* __ig = __grouping.data(); |
| 4322 | const char* __eg = __ig + __grouping.size(); |
| 4323 | for (unsigned* __r = __g; __r < __g_end-1; ++__r) |
| 4324 | { |
| 4325 | if (0 < *__ig && *__ig < numeric_limits<char>::max()) |
| 4326 | { |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 4327 | if (static_cast<unsigned>(*__ig) != *__r) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4328 | { |
| 4329 | __err = ios_base::failbit; |
| 4330 | return; |
| 4331 | } |
| 4332 | } |
| 4333 | if (__eg - __ig > 1) |
| 4334 | ++__ig; |
| 4335 | } |
| 4336 | if (0 < *__ig && *__ig < numeric_limits<char>::max()) |
| 4337 | { |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 4338 | if (static_cast<unsigned>(*__ig) < __g_end[-1] || __g_end[-1] == 0) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4339 | __err = ios_base::failbit; |
| 4340 | } |
| 4341 | } |
| 4342 | } |
| 4343 | |
| 4344 | void |
| 4345 | __num_put_base::__format_int(char* __fmtp, const char* __len, bool __signd, |
| 4346 | ios_base::fmtflags __flags) |
| 4347 | { |
| 4348 | if (__flags & ios_base::showpos) |
| 4349 | *__fmtp++ = '+'; |
| 4350 | if (__flags & ios_base::showbase) |
| 4351 | *__fmtp++ = '#'; |
| 4352 | while(*__len) |
| 4353 | *__fmtp++ = *__len++; |
| 4354 | if ((__flags & ios_base::basefield) == ios_base::oct) |
| 4355 | *__fmtp = 'o'; |
| 4356 | else if ((__flags & ios_base::basefield) == ios_base::hex) |
| 4357 | { |
| 4358 | if (__flags & ios_base::uppercase) |
| 4359 | *__fmtp = 'X'; |
| 4360 | else |
| 4361 | *__fmtp = 'x'; |
| 4362 | } |
| 4363 | else if (__signd) |
| 4364 | *__fmtp = 'd'; |
| 4365 | else |
| 4366 | *__fmtp = 'u'; |
| 4367 | } |
| 4368 | |
| 4369 | bool |
| 4370 | __num_put_base::__format_float(char* __fmtp, const char* __len, |
| 4371 | ios_base::fmtflags __flags) |
| 4372 | { |
| 4373 | bool specify_precision = true; |
| 4374 | if (__flags & ios_base::showpos) |
| 4375 | *__fmtp++ = '+'; |
| 4376 | if (__flags & ios_base::showpoint) |
| 4377 | *__fmtp++ = '#'; |
| 4378 | ios_base::fmtflags floatfield = __flags & ios_base::floatfield; |
Marshall Clow | a364513 | 2013-10-21 15:07:28 +0000 | [diff] [blame] | 4379 | bool uppercase = (__flags & ios_base::uppercase) != 0; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4380 | if (floatfield == (ios_base::fixed | ios_base::scientific)) |
| 4381 | specify_precision = false; |
| 4382 | else |
| 4383 | { |
| 4384 | *__fmtp++ = '.'; |
| 4385 | *__fmtp++ = '*'; |
| 4386 | } |
| 4387 | while(*__len) |
| 4388 | *__fmtp++ = *__len++; |
| 4389 | if (floatfield == ios_base::fixed) |
| 4390 | { |
| 4391 | if (uppercase) |
| 4392 | *__fmtp = 'F'; |
| 4393 | else |
| 4394 | *__fmtp = 'f'; |
| 4395 | } |
| 4396 | else if (floatfield == ios_base::scientific) |
| 4397 | { |
| 4398 | if (uppercase) |
| 4399 | *__fmtp = 'E'; |
| 4400 | else |
| 4401 | *__fmtp = 'e'; |
| 4402 | } |
| 4403 | else if (floatfield == (ios_base::fixed | ios_base::scientific)) |
| 4404 | { |
| 4405 | if (uppercase) |
| 4406 | *__fmtp = 'A'; |
| 4407 | else |
| 4408 | *__fmtp = 'a'; |
| 4409 | } |
| 4410 | else |
| 4411 | { |
| 4412 | if (uppercase) |
| 4413 | *__fmtp = 'G'; |
| 4414 | else |
| 4415 | *__fmtp = 'g'; |
| 4416 | } |
| 4417 | return specify_precision; |
| 4418 | } |
| 4419 | |
| 4420 | char* |
| 4421 | __num_put_base::__identify_padding(char* __nb, char* __ne, |
| 4422 | const ios_base& __iob) |
| 4423 | { |
| 4424 | switch (__iob.flags() & ios_base::adjustfield) |
| 4425 | { |
| 4426 | case ios_base::internal: |
| 4427 | if (__nb[0] == '-' || __nb[0] == '+') |
| 4428 | return __nb+1; |
| 4429 | if (__ne - __nb >= 2 && __nb[0] == '0' |
| 4430 | && (__nb[1] == 'x' || __nb[1] == 'X')) |
| 4431 | return __nb+2; |
| 4432 | break; |
| 4433 | case ios_base::left: |
| 4434 | return __ne; |
| 4435 | case ios_base::right: |
| 4436 | default: |
| 4437 | break; |
| 4438 | } |
| 4439 | return __nb; |
| 4440 | } |
| 4441 | |
| 4442 | // time_get |
| 4443 | |
| 4444 | static |
| 4445 | string* |
| 4446 | init_weeks() |
| 4447 | { |
| 4448 | static string weeks[14]; |
| 4449 | weeks[0] = "Sunday"; |
| 4450 | weeks[1] = "Monday"; |
| 4451 | weeks[2] = "Tuesday"; |
| 4452 | weeks[3] = "Wednesday"; |
| 4453 | weeks[4] = "Thursday"; |
| 4454 | weeks[5] = "Friday"; |
| 4455 | weeks[6] = "Saturday"; |
| 4456 | weeks[7] = "Sun"; |
| 4457 | weeks[8] = "Mon"; |
| 4458 | weeks[9] = "Tue"; |
| 4459 | weeks[10] = "Wed"; |
| 4460 | weeks[11] = "Thu"; |
| 4461 | weeks[12] = "Fri"; |
| 4462 | weeks[13] = "Sat"; |
| 4463 | return weeks; |
| 4464 | } |
| 4465 | |
| 4466 | static |
| 4467 | wstring* |
| 4468 | init_wweeks() |
| 4469 | { |
| 4470 | static wstring weeks[14]; |
| 4471 | weeks[0] = L"Sunday"; |
| 4472 | weeks[1] = L"Monday"; |
| 4473 | weeks[2] = L"Tuesday"; |
| 4474 | weeks[3] = L"Wednesday"; |
| 4475 | weeks[4] = L"Thursday"; |
| 4476 | weeks[5] = L"Friday"; |
| 4477 | weeks[6] = L"Saturday"; |
| 4478 | weeks[7] = L"Sun"; |
| 4479 | weeks[8] = L"Mon"; |
| 4480 | weeks[9] = L"Tue"; |
| 4481 | weeks[10] = L"Wed"; |
| 4482 | weeks[11] = L"Thu"; |
| 4483 | weeks[12] = L"Fri"; |
| 4484 | weeks[13] = L"Sat"; |
| 4485 | return weeks; |
| 4486 | } |
| 4487 | |
| 4488 | template <> |
| 4489 | const string* |
| 4490 | __time_get_c_storage<char>::__weeks() const |
| 4491 | { |
| 4492 | static const string* weeks = init_weeks(); |
| 4493 | return weeks; |
| 4494 | } |
| 4495 | |
| 4496 | template <> |
| 4497 | const wstring* |
| 4498 | __time_get_c_storage<wchar_t>::__weeks() const |
| 4499 | { |
| 4500 | static const wstring* weeks = init_wweeks(); |
| 4501 | return weeks; |
| 4502 | } |
| 4503 | |
| 4504 | static |
| 4505 | string* |
| 4506 | init_months() |
| 4507 | { |
| 4508 | static string months[24]; |
| 4509 | months[0] = "January"; |
| 4510 | months[1] = "February"; |
| 4511 | months[2] = "March"; |
| 4512 | months[3] = "April"; |
| 4513 | months[4] = "May"; |
| 4514 | months[5] = "June"; |
| 4515 | months[6] = "July"; |
| 4516 | months[7] = "August"; |
| 4517 | months[8] = "September"; |
| 4518 | months[9] = "October"; |
| 4519 | months[10] = "November"; |
| 4520 | months[11] = "December"; |
| 4521 | months[12] = "Jan"; |
| 4522 | months[13] = "Feb"; |
| 4523 | months[14] = "Mar"; |
| 4524 | months[15] = "Apr"; |
| 4525 | months[16] = "May"; |
| 4526 | months[17] = "Jun"; |
| 4527 | months[18] = "Jul"; |
| 4528 | months[19] = "Aug"; |
| 4529 | months[20] = "Sep"; |
| 4530 | months[21] = "Oct"; |
| 4531 | months[22] = "Nov"; |
| 4532 | months[23] = "Dec"; |
| 4533 | return months; |
| 4534 | } |
| 4535 | |
| 4536 | static |
| 4537 | wstring* |
| 4538 | init_wmonths() |
| 4539 | { |
| 4540 | static wstring months[24]; |
| 4541 | months[0] = L"January"; |
| 4542 | months[1] = L"February"; |
| 4543 | months[2] = L"March"; |
| 4544 | months[3] = L"April"; |
| 4545 | months[4] = L"May"; |
| 4546 | months[5] = L"June"; |
| 4547 | months[6] = L"July"; |
| 4548 | months[7] = L"August"; |
| 4549 | months[8] = L"September"; |
| 4550 | months[9] = L"October"; |
| 4551 | months[10] = L"November"; |
| 4552 | months[11] = L"December"; |
| 4553 | months[12] = L"Jan"; |
| 4554 | months[13] = L"Feb"; |
| 4555 | months[14] = L"Mar"; |
| 4556 | months[15] = L"Apr"; |
| 4557 | months[16] = L"May"; |
| 4558 | months[17] = L"Jun"; |
| 4559 | months[18] = L"Jul"; |
| 4560 | months[19] = L"Aug"; |
| 4561 | months[20] = L"Sep"; |
| 4562 | months[21] = L"Oct"; |
| 4563 | months[22] = L"Nov"; |
| 4564 | months[23] = L"Dec"; |
| 4565 | return months; |
| 4566 | } |
| 4567 | |
| 4568 | template <> |
| 4569 | const string* |
| 4570 | __time_get_c_storage<char>::__months() const |
| 4571 | { |
| 4572 | static const string* months = init_months(); |
| 4573 | return months; |
| 4574 | } |
| 4575 | |
| 4576 | template <> |
| 4577 | const wstring* |
| 4578 | __time_get_c_storage<wchar_t>::__months() const |
| 4579 | { |
| 4580 | static const wstring* months = init_wmonths(); |
| 4581 | return months; |
| 4582 | } |
| 4583 | |
| 4584 | static |
| 4585 | string* |
| 4586 | init_am_pm() |
| 4587 | { |
| 4588 | static string am_pm[24]; |
| 4589 | am_pm[0] = "AM"; |
| 4590 | am_pm[1] = "PM"; |
| 4591 | return am_pm; |
| 4592 | } |
| 4593 | |
| 4594 | static |
| 4595 | wstring* |
| 4596 | init_wam_pm() |
| 4597 | { |
| 4598 | static wstring am_pm[24]; |
| 4599 | am_pm[0] = L"AM"; |
| 4600 | am_pm[1] = L"PM"; |
| 4601 | return am_pm; |
| 4602 | } |
| 4603 | |
| 4604 | template <> |
| 4605 | const string* |
| 4606 | __time_get_c_storage<char>::__am_pm() const |
| 4607 | { |
| 4608 | static const string* am_pm = init_am_pm(); |
| 4609 | return am_pm; |
| 4610 | } |
| 4611 | |
| 4612 | template <> |
| 4613 | const wstring* |
| 4614 | __time_get_c_storage<wchar_t>::__am_pm() const |
| 4615 | { |
| 4616 | static const wstring* am_pm = init_wam_pm(); |
| 4617 | return am_pm; |
| 4618 | } |
| 4619 | |
| 4620 | template <> |
| 4621 | const string& |
| 4622 | __time_get_c_storage<char>::__x() const |
| 4623 | { |
| 4624 | static string s("%m/%d/%y"); |
| 4625 | return s; |
| 4626 | } |
| 4627 | |
| 4628 | template <> |
| 4629 | const wstring& |
| 4630 | __time_get_c_storage<wchar_t>::__x() const |
| 4631 | { |
| 4632 | static wstring s(L"%m/%d/%y"); |
| 4633 | return s; |
| 4634 | } |
| 4635 | |
| 4636 | template <> |
| 4637 | const string& |
| 4638 | __time_get_c_storage<char>::__X() const |
| 4639 | { |
| 4640 | static string s("%H:%M:%S"); |
| 4641 | return s; |
| 4642 | } |
| 4643 | |
| 4644 | template <> |
| 4645 | const wstring& |
| 4646 | __time_get_c_storage<wchar_t>::__X() const |
| 4647 | { |
| 4648 | static wstring s(L"%H:%M:%S"); |
| 4649 | return s; |
| 4650 | } |
| 4651 | |
| 4652 | template <> |
| 4653 | const string& |
| 4654 | __time_get_c_storage<char>::__c() const |
| 4655 | { |
| 4656 | static string s("%a %b %d %H:%M:%S %Y"); |
| 4657 | return s; |
| 4658 | } |
| 4659 | |
| 4660 | template <> |
| 4661 | const wstring& |
| 4662 | __time_get_c_storage<wchar_t>::__c() const |
| 4663 | { |
| 4664 | static wstring s(L"%a %b %d %H:%M:%S %Y"); |
| 4665 | return s; |
| 4666 | } |
| 4667 | |
| 4668 | template <> |
| 4669 | const string& |
| 4670 | __time_get_c_storage<char>::__r() const |
| 4671 | { |
| 4672 | static string s("%I:%M:%S %p"); |
| 4673 | return s; |
| 4674 | } |
| 4675 | |
| 4676 | template <> |
| 4677 | const wstring& |
| 4678 | __time_get_c_storage<wchar_t>::__r() const |
| 4679 | { |
| 4680 | static wstring s(L"%I:%M:%S %p"); |
| 4681 | return s; |
| 4682 | } |
| 4683 | |
| 4684 | // time_get_byname |
| 4685 | |
| 4686 | __time_get::__time_get(const char* nm) |
| 4687 | : __loc_(newlocale(LC_ALL_MASK, nm, 0)) |
| 4688 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 4689 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4690 | if (__loc_ == 0) |
| 4691 | throw runtime_error("time_get_byname" |
| 4692 | " failed to construct for " + string(nm)); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 4693 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4694 | } |
| 4695 | |
| 4696 | __time_get::__time_get(const string& nm) |
| 4697 | : __loc_(newlocale(LC_ALL_MASK, nm.c_str(), 0)) |
| 4698 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 4699 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4700 | if (__loc_ == 0) |
| 4701 | throw runtime_error("time_get_byname" |
| 4702 | " failed to construct for " + nm); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 4703 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4704 | } |
| 4705 | |
| 4706 | __time_get::~__time_get() |
| 4707 | { |
| 4708 | freelocale(__loc_); |
| 4709 | } |
Marshall Clow | a364513 | 2013-10-21 15:07:28 +0000 | [diff] [blame] | 4710 | #if defined(__clang__) |
Howard Hinnant | 335b151 | 2012-02-20 16:51:43 +0000 | [diff] [blame] | 4711 | #pragma clang diagnostic ignored "-Wmissing-field-initializers" |
Marshall Clow | a364513 | 2013-10-21 15:07:28 +0000 | [diff] [blame] | 4712 | #endif |
| 4713 | #if defined(__GNUG__) |
Howard Hinnant | 21772ec | 2012-12-28 18:15:01 +0000 | [diff] [blame] | 4714 | #pragma GCC diagnostic ignored "-Wmissing-field-initializers" |
Marshall Clow | a364513 | 2013-10-21 15:07:28 +0000 | [diff] [blame] | 4715 | #endif |
Howard Hinnant | 335b151 | 2012-02-20 16:51:43 +0000 | [diff] [blame] | 4716 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4717 | template <> |
| 4718 | string |
| 4719 | __time_get_storage<char>::__analyze(char fmt, const ctype<char>& ct) |
| 4720 | { |
Howard Hinnant | 3074a05 | 2012-02-19 14:55:32 +0000 | [diff] [blame] | 4721 | tm t = {0}; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4722 | t.tm_sec = 59; |
| 4723 | t.tm_min = 55; |
| 4724 | t.tm_hour = 23; |
| 4725 | t.tm_mday = 31; |
| 4726 | t.tm_mon = 11; |
| 4727 | t.tm_year = 161; |
| 4728 | t.tm_wday = 6; |
| 4729 | t.tm_yday = 364; |
| 4730 | t.tm_isdst = -1; |
| 4731 | char buf[100]; |
| 4732 | char f[3] = {0}; |
| 4733 | f[0] = '%'; |
| 4734 | f[1] = fmt; |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 4735 | size_t n = strftime_l(buf, countof(buf), f, &t, __loc_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4736 | char* bb = buf; |
| 4737 | char* be = buf + n; |
| 4738 | string result; |
| 4739 | while (bb != be) |
| 4740 | { |
| 4741 | if (ct.is(ctype_base::space, *bb)) |
| 4742 | { |
| 4743 | result.push_back(' '); |
| 4744 | for (++bb; bb != be && ct.is(ctype_base::space, *bb); ++bb) |
| 4745 | ; |
| 4746 | continue; |
| 4747 | } |
| 4748 | char* w = bb; |
| 4749 | ios_base::iostate err = ios_base::goodbit; |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 4750 | ptrdiff_t i = __scan_keyword(w, be, this->__weeks_, this->__weeks_+14, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4751 | ct, err, false) |
| 4752 | - this->__weeks_; |
| 4753 | if (i < 14) |
| 4754 | { |
| 4755 | result.push_back('%'); |
| 4756 | if (i < 7) |
| 4757 | result.push_back('A'); |
| 4758 | else |
| 4759 | result.push_back('a'); |
| 4760 | bb = w; |
| 4761 | continue; |
| 4762 | } |
| 4763 | w = bb; |
| 4764 | i = __scan_keyword(w, be, this->__months_, this->__months_+24, |
| 4765 | ct, err, false) |
| 4766 | - this->__months_; |
| 4767 | if (i < 24) |
| 4768 | { |
| 4769 | result.push_back('%'); |
| 4770 | if (i < 12) |
| 4771 | result.push_back('B'); |
| 4772 | else |
| 4773 | result.push_back('b'); |
| 4774 | if (fmt == 'x' && ct.is(ctype_base::digit, this->__months_[i][0])) |
| 4775 | result.back() = 'm'; |
| 4776 | bb = w; |
| 4777 | continue; |
| 4778 | } |
| 4779 | if (this->__am_pm_[0].size() + this->__am_pm_[1].size() > 0) |
| 4780 | { |
| 4781 | w = bb; |
| 4782 | i = __scan_keyword(w, be, this->__am_pm_, this->__am_pm_+2, |
| 4783 | ct, err, false) - this->__am_pm_; |
| 4784 | if (i < 2) |
| 4785 | { |
| 4786 | result.push_back('%'); |
| 4787 | result.push_back('p'); |
| 4788 | bb = w; |
| 4789 | continue; |
| 4790 | } |
| 4791 | } |
| 4792 | w = bb; |
| 4793 | if (ct.is(ctype_base::digit, *bb)) |
| 4794 | { |
| 4795 | switch(__get_up_to_n_digits(bb, be, err, ct, 4)) |
| 4796 | { |
| 4797 | case 6: |
| 4798 | result.push_back('%'); |
| 4799 | result.push_back('w'); |
| 4800 | break; |
| 4801 | case 7: |
| 4802 | result.push_back('%'); |
| 4803 | result.push_back('u'); |
| 4804 | break; |
| 4805 | case 11: |
| 4806 | result.push_back('%'); |
| 4807 | result.push_back('I'); |
| 4808 | break; |
| 4809 | case 12: |
| 4810 | result.push_back('%'); |
| 4811 | result.push_back('m'); |
| 4812 | break; |
| 4813 | case 23: |
| 4814 | result.push_back('%'); |
| 4815 | result.push_back('H'); |
| 4816 | break; |
| 4817 | case 31: |
| 4818 | result.push_back('%'); |
| 4819 | result.push_back('d'); |
| 4820 | break; |
| 4821 | case 55: |
| 4822 | result.push_back('%'); |
| 4823 | result.push_back('M'); |
| 4824 | break; |
| 4825 | case 59: |
| 4826 | result.push_back('%'); |
| 4827 | result.push_back('S'); |
| 4828 | break; |
| 4829 | case 61: |
| 4830 | result.push_back('%'); |
| 4831 | result.push_back('y'); |
| 4832 | break; |
| 4833 | case 364: |
| 4834 | result.push_back('%'); |
| 4835 | result.push_back('j'); |
| 4836 | break; |
| 4837 | case 2061: |
| 4838 | result.push_back('%'); |
| 4839 | result.push_back('Y'); |
| 4840 | break; |
| 4841 | default: |
| 4842 | for (; w != bb; ++w) |
| 4843 | result.push_back(*w); |
| 4844 | break; |
| 4845 | } |
| 4846 | continue; |
| 4847 | } |
| 4848 | if (*bb == '%') |
| 4849 | { |
| 4850 | result.push_back('%'); |
| 4851 | result.push_back('%'); |
| 4852 | ++bb; |
| 4853 | continue; |
| 4854 | } |
| 4855 | result.push_back(*bb); |
| 4856 | ++bb; |
| 4857 | } |
| 4858 | return result; |
| 4859 | } |
| 4860 | |
Marshall Clow | a364513 | 2013-10-21 15:07:28 +0000 | [diff] [blame] | 4861 | #if defined(__clang__) |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 4862 | #pragma clang diagnostic ignored "-Wmissing-braces" |
Marshall Clow | a364513 | 2013-10-21 15:07:28 +0000 | [diff] [blame] | 4863 | #endif |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 4864 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4865 | template <> |
| 4866 | wstring |
| 4867 | __time_get_storage<wchar_t>::__analyze(char fmt, const ctype<wchar_t>& ct) |
| 4868 | { |
Howard Hinnant | 3074a05 | 2012-02-19 14:55:32 +0000 | [diff] [blame] | 4869 | tm t = {0}; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4870 | t.tm_sec = 59; |
| 4871 | t.tm_min = 55; |
| 4872 | t.tm_hour = 23; |
| 4873 | t.tm_mday = 31; |
| 4874 | t.tm_mon = 11; |
| 4875 | t.tm_year = 161; |
| 4876 | t.tm_wday = 6; |
| 4877 | t.tm_yday = 364; |
| 4878 | t.tm_isdst = -1; |
| 4879 | char buf[100]; |
| 4880 | char f[3] = {0}; |
| 4881 | f[0] = '%'; |
| 4882 | f[1] = fmt; |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 4883 | strftime_l(buf, countof(buf), f, &t, __loc_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4884 | wchar_t wbuf[100]; |
| 4885 | wchar_t* wbb = wbuf; |
| 4886 | mbstate_t mb = {0}; |
| 4887 | const char* bb = buf; |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 4888 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 4889 | size_t j = mbsrtowcs_l( wbb, &bb, countof(wbuf), &mb, __loc_); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 4890 | #else |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 4891 | size_t j = __mbsrtowcs_l( wbb, &bb, countof(wbuf), &mb, __loc_); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 4892 | #endif |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 4893 | if (j == size_t(-1)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4894 | __throw_runtime_error("locale not supported"); |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 4895 | wchar_t* wbe = wbb + j; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4896 | wstring result; |
| 4897 | while (wbb != wbe) |
| 4898 | { |
| 4899 | if (ct.is(ctype_base::space, *wbb)) |
| 4900 | { |
| 4901 | result.push_back(L' '); |
| 4902 | for (++wbb; wbb != wbe && ct.is(ctype_base::space, *wbb); ++wbb) |
| 4903 | ; |
| 4904 | continue; |
| 4905 | } |
| 4906 | wchar_t* w = wbb; |
| 4907 | ios_base::iostate err = ios_base::goodbit; |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 4908 | ptrdiff_t i = __scan_keyword(w, wbe, this->__weeks_, this->__weeks_+14, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4909 | ct, err, false) |
| 4910 | - this->__weeks_; |
| 4911 | if (i < 14) |
| 4912 | { |
| 4913 | result.push_back(L'%'); |
| 4914 | if (i < 7) |
| 4915 | result.push_back(L'A'); |
| 4916 | else |
| 4917 | result.push_back(L'a'); |
| 4918 | wbb = w; |
| 4919 | continue; |
| 4920 | } |
| 4921 | w = wbb; |
| 4922 | i = __scan_keyword(w, wbe, this->__months_, this->__months_+24, |
| 4923 | ct, err, false) |
| 4924 | - this->__months_; |
| 4925 | if (i < 24) |
| 4926 | { |
| 4927 | result.push_back(L'%'); |
| 4928 | if (i < 12) |
| 4929 | result.push_back(L'B'); |
| 4930 | else |
| 4931 | result.push_back(L'b'); |
| 4932 | if (fmt == 'x' && ct.is(ctype_base::digit, this->__months_[i][0])) |
| 4933 | result.back() = L'm'; |
| 4934 | wbb = w; |
| 4935 | continue; |
| 4936 | } |
| 4937 | if (this->__am_pm_[0].size() + this->__am_pm_[1].size() > 0) |
| 4938 | { |
| 4939 | w = wbb; |
| 4940 | i = __scan_keyword(w, wbe, this->__am_pm_, this->__am_pm_+2, |
| 4941 | ct, err, false) - this->__am_pm_; |
| 4942 | if (i < 2) |
| 4943 | { |
| 4944 | result.push_back(L'%'); |
| 4945 | result.push_back(L'p'); |
| 4946 | wbb = w; |
| 4947 | continue; |
| 4948 | } |
| 4949 | } |
| 4950 | w = wbb; |
| 4951 | if (ct.is(ctype_base::digit, *wbb)) |
| 4952 | { |
| 4953 | switch(__get_up_to_n_digits(wbb, wbe, err, ct, 4)) |
| 4954 | { |
| 4955 | case 6: |
| 4956 | result.push_back(L'%'); |
| 4957 | result.push_back(L'w'); |
| 4958 | break; |
| 4959 | case 7: |
| 4960 | result.push_back(L'%'); |
| 4961 | result.push_back(L'u'); |
| 4962 | break; |
| 4963 | case 11: |
| 4964 | result.push_back(L'%'); |
| 4965 | result.push_back(L'I'); |
| 4966 | break; |
| 4967 | case 12: |
| 4968 | result.push_back(L'%'); |
| 4969 | result.push_back(L'm'); |
| 4970 | break; |
| 4971 | case 23: |
| 4972 | result.push_back(L'%'); |
| 4973 | result.push_back(L'H'); |
| 4974 | break; |
| 4975 | case 31: |
| 4976 | result.push_back(L'%'); |
| 4977 | result.push_back(L'd'); |
| 4978 | break; |
| 4979 | case 55: |
| 4980 | result.push_back(L'%'); |
| 4981 | result.push_back(L'M'); |
| 4982 | break; |
| 4983 | case 59: |
| 4984 | result.push_back(L'%'); |
| 4985 | result.push_back(L'S'); |
| 4986 | break; |
| 4987 | case 61: |
| 4988 | result.push_back(L'%'); |
| 4989 | result.push_back(L'y'); |
| 4990 | break; |
| 4991 | case 364: |
| 4992 | result.push_back(L'%'); |
| 4993 | result.push_back(L'j'); |
| 4994 | break; |
| 4995 | case 2061: |
| 4996 | result.push_back(L'%'); |
| 4997 | result.push_back(L'Y'); |
| 4998 | break; |
| 4999 | default: |
| 5000 | for (; w != wbb; ++w) |
| 5001 | result.push_back(*w); |
| 5002 | break; |
| 5003 | } |
| 5004 | continue; |
| 5005 | } |
| 5006 | if (ct.narrow(*wbb, 0) == '%') |
| 5007 | { |
| 5008 | result.push_back(L'%'); |
| 5009 | result.push_back(L'%'); |
| 5010 | ++wbb; |
| 5011 | continue; |
| 5012 | } |
| 5013 | result.push_back(*wbb); |
| 5014 | ++wbb; |
| 5015 | } |
| 5016 | return result; |
| 5017 | } |
| 5018 | |
| 5019 | template <> |
| 5020 | void |
| 5021 | __time_get_storage<char>::init(const ctype<char>& ct) |
| 5022 | { |
Howard Hinnant | cd99236 | 2012-08-02 18:44:17 +0000 | [diff] [blame] | 5023 | tm t = {0}; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5024 | char buf[100]; |
| 5025 | // __weeks_ |
| 5026 | for (int i = 0; i < 7; ++i) |
| 5027 | { |
| 5028 | t.tm_wday = i; |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5029 | strftime_l(buf, countof(buf), "%A", &t, __loc_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5030 | __weeks_[i] = buf; |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5031 | strftime_l(buf, countof(buf), "%a", &t, __loc_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5032 | __weeks_[i+7] = buf; |
| 5033 | } |
| 5034 | // __months_ |
| 5035 | for (int i = 0; i < 12; ++i) |
| 5036 | { |
| 5037 | t.tm_mon = i; |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5038 | strftime_l(buf, countof(buf), "%B", &t, __loc_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5039 | __months_[i] = buf; |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5040 | strftime_l(buf, countof(buf), "%b", &t, __loc_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5041 | __months_[i+12] = buf; |
| 5042 | } |
| 5043 | // __am_pm_ |
| 5044 | t.tm_hour = 1; |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5045 | strftime_l(buf, countof(buf), "%p", &t, __loc_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5046 | __am_pm_[0] = buf; |
| 5047 | t.tm_hour = 13; |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5048 | strftime_l(buf, countof(buf), "%p", &t, __loc_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5049 | __am_pm_[1] = buf; |
| 5050 | __c_ = __analyze('c', ct); |
| 5051 | __r_ = __analyze('r', ct); |
| 5052 | __x_ = __analyze('x', ct); |
| 5053 | __X_ = __analyze('X', ct); |
| 5054 | } |
| 5055 | |
| 5056 | template <> |
| 5057 | void |
| 5058 | __time_get_storage<wchar_t>::init(const ctype<wchar_t>& ct) |
| 5059 | { |
| 5060 | tm t = {0}; |
| 5061 | char buf[100]; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5062 | wchar_t wbuf[100]; |
| 5063 | wchar_t* wbe; |
| 5064 | mbstate_t mb = {0}; |
| 5065 | // __weeks_ |
| 5066 | for (int i = 0; i < 7; ++i) |
| 5067 | { |
| 5068 | t.tm_wday = i; |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5069 | strftime_l(buf, countof(buf), "%A", &t, __loc_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5070 | mb = mbstate_t(); |
| 5071 | const char* bb = buf; |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 5072 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5073 | size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5074 | #else |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5075 | size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5076 | #endif |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 5077 | if (j == size_t(-1)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5078 | __throw_runtime_error("locale not supported"); |
| 5079 | wbe = wbuf + j; |
| 5080 | __weeks_[i].assign(wbuf, wbe); |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5081 | strftime_l(buf, countof(buf), "%a", &t, __loc_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5082 | mb = mbstate_t(); |
| 5083 | bb = buf; |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 5084 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5085 | j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5086 | #else |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5087 | j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5088 | #endif |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 5089 | if (j == size_t(-1)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5090 | __throw_runtime_error("locale not supported"); |
| 5091 | wbe = wbuf + j; |
| 5092 | __weeks_[i+7].assign(wbuf, wbe); |
| 5093 | } |
| 5094 | // __months_ |
| 5095 | for (int i = 0; i < 12; ++i) |
| 5096 | { |
| 5097 | t.tm_mon = i; |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5098 | strftime_l(buf, countof(buf), "%B", &t, __loc_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5099 | mb = mbstate_t(); |
| 5100 | const char* bb = buf; |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 5101 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5102 | size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5103 | #else |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5104 | size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5105 | #endif |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 5106 | if (j == size_t(-1)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5107 | __throw_runtime_error("locale not supported"); |
| 5108 | wbe = wbuf + j; |
| 5109 | __months_[i].assign(wbuf, wbe); |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5110 | strftime_l(buf, countof(buf), "%b", &t, __loc_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5111 | mb = mbstate_t(); |
| 5112 | bb = buf; |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 5113 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5114 | j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5115 | #else |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5116 | j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5117 | #endif |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 5118 | if (j == size_t(-1)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5119 | __throw_runtime_error("locale not supported"); |
| 5120 | wbe = wbuf + j; |
| 5121 | __months_[i+12].assign(wbuf, wbe); |
| 5122 | } |
| 5123 | // __am_pm_ |
| 5124 | t.tm_hour = 1; |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5125 | strftime_l(buf, countof(buf), "%p", &t, __loc_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5126 | mb = mbstate_t(); |
| 5127 | const char* bb = buf; |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 5128 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5129 | size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5130 | #else |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5131 | size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5132 | #endif |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 5133 | if (j == size_t(-1)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5134 | __throw_runtime_error("locale not supported"); |
| 5135 | wbe = wbuf + j; |
| 5136 | __am_pm_[0].assign(wbuf, wbe); |
| 5137 | t.tm_hour = 13; |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5138 | strftime_l(buf, countof(buf), "%p", &t, __loc_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5139 | mb = mbstate_t(); |
| 5140 | bb = buf; |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 5141 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5142 | j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5143 | #else |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5144 | j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5145 | #endif |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 5146 | if (j == size_t(-1)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5147 | __throw_runtime_error("locale not supported"); |
| 5148 | wbe = wbuf + j; |
| 5149 | __am_pm_[1].assign(wbuf, wbe); |
| 5150 | __c_ = __analyze('c', ct); |
| 5151 | __r_ = __analyze('r', ct); |
| 5152 | __x_ = __analyze('x', ct); |
| 5153 | __X_ = __analyze('X', ct); |
| 5154 | } |
| 5155 | |
| 5156 | template <class CharT> |
| 5157 | struct _LIBCPP_HIDDEN __time_get_temp |
| 5158 | : public ctype_byname<CharT> |
| 5159 | { |
| 5160 | explicit __time_get_temp(const char* nm) |
| 5161 | : ctype_byname<CharT>(nm, 1) {} |
| 5162 | explicit __time_get_temp(const string& nm) |
| 5163 | : ctype_byname<CharT>(nm, 1) {} |
| 5164 | }; |
| 5165 | |
| 5166 | template <> |
| 5167 | __time_get_storage<char>::__time_get_storage(const char* __nm) |
| 5168 | : __time_get(__nm) |
| 5169 | { |
| 5170 | const __time_get_temp<char> ct(__nm); |
| 5171 | init(ct); |
| 5172 | } |
| 5173 | |
| 5174 | template <> |
| 5175 | __time_get_storage<char>::__time_get_storage(const string& __nm) |
| 5176 | : __time_get(__nm) |
| 5177 | { |
| 5178 | const __time_get_temp<char> ct(__nm); |
| 5179 | init(ct); |
| 5180 | } |
| 5181 | |
| 5182 | template <> |
| 5183 | __time_get_storage<wchar_t>::__time_get_storage(const char* __nm) |
| 5184 | : __time_get(__nm) |
| 5185 | { |
| 5186 | const __time_get_temp<wchar_t> ct(__nm); |
| 5187 | init(ct); |
| 5188 | } |
| 5189 | |
| 5190 | template <> |
| 5191 | __time_get_storage<wchar_t>::__time_get_storage(const string& __nm) |
| 5192 | : __time_get(__nm) |
| 5193 | { |
| 5194 | const __time_get_temp<wchar_t> ct(__nm); |
| 5195 | init(ct); |
| 5196 | } |
| 5197 | |
| 5198 | template <> |
| 5199 | time_base::dateorder |
| 5200 | __time_get_storage<char>::__do_date_order() const |
| 5201 | { |
| 5202 | unsigned i; |
| 5203 | for (i = 0; i < __x_.size(); ++i) |
| 5204 | if (__x_[i] == '%') |
| 5205 | break; |
| 5206 | ++i; |
| 5207 | switch (__x_[i]) |
| 5208 | { |
| 5209 | case 'y': |
| 5210 | case 'Y': |
| 5211 | for (++i; i < __x_.size(); ++i) |
| 5212 | if (__x_[i] == '%') |
| 5213 | break; |
| 5214 | if (i == __x_.size()) |
| 5215 | break; |
| 5216 | ++i; |
| 5217 | switch (__x_[i]) |
| 5218 | { |
| 5219 | case 'm': |
| 5220 | for (++i; i < __x_.size(); ++i) |
| 5221 | if (__x_[i] == '%') |
| 5222 | break; |
| 5223 | if (i == __x_.size()) |
| 5224 | break; |
| 5225 | ++i; |
| 5226 | if (__x_[i] == 'd') |
| 5227 | return time_base::ymd; |
| 5228 | break; |
| 5229 | case 'd': |
| 5230 | for (++i; i < __x_.size(); ++i) |
| 5231 | if (__x_[i] == '%') |
| 5232 | break; |
| 5233 | if (i == __x_.size()) |
| 5234 | break; |
| 5235 | ++i; |
| 5236 | if (__x_[i] == 'm') |
| 5237 | return time_base::ydm; |
| 5238 | break; |
| 5239 | } |
| 5240 | break; |
| 5241 | case 'm': |
| 5242 | for (++i; i < __x_.size(); ++i) |
| 5243 | if (__x_[i] == '%') |
| 5244 | break; |
| 5245 | if (i == __x_.size()) |
| 5246 | break; |
| 5247 | ++i; |
| 5248 | if (__x_[i] == 'd') |
| 5249 | { |
| 5250 | for (++i; i < __x_.size(); ++i) |
| 5251 | if (__x_[i] == '%') |
| 5252 | break; |
| 5253 | if (i == __x_.size()) |
| 5254 | break; |
| 5255 | ++i; |
| 5256 | if (__x_[i] == 'y' || __x_[i] == 'Y') |
| 5257 | return time_base::mdy; |
| 5258 | break; |
| 5259 | } |
| 5260 | break; |
| 5261 | case 'd': |
| 5262 | for (++i; i < __x_.size(); ++i) |
| 5263 | if (__x_[i] == '%') |
| 5264 | break; |
| 5265 | if (i == __x_.size()) |
| 5266 | break; |
| 5267 | ++i; |
| 5268 | if (__x_[i] == 'm') |
| 5269 | { |
| 5270 | for (++i; i < __x_.size(); ++i) |
| 5271 | if (__x_[i] == '%') |
| 5272 | break; |
| 5273 | if (i == __x_.size()) |
| 5274 | break; |
| 5275 | ++i; |
| 5276 | if (__x_[i] == 'y' || __x_[i] == 'Y') |
| 5277 | return time_base::dmy; |
| 5278 | break; |
| 5279 | } |
| 5280 | break; |
| 5281 | } |
| 5282 | return time_base::no_order; |
| 5283 | } |
| 5284 | |
| 5285 | template <> |
| 5286 | time_base::dateorder |
| 5287 | __time_get_storage<wchar_t>::__do_date_order() const |
| 5288 | { |
| 5289 | unsigned i; |
| 5290 | for (i = 0; i < __x_.size(); ++i) |
| 5291 | if (__x_[i] == L'%') |
| 5292 | break; |
| 5293 | ++i; |
| 5294 | switch (__x_[i]) |
| 5295 | { |
| 5296 | case L'y': |
| 5297 | case L'Y': |
| 5298 | for (++i; i < __x_.size(); ++i) |
| 5299 | if (__x_[i] == L'%') |
| 5300 | break; |
| 5301 | if (i == __x_.size()) |
| 5302 | break; |
| 5303 | ++i; |
| 5304 | switch (__x_[i]) |
| 5305 | { |
| 5306 | case L'm': |
| 5307 | for (++i; i < __x_.size(); ++i) |
| 5308 | if (__x_[i] == L'%') |
| 5309 | break; |
| 5310 | if (i == __x_.size()) |
| 5311 | break; |
| 5312 | ++i; |
| 5313 | if (__x_[i] == L'd') |
| 5314 | return time_base::ymd; |
| 5315 | break; |
| 5316 | case L'd': |
| 5317 | for (++i; i < __x_.size(); ++i) |
| 5318 | if (__x_[i] == L'%') |
| 5319 | break; |
| 5320 | if (i == __x_.size()) |
| 5321 | break; |
| 5322 | ++i; |
| 5323 | if (__x_[i] == L'm') |
| 5324 | return time_base::ydm; |
| 5325 | break; |
| 5326 | } |
| 5327 | break; |
| 5328 | case L'm': |
| 5329 | for (++i; i < __x_.size(); ++i) |
| 5330 | if (__x_[i] == L'%') |
| 5331 | break; |
| 5332 | if (i == __x_.size()) |
| 5333 | break; |
| 5334 | ++i; |
| 5335 | if (__x_[i] == L'd') |
| 5336 | { |
| 5337 | for (++i; i < __x_.size(); ++i) |
| 5338 | if (__x_[i] == L'%') |
| 5339 | break; |
| 5340 | if (i == __x_.size()) |
| 5341 | break; |
| 5342 | ++i; |
| 5343 | if (__x_[i] == L'y' || __x_[i] == L'Y') |
| 5344 | return time_base::mdy; |
| 5345 | break; |
| 5346 | } |
| 5347 | break; |
| 5348 | case L'd': |
| 5349 | for (++i; i < __x_.size(); ++i) |
| 5350 | if (__x_[i] == L'%') |
| 5351 | break; |
| 5352 | if (i == __x_.size()) |
| 5353 | break; |
| 5354 | ++i; |
| 5355 | if (__x_[i] == L'm') |
| 5356 | { |
| 5357 | for (++i; i < __x_.size(); ++i) |
| 5358 | if (__x_[i] == L'%') |
| 5359 | break; |
| 5360 | if (i == __x_.size()) |
| 5361 | break; |
| 5362 | ++i; |
| 5363 | if (__x_[i] == L'y' || __x_[i] == L'Y') |
| 5364 | return time_base::dmy; |
| 5365 | break; |
| 5366 | } |
| 5367 | break; |
| 5368 | } |
| 5369 | return time_base::no_order; |
| 5370 | } |
| 5371 | |
| 5372 | // time_put |
| 5373 | |
| 5374 | __time_put::__time_put(const char* nm) |
| 5375 | : __loc_(newlocale(LC_ALL_MASK, nm, 0)) |
| 5376 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 5377 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5378 | if (__loc_ == 0) |
| 5379 | throw runtime_error("time_put_byname" |
| 5380 | " failed to construct for " + string(nm)); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 5381 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5382 | } |
| 5383 | |
| 5384 | __time_put::__time_put(const string& nm) |
| 5385 | : __loc_(newlocale(LC_ALL_MASK, nm.c_str(), 0)) |
| 5386 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 5387 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5388 | if (__loc_ == 0) |
| 5389 | throw runtime_error("time_put_byname" |
| 5390 | " failed to construct for " + nm); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 5391 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5392 | } |
| 5393 | |
| 5394 | __time_put::~__time_put() |
| 5395 | { |
Joerg Sonnenberger | 9a06b9d | 2013-07-02 19:46:18 +0000 | [diff] [blame] | 5396 | if (__loc_ != _LIBCPP_GET_C_LOCALE) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5397 | freelocale(__loc_); |
| 5398 | } |
| 5399 | |
| 5400 | void |
| 5401 | __time_put::__do_put(char* __nb, char*& __ne, const tm* __tm, |
| 5402 | char __fmt, char __mod) const |
| 5403 | { |
| 5404 | char fmt[] = {'%', __fmt, __mod, 0}; |
| 5405 | if (__mod != 0) |
| 5406 | swap(fmt[1], fmt[2]); |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5407 | size_t n = strftime_l(__nb, countof(__nb, __ne), fmt, __tm, __loc_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5408 | __ne = __nb + n; |
| 5409 | } |
| 5410 | |
| 5411 | void |
| 5412 | __time_put::__do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm, |
| 5413 | char __fmt, char __mod) const |
| 5414 | { |
| 5415 | char __nar[100]; |
| 5416 | char* __ne = __nar + 100; |
| 5417 | __do_put(__nar, __ne, __tm, __fmt, __mod); |
| 5418 | mbstate_t mb = {0}; |
| 5419 | const char* __nb = __nar; |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 5420 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5421 | size_t j = mbsrtowcs_l(__wb, &__nb, countof(__wb, __we), &mb, __loc_); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5422 | #else |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5423 | size_t j = __mbsrtowcs_l(__wb, &__nb, countof(__wb, __we), &mb, __loc_); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5424 | #endif |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 5425 | if (j == size_t(-1)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5426 | __throw_runtime_error("locale not supported"); |
| 5427 | __we = __wb + j; |
| 5428 | } |
| 5429 | |
| 5430 | // moneypunct_byname |
| 5431 | |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5432 | template <class charT> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5433 | static |
| 5434 | void |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5435 | __init_pat(money_base::pattern& pat, basic_string<charT>& __curr_symbol_, |
| 5436 | bool intl, char cs_precedes, char sep_by_space, char sign_posn, |
| 5437 | charT space_char) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5438 | { |
| 5439 | const char sign = static_cast<char>(money_base::sign); |
| 5440 | const char space = static_cast<char>(money_base::space); |
| 5441 | const char none = static_cast<char>(money_base::none); |
| 5442 | const char symbol = static_cast<char>(money_base::symbol); |
| 5443 | const char value = static_cast<char>(money_base::value); |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5444 | const bool symbol_contains_sep = intl && __curr_symbol_.size() == 4; |
| 5445 | |
| 5446 | // Comments on case branches reflect 'C11 7.11.2.1 The localeconv |
| 5447 | // function'. "Space between sign and symbol or value" means that |
| 5448 | // if the sign is adjacent to the symbol, there's a space between |
| 5449 | // them, and otherwise there's a space between the sign and value. |
| 5450 | // |
| 5451 | // C11's localeconv specifies that the fourth character of an |
| 5452 | // international curr_symbol is used to separate the sign and |
| 5453 | // value when sep_by_space says to do so. C++ can't represent |
| 5454 | // that, so we just use a space. When sep_by_space says to |
| 5455 | // separate the symbol and value-or-sign with a space, we rearrange the |
| 5456 | // curr_symbol to put its spacing character on the correct side of |
| 5457 | // the symbol. |
| 5458 | // |
| 5459 | // We also need to avoid adding an extra space between the sign |
| 5460 | // and value when the currency symbol is suppressed (by not |
| 5461 | // setting showbase). We match glibc's strfmon by interpreting |
| 5462 | // sep_by_space==1 as "omit the space when the currency symbol is |
| 5463 | // absent". |
| 5464 | // |
| 5465 | // Users who want to get this right should use ICU instead. |
| 5466 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5467 | switch (cs_precedes) |
| 5468 | { |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5469 | case 0: // value before curr_symbol |
| 5470 | if (symbol_contains_sep) { |
| 5471 | // Move the separator to before the symbol, to place it |
| 5472 | // between the value and symbol. |
| 5473 | rotate(__curr_symbol_.begin(), __curr_symbol_.begin() + 3, |
| 5474 | __curr_symbol_.end()); |
| 5475 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5476 | switch (sign_posn) |
| 5477 | { |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5478 | case 0: // Parentheses surround the quantity and currency symbol. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5479 | pat.field[0] = sign; |
| 5480 | pat.field[1] = value; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5481 | pat.field[2] = none; // Any space appears in the symbol. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5482 | pat.field[3] = symbol; |
| 5483 | switch (sep_by_space) |
| 5484 | { |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5485 | case 0: // No space separates the currency symbol and value. |
| 5486 | // This case may have changed between C99 and C11; |
| 5487 | // assume the currency symbol matches the intention. |
| 5488 | case 2: // Space between sign and currency or value. |
| 5489 | // The "sign" is two parentheses, so no space here either. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5490 | return; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5491 | case 1: // Space between currency-and-sign or currency and value. |
| 5492 | if (!symbol_contains_sep) { |
| 5493 | // We insert the space into the symbol instead of |
| 5494 | // setting pat.field[2]=space so that when |
| 5495 | // showbase is not set, the space goes away too. |
| 5496 | __curr_symbol_.insert(0, 1, space_char); |
| 5497 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5498 | return; |
| 5499 | default: |
| 5500 | break; |
| 5501 | } |
| 5502 | break; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5503 | case 1: // The sign string precedes the quantity and currency symbol. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5504 | pat.field[0] = sign; |
| 5505 | pat.field[3] = symbol; |
| 5506 | switch (sep_by_space) |
| 5507 | { |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5508 | case 0: // No space separates the currency symbol and value. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5509 | pat.field[1] = value; |
| 5510 | pat.field[2] = none; |
| 5511 | return; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5512 | case 1: // Space between currency-and-sign or currency and value. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5513 | pat.field[1] = value; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5514 | pat.field[2] = none; |
| 5515 | if (!symbol_contains_sep) { |
| 5516 | // We insert the space into the symbol instead of |
| 5517 | // setting pat.field[2]=space so that when |
| 5518 | // showbase is not set, the space goes away too. |
| 5519 | __curr_symbol_.insert(0, 1, space_char); |
| 5520 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5521 | return; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5522 | case 2: // Space between sign and currency or value. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5523 | pat.field[1] = space; |
| 5524 | pat.field[2] = value; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5525 | if (symbol_contains_sep) { |
| 5526 | // Remove the separator from the symbol, since it |
| 5527 | // has already appeared after the sign. |
| 5528 | __curr_symbol_.erase(__curr_symbol_.begin()); |
| 5529 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5530 | return; |
| 5531 | default: |
| 5532 | break; |
| 5533 | } |
| 5534 | break; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5535 | case 2: // The sign string succeeds the quantity and currency symbol. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5536 | pat.field[0] = value; |
| 5537 | pat.field[3] = sign; |
| 5538 | switch (sep_by_space) |
| 5539 | { |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5540 | case 0: // No space separates the currency symbol and value. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5541 | pat.field[1] = none; |
| 5542 | pat.field[2] = symbol; |
| 5543 | return; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5544 | case 1: // Space between currency-and-sign or currency and value. |
| 5545 | if (!symbol_contains_sep) { |
| 5546 | // We insert the space into the symbol instead of |
| 5547 | // setting pat.field[1]=space so that when |
| 5548 | // showbase is not set, the space goes away too. |
| 5549 | __curr_symbol_.insert(0, 1, space_char); |
| 5550 | } |
| 5551 | pat.field[1] = none; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5552 | pat.field[2] = symbol; |
| 5553 | return; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5554 | case 2: // Space between sign and currency or value. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5555 | pat.field[1] = symbol; |
| 5556 | pat.field[2] = space; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5557 | if (symbol_contains_sep) { |
| 5558 | // Remove the separator from the symbol, since it |
| 5559 | // should not be removed if showbase is absent. |
| 5560 | __curr_symbol_.erase(__curr_symbol_.begin()); |
| 5561 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5562 | return; |
| 5563 | default: |
| 5564 | break; |
| 5565 | } |
| 5566 | break; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5567 | case 3: // The sign string immediately precedes the currency symbol. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5568 | pat.field[0] = value; |
| 5569 | pat.field[3] = symbol; |
| 5570 | switch (sep_by_space) |
| 5571 | { |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5572 | case 0: // No space separates the currency symbol and value. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5573 | pat.field[1] = none; |
| 5574 | pat.field[2] = sign; |
| 5575 | return; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5576 | case 1: // Space between currency-and-sign or currency and value. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5577 | pat.field[1] = space; |
| 5578 | pat.field[2] = sign; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5579 | if (symbol_contains_sep) { |
| 5580 | // Remove the separator from the symbol, since it |
| 5581 | // has already appeared before the sign. |
| 5582 | __curr_symbol_.erase(__curr_symbol_.begin()); |
| 5583 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5584 | return; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5585 | case 2: // Space between sign and currency or value. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5586 | pat.field[1] = sign; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5587 | pat.field[2] = none; |
| 5588 | if (!symbol_contains_sep) { |
| 5589 | // We insert the space into the symbol instead of |
| 5590 | // setting pat.field[2]=space so that when |
| 5591 | // showbase is not set, the space goes away too. |
| 5592 | __curr_symbol_.insert(0, 1, space_char); |
| 5593 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5594 | return; |
| 5595 | default: |
| 5596 | break; |
| 5597 | } |
| 5598 | break; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5599 | case 4: // The sign string immediately succeeds the currency symbol. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5600 | pat.field[0] = value; |
| 5601 | pat.field[3] = sign; |
| 5602 | switch (sep_by_space) |
| 5603 | { |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5604 | case 0: // No space separates the currency symbol and value. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5605 | pat.field[1] = none; |
| 5606 | pat.field[2] = symbol; |
| 5607 | return; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5608 | case 1: // Space between currency-and-sign or currency and value. |
| 5609 | pat.field[1] = none; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5610 | pat.field[2] = symbol; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5611 | if (!symbol_contains_sep) { |
| 5612 | // We insert the space into the symbol instead of |
| 5613 | // setting pat.field[1]=space so that when |
| 5614 | // showbase is not set, the space goes away too. |
| 5615 | __curr_symbol_.insert(0, 1, space_char); |
| 5616 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5617 | return; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5618 | case 2: // Space between sign and currency or value. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5619 | pat.field[1] = symbol; |
| 5620 | pat.field[2] = space; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5621 | if (symbol_contains_sep) { |
| 5622 | // Remove the separator from the symbol, since it |
| 5623 | // should not disappear when showbase is absent. |
| 5624 | __curr_symbol_.erase(__curr_symbol_.begin()); |
| 5625 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5626 | return; |
| 5627 | default: |
| 5628 | break; |
| 5629 | } |
| 5630 | break; |
| 5631 | default: |
| 5632 | break; |
| 5633 | } |
| 5634 | break; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5635 | case 1: // curr_symbol before value |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5636 | switch (sign_posn) |
| 5637 | { |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5638 | case 0: // Parentheses surround the quantity and currency symbol. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5639 | pat.field[0] = sign; |
| 5640 | pat.field[1] = symbol; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5641 | pat.field[2] = none; // Any space appears in the symbol. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5642 | pat.field[3] = value; |
| 5643 | switch (sep_by_space) |
| 5644 | { |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5645 | case 0: // No space separates the currency symbol and value. |
| 5646 | // This case may have changed between C99 and C11; |
| 5647 | // assume the currency symbol matches the intention. |
| 5648 | case 2: // Space between sign and currency or value. |
| 5649 | // The "sign" is two parentheses, so no space here either. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5650 | return; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5651 | case 1: // Space between currency-and-sign or currency and value. |
| 5652 | if (!symbol_contains_sep) { |
| 5653 | // We insert the space into the symbol instead of |
| 5654 | // setting pat.field[2]=space so that when |
| 5655 | // showbase is not set, the space goes away too. |
| 5656 | __curr_symbol_.insert(0, 1, space_char); |
| 5657 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5658 | return; |
| 5659 | default: |
| 5660 | break; |
| 5661 | } |
| 5662 | break; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5663 | case 1: // The sign string precedes the quantity and currency symbol. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5664 | pat.field[0] = sign; |
| 5665 | pat.field[3] = value; |
| 5666 | switch (sep_by_space) |
| 5667 | { |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5668 | case 0: // No space separates the currency symbol and value. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5669 | pat.field[1] = symbol; |
| 5670 | pat.field[2] = none; |
| 5671 | return; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5672 | case 1: // Space between currency-and-sign or currency and value. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5673 | pat.field[1] = symbol; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5674 | pat.field[2] = none; |
| 5675 | if (!symbol_contains_sep) { |
| 5676 | // We insert the space into the symbol instead of |
| 5677 | // setting pat.field[2]=space so that when |
| 5678 | // showbase is not set, the space goes away too. |
| 5679 | __curr_symbol_.push_back(space_char); |
| 5680 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5681 | return; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5682 | case 2: // Space between sign and currency or value. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5683 | pat.field[1] = space; |
| 5684 | pat.field[2] = symbol; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5685 | if (symbol_contains_sep) { |
| 5686 | // Remove the separator from the symbol, since it |
| 5687 | // has already appeared after the sign. |
| 5688 | __curr_symbol_.pop_back(); |
| 5689 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5690 | return; |
| 5691 | default: |
| 5692 | break; |
| 5693 | } |
| 5694 | break; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5695 | case 2: // The sign string succeeds the quantity and currency symbol. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5696 | pat.field[0] = symbol; |
| 5697 | pat.field[3] = sign; |
| 5698 | switch (sep_by_space) |
| 5699 | { |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5700 | case 0: // No space separates the currency symbol and value. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5701 | pat.field[1] = none; |
| 5702 | pat.field[2] = value; |
| 5703 | return; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5704 | case 1: // Space between currency-and-sign or currency and value. |
| 5705 | pat.field[1] = none; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5706 | pat.field[2] = value; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5707 | if (!symbol_contains_sep) { |
| 5708 | // We insert the space into the symbol instead of |
| 5709 | // setting pat.field[1]=space so that when |
| 5710 | // showbase is not set, the space goes away too. |
| 5711 | __curr_symbol_.push_back(space_char); |
| 5712 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5713 | return; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5714 | case 2: // Space between sign and currency or value. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5715 | pat.field[1] = value; |
| 5716 | pat.field[2] = space; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5717 | if (symbol_contains_sep) { |
| 5718 | // Remove the separator from the symbol, since it |
| 5719 | // will appear before the sign. |
| 5720 | __curr_symbol_.pop_back(); |
| 5721 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5722 | return; |
| 5723 | default: |
| 5724 | break; |
| 5725 | } |
| 5726 | break; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5727 | case 3: // The sign string immediately precedes the currency symbol. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5728 | pat.field[0] = sign; |
| 5729 | pat.field[3] = value; |
| 5730 | switch (sep_by_space) |
| 5731 | { |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5732 | case 0: // No space separates the currency symbol and value. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5733 | pat.field[1] = symbol; |
| 5734 | pat.field[2] = none; |
| 5735 | return; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5736 | case 1: // Space between currency-and-sign or currency and value. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5737 | pat.field[1] = symbol; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5738 | pat.field[2] = none; |
| 5739 | if (!symbol_contains_sep) { |
| 5740 | // We insert the space into the symbol instead of |
| 5741 | // setting pat.field[2]=space so that when |
| 5742 | // showbase is not set, the space goes away too. |
| 5743 | __curr_symbol_.push_back(space_char); |
| 5744 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5745 | return; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5746 | case 2: // Space between sign and currency or value. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5747 | pat.field[1] = space; |
| 5748 | pat.field[2] = symbol; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5749 | if (symbol_contains_sep) { |
| 5750 | // Remove the separator from the symbol, since it |
| 5751 | // has already appeared after the sign. |
| 5752 | __curr_symbol_.pop_back(); |
| 5753 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5754 | return; |
| 5755 | default: |
| 5756 | break; |
| 5757 | } |
| 5758 | break; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5759 | case 4: // The sign string immediately succeeds the currency symbol. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5760 | pat.field[0] = symbol; |
| 5761 | pat.field[3] = value; |
| 5762 | switch (sep_by_space) |
| 5763 | { |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5764 | case 0: // No space separates the currency symbol and value. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5765 | pat.field[1] = sign; |
| 5766 | pat.field[2] = none; |
| 5767 | return; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5768 | case 1: // Space between currency-and-sign or currency and value. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5769 | pat.field[1] = sign; |
| 5770 | pat.field[2] = space; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5771 | if (symbol_contains_sep) { |
| 5772 | // Remove the separator from the symbol, since it |
| 5773 | // should not disappear when showbase is absent. |
| 5774 | __curr_symbol_.pop_back(); |
| 5775 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5776 | return; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5777 | case 2: // Space between sign and currency or value. |
| 5778 | pat.field[1] = none; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5779 | pat.field[2] = sign; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5780 | if (!symbol_contains_sep) { |
| 5781 | // We insert the space into the symbol instead of |
| 5782 | // setting pat.field[1]=space so that when |
| 5783 | // showbase is not set, the space goes away too. |
| 5784 | __curr_symbol_.push_back(space_char); |
| 5785 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5786 | return; |
| 5787 | default: |
| 5788 | break; |
| 5789 | } |
| 5790 | break; |
| 5791 | default: |
| 5792 | break; |
| 5793 | } |
| 5794 | break; |
| 5795 | default: |
| 5796 | break; |
| 5797 | } |
| 5798 | pat.field[0] = symbol; |
| 5799 | pat.field[1] = sign; |
| 5800 | pat.field[2] = none; |
| 5801 | pat.field[3] = value; |
| 5802 | } |
| 5803 | |
| 5804 | template<> |
| 5805 | void |
| 5806 | moneypunct_byname<char, false>::init(const char* nm) |
| 5807 | { |
| 5808 | typedef moneypunct<char, false> base; |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5809 | __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 5810 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 05b57d5 | 2012-03-07 20:37:43 +0000 | [diff] [blame] | 5811 | if (loc == nullptr) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5812 | throw runtime_error("moneypunct_byname" |
| 5813 | " failed to construct for " + string(nm)); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 5814 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 5815 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5816 | lconv* lc = localeconv_l(loc.get()); |
| 5817 | #else |
| 5818 | lconv* lc = __localeconv_l(loc.get()); |
| 5819 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5820 | if (*lc->mon_decimal_point) |
| 5821 | __decimal_point_ = *lc->mon_decimal_point; |
| 5822 | else |
| 5823 | __decimal_point_ = base::do_decimal_point(); |
| 5824 | if (*lc->mon_thousands_sep) |
| 5825 | __thousands_sep_ = *lc->mon_thousands_sep; |
| 5826 | else |
| 5827 | __thousands_sep_ = base::do_thousands_sep(); |
| 5828 | __grouping_ = lc->mon_grouping; |
| 5829 | __curr_symbol_ = lc->currency_symbol; |
| 5830 | if (lc->frac_digits != CHAR_MAX) |
| 5831 | __frac_digits_ = lc->frac_digits; |
| 5832 | else |
| 5833 | __frac_digits_ = base::do_frac_digits(); |
| 5834 | if (lc->p_sign_posn == 0) |
| 5835 | __positive_sign_ = "()"; |
| 5836 | else |
| 5837 | __positive_sign_ = lc->positive_sign; |
| 5838 | if (lc->n_sign_posn == 0) |
| 5839 | __negative_sign_ = "()"; |
| 5840 | else |
| 5841 | __negative_sign_ = lc->negative_sign; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5842 | // Assume the positive and negative formats will want spaces in |
| 5843 | // the same places in curr_symbol since there's no way to |
| 5844 | // represent anything else. |
| 5845 | string_type __dummy_curr_symbol = __curr_symbol_; |
| 5846 | __init_pat(__pos_format_, __dummy_curr_symbol, false, |
| 5847 | lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn, ' '); |
| 5848 | __init_pat(__neg_format_, __curr_symbol_, false, |
| 5849 | lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn, ' '); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5850 | } |
| 5851 | |
| 5852 | template<> |
| 5853 | void |
| 5854 | moneypunct_byname<char, true>::init(const char* nm) |
| 5855 | { |
| 5856 | typedef moneypunct<char, true> base; |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5857 | __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 5858 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 05b57d5 | 2012-03-07 20:37:43 +0000 | [diff] [blame] | 5859 | if (loc == nullptr) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5860 | throw runtime_error("moneypunct_byname" |
| 5861 | " failed to construct for " + string(nm)); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 5862 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 5863 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5864 | lconv* lc = localeconv_l(loc.get()); |
| 5865 | #else |
| 5866 | lconv* lc = __localeconv_l(loc.get()); |
| 5867 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5868 | if (*lc->mon_decimal_point) |
| 5869 | __decimal_point_ = *lc->mon_decimal_point; |
| 5870 | else |
| 5871 | __decimal_point_ = base::do_decimal_point(); |
| 5872 | if (*lc->mon_thousands_sep) |
| 5873 | __thousands_sep_ = *lc->mon_thousands_sep; |
| 5874 | else |
| 5875 | __thousands_sep_ = base::do_thousands_sep(); |
| 5876 | __grouping_ = lc->mon_grouping; |
| 5877 | __curr_symbol_ = lc->int_curr_symbol; |
| 5878 | if (lc->int_frac_digits != CHAR_MAX) |
| 5879 | __frac_digits_ = lc->int_frac_digits; |
| 5880 | else |
| 5881 | __frac_digits_ = base::do_frac_digits(); |
Howard Hinnant | ef5aa93 | 2013-09-17 01:34:47 +0000 | [diff] [blame] | 5882 | #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__) |
Howard Hinnant | 6cd05ee | 2011-09-23 16:11:27 +0000 | [diff] [blame] | 5883 | if (lc->p_sign_posn == 0) |
Howard Hinnant | e9df0a5 | 2013-08-01 18:17:34 +0000 | [diff] [blame] | 5884 | #else // _LIBCPP_MSVCRT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5885 | if (lc->int_p_sign_posn == 0) |
Howard Hinnant | e9df0a5 | 2013-08-01 18:17:34 +0000 | [diff] [blame] | 5886 | #endif // !_LIBCPP_MSVCRT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5887 | __positive_sign_ = "()"; |
| 5888 | else |
| 5889 | __positive_sign_ = lc->positive_sign; |
Howard Hinnant | ef5aa93 | 2013-09-17 01:34:47 +0000 | [diff] [blame] | 5890 | #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__) |
Howard Hinnant | 6cd05ee | 2011-09-23 16:11:27 +0000 | [diff] [blame] | 5891 | if(lc->n_sign_posn == 0) |
Howard Hinnant | e9df0a5 | 2013-08-01 18:17:34 +0000 | [diff] [blame] | 5892 | #else // _LIBCPP_MSVCRT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5893 | if (lc->int_n_sign_posn == 0) |
Howard Hinnant | e9df0a5 | 2013-08-01 18:17:34 +0000 | [diff] [blame] | 5894 | #endif // !_LIBCPP_MSVCRT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5895 | __negative_sign_ = "()"; |
| 5896 | else |
| 5897 | __negative_sign_ = lc->negative_sign; |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5898 | // Assume the positive and negative formats will want spaces in |
| 5899 | // the same places in curr_symbol since there's no way to |
| 5900 | // represent anything else. |
| 5901 | string_type __dummy_curr_symbol = __curr_symbol_; |
Howard Hinnant | ef5aa93 | 2013-09-17 01:34:47 +0000 | [diff] [blame] | 5902 | #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__) |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5903 | __init_pat(__pos_format_, __dummy_curr_symbol, true, |
| 5904 | lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn, ' '); |
| 5905 | __init_pat(__neg_format_, __curr_symbol_, true, |
| 5906 | lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn, ' '); |
Howard Hinnant | e9df0a5 | 2013-08-01 18:17:34 +0000 | [diff] [blame] | 5907 | #else // _LIBCPP_MSVCRT |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5908 | __init_pat(__pos_format_, __dummy_curr_symbol, true, |
| 5909 | lc->int_p_cs_precedes, lc->int_p_sep_by_space, |
| 5910 | lc->int_p_sign_posn, ' '); |
| 5911 | __init_pat(__neg_format_, __curr_symbol_, true, |
| 5912 | lc->int_n_cs_precedes, lc->int_n_sep_by_space, |
| 5913 | lc->int_n_sign_posn, ' '); |
Howard Hinnant | e9df0a5 | 2013-08-01 18:17:34 +0000 | [diff] [blame] | 5914 | #endif // !_LIBCPP_MSVCRT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5915 | } |
| 5916 | |
| 5917 | template<> |
| 5918 | void |
| 5919 | moneypunct_byname<wchar_t, false>::init(const char* nm) |
| 5920 | { |
| 5921 | typedef moneypunct<wchar_t, false> base; |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5922 | __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 5923 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 05b57d5 | 2012-03-07 20:37:43 +0000 | [diff] [blame] | 5924 | if (loc == nullptr) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5925 | throw runtime_error("moneypunct_byname" |
| 5926 | " failed to construct for " + string(nm)); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 5927 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 5928 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5929 | lconv* lc = localeconv_l(loc.get()); |
| 5930 | #else |
| 5931 | lconv* lc = __localeconv_l(loc.get()); |
| 5932 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5933 | if (*lc->mon_decimal_point) |
| 5934 | __decimal_point_ = static_cast<wchar_t>(*lc->mon_decimal_point); |
| 5935 | else |
| 5936 | __decimal_point_ = base::do_decimal_point(); |
| 5937 | if (*lc->mon_thousands_sep) |
| 5938 | __thousands_sep_ = static_cast<wchar_t>(*lc->mon_thousands_sep); |
| 5939 | else |
| 5940 | __thousands_sep_ = base::do_thousands_sep(); |
| 5941 | __grouping_ = lc->mon_grouping; |
| 5942 | wchar_t wbuf[100]; |
| 5943 | mbstate_t mb = {0}; |
| 5944 | const char* bb = lc->currency_symbol; |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 5945 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5946 | size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5947 | #else |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5948 | size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5949 | #endif |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 5950 | if (j == size_t(-1)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5951 | __throw_runtime_error("locale not supported"); |
| 5952 | wchar_t* wbe = wbuf + j; |
| 5953 | __curr_symbol_.assign(wbuf, wbe); |
| 5954 | if (lc->frac_digits != CHAR_MAX) |
| 5955 | __frac_digits_ = lc->frac_digits; |
| 5956 | else |
| 5957 | __frac_digits_ = base::do_frac_digits(); |
| 5958 | if (lc->p_sign_posn == 0) |
| 5959 | __positive_sign_ = L"()"; |
| 5960 | else |
| 5961 | { |
| 5962 | mb = mbstate_t(); |
| 5963 | bb = lc->positive_sign; |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 5964 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5965 | j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5966 | #else |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5967 | j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5968 | #endif |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 5969 | if (j == size_t(-1)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5970 | __throw_runtime_error("locale not supported"); |
| 5971 | wbe = wbuf + j; |
| 5972 | __positive_sign_.assign(wbuf, wbe); |
| 5973 | } |
| 5974 | if (lc->n_sign_posn == 0) |
| 5975 | __negative_sign_ = L"()"; |
| 5976 | else |
| 5977 | { |
| 5978 | mb = mbstate_t(); |
| 5979 | bb = lc->negative_sign; |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 5980 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5981 | j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5982 | #else |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 5983 | j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 5984 | #endif |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 5985 | if (j == size_t(-1)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5986 | __throw_runtime_error("locale not supported"); |
| 5987 | wbe = wbuf + j; |
| 5988 | __negative_sign_.assign(wbuf, wbe); |
| 5989 | } |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 5990 | // Assume the positive and negative formats will want spaces in |
| 5991 | // the same places in curr_symbol since there's no way to |
| 5992 | // represent anything else. |
| 5993 | string_type __dummy_curr_symbol = __curr_symbol_; |
| 5994 | __init_pat(__pos_format_, __dummy_curr_symbol, false, |
| 5995 | lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn, L' '); |
| 5996 | __init_pat(__neg_format_, __curr_symbol_, false, |
| 5997 | lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn, L' '); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5998 | } |
| 5999 | |
| 6000 | template<> |
| 6001 | void |
| 6002 | moneypunct_byname<wchar_t, true>::init(const char* nm) |
| 6003 | { |
| 6004 | typedef moneypunct<wchar_t, true> base; |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 6005 | __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 6006 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 05b57d5 | 2012-03-07 20:37:43 +0000 | [diff] [blame] | 6007 | if (loc == nullptr) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6008 | throw runtime_error("moneypunct_byname" |
| 6009 | " failed to construct for " + string(nm)); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 6010 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 6011 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 6012 | lconv* lc = localeconv_l(loc.get()); |
| 6013 | #else |
| 6014 | lconv* lc = __localeconv_l(loc.get()); |
| 6015 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6016 | if (*lc->mon_decimal_point) |
| 6017 | __decimal_point_ = static_cast<wchar_t>(*lc->mon_decimal_point); |
| 6018 | else |
| 6019 | __decimal_point_ = base::do_decimal_point(); |
| 6020 | if (*lc->mon_thousands_sep) |
| 6021 | __thousands_sep_ = static_cast<wchar_t>(*lc->mon_thousands_sep); |
| 6022 | else |
| 6023 | __thousands_sep_ = base::do_thousands_sep(); |
| 6024 | __grouping_ = lc->mon_grouping; |
| 6025 | wchar_t wbuf[100]; |
| 6026 | mbstate_t mb = {0}; |
| 6027 | const char* bb = lc->int_curr_symbol; |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 6028 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 6029 | size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 6030 | #else |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 6031 | size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 6032 | #endif |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 6033 | if (j == size_t(-1)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6034 | __throw_runtime_error("locale not supported"); |
| 6035 | wchar_t* wbe = wbuf + j; |
| 6036 | __curr_symbol_.assign(wbuf, wbe); |
| 6037 | if (lc->int_frac_digits != CHAR_MAX) |
| 6038 | __frac_digits_ = lc->int_frac_digits; |
| 6039 | else |
| 6040 | __frac_digits_ = base::do_frac_digits(); |
Howard Hinnant | ef5aa93 | 2013-09-17 01:34:47 +0000 | [diff] [blame] | 6041 | #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__) |
Howard Hinnant | 6cd05ee | 2011-09-23 16:11:27 +0000 | [diff] [blame] | 6042 | if (lc->p_sign_posn == 0) |
Howard Hinnant | e9df0a5 | 2013-08-01 18:17:34 +0000 | [diff] [blame] | 6043 | #else // _LIBCPP_MSVCRT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6044 | if (lc->int_p_sign_posn == 0) |
Howard Hinnant | e9df0a5 | 2013-08-01 18:17:34 +0000 | [diff] [blame] | 6045 | #endif // !_LIBCPP_MSVCRT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6046 | __positive_sign_ = L"()"; |
| 6047 | else |
| 6048 | { |
| 6049 | mb = mbstate_t(); |
| 6050 | bb = lc->positive_sign; |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 6051 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 6052 | j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 6053 | #else |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 6054 | j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 6055 | #endif |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 6056 | if (j == size_t(-1)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6057 | __throw_runtime_error("locale not supported"); |
| 6058 | wbe = wbuf + j; |
| 6059 | __positive_sign_.assign(wbuf, wbe); |
| 6060 | } |
Howard Hinnant | ef5aa93 | 2013-09-17 01:34:47 +0000 | [diff] [blame] | 6061 | #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__) |
Howard Hinnant | 6cd05ee | 2011-09-23 16:11:27 +0000 | [diff] [blame] | 6062 | if (lc->n_sign_posn == 0) |
Howard Hinnant | e9df0a5 | 2013-08-01 18:17:34 +0000 | [diff] [blame] | 6063 | #else // _LIBCPP_MSVCRT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6064 | if (lc->int_n_sign_posn == 0) |
Howard Hinnant | e9df0a5 | 2013-08-01 18:17:34 +0000 | [diff] [blame] | 6065 | #endif // !_LIBCPP_MSVCRT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6066 | __negative_sign_ = L"()"; |
| 6067 | else |
| 6068 | { |
| 6069 | mb = mbstate_t(); |
| 6070 | bb = lc->negative_sign; |
Howard Hinnant | 866569b | 2011-09-28 23:39:33 +0000 | [diff] [blame] | 6071 | #ifdef _LIBCPP_LOCALE__L_EXTENSIONS |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 6072 | j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 6073 | #else |
Howard Hinnant | bf68bdc | 2012-12-27 21:17:53 +0000 | [diff] [blame] | 6074 | j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get()); |
Sean Hunt | f3907e6 | 2011-07-15 05:40:33 +0000 | [diff] [blame] | 6075 | #endif |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 6076 | if (j == size_t(-1)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6077 | __throw_runtime_error("locale not supported"); |
| 6078 | wbe = wbuf + j; |
| 6079 | __negative_sign_.assign(wbuf, wbe); |
| 6080 | } |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 6081 | // Assume the positive and negative formats will want spaces in |
| 6082 | // the same places in curr_symbol since there's no way to |
| 6083 | // represent anything else. |
| 6084 | string_type __dummy_curr_symbol = __curr_symbol_; |
Howard Hinnant | ef5aa93 | 2013-09-17 01:34:47 +0000 | [diff] [blame] | 6085 | #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__) |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 6086 | __init_pat(__pos_format_, __dummy_curr_symbol, true, |
| 6087 | lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn, L' '); |
| 6088 | __init_pat(__neg_format_, __curr_symbol_, true, |
| 6089 | lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn, L' '); |
Howard Hinnant | e9df0a5 | 2013-08-01 18:17:34 +0000 | [diff] [blame] | 6090 | #else // _LIBCPP_MSVCRT |
Jeffrey Yasskin | 558ae17 | 2012-03-10 18:31:43 +0000 | [diff] [blame] | 6091 | __init_pat(__pos_format_, __dummy_curr_symbol, true, |
| 6092 | lc->int_p_cs_precedes, lc->int_p_sep_by_space, |
| 6093 | lc->int_p_sign_posn, L' '); |
| 6094 | __init_pat(__neg_format_, __curr_symbol_, true, |
| 6095 | lc->int_n_cs_precedes, lc->int_n_sep_by_space, |
| 6096 | lc->int_n_sign_posn, L' '); |
Howard Hinnant | e9df0a5 | 2013-08-01 18:17:34 +0000 | [diff] [blame] | 6097 | #endif // !_LIBCPP_MSVCRT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6098 | } |
| 6099 | |
| 6100 | void __do_nothing(void*) {} |
| 6101 | |
| 6102 | void __throw_runtime_error(const char* msg) |
| 6103 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 6104 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6105 | throw runtime_error(msg); |
Howard Hinnant | db4d478 | 2013-03-28 18:56:26 +0000 | [diff] [blame] | 6106 | #else |
| 6107 | (void)msg; |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 6108 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6109 | } |
| 6110 | |
| 6111 | template class collate<char>; |
| 6112 | template class collate<wchar_t>; |
| 6113 | |
| 6114 | template class num_get<char>; |
| 6115 | template class num_get<wchar_t>; |
| 6116 | |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 6117 | template struct __num_get<char>; |
| 6118 | template struct __num_get<wchar_t>; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6119 | |
| 6120 | template class num_put<char>; |
| 6121 | template class num_put<wchar_t>; |
| 6122 | |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 6123 | template struct __num_put<char>; |
| 6124 | template struct __num_put<wchar_t>; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6125 | |
| 6126 | template class time_get<char>; |
| 6127 | template class time_get<wchar_t>; |
| 6128 | |
| 6129 | template class time_get_byname<char>; |
| 6130 | template class time_get_byname<wchar_t>; |
| 6131 | |
| 6132 | template class time_put<char>; |
| 6133 | template class time_put<wchar_t>; |
| 6134 | |
| 6135 | template class time_put_byname<char>; |
| 6136 | template class time_put_byname<wchar_t>; |
| 6137 | |
| 6138 | template class moneypunct<char, false>; |
| 6139 | template class moneypunct<char, true>; |
| 6140 | template class moneypunct<wchar_t, false>; |
| 6141 | template class moneypunct<wchar_t, true>; |
| 6142 | |
| 6143 | template class moneypunct_byname<char, false>; |
| 6144 | template class moneypunct_byname<char, true>; |
| 6145 | template class moneypunct_byname<wchar_t, false>; |
| 6146 | template class moneypunct_byname<wchar_t, true>; |
| 6147 | |
| 6148 | template class money_get<char>; |
| 6149 | template class money_get<wchar_t>; |
| 6150 | |
| 6151 | template class __money_get<char>; |
| 6152 | template class __money_get<wchar_t>; |
| 6153 | |
| 6154 | template class money_put<char>; |
| 6155 | template class money_put<wchar_t>; |
| 6156 | |
| 6157 | template class __money_put<char>; |
| 6158 | template class __money_put<wchar_t>; |
| 6159 | |
| 6160 | template class messages<char>; |
| 6161 | template class messages<wchar_t>; |
| 6162 | |
| 6163 | template class messages_byname<char>; |
| 6164 | template class messages_byname<wchar_t>; |
| 6165 | |
| 6166 | template class codecvt_byname<char, char, mbstate_t>; |
| 6167 | template class codecvt_byname<wchar_t, char, mbstate_t>; |
| 6168 | template class codecvt_byname<char16_t, char, mbstate_t>; |
| 6169 | template class codecvt_byname<char32_t, char, mbstate_t>; |
| 6170 | |
| 6171 | template class __vector_base_common<true>; |
| 6172 | |
| 6173 | _LIBCPP_END_NAMESPACE_STD |