blob: 48e7b642f884f2c32caef69aa5567b52018d8a0b [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___LOCALE
11#define _LIBCPP___LOCALE
12
13#include <__config>
Louis Dionne8d053eb2020-10-09 15:31:05 -040014
15#if defined(_LIBCPP_HAS_NO_LOCALIZATION)
16# error "Localization is not supported by this configuration of libc++"
17#endif
18
Howard Hinnantc51e1022010-05-11 19:42:16 +000019#include <string>
20#include <memory>
21#include <utility>
22#include <mutex>
23#include <cstdint>
24#include <cctype>
Howard Hinnant155c2af2010-05-24 17:49:41 +000025#include <locale.h>
Eric Fiselierbb999f92017-05-31 22:14:05 +000026#if defined(_LIBCPP_MSVCRT_LIKE)
Thomas Anderson094acbc2019-03-27 18:09:30 +000027# include <cstring>
Howard Hinnantae0f80b2011-09-29 20:33:10 +000028# include <support/win32/locale_win32.h>
Marshall Clow2f96ec62014-03-11 17:18:47 +000029#elif defined(_AIX)
Howard Hinnantea382952013-08-14 18:00:20 +000030# include <support/ibm/xlocale.h>
Marshall Clow3477ec92014-07-10 15:20:28 +000031#elif defined(__ANDROID__)
Saleem Abdulrasoolac039f32018-04-13 18:14:57 +000032# include <support/android/locale_bionic.h>
Eric Fiselier7274c652014-11-25 21:57:41 +000033#elif defined(__sun__)
Eric Fiselier90adc202015-01-23 22:22:36 +000034# include <xlocale.h>
Eric Fiselier7274c652014-11-25 21:57:41 +000035# include <support/solaris/xlocale.h>
Sergey Dmitrouk9935bd42014-12-12 08:36:16 +000036#elif defined(_NEWLIB_VERSION)
37# include <support/newlib/xlocale.h>
Eric Fiseliercee57932017-08-03 04:28:10 +000038#elif (defined(__APPLE__) || defined(__FreeBSD__) \
Eric Fiselier7274c652014-11-25 21:57:41 +000039 || defined(__EMSCRIPTEN__) || defined(__IBMCPP__))
Howard Hinnantdd0d7022011-09-22 19:10:18 +000040# include <xlocale.h>
Petr Hosekfdb4a872017-04-13 21:29:21 +000041#elif defined(__Fuchsia__)
42# include <support/fuchsia/xlocale.h>
Dan Gohman280fd6b2019-05-01 16:47:30 +000043#elif defined(__wasi__)
44// WASI libc uses musl's locales support.
45# include <support/musl/xlocale.h>
Vasileios Kalintiris281b4cf2015-11-09 10:21:04 +000046#elif defined(_LIBCPP_HAS_MUSL_LIBC)
47# include <support/musl/xlocale.h>
Petr Hosekfdb4a872017-04-13 21:29:21 +000048#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000049
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000050#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000051#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000052#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000053
54_LIBCPP_BEGIN_NAMESPACE_STD
55
Martin Storsjo5482ac62017-11-23 10:38:18 +000056#if !defined(_LIBCPP_LOCALE__L_EXTENSIONS)
Eric Fiselierebc2d2c2017-05-08 22:02:43 +000057struct __libcpp_locale_guard {
58 _LIBCPP_INLINE_VISIBILITY
59 __libcpp_locale_guard(locale_t& __loc) : __old_loc_(uselocale(__loc)) {}
60
61 _LIBCPP_INLINE_VISIBILITY
62 ~__libcpp_locale_guard() {
63 if (__old_loc_)
64 uselocale(__old_loc_);
65 }
66
67 locale_t __old_loc_;
68private:
69 __libcpp_locale_guard(__libcpp_locale_guard const&);
70 __libcpp_locale_guard& operator=(__libcpp_locale_guard const&);
71};
Martin Storsjo5482ac62017-11-23 10:38:18 +000072#elif defined(_LIBCPP_MSVCRT_LIKE)
73struct __libcpp_locale_guard {
74 __libcpp_locale_guard(locale_t __l) :
Thomas Anderson094acbc2019-03-27 18:09:30 +000075 __status(_configthreadlocale(_ENABLE_PER_THREAD_LOCALE)) {
76 // Setting the locale can be expensive even when the locale given is
77 // already the current locale, so do an explicit check to see if the
78 // current locale is already the one we want.
79 const char* __lc = __setlocale(nullptr);
80 // If every category is the same, the locale string will simply be the
81 // locale name, otherwise it will be a semicolon-separated string listing
82 // each category. In the second case, we know at least one category won't
83 // be what we want, so we only have to check the first case.
84 if (strcmp(__l.__get_locale(), __lc) != 0) {
85 __locale_all = _strdup(__lc);
86 if (__locale_all == nullptr)
87 __throw_bad_alloc();
88 __setlocale(__l.__get_locale());
89 }
90 }
Martin Storsjo5482ac62017-11-23 10:38:18 +000091 ~__libcpp_locale_guard() {
Thomas Anderson094acbc2019-03-27 18:09:30 +000092 // The CRT documentation doesn't explicitly say, but setlocale() does the
93 // right thing when given a semicolon-separated list of locale settings
94 // for the different categories in the same format as returned by
95 // setlocale(LC_ALL, nullptr).
96 if (__locale_all != nullptr) {
97 __setlocale(__locale_all);
98 free(__locale_all);
99 }
100 _configthreadlocale(__status);
101 }
102 static const char* __setlocale(const char* __locale) {
103 const char* __new_locale = setlocale(LC_ALL, __locale);
104 if (__new_locale == nullptr)
105 __throw_bad_alloc();
106 return __new_locale;
Martin Storsjo5482ac62017-11-23 10:38:18 +0000107 }
108 int __status;
Thomas Anderson094acbc2019-03-27 18:09:30 +0000109 char* __locale_all = nullptr;
Martin Storsjo5482ac62017-11-23 10:38:18 +0000110};
Eric Fiselierebc2d2c2017-05-08 22:02:43 +0000111#endif
112
113
Howard Hinnant8331b762013-03-06 23:30:19 +0000114class _LIBCPP_TYPE_VIS locale;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000115
Howard Hinnanta54386e2012-09-14 00:39:16 +0000116template <class _Facet>
117_LIBCPP_INLINE_VISIBILITY
118bool
119has_facet(const locale&) _NOEXCEPT;
120
121template <class _Facet>
122_LIBCPP_INLINE_VISIBILITY
123const _Facet&
124use_facet(const locale&);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000125
Howard Hinnant8331b762013-03-06 23:30:19 +0000126class _LIBCPP_TYPE_VIS locale
Howard Hinnantc51e1022010-05-11 19:42:16 +0000127{
128public:
129 // types:
Howard Hinnant8331b762013-03-06 23:30:19 +0000130 class _LIBCPP_TYPE_VIS facet;
131 class _LIBCPP_TYPE_VIS id;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132
133 typedef int category;
Mehdi Amini228053d2017-05-04 17:08:54 +0000134 _LIBCPP_AVAILABILITY_LOCALE_CATEGORY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135 static const category // values assigned here are for exposition only
136 none = 0,
137 collate = LC_COLLATE_MASK,
138 ctype = LC_CTYPE_MASK,
139 monetary = LC_MONETARY_MASK,
140 numeric = LC_NUMERIC_MASK,
141 time = LC_TIME_MASK,
142 messages = LC_MESSAGES_MASK,
143 all = collate | ctype | monetary | numeric | time | messages;
144
145 // construct/copy/destroy:
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000146 locale() _NOEXCEPT;
147 locale(const locale&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000148 explicit locale(const char*);
149 explicit locale(const string&);
150 locale(const locale&, const char*, category);
151 locale(const locale&, const string&, category);
Howard Hinnantcf823322010-12-17 14:46:43 +0000152 template <class _Facet>
153 _LIBCPP_INLINE_VISIBILITY locale(const locale&, _Facet*);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154 locale(const locale&, const locale&, category);
155
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000156 ~locale();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000158 const locale& operator=(const locale&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000159
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000160 template <class _Facet>
161 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
162 locale combine(const locale&) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000163
164 // locale operations:
165 string name() const;
166 bool operator==(const locale&) const;
167 bool operator!=(const locale& __y) const {return !(*this == __y);}
168 template <class _CharT, class _Traits, class _Allocator>
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000169 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000170 bool operator()(const basic_string<_CharT, _Traits, _Allocator>&,
171 const basic_string<_CharT, _Traits, _Allocator>&) const;
172
173 // global locale objects:
174 static locale global(const locale&);
175 static const locale& classic();
176
177private:
178 class __imp;
179 __imp* __locale_;
180
181 void __install_ctor(const locale&, facet*, long);
182 static locale& __global();
183 bool has_facet(id&) const;
184 const facet* use_facet(id&) const;
185
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000186 template <class _Facet> friend bool has_facet(const locale&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187 template <class _Facet> friend const _Facet& use_facet(const locale&);
188};
189
Howard Hinnant8331b762013-03-06 23:30:19 +0000190class _LIBCPP_TYPE_VIS locale::facet
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191 : public __shared_count
192{
193protected:
Howard Hinnant9833cad2010-09-21 18:58:51 +0000194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000195 explicit facet(size_t __refs = 0)
196 : __shared_count(static_cast<long>(__refs)-1) {}
197
198 virtual ~facet();
199
200// facet(const facet&) = delete; // effectively done in __shared_count
201// void operator=(const facet&) = delete;
202private:
Howard Hinnant719bda32011-05-28 14:41:13 +0000203 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000204};
205
Howard Hinnant8331b762013-03-06 23:30:19 +0000206class _LIBCPP_TYPE_VIS locale::id
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207{
208 once_flag __flag_;
209 int32_t __id_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000210
Howard Hinnantc51e1022010-05-11 19:42:16 +0000211 static int32_t __next_id;
212public:
Howard Hinnantac7d9f02012-07-26 16:14:37 +0000213 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR id() :__id_(0) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000214private:
215 void __init();
216 void operator=(const id&); // = delete;
217 id(const id&); // = delete;
218public: // only needed for tests
219 long __get();
220
221 friend class locale;
222 friend class locale::__imp;
223};
224
225template <class _Facet>
226inline _LIBCPP_INLINE_VISIBILITY
227locale::locale(const locale& __other, _Facet* __f)
228{
229 __install_ctor(__other, __f, __f ? __f->id.__get() : 0);
230}
231
232template <class _Facet>
233locale
234locale::combine(const locale& __other) const
235{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000236 if (!_VSTD::has_facet<_Facet>(__other))
Marshall Clow8fea1612016-08-25 15:09:01 +0000237 __throw_runtime_error("locale::combine: locale missing facet");
238
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000239 return locale(*this, &const_cast<_Facet&>(_VSTD::use_facet<_Facet>(__other)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000240}
241
242template <class _Facet>
243inline _LIBCPP_INLINE_VISIBILITY
244bool
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000245has_facet(const locale& __l) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000246{
247 return __l.has_facet(_Facet::id);
248}
249
250template <class _Facet>
251inline _LIBCPP_INLINE_VISIBILITY
252const _Facet&
253use_facet(const locale& __l)
254{
255 return static_cast<const _Facet&>(*__l.use_facet(_Facet::id));
256}
257
258// template <class _CharT> class collate;
259
260template <class _CharT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000261class _LIBCPP_TEMPLATE_VIS collate
Howard Hinnantc51e1022010-05-11 19:42:16 +0000262 : public locale::facet
263{
264public:
265 typedef _CharT char_type;
266 typedef basic_string<char_type> string_type;
267
Howard Hinnant9833cad2010-09-21 18:58:51 +0000268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000269 explicit collate(size_t __refs = 0)
270 : locale::facet(__refs) {}
271
Howard Hinnant9833cad2010-09-21 18:58:51 +0000272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000273 int compare(const char_type* __lo1, const char_type* __hi1,
274 const char_type* __lo2, const char_type* __hi2) const
275 {
276 return do_compare(__lo1, __hi1, __lo2, __hi2);
277 }
278
Eric Fiselier39b86ea2019-03-08 23:59:29 +0000279 // FIXME(EricWF): The _LIBCPP_ALWAYS_INLINE is needed on Windows to work
280 // around a dllimport bug that expects an external instantiation.
Howard Hinnant9833cad2010-09-21 18:58:51 +0000281 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier39b86ea2019-03-08 23:59:29 +0000282 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +0000283 string_type transform(const char_type* __lo, const char_type* __hi) const
284 {
285 return do_transform(__lo, __hi);
286 }
287
Howard Hinnant9833cad2010-09-21 18:58:51 +0000288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289 long hash(const char_type* __lo, const char_type* __hi) const
290 {
291 return do_hash(__lo, __hi);
292 }
293
294 static locale::id id;
295
296protected:
297 ~collate();
298 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
299 const char_type* __lo2, const char_type* __hi2) const;
300 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const
301 {return string_type(__lo, __hi);}
302 virtual long do_hash(const char_type* __lo, const char_type* __hi) const;
303};
304
305template <class _CharT> locale::id collate<_CharT>::id;
306
307template <class _CharT>
308collate<_CharT>::~collate()
309{
310}
311
312template <class _CharT>
313int
314collate<_CharT>::do_compare(const char_type* __lo1, const char_type* __hi1,
315 const char_type* __lo2, const char_type* __hi2) const
316{
317 for (; __lo2 != __hi2; ++__lo1, ++__lo2)
318 {
319 if (__lo1 == __hi1 || *__lo1 < *__lo2)
320 return -1;
321 if (*__lo2 < *__lo1)
322 return 1;
323 }
324 return __lo1 != __hi1;
325}
326
327template <class _CharT>
328long
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000329collate<_CharT>::do_hash(const char_type* __lo, const char_type* __hi) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000330{
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000331 size_t __h = 0;
332 const size_t __sr = __CHAR_BIT__ * sizeof(size_t) - 8;
333 const size_t __mask = size_t(0xF) << (__sr + 4);
334 for(const char_type* __p = __lo; __p != __hi; ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000335 {
Howard Hinnant28b24882011-12-01 20:21:04 +0000336 __h = (__h << 4) + static_cast<size_t>(*__p);
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000337 size_t __g = __h & __mask;
338 __h ^= __g | (__g >> __sr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339 }
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000340 return static_cast<long>(__h);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000341}
342
Louis Dionneb5ae3a72020-10-02 14:29:48 -0400343_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<char>)
344_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000345
346// template <class CharT> class collate_byname;
347
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000348template <class _CharT> class _LIBCPP_TEMPLATE_VIS collate_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000349
350template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000351class _LIBCPP_TYPE_VIS collate_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000352 : public collate<char>
353{
354 locale_t __l;
355public:
356 typedef char char_type;
357 typedef basic_string<char_type> string_type;
358
359 explicit collate_byname(const char* __n, size_t __refs = 0);
360 explicit collate_byname(const string& __n, size_t __refs = 0);
361
362protected:
363 ~collate_byname();
364 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
365 const char_type* __lo2, const char_type* __hi2) const;
366 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
367};
368
369template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000370class _LIBCPP_TYPE_VIS collate_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000371 : public collate<wchar_t>
372{
373 locale_t __l;
374public:
375 typedef wchar_t char_type;
376 typedef basic_string<char_type> string_type;
377
378 explicit collate_byname(const char* __n, size_t __refs = 0);
379 explicit collate_byname(const string& __n, size_t __refs = 0);
380
381protected:
382 ~collate_byname();
383
384 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
385 const char_type* __lo2, const char_type* __hi2) const;
386 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
387};
388
389template <class _CharT, class _Traits, class _Allocator>
390bool
391locale::operator()(const basic_string<_CharT, _Traits, _Allocator>& __x,
392 const basic_string<_CharT, _Traits, _Allocator>& __y) const
393{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000394 return _VSTD::use_facet<_VSTD::collate<_CharT> >(*this).compare(
Howard Hinnantc51e1022010-05-11 19:42:16 +0000395 __x.data(), __x.data() + __x.size(),
396 __y.data(), __y.data() + __y.size()) < 0;
397}
398
399// template <class charT> class ctype
400
Howard Hinnant8331b762013-03-06 23:30:19 +0000401class _LIBCPP_TYPE_VIS ctype_base
Howard Hinnant9833cad2010-09-21 18:58:51 +0000402{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403public:
Vasileios Kalintiris281b4cf2015-11-09 10:21:04 +0000404#if defined(__GLIBC__)
Alexis Hunt92b0c812011-07-09 00:56:23 +0000405 typedef unsigned short mask;
Howard Hinnant155c2af2010-05-24 17:49:41 +0000406 static const mask space = _ISspace;
407 static const mask print = _ISprint;
408 static const mask cntrl = _IScntrl;
409 static const mask upper = _ISupper;
410 static const mask lower = _ISlower;
411 static const mask alpha = _ISalpha;
412 static const mask digit = _ISdigit;
413 static const mask punct = _ISpunct;
414 static const mask xdigit = _ISxdigit;
415 static const mask blank = _ISblank;
Mikhail Maltsev014ed062019-06-14 09:04:16 +0000416#if defined(__mips__)
Mikhail Maltsev12eb5af2019-08-20 10:19:55 +0000417 static const mask __regex_word = static_cast<mask>(_ISbit(15));
Mikhail Maltsev014ed062019-06-14 09:04:16 +0000418#else
419 static const mask __regex_word = 0x80;
420#endif
Eric Fiselierbb999f92017-05-31 22:14:05 +0000421#elif defined(_LIBCPP_MSVCRT_LIKE)
Howard Hinnantd7a78632011-09-29 13:33:15 +0000422 typedef unsigned short mask;
Howard Hinnantdd0d7022011-09-22 19:10:18 +0000423 static const mask space = _SPACE;
424 static const mask print = _BLANK|_PUNCT|_ALPHA|_DIGIT;
425 static const mask cntrl = _CONTROL;
426 static const mask upper = _UPPER;
427 static const mask lower = _LOWER;
428 static const mask alpha = _ALPHA;
429 static const mask digit = _DIGIT;
430 static const mask punct = _PUNCT;
431 static const mask xdigit = _HEX;
432 static const mask blank = _BLANK;
Mikhail Maltsev014ed062019-06-14 09:04:16 +0000433 static const mask __regex_word = 0x80;
Jonathan Roelofsae9ab252015-03-11 17:00:28 +0000434# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
Dan Albertebdf28b2015-03-11 00:51:06 +0000435#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__)
JF Bastien0c265d82015-02-25 22:16:46 +0000436# ifdef __APPLE__
David Chisnall1d581062011-09-21 08:39:44 +0000437 typedef __uint32_t mask;
JF Bastien0c265d82015-02-25 22:16:46 +0000438# elif defined(__FreeBSD__)
David Chisnall1d581062011-09-21 08:39:44 +0000439 typedef unsigned long mask;
Vasileios Kalintirisc5dd8942015-11-24 10:24:54 +0000440# elif defined(__EMSCRIPTEN__) || defined(__NetBSD__)
Howard Hinnant942dbd22013-03-29 18:27:28 +0000441 typedef unsigned short mask;
JF Bastien0c265d82015-02-25 22:16:46 +0000442# endif
David Chisnall1d581062011-09-21 08:39:44 +0000443 static const mask space = _CTYPE_S;
444 static const mask print = _CTYPE_R;
445 static const mask cntrl = _CTYPE_C;
446 static const mask upper = _CTYPE_U;
447 static const mask lower = _CTYPE_L;
448 static const mask alpha = _CTYPE_A;
449 static const mask digit = _CTYPE_D;
450 static const mask punct = _CTYPE_P;
451 static const mask xdigit = _CTYPE_X;
Dan Albert2dca4072014-07-23 19:32:03 +0000452
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000453# if defined(__NetBSD__)
454 static const mask blank = _CTYPE_BL;
Mikhail Maltsev014ed062019-06-14 09:04:16 +0000455 // NetBSD defines classes up to 0x2000
456 // see sys/ctype_bits.h, _CTYPE_Q
457 static const mask __regex_word = 0x8000;
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000458# else
David Chisnall1d581062011-09-21 08:39:44 +0000459 static const mask blank = _CTYPE_B;
Mikhail Maltsev014ed062019-06-14 09:04:16 +0000460 static const mask __regex_word = 0x80;
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000461# endif
Howard Hinnanta47505d2013-08-30 14:42:39 +0000462#elif defined(__sun__) || defined(_AIX)
David Chisnall8074c342012-02-29 13:05:08 +0000463 typedef unsigned int mask;
464 static const mask space = _ISSPACE;
465 static const mask print = _ISPRINT;
466 static const mask cntrl = _ISCNTRL;
467 static const mask upper = _ISUPPER;
468 static const mask lower = _ISLOWER;
469 static const mask alpha = _ISALPHA;
470 static const mask digit = _ISDIGIT;
471 static const mask punct = _ISPUNCT;
472 static const mask xdigit = _ISXDIGIT;
473 static const mask blank = _ISBLANK;
Mikhail Maltsev014ed062019-06-14 09:04:16 +0000474 static const mask __regex_word = 0x80;
JF Bastien0c265d82015-02-25 22:16:46 +0000475#elif defined(_NEWLIB_VERSION)
476 // Same type as Newlib's _ctype_ array in newlib/libc/include/ctype.h.
477 typedef char mask;
478 static const mask space = _S;
479 static const mask print = _P | _U | _L | _N | _B;
480 static const mask cntrl = _C;
481 static const mask upper = _U;
482 static const mask lower = _L;
483 static const mask alpha = _U | _L;
484 static const mask digit = _N;
485 static const mask punct = _P;
486 static const mask xdigit = _X | _N;
487 static const mask blank = _B;
Mikhail Maltsev014ed062019-06-14 09:04:16 +0000488 static const mask __regex_word = 0x80;
Jonathan Roelofsae9ab252015-03-11 17:00:28 +0000489# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
490# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
491# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_XDIGIT
JF Bastien0c265d82015-02-25 22:16:46 +0000492#else
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000493 typedef unsigned long mask;
494 static const mask space = 1<<0;
495 static const mask print = 1<<1;
496 static const mask cntrl = 1<<2;
497 static const mask upper = 1<<3;
498 static const mask lower = 1<<4;
499 static const mask alpha = 1<<5;
500 static const mask digit = 1<<6;
501 static const mask punct = 1<<7;
502 static const mask xdigit = 1<<8;
503 static const mask blank = 1<<9;
Dan Albert70ee07e2020-04-06 13:34:27 -0700504#if defined(__BIONIC__)
505 // Historically this was a part of regex_traits rather than ctype_base. The
506 // historical value of the constant is preserved for ABI compatibility.
507 static const mask __regex_word = 0x8000;
508#else
Mikhail Maltsev014ed062019-06-14 09:04:16 +0000509 static const mask __regex_word = 1<<10;
Dan Albert70ee07e2020-04-06 13:34:27 -0700510#endif // defined(__BIONIC__)
JF Bastien0c265d82015-02-25 22:16:46 +0000511#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000512 static const mask alnum = alpha | digit;
513 static const mask graph = alnum | punct;
514
Louis Dionne16fe2952018-07-11 23:14:33 +0000515 _LIBCPP_INLINE_VISIBILITY ctype_base() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000516};
517
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000518template <class _CharT> class _LIBCPP_TEMPLATE_VIS ctype;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000519
520template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000521class _LIBCPP_TYPE_VIS ctype<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000522 : public locale::facet,
523 public ctype_base
524{
525public:
526 typedef wchar_t char_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000527
Louis Dionne16fe2952018-07-11 23:14:33 +0000528 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000529 explicit ctype(size_t __refs = 0)
530 : locale::facet(__refs) {}
531
Louis Dionne16fe2952018-07-11 23:14:33 +0000532 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000533 bool is(mask __m, char_type __c) const
534 {
535 return do_is(__m, __c);
536 }
537
Louis Dionne16fe2952018-07-11 23:14:33 +0000538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000539 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
540 {
541 return do_is(__low, __high, __vec);
542 }
543
Louis Dionne16fe2952018-07-11 23:14:33 +0000544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000545 const char_type* scan_is(mask __m, const char_type* __low, const char_type* __high) const
546 {
547 return do_scan_is(__m, __low, __high);
548 }
549
Louis Dionne16fe2952018-07-11 23:14:33 +0000550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000551 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
552 {
553 return do_scan_not(__m, __low, __high);
554 }
555
Louis Dionne16fe2952018-07-11 23:14:33 +0000556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000557 char_type toupper(char_type __c) const
558 {
559 return do_toupper(__c);
560 }
561
Louis Dionne16fe2952018-07-11 23:14:33 +0000562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000563 const char_type* toupper(char_type* __low, const char_type* __high) const
564 {
565 return do_toupper(__low, __high);
566 }
567
Louis Dionne16fe2952018-07-11 23:14:33 +0000568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000569 char_type tolower(char_type __c) const
570 {
571 return do_tolower(__c);
572 }
573
Louis Dionne16fe2952018-07-11 23:14:33 +0000574 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000575 const char_type* tolower(char_type* __low, const char_type* __high) const
576 {
577 return do_tolower(__low, __high);
578 }
579
Louis Dionne16fe2952018-07-11 23:14:33 +0000580 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000581 char_type widen(char __c) const
582 {
583 return do_widen(__c);
584 }
585
Louis Dionne16fe2952018-07-11 23:14:33 +0000586 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000587 const char* widen(const char* __low, const char* __high, char_type* __to) const
588 {
589 return do_widen(__low, __high, __to);
590 }
591
Louis Dionne16fe2952018-07-11 23:14:33 +0000592 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593 char narrow(char_type __c, char __dfault) const
594 {
595 return do_narrow(__c, __dfault);
596 }
597
Louis Dionne16fe2952018-07-11 23:14:33 +0000598 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000599 const char_type* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
600 {
601 return do_narrow(__low, __high, __dfault, __to);
602 }
603
604 static locale::id id;
605
606protected:
607 ~ctype();
608 virtual bool do_is(mask __m, char_type __c) const;
609 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
610 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
611 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
612 virtual char_type do_toupper(char_type) const;
613 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
614 virtual char_type do_tolower(char_type) const;
615 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
616 virtual char_type do_widen(char) const;
617 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
618 virtual char do_narrow(char_type, char __dfault) const;
619 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
620};
621
622template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000623class _LIBCPP_TYPE_VIS ctype<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000624 : public locale::facet, public ctype_base
625{
626 const mask* __tab_;
627 bool __del_;
628public:
629 typedef char char_type;
630
631 explicit ctype(const mask* __tab = 0, bool __del = false, size_t __refs = 0);
632
Louis Dionne16fe2952018-07-11 23:14:33 +0000633 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000634 bool is(mask __m, char_type __c) const
635 {
Marshall Clow11de4872013-10-21 14:41:05 +0000636 return isascii(__c) ? (__tab_[static_cast<int>(__c)] & __m) !=0 : false;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000637 }
638
Louis Dionne16fe2952018-07-11 23:14:33 +0000639 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000640 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
641 {
642 for (; __low != __high; ++__low, ++__vec)
Howard Hinnant28b24882011-12-01 20:21:04 +0000643 *__vec = isascii(*__low) ? __tab_[static_cast<int>(*__low)] : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000644 return __low;
645 }
646
Louis Dionne16fe2952018-07-11 23:14:33 +0000647 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000648 const char_type* scan_is (mask __m, const char_type* __low, const char_type* __high) const
649 {
650 for (; __low != __high; ++__low)
Howard Hinnant28b24882011-12-01 20:21:04 +0000651 if (isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000652 break;
653 return __low;
654 }
655
Louis Dionne16fe2952018-07-11 23:14:33 +0000656 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000657 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
658 {
659 for (; __low != __high; ++__low)
Howard Hinnant28b24882011-12-01 20:21:04 +0000660 if (!(isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m)))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000661 break;
662 return __low;
663 }
664
Louis Dionne16fe2952018-07-11 23:14:33 +0000665 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000666 char_type toupper(char_type __c) const
667 {
668 return do_toupper(__c);
669 }
670
Louis Dionne16fe2952018-07-11 23:14:33 +0000671 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672 const char_type* toupper(char_type* __low, const char_type* __high) const
673 {
674 return do_toupper(__low, __high);
675 }
676
Louis Dionne16fe2952018-07-11 23:14:33 +0000677 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000678 char_type tolower(char_type __c) const
679 {
680 return do_tolower(__c);
681 }
682
Louis Dionne16fe2952018-07-11 23:14:33 +0000683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000684 const char_type* tolower(char_type* __low, const char_type* __high) const
685 {
686 return do_tolower(__low, __high);
687 }
688
Louis Dionne16fe2952018-07-11 23:14:33 +0000689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000690 char_type widen(char __c) const
691 {
692 return do_widen(__c);
693 }
694
Louis Dionne16fe2952018-07-11 23:14:33 +0000695 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000696 const char* widen(const char* __low, const char* __high, char_type* __to) const
697 {
698 return do_widen(__low, __high, __to);
699 }
700
Louis Dionne16fe2952018-07-11 23:14:33 +0000701 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000702 char narrow(char_type __c, char __dfault) const
703 {
704 return do_narrow(__c, __dfault);
705 }
706
Louis Dionne16fe2952018-07-11 23:14:33 +0000707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000708 const char* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
709 {
710 return do_narrow(__low, __high, __dfault, __to);
711 }
712
713 static locale::id id;
714
Howard Hinnant155c2af2010-05-24 17:49:41 +0000715#ifdef _CACHED_RUNES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000716 static const size_t table_size = _CACHED_RUNES;
Howard Hinnant155c2af2010-05-24 17:49:41 +0000717#else
718 static const size_t table_size = 256; // FIXME: Don't hardcode this.
719#endif
Louis Dionne16fe2952018-07-11 23:14:33 +0000720 _LIBCPP_INLINE_VISIBILITY const mask* table() const _NOEXCEPT {return __tab_;}
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000721 static const mask* classic_table() _NOEXCEPT;
Vasileios Kalintirisc5dd8942015-11-24 10:24:54 +0000722#if defined(__GLIBC__) || defined(__EMSCRIPTEN__)
Alexis Hunt92b0c812011-07-09 00:56:23 +0000723 static const int* __classic_upper_table() _NOEXCEPT;
724 static const int* __classic_lower_table() _NOEXCEPT;
Alexis Hunt5a4dd562011-07-09 01:09:31 +0000725#endif
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000726#if defined(__NetBSD__)
727 static const short* __classic_upper_table() _NOEXCEPT;
728 static const short* __classic_lower_table() _NOEXCEPT;
729#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000730
731protected:
732 ~ctype();
733 virtual char_type do_toupper(char_type __c) const;
734 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
735 virtual char_type do_tolower(char_type __c) const;
736 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
737 virtual char_type do_widen(char __c) const;
738 virtual const char* do_widen(const char* __low, const char* __high, char_type* __to) const;
739 virtual char do_narrow(char_type __c, char __dfault) const;
740 virtual const char* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const;
741};
742
743// template <class CharT> class ctype_byname;
744
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000745template <class _CharT> class _LIBCPP_TEMPLATE_VIS ctype_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746
747template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000748class _LIBCPP_TYPE_VIS ctype_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749 : public ctype<char>
750{
751 locale_t __l;
752
753public:
754 explicit ctype_byname(const char*, size_t = 0);
755 explicit ctype_byname(const string&, size_t = 0);
756
757protected:
758 ~ctype_byname();
759 virtual char_type do_toupper(char_type) const;
760 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
761 virtual char_type do_tolower(char_type) const;
762 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
763};
764
765template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000766class _LIBCPP_TYPE_VIS ctype_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000767 : public ctype<wchar_t>
768{
769 locale_t __l;
770
771public:
772 explicit ctype_byname(const char*, size_t = 0);
773 explicit ctype_byname(const string&, size_t = 0);
774
775protected:
776 ~ctype_byname();
777 virtual bool do_is(mask __m, char_type __c) const;
778 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
779 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
780 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
781 virtual char_type do_toupper(char_type) const;
782 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
783 virtual char_type do_tolower(char_type) const;
784 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
785 virtual char_type do_widen(char) const;
786 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
787 virtual char do_narrow(char_type, char __dfault) const;
788 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
789};
790
791template <class _CharT>
792inline _LIBCPP_INLINE_VISIBILITY
793bool
794isspace(_CharT __c, const locale& __loc)
795{
796 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
797}
798
799template <class _CharT>
800inline _LIBCPP_INLINE_VISIBILITY
801bool
802isprint(_CharT __c, const locale& __loc)
803{
804 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c);
805}
806
807template <class _CharT>
808inline _LIBCPP_INLINE_VISIBILITY
809bool
810iscntrl(_CharT __c, const locale& __loc)
811{
812 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c);
813}
814
815template <class _CharT>
816inline _LIBCPP_INLINE_VISIBILITY
817bool
818isupper(_CharT __c, const locale& __loc)
819{
820 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c);
821}
822
823template <class _CharT>
824inline _LIBCPP_INLINE_VISIBILITY
825bool
826islower(_CharT __c, const locale& __loc)
827{
828 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c);
829}
830
831template <class _CharT>
832inline _LIBCPP_INLINE_VISIBILITY
833bool
834isalpha(_CharT __c, const locale& __loc)
835{
836 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c);
837}
838
839template <class _CharT>
840inline _LIBCPP_INLINE_VISIBILITY
841bool
842isdigit(_CharT __c, const locale& __loc)
843{
844 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c);
845}
846
847template <class _CharT>
848inline _LIBCPP_INLINE_VISIBILITY
849bool
850ispunct(_CharT __c, const locale& __loc)
851{
852 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c);
853}
854
855template <class _CharT>
856inline _LIBCPP_INLINE_VISIBILITY
857bool
858isxdigit(_CharT __c, const locale& __loc)
859{
860 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c);
861}
862
863template <class _CharT>
864inline _LIBCPP_INLINE_VISIBILITY
865bool
866isalnum(_CharT __c, const locale& __loc)
867{
868 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c);
869}
870
871template <class _CharT>
872inline _LIBCPP_INLINE_VISIBILITY
873bool
874isgraph(_CharT __c, const locale& __loc)
875{
876 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c);
877}
878
879template <class _CharT>
880inline _LIBCPP_INLINE_VISIBILITY
881_CharT
882toupper(_CharT __c, const locale& __loc)
883{
884 return use_facet<ctype<_CharT> >(__loc).toupper(__c);
885}
886
887template <class _CharT>
888inline _LIBCPP_INLINE_VISIBILITY
889_CharT
890tolower(_CharT __c, const locale& __loc)
891{
892 return use_facet<ctype<_CharT> >(__loc).tolower(__c);
893}
894
895// codecvt_base
896
Howard Hinnant8331b762013-03-06 23:30:19 +0000897class _LIBCPP_TYPE_VIS codecvt_base
Howard Hinnantc51e1022010-05-11 19:42:16 +0000898{
899public:
Louis Dionne16fe2952018-07-11 23:14:33 +0000900 _LIBCPP_INLINE_VISIBILITY codecvt_base() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000901 enum result {ok, partial, error, noconv};
902};
903
904// template <class internT, class externT, class stateT> class codecvt;
905
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000906template <class _InternT, class _ExternT, class _StateT> class _LIBCPP_TEMPLATE_VIS codecvt;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000907
908// template <> class codecvt<char, char, mbstate_t>
909
910template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000911class _LIBCPP_TYPE_VIS codecvt<char, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000912 : public locale::facet,
913 public codecvt_base
914{
915public:
916 typedef char intern_type;
917 typedef char extern_type;
918 typedef mbstate_t state_type;
919
Louis Dionne16fe2952018-07-11 23:14:33 +0000920 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000921 explicit codecvt(size_t __refs = 0)
922 : locale::facet(__refs) {}
923
Louis Dionne16fe2952018-07-11 23:14:33 +0000924 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000925 result out(state_type& __st,
926 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
927 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
928 {
929 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
930 }
931
Louis Dionne16fe2952018-07-11 23:14:33 +0000932 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000933 result unshift(state_type& __st,
934 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
935 {
936 return do_unshift(__st, __to, __to_end, __to_nxt);
937 }
938
Louis Dionne16fe2952018-07-11 23:14:33 +0000939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000940 result in(state_type& __st,
941 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
942 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
943 {
944 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
945 }
946
Louis Dionne16fe2952018-07-11 23:14:33 +0000947 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000948 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949 {
950 return do_encoding();
951 }
952
Louis Dionne16fe2952018-07-11 23:14:33 +0000953 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000954 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000955 {
956 return do_always_noconv();
957 }
958
Louis Dionne16fe2952018-07-11 23:14:33 +0000959 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000960 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
961 {
962 return do_length(__st, __frm, __end, __mx);
963 }
964
Louis Dionne16fe2952018-07-11 23:14:33 +0000965 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000966 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967 {
968 return do_max_length();
969 }
970
971 static locale::id id;
972
973protected:
Louis Dionne16fe2952018-07-11 23:14:33 +0000974 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000975 explicit codecvt(const char*, size_t __refs = 0)
976 : locale::facet(__refs) {}
977
978 ~codecvt();
979
980 virtual result do_out(state_type& __st,
981 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
982 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
983 virtual result do_in(state_type& __st,
984 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
985 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
986 virtual result do_unshift(state_type& __st,
987 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000988 virtual int do_encoding() const _NOEXCEPT;
989 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990 virtual int do_length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000991 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000992};
993
994// template <> class codecvt<wchar_t, char, mbstate_t>
995
996template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000997class _LIBCPP_TYPE_VIS codecvt<wchar_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000998 : public locale::facet,
999 public codecvt_base
1000{
1001 locale_t __l;
1002public:
1003 typedef wchar_t intern_type;
1004 typedef char extern_type;
1005 typedef mbstate_t state_type;
1006
1007 explicit codecvt(size_t __refs = 0);
1008
Louis Dionne16fe2952018-07-11 23:14:33 +00001009 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010 result out(state_type& __st,
1011 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1012 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1013 {
1014 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1015 }
1016
Louis Dionne16fe2952018-07-11 23:14:33 +00001017 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018 result unshift(state_type& __st,
1019 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1020 {
1021 return do_unshift(__st, __to, __to_end, __to_nxt);
1022 }
1023
Louis Dionne16fe2952018-07-11 23:14:33 +00001024 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025 result in(state_type& __st,
1026 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1027 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1028 {
1029 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1030 }
1031
Louis Dionne16fe2952018-07-11 23:14:33 +00001032 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001033 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001034 {
1035 return do_encoding();
1036 }
1037
Louis Dionne16fe2952018-07-11 23:14:33 +00001038 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001039 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040 {
1041 return do_always_noconv();
1042 }
1043
Louis Dionne16fe2952018-07-11 23:14:33 +00001044 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001045 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1046 {
1047 return do_length(__st, __frm, __end, __mx);
1048 }
1049
Louis Dionne16fe2952018-07-11 23:14:33 +00001050 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001051 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001052 {
1053 return do_max_length();
1054 }
1055
1056 static locale::id id;
1057
1058protected:
1059 explicit codecvt(const char*, size_t __refs = 0);
1060
1061 ~codecvt();
1062
1063 virtual result do_out(state_type& __st,
1064 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1065 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1066 virtual result do_in(state_type& __st,
1067 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1068 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1069 virtual result do_unshift(state_type& __st,
1070 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001071 virtual int do_encoding() const _NOEXCEPT;
1072 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001073 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001074 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075};
1076
1077// template <> class codecvt<char16_t, char, mbstate_t>
1078
1079template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001080class _LIBCPP_TYPE_VIS codecvt<char16_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001081 : public locale::facet,
1082 public codecvt_base
1083{
1084public:
1085 typedef char16_t intern_type;
1086 typedef char extern_type;
1087 typedef mbstate_t state_type;
1088
Louis Dionne16fe2952018-07-11 23:14:33 +00001089 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001090 explicit codecvt(size_t __refs = 0)
1091 : locale::facet(__refs) {}
1092
Louis Dionne16fe2952018-07-11 23:14:33 +00001093 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094 result out(state_type& __st,
1095 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1096 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1097 {
1098 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1099 }
1100
Louis Dionne16fe2952018-07-11 23:14:33 +00001101 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001102 result unshift(state_type& __st,
1103 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1104 {
1105 return do_unshift(__st, __to, __to_end, __to_nxt);
1106 }
1107
Louis Dionne16fe2952018-07-11 23:14:33 +00001108 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001109 result in(state_type& __st,
1110 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1111 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1112 {
1113 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1114 }
1115
Louis Dionne16fe2952018-07-11 23:14:33 +00001116 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001117 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118 {
1119 return do_encoding();
1120 }
1121
Louis Dionne16fe2952018-07-11 23:14:33 +00001122 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001123 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124 {
1125 return do_always_noconv();
1126 }
1127
Louis Dionne16fe2952018-07-11 23:14:33 +00001128 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1130 {
1131 return do_length(__st, __frm, __end, __mx);
1132 }
1133
Louis Dionne16fe2952018-07-11 23:14:33 +00001134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001135 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136 {
1137 return do_max_length();
1138 }
1139
1140 static locale::id id;
1141
1142protected:
Louis Dionne16fe2952018-07-11 23:14:33 +00001143 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144 explicit codecvt(const char*, size_t __refs = 0)
1145 : locale::facet(__refs) {}
1146
1147 ~codecvt();
1148
1149 virtual result do_out(state_type& __st,
1150 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1151 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1152 virtual result do_in(state_type& __st,
1153 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1154 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1155 virtual result do_unshift(state_type& __st,
1156 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001157 virtual int do_encoding() const _NOEXCEPT;
1158 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001160 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161};
1162
1163// template <> class codecvt<char32_t, char, mbstate_t>
1164
1165template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001166class _LIBCPP_TYPE_VIS codecvt<char32_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167 : public locale::facet,
1168 public codecvt_base
1169{
1170public:
1171 typedef char32_t intern_type;
1172 typedef char extern_type;
1173 typedef mbstate_t state_type;
1174
Louis Dionne16fe2952018-07-11 23:14:33 +00001175 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176 explicit codecvt(size_t __refs = 0)
1177 : locale::facet(__refs) {}
1178
Louis Dionne16fe2952018-07-11 23:14:33 +00001179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001180 result out(state_type& __st,
1181 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1182 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1183 {
1184 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1185 }
1186
Louis Dionne16fe2952018-07-11 23:14:33 +00001187 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001188 result unshift(state_type& __st,
1189 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1190 {
1191 return do_unshift(__st, __to, __to_end, __to_nxt);
1192 }
1193
Louis Dionne16fe2952018-07-11 23:14:33 +00001194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195 result in(state_type& __st,
1196 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1197 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1198 {
1199 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1200 }
1201
Louis Dionne16fe2952018-07-11 23:14:33 +00001202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001203 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001204 {
1205 return do_encoding();
1206 }
1207
Louis Dionne16fe2952018-07-11 23:14:33 +00001208 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001209 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210 {
1211 return do_always_noconv();
1212 }
1213
Louis Dionne16fe2952018-07-11 23:14:33 +00001214 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001215 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1216 {
1217 return do_length(__st, __frm, __end, __mx);
1218 }
1219
Louis Dionne16fe2952018-07-11 23:14:33 +00001220 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001221 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001222 {
1223 return do_max_length();
1224 }
1225
1226 static locale::id id;
1227
1228protected:
Louis Dionne16fe2952018-07-11 23:14:33 +00001229 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001230 explicit codecvt(const char*, size_t __refs = 0)
1231 : locale::facet(__refs) {}
1232
1233 ~codecvt();
1234
1235 virtual result do_out(state_type& __st,
1236 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1237 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1238 virtual result do_in(state_type& __st,
1239 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1240 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1241 virtual result do_unshift(state_type& __st,
1242 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001243 virtual int do_encoding() const _NOEXCEPT;
1244 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001246 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247};
1248
Howard Hinnantc51e1022010-05-11 19:42:16 +00001249// template <class _InternT, class _ExternT, class _StateT> class codecvt_byname
1250
1251template <class _InternT, class _ExternT, class _StateT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001252class _LIBCPP_TEMPLATE_VIS codecvt_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00001253 : public codecvt<_InternT, _ExternT, _StateT>
1254{
1255public:
Louis Dionne16fe2952018-07-11 23:14:33 +00001256 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001257 explicit codecvt_byname(const char* __nm, size_t __refs = 0)
1258 : codecvt<_InternT, _ExternT, _StateT>(__nm, __refs) {}
Louis Dionne16fe2952018-07-11 23:14:33 +00001259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260 explicit codecvt_byname(const string& __nm, size_t __refs = 0)
1261 : codecvt<_InternT, _ExternT, _StateT>(__nm.c_str(), __refs) {}
1262protected:
1263 ~codecvt_byname();
1264};
1265
1266template <class _InternT, class _ExternT, class _StateT>
1267codecvt_byname<_InternT, _ExternT, _StateT>::~codecvt_byname()
1268{
1269}
1270
Louis Dionneb5ae3a72020-10-02 14:29:48 -04001271_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char, char, mbstate_t>)
1272_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<wchar_t, char, mbstate_t>)
1273_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char16_t, char, mbstate_t>)
1274_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char32_t, char, mbstate_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275
Howard Hinnantc834c512011-11-29 18:15:50 +00001276template <size_t _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001277struct __narrow_to_utf8
1278{
1279 template <class _OutputIterator, class _CharT>
1280 _OutputIterator
1281 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const;
1282};
1283
1284template <>
1285struct __narrow_to_utf8<8>
1286{
1287 template <class _OutputIterator, class _CharT>
Louis Dionne16fe2952018-07-11 23:14:33 +00001288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001289 _OutputIterator
1290 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1291 {
1292 for (; __wb < __we; ++__wb, ++__s)
1293 *__s = *__wb;
1294 return __s;
1295 }
1296};
1297
1298template <>
Louis Dionne5254b372018-10-25 12:13:43 +00001299struct _LIBCPP_TEMPLATE_VIS __narrow_to_utf8<16>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001300 : public codecvt<char16_t, char, mbstate_t>
1301{
Louis Dionne16fe2952018-07-11 23:14:33 +00001302 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001303 __narrow_to_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1304
Louis Dionne5254b372018-10-25 12:13:43 +00001305 _LIBCPP_EXPORTED_FROM_ABI ~__narrow_to_utf8();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001306
1307 template <class _OutputIterator, class _CharT>
Louis Dionne16fe2952018-07-11 23:14:33 +00001308 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309 _OutputIterator
1310 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1311 {
1312 result __r = ok;
1313 mbstate_t __mb;
1314 while (__wb < __we && __r != error)
1315 {
1316 const int __sz = 32;
1317 char __buf[__sz];
1318 char* __bn;
1319 const char16_t* __wn = (const char16_t*)__wb;
1320 __r = do_out(__mb, (const char16_t*)__wb, (const char16_t*)__we, __wn,
1321 __buf, __buf+__sz, __bn);
1322 if (__r == codecvt_base::error || __wn == (const char16_t*)__wb)
1323 __throw_runtime_error("locale not supported");
1324 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1325 *__s = *__p;
1326 __wb = (const _CharT*)__wn;
1327 }
1328 return __s;
1329 }
1330};
1331
1332template <>
Louis Dionne5254b372018-10-25 12:13:43 +00001333struct _LIBCPP_TEMPLATE_VIS __narrow_to_utf8<32>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001334 : public codecvt<char32_t, char, mbstate_t>
1335{
Louis Dionne16fe2952018-07-11 23:14:33 +00001336 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001337 __narrow_to_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1338
Louis Dionne5254b372018-10-25 12:13:43 +00001339 _LIBCPP_EXPORTED_FROM_ABI ~__narrow_to_utf8();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001340
1341 template <class _OutputIterator, class _CharT>
Louis Dionne16fe2952018-07-11 23:14:33 +00001342 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001343 _OutputIterator
1344 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1345 {
1346 result __r = ok;
1347 mbstate_t __mb;
1348 while (__wb < __we && __r != error)
1349 {
1350 const int __sz = 32;
1351 char __buf[__sz];
1352 char* __bn;
1353 const char32_t* __wn = (const char32_t*)__wb;
1354 __r = do_out(__mb, (const char32_t*)__wb, (const char32_t*)__we, __wn,
1355 __buf, __buf+__sz, __bn);
1356 if (__r == codecvt_base::error || __wn == (const char32_t*)__wb)
1357 __throw_runtime_error("locale not supported");
1358 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1359 *__s = *__p;
1360 __wb = (const _CharT*)__wn;
1361 }
1362 return __s;
1363 }
1364};
1365
Howard Hinnantc834c512011-11-29 18:15:50 +00001366template <size_t _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001367struct __widen_from_utf8
1368{
1369 template <class _OutputIterator>
1370 _OutputIterator
1371 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const;
1372};
1373
1374template <>
1375struct __widen_from_utf8<8>
1376{
1377 template <class _OutputIterator>
Louis Dionne16fe2952018-07-11 23:14:33 +00001378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001379 _OutputIterator
1380 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1381 {
1382 for (; __nb < __ne; ++__nb, ++__s)
1383 *__s = *__nb;
1384 return __s;
1385 }
1386};
1387
1388template <>
Louis Dionne5254b372018-10-25 12:13:43 +00001389struct _LIBCPP_TEMPLATE_VIS __widen_from_utf8<16>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001390 : public codecvt<char16_t, char, mbstate_t>
1391{
Louis Dionne16fe2952018-07-11 23:14:33 +00001392 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393 __widen_from_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1394
Louis Dionne5254b372018-10-25 12:13:43 +00001395 _LIBCPP_EXPORTED_FROM_ABI ~__widen_from_utf8();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396
1397 template <class _OutputIterator>
Louis Dionne16fe2952018-07-11 23:14:33 +00001398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001399 _OutputIterator
1400 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1401 {
1402 result __r = ok;
1403 mbstate_t __mb;
1404 while (__nb < __ne && __r != error)
1405 {
1406 const int __sz = 32;
1407 char16_t __buf[__sz];
1408 char16_t* __bn;
1409 const char* __nn = __nb;
1410 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1411 __buf, __buf+__sz, __bn);
1412 if (__r == codecvt_base::error || __nn == __nb)
1413 __throw_runtime_error("locale not supported");
1414 for (const char16_t* __p = __buf; __p < __bn; ++__p, ++__s)
1415 *__s = (wchar_t)*__p;
1416 __nb = __nn;
1417 }
1418 return __s;
1419 }
1420};
1421
1422template <>
Louis Dionne5254b372018-10-25 12:13:43 +00001423struct _LIBCPP_TEMPLATE_VIS __widen_from_utf8<32>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001424 : public codecvt<char32_t, char, mbstate_t>
1425{
Louis Dionne16fe2952018-07-11 23:14:33 +00001426 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001427 __widen_from_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1428
Louis Dionne5254b372018-10-25 12:13:43 +00001429 _LIBCPP_EXPORTED_FROM_ABI ~__widen_from_utf8();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001430
1431 template <class _OutputIterator>
Louis Dionne16fe2952018-07-11 23:14:33 +00001432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433 _OutputIterator
1434 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1435 {
1436 result __r = ok;
1437 mbstate_t __mb;
1438 while (__nb < __ne && __r != error)
1439 {
1440 const int __sz = 32;
1441 char32_t __buf[__sz];
1442 char32_t* __bn;
1443 const char* __nn = __nb;
1444 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1445 __buf, __buf+__sz, __bn);
1446 if (__r == codecvt_base::error || __nn == __nb)
1447 __throw_runtime_error("locale not supported");
1448 for (const char32_t* __p = __buf; __p < __bn; ++__p, ++__s)
1449 *__s = (wchar_t)*__p;
1450 __nb = __nn;
1451 }
1452 return __s;
1453 }
1454};
1455
1456// template <class charT> class numpunct
1457
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001458template <class _CharT> class _LIBCPP_TEMPLATE_VIS numpunct;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001459
1460template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001461class _LIBCPP_TYPE_VIS numpunct<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001462 : public locale::facet
1463{
1464public:
1465 typedef char char_type;
1466 typedef basic_string<char_type> string_type;
1467
1468 explicit numpunct(size_t __refs = 0);
1469
Louis Dionne16fe2952018-07-11 23:14:33 +00001470 _LIBCPP_INLINE_VISIBILITY char_type decimal_point() const {return do_decimal_point();}
1471 _LIBCPP_INLINE_VISIBILITY char_type thousands_sep() const {return do_thousands_sep();}
1472 _LIBCPP_INLINE_VISIBILITY string grouping() const {return do_grouping();}
1473 _LIBCPP_INLINE_VISIBILITY string_type truename() const {return do_truename();}
1474 _LIBCPP_INLINE_VISIBILITY string_type falsename() const {return do_falsename();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001475
1476 static locale::id id;
1477
1478protected:
1479 ~numpunct();
1480 virtual char_type do_decimal_point() const;
1481 virtual char_type do_thousands_sep() const;
1482 virtual string do_grouping() const;
1483 virtual string_type do_truename() const;
1484 virtual string_type do_falsename() const;
1485
1486 char_type __decimal_point_;
1487 char_type __thousands_sep_;
1488 string __grouping_;
1489};
1490
1491template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001492class _LIBCPP_TYPE_VIS numpunct<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001493 : public locale::facet
1494{
1495public:
1496 typedef wchar_t char_type;
1497 typedef basic_string<char_type> string_type;
1498
1499 explicit numpunct(size_t __refs = 0);
1500
Louis Dionne16fe2952018-07-11 23:14:33 +00001501 _LIBCPP_INLINE_VISIBILITY char_type decimal_point() const {return do_decimal_point();}
1502 _LIBCPP_INLINE_VISIBILITY char_type thousands_sep() const {return do_thousands_sep();}
1503 _LIBCPP_INLINE_VISIBILITY string grouping() const {return do_grouping();}
1504 _LIBCPP_INLINE_VISIBILITY string_type truename() const {return do_truename();}
1505 _LIBCPP_INLINE_VISIBILITY string_type falsename() const {return do_falsename();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001506
1507 static locale::id id;
1508
1509protected:
1510 ~numpunct();
1511 virtual char_type do_decimal_point() const;
1512 virtual char_type do_thousands_sep() const;
1513 virtual string do_grouping() const;
1514 virtual string_type do_truename() const;
1515 virtual string_type do_falsename() const;
1516
1517 char_type __decimal_point_;
1518 char_type __thousands_sep_;
1519 string __grouping_;
1520};
1521
1522// template <class charT> class numpunct_byname
1523
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001524template <class _CharT> class _LIBCPP_TEMPLATE_VIS numpunct_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001525
1526template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001527class _LIBCPP_TYPE_VIS numpunct_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001528: public numpunct<char>
1529{
1530public:
1531 typedef char char_type;
1532 typedef basic_string<char_type> string_type;
1533
1534 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1535 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1536
1537protected:
1538 ~numpunct_byname();
1539
1540private:
1541 void __init(const char*);
1542};
1543
1544template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001545class _LIBCPP_TYPE_VIS numpunct_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001546: public numpunct<wchar_t>
1547{
1548public:
1549 typedef wchar_t char_type;
1550 typedef basic_string<char_type> string_type;
1551
1552 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1553 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1554
1555protected:
1556 ~numpunct_byname();
1557
1558private:
1559 void __init(const char*);
1560};
1561
1562_LIBCPP_END_NAMESPACE_STD
1563
1564#endif // _LIBCPP___LOCALE