blob: d4f3cda08c98cc308cc3419427eb49d660c80182 [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 Dionne73912b22020-11-04 15:01:25 -050014#include <__availability>
Howard Hinnantc51e1022010-05-11 19:42:16 +000015#include <string>
16#include <memory>
17#include <utility>
18#include <mutex>
19#include <cstdint>
20#include <cctype>
Howard Hinnant155c2af2010-05-24 17:49:41 +000021#include <locale.h>
Eric Fiselierbb999f92017-05-31 22:14:05 +000022#if defined(_LIBCPP_MSVCRT_LIKE)
Thomas Anderson094acbc2019-03-27 18:09:30 +000023# include <cstring>
Howard Hinnantae0f80b2011-09-29 20:33:10 +000024# include <support/win32/locale_win32.h>
Muiez Ahmedaf108fb2020-11-10 09:54:03 -050025#elif defined(_AIX) || defined(__MVS__)
Howard Hinnantea382952013-08-14 18:00:20 +000026# include <support/ibm/xlocale.h>
Marshall Clow3477ec92014-07-10 15:20:28 +000027#elif defined(__ANDROID__)
Saleem Abdulrasoolac039f32018-04-13 18:14:57 +000028# include <support/android/locale_bionic.h>
Eric Fiselier7274c652014-11-25 21:57:41 +000029#elif defined(__sun__)
Eric Fiselier90adc202015-01-23 22:22:36 +000030# include <xlocale.h>
Eric Fiselier7274c652014-11-25 21:57:41 +000031# include <support/solaris/xlocale.h>
Sergey Dmitrouk9935bd42014-12-12 08:36:16 +000032#elif defined(_NEWLIB_VERSION)
33# include <support/newlib/xlocale.h>
Eric Fiseliercee57932017-08-03 04:28:10 +000034#elif (defined(__APPLE__) || defined(__FreeBSD__) \
Eric Fiselier7274c652014-11-25 21:57:41 +000035 || defined(__EMSCRIPTEN__) || defined(__IBMCPP__))
Howard Hinnantdd0d7022011-09-22 19:10:18 +000036# include <xlocale.h>
Petr Hosekfdb4a872017-04-13 21:29:21 +000037#elif defined(__Fuchsia__)
38# include <support/fuchsia/xlocale.h>
Dan Gohman280fd6b2019-05-01 16:47:30 +000039#elif defined(__wasi__)
40// WASI libc uses musl's locales support.
41# include <support/musl/xlocale.h>
Vasileios Kalintiris281b4cf2015-11-09 10:21:04 +000042#elif defined(_LIBCPP_HAS_MUSL_LIBC)
43# include <support/musl/xlocale.h>
Petr Hosekfdb4a872017-04-13 21:29:21 +000044#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000045
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000046#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000047#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000048#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000049
50_LIBCPP_BEGIN_NAMESPACE_STD
51
Martin Storsjo5482ac62017-11-23 10:38:18 +000052#if !defined(_LIBCPP_LOCALE__L_EXTENSIONS)
Eric Fiselierebc2d2c2017-05-08 22:02:43 +000053struct __libcpp_locale_guard {
54 _LIBCPP_INLINE_VISIBILITY
55 __libcpp_locale_guard(locale_t& __loc) : __old_loc_(uselocale(__loc)) {}
56
57 _LIBCPP_INLINE_VISIBILITY
58 ~__libcpp_locale_guard() {
59 if (__old_loc_)
60 uselocale(__old_loc_);
61 }
62
63 locale_t __old_loc_;
64private:
65 __libcpp_locale_guard(__libcpp_locale_guard const&);
66 __libcpp_locale_guard& operator=(__libcpp_locale_guard const&);
67};
Martin Storsjo5482ac62017-11-23 10:38:18 +000068#elif defined(_LIBCPP_MSVCRT_LIKE)
69struct __libcpp_locale_guard {
70 __libcpp_locale_guard(locale_t __l) :
Thomas Anderson094acbc2019-03-27 18:09:30 +000071 __status(_configthreadlocale(_ENABLE_PER_THREAD_LOCALE)) {
72 // Setting the locale can be expensive even when the locale given is
73 // already the current locale, so do an explicit check to see if the
74 // current locale is already the one we want.
75 const char* __lc = __setlocale(nullptr);
76 // If every category is the same, the locale string will simply be the
77 // locale name, otherwise it will be a semicolon-separated string listing
78 // each category. In the second case, we know at least one category won't
79 // be what we want, so we only have to check the first case.
80 if (strcmp(__l.__get_locale(), __lc) != 0) {
81 __locale_all = _strdup(__lc);
82 if (__locale_all == nullptr)
83 __throw_bad_alloc();
84 __setlocale(__l.__get_locale());
85 }
86 }
Martin Storsjo5482ac62017-11-23 10:38:18 +000087 ~__libcpp_locale_guard() {
Thomas Anderson094acbc2019-03-27 18:09:30 +000088 // The CRT documentation doesn't explicitly say, but setlocale() does the
89 // right thing when given a semicolon-separated list of locale settings
90 // for the different categories in the same format as returned by
91 // setlocale(LC_ALL, nullptr).
92 if (__locale_all != nullptr) {
93 __setlocale(__locale_all);
94 free(__locale_all);
95 }
96 _configthreadlocale(__status);
97 }
98 static const char* __setlocale(const char* __locale) {
99 const char* __new_locale = setlocale(LC_ALL, __locale);
100 if (__new_locale == nullptr)
101 __throw_bad_alloc();
102 return __new_locale;
Martin Storsjo5482ac62017-11-23 10:38:18 +0000103 }
104 int __status;
Thomas Anderson094acbc2019-03-27 18:09:30 +0000105 char* __locale_all = nullptr;
Martin Storsjo5482ac62017-11-23 10:38:18 +0000106};
Eric Fiselierebc2d2c2017-05-08 22:02:43 +0000107#endif
108
109
Howard Hinnant8331b762013-03-06 23:30:19 +0000110class _LIBCPP_TYPE_VIS locale;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000111
Howard Hinnanta54386e2012-09-14 00:39:16 +0000112template <class _Facet>
113_LIBCPP_INLINE_VISIBILITY
114bool
115has_facet(const locale&) _NOEXCEPT;
116
117template <class _Facet>
118_LIBCPP_INLINE_VISIBILITY
119const _Facet&
120use_facet(const locale&);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000121
Howard Hinnant8331b762013-03-06 23:30:19 +0000122class _LIBCPP_TYPE_VIS locale
Howard Hinnantc51e1022010-05-11 19:42:16 +0000123{
124public:
125 // types:
Howard Hinnant8331b762013-03-06 23:30:19 +0000126 class _LIBCPP_TYPE_VIS facet;
127 class _LIBCPP_TYPE_VIS id;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000128
129 typedef int category;
Mehdi Amini228053d2017-05-04 17:08:54 +0000130 _LIBCPP_AVAILABILITY_LOCALE_CATEGORY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000131 static const category // values assigned here are for exposition only
132 none = 0,
133 collate = LC_COLLATE_MASK,
134 ctype = LC_CTYPE_MASK,
135 monetary = LC_MONETARY_MASK,
136 numeric = LC_NUMERIC_MASK,
137 time = LC_TIME_MASK,
138 messages = LC_MESSAGES_MASK,
139 all = collate | ctype | monetary | numeric | time | messages;
140
141 // construct/copy/destroy:
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000142 locale() _NOEXCEPT;
143 locale(const locale&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000144 explicit locale(const char*);
145 explicit locale(const string&);
146 locale(const locale&, const char*, category);
147 locale(const locale&, const string&, category);
Howard Hinnantcf823322010-12-17 14:46:43 +0000148 template <class _Facet>
149 _LIBCPP_INLINE_VISIBILITY locale(const locale&, _Facet*);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000150 locale(const locale&, const locale&, category);
151
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000152 ~locale();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000153
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000154 const locale& operator=(const locale&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000155
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000156 template <class _Facet>
157 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
158 locale combine(const locale&) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000159
160 // locale operations:
161 string name() const;
162 bool operator==(const locale&) const;
163 bool operator!=(const locale& __y) const {return !(*this == __y);}
164 template <class _CharT, class _Traits, class _Allocator>
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000165 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 bool operator()(const basic_string<_CharT, _Traits, _Allocator>&,
167 const basic_string<_CharT, _Traits, _Allocator>&) const;
168
169 // global locale objects:
170 static locale global(const locale&);
171 static const locale& classic();
172
173private:
174 class __imp;
175 __imp* __locale_;
176
177 void __install_ctor(const locale&, facet*, long);
178 static locale& __global();
179 bool has_facet(id&) const;
180 const facet* use_facet(id&) const;
181
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000182 template <class _Facet> friend bool has_facet(const locale&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000183 template <class _Facet> friend const _Facet& use_facet(const locale&);
184};
185
Howard Hinnant8331b762013-03-06 23:30:19 +0000186class _LIBCPP_TYPE_VIS locale::facet
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187 : public __shared_count
188{
189protected:
Howard Hinnant9833cad2010-09-21 18:58:51 +0000190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191 explicit facet(size_t __refs = 0)
192 : __shared_count(static_cast<long>(__refs)-1) {}
193
194 virtual ~facet();
195
196// facet(const facet&) = delete; // effectively done in __shared_count
197// void operator=(const facet&) = delete;
198private:
Howard Hinnant719bda32011-05-28 14:41:13 +0000199 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000200};
201
Howard Hinnant8331b762013-03-06 23:30:19 +0000202class _LIBCPP_TYPE_VIS locale::id
Howard Hinnantc51e1022010-05-11 19:42:16 +0000203{
204 once_flag __flag_;
205 int32_t __id_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000206
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207 static int32_t __next_id;
208public:
Howard Hinnantac7d9f02012-07-26 16:14:37 +0000209 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR id() :__id_(0) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000210private:
211 void __init();
212 void operator=(const id&); // = delete;
213 id(const id&); // = delete;
214public: // only needed for tests
215 long __get();
216
217 friend class locale;
218 friend class locale::__imp;
219};
220
221template <class _Facet>
222inline _LIBCPP_INLINE_VISIBILITY
223locale::locale(const locale& __other, _Facet* __f)
224{
225 __install_ctor(__other, __f, __f ? __f->id.__get() : 0);
226}
227
228template <class _Facet>
229locale
230locale::combine(const locale& __other) const
231{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000232 if (!_VSTD::has_facet<_Facet>(__other))
Marshall Clow8fea1612016-08-25 15:09:01 +0000233 __throw_runtime_error("locale::combine: locale missing facet");
234
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000235 return locale(*this, &const_cast<_Facet&>(_VSTD::use_facet<_Facet>(__other)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000236}
237
238template <class _Facet>
239inline _LIBCPP_INLINE_VISIBILITY
240bool
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000241has_facet(const locale& __l) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000242{
243 return __l.has_facet(_Facet::id);
244}
245
246template <class _Facet>
247inline _LIBCPP_INLINE_VISIBILITY
248const _Facet&
249use_facet(const locale& __l)
250{
251 return static_cast<const _Facet&>(*__l.use_facet(_Facet::id));
252}
253
254// template <class _CharT> class collate;
255
256template <class _CharT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000257class _LIBCPP_TEMPLATE_VIS collate
Howard Hinnantc51e1022010-05-11 19:42:16 +0000258 : public locale::facet
259{
260public:
261 typedef _CharT char_type;
262 typedef basic_string<char_type> string_type;
263
Howard Hinnant9833cad2010-09-21 18:58:51 +0000264 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000265 explicit collate(size_t __refs = 0)
266 : locale::facet(__refs) {}
267
Howard Hinnant9833cad2010-09-21 18:58:51 +0000268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000269 int compare(const char_type* __lo1, const char_type* __hi1,
270 const char_type* __lo2, const char_type* __hi2) const
271 {
272 return do_compare(__lo1, __hi1, __lo2, __hi2);
273 }
274
Eric Fiselier39b86ea2019-03-08 23:59:29 +0000275 // FIXME(EricWF): The _LIBCPP_ALWAYS_INLINE is needed on Windows to work
276 // around a dllimport bug that expects an external instantiation.
Howard Hinnant9833cad2010-09-21 18:58:51 +0000277 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier39b86ea2019-03-08 23:59:29 +0000278 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +0000279 string_type transform(const char_type* __lo, const char_type* __hi) const
280 {
281 return do_transform(__lo, __hi);
282 }
283
Howard Hinnant9833cad2010-09-21 18:58:51 +0000284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000285 long hash(const char_type* __lo, const char_type* __hi) const
286 {
287 return do_hash(__lo, __hi);
288 }
289
290 static locale::id id;
291
292protected:
293 ~collate();
294 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
295 const char_type* __lo2, const char_type* __hi2) const;
296 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const
297 {return string_type(__lo, __hi);}
298 virtual long do_hash(const char_type* __lo, const char_type* __hi) const;
299};
300
301template <class _CharT> locale::id collate<_CharT>::id;
302
303template <class _CharT>
304collate<_CharT>::~collate()
305{
306}
307
308template <class _CharT>
309int
310collate<_CharT>::do_compare(const char_type* __lo1, const char_type* __hi1,
311 const char_type* __lo2, const char_type* __hi2) const
312{
313 for (; __lo2 != __hi2; ++__lo1, ++__lo2)
314 {
315 if (__lo1 == __hi1 || *__lo1 < *__lo2)
316 return -1;
317 if (*__lo2 < *__lo1)
318 return 1;
319 }
320 return __lo1 != __hi1;
321}
322
323template <class _CharT>
324long
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000325collate<_CharT>::do_hash(const char_type* __lo, const char_type* __hi) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000326{
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000327 size_t __h = 0;
328 const size_t __sr = __CHAR_BIT__ * sizeof(size_t) - 8;
329 const size_t __mask = size_t(0xF) << (__sr + 4);
330 for(const char_type* __p = __lo; __p != __hi; ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000331 {
Howard Hinnant28b24882011-12-01 20:21:04 +0000332 __h = (__h << 4) + static_cast<size_t>(*__p);
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000333 size_t __g = __h & __mask;
334 __h ^= __g | (__g >> __sr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000335 }
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000336 return static_cast<long>(__h);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000337}
338
Louis Dionneb5ae3a72020-10-02 14:29:48 -0400339_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<char>)
340_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000341
342// template <class CharT> class collate_byname;
343
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000344template <class _CharT> class _LIBCPP_TEMPLATE_VIS collate_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000345
346template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000347class _LIBCPP_TYPE_VIS collate_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000348 : public collate<char>
349{
350 locale_t __l;
351public:
352 typedef char char_type;
353 typedef basic_string<char_type> string_type;
354
355 explicit collate_byname(const char* __n, size_t __refs = 0);
356 explicit collate_byname(const string& __n, size_t __refs = 0);
357
358protected:
359 ~collate_byname();
360 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
361 const char_type* __lo2, const char_type* __hi2) const;
362 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
363};
364
365template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000366class _LIBCPP_TYPE_VIS collate_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000367 : public collate<wchar_t>
368{
369 locale_t __l;
370public:
371 typedef wchar_t char_type;
372 typedef basic_string<char_type> string_type;
373
374 explicit collate_byname(const char* __n, size_t __refs = 0);
375 explicit collate_byname(const string& __n, size_t __refs = 0);
376
377protected:
378 ~collate_byname();
379
380 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
381 const char_type* __lo2, const char_type* __hi2) const;
382 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
383};
384
385template <class _CharT, class _Traits, class _Allocator>
386bool
387locale::operator()(const basic_string<_CharT, _Traits, _Allocator>& __x,
388 const basic_string<_CharT, _Traits, _Allocator>& __y) const
389{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000390 return _VSTD::use_facet<_VSTD::collate<_CharT> >(*this).compare(
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391 __x.data(), __x.data() + __x.size(),
392 __y.data(), __y.data() + __y.size()) < 0;
393}
394
395// template <class charT> class ctype
396
Howard Hinnant8331b762013-03-06 23:30:19 +0000397class _LIBCPP_TYPE_VIS ctype_base
Howard Hinnant9833cad2010-09-21 18:58:51 +0000398{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399public:
Xiang Xiaoa0562e92020-11-11 15:30:21 -0500400#if defined(_LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE)
401 typedef unsigned long mask;
402 static const mask space = 1<<0;
403 static const mask print = 1<<1;
404 static const mask cntrl = 1<<2;
405 static const mask upper = 1<<3;
406 static const mask lower = 1<<4;
407 static const mask alpha = 1<<5;
408 static const mask digit = 1<<6;
409 static const mask punct = 1<<7;
410 static const mask xdigit = 1<<8;
411 static const mask blank = 1<<9;
412#if defined(__BIONIC__)
413 // Historically this was a part of regex_traits rather than ctype_base. The
414 // historical value of the constant is preserved for ABI compatibility.
415 static const mask __regex_word = 0x8000;
416#else
417 static const mask __regex_word = 1<<10;
418#endif // defined(__BIONIC__)
419#elif defined(__GLIBC__)
Alexis Hunt92b0c812011-07-09 00:56:23 +0000420 typedef unsigned short mask;
Howard Hinnant155c2af2010-05-24 17:49:41 +0000421 static const mask space = _ISspace;
422 static const mask print = _ISprint;
423 static const mask cntrl = _IScntrl;
424 static const mask upper = _ISupper;
425 static const mask lower = _ISlower;
426 static const mask alpha = _ISalpha;
427 static const mask digit = _ISdigit;
428 static const mask punct = _ISpunct;
429 static const mask xdigit = _ISxdigit;
430 static const mask blank = _ISblank;
Mikhail Maltsev014ed062019-06-14 09:04:16 +0000431#if defined(__mips__)
Mikhail Maltsev12eb5af2019-08-20 10:19:55 +0000432 static const mask __regex_word = static_cast<mask>(_ISbit(15));
Mikhail Maltsev014ed062019-06-14 09:04:16 +0000433#else
434 static const mask __regex_word = 0x80;
435#endif
Eric Fiselierbb999f92017-05-31 22:14:05 +0000436#elif defined(_LIBCPP_MSVCRT_LIKE)
Howard Hinnantd7a78632011-09-29 13:33:15 +0000437 typedef unsigned short mask;
Howard Hinnantdd0d7022011-09-22 19:10:18 +0000438 static const mask space = _SPACE;
439 static const mask print = _BLANK|_PUNCT|_ALPHA|_DIGIT;
440 static const mask cntrl = _CONTROL;
441 static const mask upper = _UPPER;
442 static const mask lower = _LOWER;
443 static const mask alpha = _ALPHA;
444 static const mask digit = _DIGIT;
445 static const mask punct = _PUNCT;
446 static const mask xdigit = _HEX;
447 static const mask blank = _BLANK;
Mikhail Maltsev014ed062019-06-14 09:04:16 +0000448 static const mask __regex_word = 0x80;
Jonathan Roelofsae9ab252015-03-11 17:00:28 +0000449# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
Dan Albertebdf28b2015-03-11 00:51:06 +0000450#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__)
JF Bastien0c265d82015-02-25 22:16:46 +0000451# ifdef __APPLE__
David Chisnall1d581062011-09-21 08:39:44 +0000452 typedef __uint32_t mask;
JF Bastien0c265d82015-02-25 22:16:46 +0000453# elif defined(__FreeBSD__)
David Chisnall1d581062011-09-21 08:39:44 +0000454 typedef unsigned long mask;
Vasileios Kalintirisc5dd8942015-11-24 10:24:54 +0000455# elif defined(__EMSCRIPTEN__) || defined(__NetBSD__)
Howard Hinnant942dbd22013-03-29 18:27:28 +0000456 typedef unsigned short mask;
JF Bastien0c265d82015-02-25 22:16:46 +0000457# endif
David Chisnall1d581062011-09-21 08:39:44 +0000458 static const mask space = _CTYPE_S;
459 static const mask print = _CTYPE_R;
460 static const mask cntrl = _CTYPE_C;
461 static const mask upper = _CTYPE_U;
462 static const mask lower = _CTYPE_L;
463 static const mask alpha = _CTYPE_A;
464 static const mask digit = _CTYPE_D;
465 static const mask punct = _CTYPE_P;
466 static const mask xdigit = _CTYPE_X;
Dan Albert2dca4072014-07-23 19:32:03 +0000467
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000468# if defined(__NetBSD__)
469 static const mask blank = _CTYPE_BL;
Mikhail Maltsev014ed062019-06-14 09:04:16 +0000470 // NetBSD defines classes up to 0x2000
471 // see sys/ctype_bits.h, _CTYPE_Q
472 static const mask __regex_word = 0x8000;
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000473# else
David Chisnall1d581062011-09-21 08:39:44 +0000474 static const mask blank = _CTYPE_B;
Mikhail Maltsev014ed062019-06-14 09:04:16 +0000475 static const mask __regex_word = 0x80;
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000476# endif
Howard Hinnanta47505d2013-08-30 14:42:39 +0000477#elif defined(__sun__) || defined(_AIX)
David Chisnall8074c342012-02-29 13:05:08 +0000478 typedef unsigned int mask;
479 static const mask space = _ISSPACE;
480 static const mask print = _ISPRINT;
481 static const mask cntrl = _ISCNTRL;
482 static const mask upper = _ISUPPER;
483 static const mask lower = _ISLOWER;
484 static const mask alpha = _ISALPHA;
485 static const mask digit = _ISDIGIT;
486 static const mask punct = _ISPUNCT;
487 static const mask xdigit = _ISXDIGIT;
488 static const mask blank = _ISBLANK;
Mikhail Maltsev014ed062019-06-14 09:04:16 +0000489 static const mask __regex_word = 0x80;
JF Bastien0c265d82015-02-25 22:16:46 +0000490#elif defined(_NEWLIB_VERSION)
491 // Same type as Newlib's _ctype_ array in newlib/libc/include/ctype.h.
492 typedef char mask;
493 static const mask space = _S;
494 static const mask print = _P | _U | _L | _N | _B;
495 static const mask cntrl = _C;
496 static const mask upper = _U;
497 static const mask lower = _L;
498 static const mask alpha = _U | _L;
499 static const mask digit = _N;
500 static const mask punct = _P;
501 static const mask xdigit = _X | _N;
502 static const mask blank = _B;
Mikhail Maltsev014ed062019-06-14 09:04:16 +0000503 static const mask __regex_word = 0x80;
Jonathan Roelofsae9ab252015-03-11 17:00:28 +0000504# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
505# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
506# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_XDIGIT
JF Bastien0c265d82015-02-25 22:16:46 +0000507#else
Xiang Xiaoa0562e92020-11-11 15:30:21 -0500508# error unknown rune table for this platform -- do you mean to define _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE?
JF Bastien0c265d82015-02-25 22:16:46 +0000509#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000510 static const mask alnum = alpha | digit;
511 static const mask graph = alnum | punct;
512
Louis Dionne16fe2952018-07-11 23:14:33 +0000513 _LIBCPP_INLINE_VISIBILITY ctype_base() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000514};
515
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000516template <class _CharT> class _LIBCPP_TEMPLATE_VIS ctype;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000517
518template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000519class _LIBCPP_TYPE_VIS ctype<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000520 : public locale::facet,
521 public ctype_base
522{
523public:
524 typedef wchar_t char_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000525
Louis Dionne16fe2952018-07-11 23:14:33 +0000526 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000527 explicit ctype(size_t __refs = 0)
528 : locale::facet(__refs) {}
529
Louis Dionne16fe2952018-07-11 23:14:33 +0000530 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000531 bool is(mask __m, char_type __c) const
532 {
533 return do_is(__m, __c);
534 }
535
Louis Dionne16fe2952018-07-11 23:14:33 +0000536 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000537 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
538 {
539 return do_is(__low, __high, __vec);
540 }
541
Louis Dionne16fe2952018-07-11 23:14:33 +0000542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543 const char_type* scan_is(mask __m, const char_type* __low, const char_type* __high) const
544 {
545 return do_scan_is(__m, __low, __high);
546 }
547
Louis Dionne16fe2952018-07-11 23:14:33 +0000548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000549 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
550 {
551 return do_scan_not(__m, __low, __high);
552 }
553
Louis Dionne16fe2952018-07-11 23:14:33 +0000554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000555 char_type toupper(char_type __c) const
556 {
557 return do_toupper(__c);
558 }
559
Louis Dionne16fe2952018-07-11 23:14:33 +0000560 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000561 const char_type* toupper(char_type* __low, const char_type* __high) const
562 {
563 return do_toupper(__low, __high);
564 }
565
Louis Dionne16fe2952018-07-11 23:14:33 +0000566 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000567 char_type tolower(char_type __c) const
568 {
569 return do_tolower(__c);
570 }
571
Louis Dionne16fe2952018-07-11 23:14:33 +0000572 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000573 const char_type* tolower(char_type* __low, const char_type* __high) const
574 {
575 return do_tolower(__low, __high);
576 }
577
Louis Dionne16fe2952018-07-11 23:14:33 +0000578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000579 char_type widen(char __c) const
580 {
581 return do_widen(__c);
582 }
583
Louis Dionne16fe2952018-07-11 23:14:33 +0000584 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000585 const char* widen(const char* __low, const char* __high, char_type* __to) const
586 {
587 return do_widen(__low, __high, __to);
588 }
589
Louis Dionne16fe2952018-07-11 23:14:33 +0000590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000591 char narrow(char_type __c, char __dfault) const
592 {
593 return do_narrow(__c, __dfault);
594 }
595
Louis Dionne16fe2952018-07-11 23:14:33 +0000596 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000597 const char_type* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
598 {
599 return do_narrow(__low, __high, __dfault, __to);
600 }
601
602 static locale::id id;
603
604protected:
605 ~ctype();
606 virtual bool do_is(mask __m, char_type __c) const;
607 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
608 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
609 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
610 virtual char_type do_toupper(char_type) const;
611 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
612 virtual char_type do_tolower(char_type) const;
613 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
614 virtual char_type do_widen(char) const;
615 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
616 virtual char do_narrow(char_type, char __dfault) const;
617 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
618};
619
620template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000621class _LIBCPP_TYPE_VIS ctype<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000622 : public locale::facet, public ctype_base
623{
624 const mask* __tab_;
625 bool __del_;
626public:
627 typedef char char_type;
628
629 explicit ctype(const mask* __tab = 0, bool __del = false, size_t __refs = 0);
630
Louis Dionne16fe2952018-07-11 23:14:33 +0000631 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000632 bool is(mask __m, char_type __c) const
633 {
Marshall Clow11de4872013-10-21 14:41:05 +0000634 return isascii(__c) ? (__tab_[static_cast<int>(__c)] & __m) !=0 : false;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000635 }
636
Louis Dionne16fe2952018-07-11 23:14:33 +0000637 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000638 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
639 {
640 for (; __low != __high; ++__low, ++__vec)
Howard Hinnant28b24882011-12-01 20:21:04 +0000641 *__vec = isascii(*__low) ? __tab_[static_cast<int>(*__low)] : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000642 return __low;
643 }
644
Louis Dionne16fe2952018-07-11 23:14:33 +0000645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000646 const char_type* scan_is (mask __m, const char_type* __low, const char_type* __high) const
647 {
648 for (; __low != __high; ++__low)
Howard Hinnant28b24882011-12-01 20:21:04 +0000649 if (isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000650 break;
651 return __low;
652 }
653
Louis Dionne16fe2952018-07-11 23:14:33 +0000654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000655 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
656 {
657 for (; __low != __high; ++__low)
Howard Hinnant28b24882011-12-01 20:21:04 +0000658 if (!(isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m)))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000659 break;
660 return __low;
661 }
662
Louis Dionne16fe2952018-07-11 23:14:33 +0000663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000664 char_type toupper(char_type __c) const
665 {
666 return do_toupper(__c);
667 }
668
Louis Dionne16fe2952018-07-11 23:14:33 +0000669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000670 const char_type* toupper(char_type* __low, const char_type* __high) const
671 {
672 return do_toupper(__low, __high);
673 }
674
Louis Dionne16fe2952018-07-11 23:14:33 +0000675 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000676 char_type tolower(char_type __c) const
677 {
678 return do_tolower(__c);
679 }
680
Louis Dionne16fe2952018-07-11 23:14:33 +0000681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000682 const char_type* tolower(char_type* __low, const char_type* __high) const
683 {
684 return do_tolower(__low, __high);
685 }
686
Louis Dionne16fe2952018-07-11 23:14:33 +0000687 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688 char_type widen(char __c) const
689 {
690 return do_widen(__c);
691 }
692
Louis Dionne16fe2952018-07-11 23:14:33 +0000693 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000694 const char* widen(const char* __low, const char* __high, char_type* __to) const
695 {
696 return do_widen(__low, __high, __to);
697 }
698
Louis Dionne16fe2952018-07-11 23:14:33 +0000699 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000700 char narrow(char_type __c, char __dfault) const
701 {
702 return do_narrow(__c, __dfault);
703 }
704
Louis Dionne16fe2952018-07-11 23:14:33 +0000705 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000706 const char* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
707 {
708 return do_narrow(__low, __high, __dfault, __to);
709 }
710
711 static locale::id id;
712
Howard Hinnant155c2af2010-05-24 17:49:41 +0000713#ifdef _CACHED_RUNES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000714 static const size_t table_size = _CACHED_RUNES;
Howard Hinnant155c2af2010-05-24 17:49:41 +0000715#else
716 static const size_t table_size = 256; // FIXME: Don't hardcode this.
717#endif
Louis Dionne16fe2952018-07-11 23:14:33 +0000718 _LIBCPP_INLINE_VISIBILITY const mask* table() const _NOEXCEPT {return __tab_;}
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000719 static const mask* classic_table() _NOEXCEPT;
Vasileios Kalintirisc5dd8942015-11-24 10:24:54 +0000720#if defined(__GLIBC__) || defined(__EMSCRIPTEN__)
Alexis Hunt92b0c812011-07-09 00:56:23 +0000721 static const int* __classic_upper_table() _NOEXCEPT;
722 static const int* __classic_lower_table() _NOEXCEPT;
Alexis Hunt5a4dd562011-07-09 01:09:31 +0000723#endif
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000724#if defined(__NetBSD__)
725 static const short* __classic_upper_table() _NOEXCEPT;
726 static const short* __classic_lower_table() _NOEXCEPT;
727#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000728
729protected:
730 ~ctype();
731 virtual char_type do_toupper(char_type __c) const;
732 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
733 virtual char_type do_tolower(char_type __c) const;
734 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
735 virtual char_type do_widen(char __c) const;
736 virtual const char* do_widen(const char* __low, const char* __high, char_type* __to) const;
737 virtual char do_narrow(char_type __c, char __dfault) const;
738 virtual const char* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const;
739};
740
741// template <class CharT> class ctype_byname;
742
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000743template <class _CharT> class _LIBCPP_TEMPLATE_VIS ctype_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000744
745template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000746class _LIBCPP_TYPE_VIS ctype_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747 : public ctype<char>
748{
749 locale_t __l;
750
751public:
752 explicit ctype_byname(const char*, size_t = 0);
753 explicit ctype_byname(const string&, size_t = 0);
754
755protected:
756 ~ctype_byname();
757 virtual char_type do_toupper(char_type) const;
758 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
759 virtual char_type do_tolower(char_type) const;
760 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
761};
762
763template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000764class _LIBCPP_TYPE_VIS ctype_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765 : public ctype<wchar_t>
766{
767 locale_t __l;
768
769public:
770 explicit ctype_byname(const char*, size_t = 0);
771 explicit ctype_byname(const string&, size_t = 0);
772
773protected:
774 ~ctype_byname();
775 virtual bool do_is(mask __m, char_type __c) const;
776 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
777 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
778 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
779 virtual char_type do_toupper(char_type) const;
780 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
781 virtual char_type do_tolower(char_type) const;
782 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
783 virtual char_type do_widen(char) const;
784 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
785 virtual char do_narrow(char_type, char __dfault) const;
786 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
787};
788
789template <class _CharT>
790inline _LIBCPP_INLINE_VISIBILITY
791bool
792isspace(_CharT __c, const locale& __loc)
793{
794 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
795}
796
797template <class _CharT>
798inline _LIBCPP_INLINE_VISIBILITY
799bool
800isprint(_CharT __c, const locale& __loc)
801{
802 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c);
803}
804
805template <class _CharT>
806inline _LIBCPP_INLINE_VISIBILITY
807bool
808iscntrl(_CharT __c, const locale& __loc)
809{
810 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c);
811}
812
813template <class _CharT>
814inline _LIBCPP_INLINE_VISIBILITY
815bool
816isupper(_CharT __c, const locale& __loc)
817{
818 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c);
819}
820
821template <class _CharT>
822inline _LIBCPP_INLINE_VISIBILITY
823bool
824islower(_CharT __c, const locale& __loc)
825{
826 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c);
827}
828
829template <class _CharT>
830inline _LIBCPP_INLINE_VISIBILITY
831bool
832isalpha(_CharT __c, const locale& __loc)
833{
834 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c);
835}
836
837template <class _CharT>
838inline _LIBCPP_INLINE_VISIBILITY
839bool
840isdigit(_CharT __c, const locale& __loc)
841{
842 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c);
843}
844
845template <class _CharT>
846inline _LIBCPP_INLINE_VISIBILITY
847bool
848ispunct(_CharT __c, const locale& __loc)
849{
850 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c);
851}
852
853template <class _CharT>
854inline _LIBCPP_INLINE_VISIBILITY
855bool
856isxdigit(_CharT __c, const locale& __loc)
857{
858 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c);
859}
860
861template <class _CharT>
862inline _LIBCPP_INLINE_VISIBILITY
863bool
864isalnum(_CharT __c, const locale& __loc)
865{
866 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c);
867}
868
869template <class _CharT>
870inline _LIBCPP_INLINE_VISIBILITY
871bool
872isgraph(_CharT __c, const locale& __loc)
873{
874 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c);
875}
876
877template <class _CharT>
878inline _LIBCPP_INLINE_VISIBILITY
879_CharT
880toupper(_CharT __c, const locale& __loc)
881{
882 return use_facet<ctype<_CharT> >(__loc).toupper(__c);
883}
884
885template <class _CharT>
886inline _LIBCPP_INLINE_VISIBILITY
887_CharT
888tolower(_CharT __c, const locale& __loc)
889{
890 return use_facet<ctype<_CharT> >(__loc).tolower(__c);
891}
892
893// codecvt_base
894
Howard Hinnant8331b762013-03-06 23:30:19 +0000895class _LIBCPP_TYPE_VIS codecvt_base
Howard Hinnantc51e1022010-05-11 19:42:16 +0000896{
897public:
Louis Dionne16fe2952018-07-11 23:14:33 +0000898 _LIBCPP_INLINE_VISIBILITY codecvt_base() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000899 enum result {ok, partial, error, noconv};
900};
901
902// template <class internT, class externT, class stateT> class codecvt;
903
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000904template <class _InternT, class _ExternT, class _StateT> class _LIBCPP_TEMPLATE_VIS codecvt;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000905
906// template <> class codecvt<char, char, mbstate_t>
907
908template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000909class _LIBCPP_TYPE_VIS codecvt<char, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910 : public locale::facet,
911 public codecvt_base
912{
913public:
914 typedef char intern_type;
915 typedef char extern_type;
916 typedef mbstate_t state_type;
917
Louis Dionne16fe2952018-07-11 23:14:33 +0000918 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000919 explicit codecvt(size_t __refs = 0)
920 : locale::facet(__refs) {}
921
Louis Dionne16fe2952018-07-11 23:14:33 +0000922 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000923 result out(state_type& __st,
924 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
925 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
926 {
927 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
928 }
929
Louis Dionne16fe2952018-07-11 23:14:33 +0000930 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000931 result unshift(state_type& __st,
932 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
933 {
934 return do_unshift(__st, __to, __to_end, __to_nxt);
935 }
936
Louis Dionne16fe2952018-07-11 23:14:33 +0000937 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000938 result in(state_type& __st,
939 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
940 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
941 {
942 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
943 }
944
Louis Dionne16fe2952018-07-11 23:14:33 +0000945 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000946 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947 {
948 return do_encoding();
949 }
950
Louis Dionne16fe2952018-07-11 23:14:33 +0000951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000952 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000953 {
954 return do_always_noconv();
955 }
956
Louis Dionne16fe2952018-07-11 23:14:33 +0000957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
959 {
960 return do_length(__st, __frm, __end, __mx);
961 }
962
Louis Dionne16fe2952018-07-11 23:14:33 +0000963 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000964 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965 {
966 return do_max_length();
967 }
968
969 static locale::id id;
970
971protected:
Louis Dionne16fe2952018-07-11 23:14:33 +0000972 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973 explicit codecvt(const char*, size_t __refs = 0)
974 : locale::facet(__refs) {}
975
976 ~codecvt();
977
978 virtual result do_out(state_type& __st,
979 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
980 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
981 virtual result do_in(state_type& __st,
982 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
983 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
984 virtual result do_unshift(state_type& __st,
985 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000986 virtual int do_encoding() const _NOEXCEPT;
987 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988 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 +0000989 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990};
991
992// template <> class codecvt<wchar_t, char, mbstate_t>
993
994template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000995class _LIBCPP_TYPE_VIS codecvt<wchar_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000996 : public locale::facet,
997 public codecvt_base
998{
999 locale_t __l;
1000public:
1001 typedef wchar_t intern_type;
1002 typedef char extern_type;
1003 typedef mbstate_t state_type;
1004
1005 explicit codecvt(size_t __refs = 0);
1006
Louis Dionne16fe2952018-07-11 23:14:33 +00001007 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008 result out(state_type& __st,
1009 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1010 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1011 {
1012 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1013 }
1014
Louis Dionne16fe2952018-07-11 23:14:33 +00001015 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016 result unshift(state_type& __st,
1017 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1018 {
1019 return do_unshift(__st, __to, __to_end, __to_nxt);
1020 }
1021
Louis Dionne16fe2952018-07-11 23:14:33 +00001022 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023 result in(state_type& __st,
1024 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1025 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1026 {
1027 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1028 }
1029
Louis Dionne16fe2952018-07-11 23:14:33 +00001030 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001031 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032 {
1033 return do_encoding();
1034 }
1035
Louis Dionne16fe2952018-07-11 23:14:33 +00001036 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001037 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038 {
1039 return do_always_noconv();
1040 }
1041
Louis Dionne16fe2952018-07-11 23:14:33 +00001042 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1044 {
1045 return do_length(__st, __frm, __end, __mx);
1046 }
1047
Louis Dionne16fe2952018-07-11 23:14:33 +00001048 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001049 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001050 {
1051 return do_max_length();
1052 }
1053
1054 static locale::id id;
1055
1056protected:
1057 explicit codecvt(const char*, size_t __refs = 0);
1058
1059 ~codecvt();
1060
1061 virtual result do_out(state_type& __st,
1062 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1063 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1064 virtual result do_in(state_type& __st,
1065 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1066 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1067 virtual result do_unshift(state_type& __st,
1068 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001069 virtual int do_encoding() const _NOEXCEPT;
1070 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001071 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 +00001072 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001073};
1074
1075// template <> class codecvt<char16_t, char, mbstate_t>
1076
1077template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001078class _LIBCPP_TYPE_VIS codecvt<char16_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079 : public locale::facet,
1080 public codecvt_base
1081{
1082public:
1083 typedef char16_t intern_type;
1084 typedef char extern_type;
1085 typedef mbstate_t state_type;
1086
Louis Dionne16fe2952018-07-11 23:14:33 +00001087 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001088 explicit codecvt(size_t __refs = 0)
1089 : locale::facet(__refs) {}
1090
Louis Dionne16fe2952018-07-11 23:14:33 +00001091 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092 result out(state_type& __st,
1093 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1094 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1095 {
1096 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1097 }
1098
Louis Dionne16fe2952018-07-11 23:14:33 +00001099 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001100 result unshift(state_type& __st,
1101 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1102 {
1103 return do_unshift(__st, __to, __to_end, __to_nxt);
1104 }
1105
Louis Dionne16fe2952018-07-11 23:14:33 +00001106 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001107 result in(state_type& __st,
1108 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1109 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1110 {
1111 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1112 }
1113
Louis Dionne16fe2952018-07-11 23:14:33 +00001114 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001115 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116 {
1117 return do_encoding();
1118 }
1119
Louis Dionne16fe2952018-07-11 23:14:33 +00001120 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001121 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122 {
1123 return do_always_noconv();
1124 }
1125
Louis Dionne16fe2952018-07-11 23:14:33 +00001126 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1128 {
1129 return do_length(__st, __frm, __end, __mx);
1130 }
1131
Louis Dionne16fe2952018-07-11 23:14:33 +00001132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001133 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134 {
1135 return do_max_length();
1136 }
1137
1138 static locale::id id;
1139
1140protected:
Louis Dionne16fe2952018-07-11 23:14:33 +00001141 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142 explicit codecvt(const char*, size_t __refs = 0)
1143 : locale::facet(__refs) {}
1144
1145 ~codecvt();
1146
1147 virtual result do_out(state_type& __st,
1148 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1149 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1150 virtual result do_in(state_type& __st,
1151 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1152 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1153 virtual result do_unshift(state_type& __st,
1154 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001155 virtual int do_encoding() const _NOEXCEPT;
1156 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157 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 +00001158 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159};
1160
1161// template <> class codecvt<char32_t, char, mbstate_t>
1162
1163template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001164class _LIBCPP_TYPE_VIS codecvt<char32_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165 : public locale::facet,
1166 public codecvt_base
1167{
1168public:
1169 typedef char32_t intern_type;
1170 typedef char extern_type;
1171 typedef mbstate_t state_type;
1172
Louis Dionne16fe2952018-07-11 23:14:33 +00001173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174 explicit codecvt(size_t __refs = 0)
1175 : locale::facet(__refs) {}
1176
Louis Dionne16fe2952018-07-11 23:14:33 +00001177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178 result out(state_type& __st,
1179 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1180 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1181 {
1182 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1183 }
1184
Louis Dionne16fe2952018-07-11 23:14:33 +00001185 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186 result unshift(state_type& __st,
1187 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1188 {
1189 return do_unshift(__st, __to, __to_end, __to_nxt);
1190 }
1191
Louis Dionne16fe2952018-07-11 23:14:33 +00001192 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001193 result in(state_type& __st,
1194 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1195 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1196 {
1197 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1198 }
1199
Louis Dionne16fe2952018-07-11 23:14:33 +00001200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001201 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202 {
1203 return do_encoding();
1204 }
1205
Louis Dionne16fe2952018-07-11 23:14:33 +00001206 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001207 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001208 {
1209 return do_always_noconv();
1210 }
1211
Louis Dionne16fe2952018-07-11 23:14:33 +00001212 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1214 {
1215 return do_length(__st, __frm, __end, __mx);
1216 }
1217
Louis Dionne16fe2952018-07-11 23:14:33 +00001218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001219 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220 {
1221 return do_max_length();
1222 }
1223
1224 static locale::id id;
1225
1226protected:
Louis Dionne16fe2952018-07-11 23:14:33 +00001227 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228 explicit codecvt(const char*, size_t __refs = 0)
1229 : locale::facet(__refs) {}
1230
1231 ~codecvt();
1232
1233 virtual result do_out(state_type& __st,
1234 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1235 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1236 virtual result do_in(state_type& __st,
1237 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1238 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1239 virtual result do_unshift(state_type& __st,
1240 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001241 virtual int do_encoding() const _NOEXCEPT;
1242 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243 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 +00001244 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245};
1246
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247// template <class _InternT, class _ExternT, class _StateT> class codecvt_byname
1248
1249template <class _InternT, class _ExternT, class _StateT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001250class _LIBCPP_TEMPLATE_VIS codecvt_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251 : public codecvt<_InternT, _ExternT, _StateT>
1252{
1253public:
Louis Dionne16fe2952018-07-11 23:14:33 +00001254 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001255 explicit codecvt_byname(const char* __nm, size_t __refs = 0)
1256 : codecvt<_InternT, _ExternT, _StateT>(__nm, __refs) {}
Louis Dionne16fe2952018-07-11 23:14:33 +00001257 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258 explicit codecvt_byname(const string& __nm, size_t __refs = 0)
1259 : codecvt<_InternT, _ExternT, _StateT>(__nm.c_str(), __refs) {}
1260protected:
1261 ~codecvt_byname();
1262};
1263
1264template <class _InternT, class _ExternT, class _StateT>
1265codecvt_byname<_InternT, _ExternT, _StateT>::~codecvt_byname()
1266{
1267}
1268
Louis Dionneb5ae3a72020-10-02 14:29:48 -04001269_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char, char, mbstate_t>)
1270_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<wchar_t, char, mbstate_t>)
1271_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char16_t, char, mbstate_t>)
1272_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char32_t, char, mbstate_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001273
Howard Hinnantc834c512011-11-29 18:15:50 +00001274template <size_t _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275struct __narrow_to_utf8
1276{
1277 template <class _OutputIterator, class _CharT>
1278 _OutputIterator
1279 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const;
1280};
1281
1282template <>
1283struct __narrow_to_utf8<8>
1284{
1285 template <class _OutputIterator, class _CharT>
Louis Dionne16fe2952018-07-11 23:14:33 +00001286 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287 _OutputIterator
1288 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1289 {
1290 for (; __wb < __we; ++__wb, ++__s)
1291 *__s = *__wb;
1292 return __s;
1293 }
1294};
1295
1296template <>
Louis Dionne5254b372018-10-25 12:13:43 +00001297struct _LIBCPP_TEMPLATE_VIS __narrow_to_utf8<16>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001298 : public codecvt<char16_t, char, mbstate_t>
1299{
Louis Dionne16fe2952018-07-11 23:14:33 +00001300 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001301 __narrow_to_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1302
Louis Dionne5254b372018-10-25 12:13:43 +00001303 _LIBCPP_EXPORTED_FROM_ABI ~__narrow_to_utf8();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001304
1305 template <class _OutputIterator, class _CharT>
Louis Dionne16fe2952018-07-11 23:14:33 +00001306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001307 _OutputIterator
1308 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1309 {
1310 result __r = ok;
1311 mbstate_t __mb;
1312 while (__wb < __we && __r != error)
1313 {
1314 const int __sz = 32;
1315 char __buf[__sz];
1316 char* __bn;
1317 const char16_t* __wn = (const char16_t*)__wb;
1318 __r = do_out(__mb, (const char16_t*)__wb, (const char16_t*)__we, __wn,
1319 __buf, __buf+__sz, __bn);
1320 if (__r == codecvt_base::error || __wn == (const char16_t*)__wb)
1321 __throw_runtime_error("locale not supported");
1322 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1323 *__s = *__p;
1324 __wb = (const _CharT*)__wn;
1325 }
1326 return __s;
1327 }
1328};
1329
1330template <>
Louis Dionne5254b372018-10-25 12:13:43 +00001331struct _LIBCPP_TEMPLATE_VIS __narrow_to_utf8<32>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001332 : public codecvt<char32_t, char, mbstate_t>
1333{
Louis Dionne16fe2952018-07-11 23:14:33 +00001334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001335 __narrow_to_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1336
Louis Dionne5254b372018-10-25 12:13:43 +00001337 _LIBCPP_EXPORTED_FROM_ABI ~__narrow_to_utf8();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338
1339 template <class _OutputIterator, class _CharT>
Louis Dionne16fe2952018-07-11 23:14:33 +00001340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001341 _OutputIterator
1342 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1343 {
1344 result __r = ok;
1345 mbstate_t __mb;
1346 while (__wb < __we && __r != error)
1347 {
1348 const int __sz = 32;
1349 char __buf[__sz];
1350 char* __bn;
1351 const char32_t* __wn = (const char32_t*)__wb;
1352 __r = do_out(__mb, (const char32_t*)__wb, (const char32_t*)__we, __wn,
1353 __buf, __buf+__sz, __bn);
1354 if (__r == codecvt_base::error || __wn == (const char32_t*)__wb)
1355 __throw_runtime_error("locale not supported");
1356 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1357 *__s = *__p;
1358 __wb = (const _CharT*)__wn;
1359 }
1360 return __s;
1361 }
1362};
1363
Howard Hinnantc834c512011-11-29 18:15:50 +00001364template <size_t _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001365struct __widen_from_utf8
1366{
1367 template <class _OutputIterator>
1368 _OutputIterator
1369 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const;
1370};
1371
1372template <>
1373struct __widen_from_utf8<8>
1374{
1375 template <class _OutputIterator>
Louis Dionne16fe2952018-07-11 23:14:33 +00001376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377 _OutputIterator
1378 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1379 {
1380 for (; __nb < __ne; ++__nb, ++__s)
1381 *__s = *__nb;
1382 return __s;
1383 }
1384};
1385
1386template <>
Louis Dionne5254b372018-10-25 12:13:43 +00001387struct _LIBCPP_TEMPLATE_VIS __widen_from_utf8<16>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001388 : public codecvt<char16_t, char, mbstate_t>
1389{
Louis Dionne16fe2952018-07-11 23:14:33 +00001390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001391 __widen_from_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1392
Louis Dionne5254b372018-10-25 12:13:43 +00001393 _LIBCPP_EXPORTED_FROM_ABI ~__widen_from_utf8();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001394
1395 template <class _OutputIterator>
Louis Dionne16fe2952018-07-11 23:14:33 +00001396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001397 _OutputIterator
1398 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1399 {
1400 result __r = ok;
1401 mbstate_t __mb;
1402 while (__nb < __ne && __r != error)
1403 {
1404 const int __sz = 32;
1405 char16_t __buf[__sz];
1406 char16_t* __bn;
1407 const char* __nn = __nb;
1408 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1409 __buf, __buf+__sz, __bn);
1410 if (__r == codecvt_base::error || __nn == __nb)
1411 __throw_runtime_error("locale not supported");
1412 for (const char16_t* __p = __buf; __p < __bn; ++__p, ++__s)
Martin Storsjö971fc1c2020-10-27 13:01:54 +02001413 *__s = *__p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001414 __nb = __nn;
1415 }
1416 return __s;
1417 }
1418};
1419
1420template <>
Louis Dionne5254b372018-10-25 12:13:43 +00001421struct _LIBCPP_TEMPLATE_VIS __widen_from_utf8<32>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001422 : public codecvt<char32_t, char, mbstate_t>
1423{
Louis Dionne16fe2952018-07-11 23:14:33 +00001424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001425 __widen_from_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1426
Louis Dionne5254b372018-10-25 12:13:43 +00001427 _LIBCPP_EXPORTED_FROM_ABI ~__widen_from_utf8();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428
1429 template <class _OutputIterator>
Louis Dionne16fe2952018-07-11 23:14:33 +00001430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001431 _OutputIterator
1432 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1433 {
1434 result __r = ok;
1435 mbstate_t __mb;
1436 while (__nb < __ne && __r != error)
1437 {
1438 const int __sz = 32;
1439 char32_t __buf[__sz];
1440 char32_t* __bn;
1441 const char* __nn = __nb;
1442 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1443 __buf, __buf+__sz, __bn);
1444 if (__r == codecvt_base::error || __nn == __nb)
1445 __throw_runtime_error("locale not supported");
1446 for (const char32_t* __p = __buf; __p < __bn; ++__p, ++__s)
Martin Storsjö971fc1c2020-10-27 13:01:54 +02001447 *__s = *__p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448 __nb = __nn;
1449 }
1450 return __s;
1451 }
1452};
1453
1454// template <class charT> class numpunct
1455
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001456template <class _CharT> class _LIBCPP_TEMPLATE_VIS numpunct;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457
1458template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001459class _LIBCPP_TYPE_VIS numpunct<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460 : public locale::facet
1461{
1462public:
1463 typedef char char_type;
1464 typedef basic_string<char_type> string_type;
1465
1466 explicit numpunct(size_t __refs = 0);
1467
Louis Dionne16fe2952018-07-11 23:14:33 +00001468 _LIBCPP_INLINE_VISIBILITY char_type decimal_point() const {return do_decimal_point();}
1469 _LIBCPP_INLINE_VISIBILITY char_type thousands_sep() const {return do_thousands_sep();}
1470 _LIBCPP_INLINE_VISIBILITY string grouping() const {return do_grouping();}
1471 _LIBCPP_INLINE_VISIBILITY string_type truename() const {return do_truename();}
1472 _LIBCPP_INLINE_VISIBILITY string_type falsename() const {return do_falsename();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001473
1474 static locale::id id;
1475
1476protected:
1477 ~numpunct();
1478 virtual char_type do_decimal_point() const;
1479 virtual char_type do_thousands_sep() const;
1480 virtual string do_grouping() const;
1481 virtual string_type do_truename() const;
1482 virtual string_type do_falsename() const;
1483
1484 char_type __decimal_point_;
1485 char_type __thousands_sep_;
1486 string __grouping_;
1487};
1488
1489template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001490class _LIBCPP_TYPE_VIS numpunct<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001491 : public locale::facet
1492{
1493public:
1494 typedef wchar_t char_type;
1495 typedef basic_string<char_type> string_type;
1496
1497 explicit numpunct(size_t __refs = 0);
1498
Louis Dionne16fe2952018-07-11 23:14:33 +00001499 _LIBCPP_INLINE_VISIBILITY char_type decimal_point() const {return do_decimal_point();}
1500 _LIBCPP_INLINE_VISIBILITY char_type thousands_sep() const {return do_thousands_sep();}
1501 _LIBCPP_INLINE_VISIBILITY string grouping() const {return do_grouping();}
1502 _LIBCPP_INLINE_VISIBILITY string_type truename() const {return do_truename();}
1503 _LIBCPP_INLINE_VISIBILITY string_type falsename() const {return do_falsename();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001504
1505 static locale::id id;
1506
1507protected:
1508 ~numpunct();
1509 virtual char_type do_decimal_point() const;
1510 virtual char_type do_thousands_sep() const;
1511 virtual string do_grouping() const;
1512 virtual string_type do_truename() const;
1513 virtual string_type do_falsename() const;
1514
1515 char_type __decimal_point_;
1516 char_type __thousands_sep_;
1517 string __grouping_;
1518};
1519
1520// template <class charT> class numpunct_byname
1521
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001522template <class _CharT> class _LIBCPP_TEMPLATE_VIS numpunct_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001523
1524template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001525class _LIBCPP_TYPE_VIS numpunct_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001526: public numpunct<char>
1527{
1528public:
1529 typedef char char_type;
1530 typedef basic_string<char_type> string_type;
1531
1532 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1533 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1534
1535protected:
1536 ~numpunct_byname();
1537
1538private:
1539 void __init(const char*);
1540};
1541
1542template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001543class _LIBCPP_TYPE_VIS numpunct_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001544: public numpunct<wchar_t>
1545{
1546public:
1547 typedef wchar_t char_type;
1548 typedef basic_string<char_type> string_type;
1549
1550 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1551 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1552
1553protected:
1554 ~numpunct_byname();
1555
1556private:
1557 void __init(const char*);
1558};
1559
1560_LIBCPP_END_NAMESPACE_STD
1561
1562#endif // _LIBCPP___LOCALE