blob: b79d78ddafeed4717bbf184dba04e5c5f24aef2c [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>
14#include <string>
15#include <memory>
16#include <utility>
17#include <mutex>
18#include <cstdint>
19#include <cctype>
Howard Hinnant155c2af2010-05-24 17:49:41 +000020#include <locale.h>
Eric Fiselierbb999f92017-05-31 22:14:05 +000021#if defined(_LIBCPP_MSVCRT_LIKE)
Howard Hinnantae0f80b2011-09-29 20:33:10 +000022# include <support/win32/locale_win32.h>
Marshall Clow2f96ec62014-03-11 17:18:47 +000023#elif defined(_AIX)
Howard Hinnantea382952013-08-14 18:00:20 +000024# include <support/ibm/xlocale.h>
Marshall Clow3477ec92014-07-10 15:20:28 +000025#elif defined(__ANDROID__)
Saleem Abdulrasoolac039f32018-04-13 18:14:57 +000026# include <support/android/locale_bionic.h>
Eric Fiselier7274c652014-11-25 21:57:41 +000027#elif defined(__sun__)
Eric Fiselier90adc202015-01-23 22:22:36 +000028# include <xlocale.h>
Eric Fiselier7274c652014-11-25 21:57:41 +000029# include <support/solaris/xlocale.h>
Sergey Dmitrouk9935bd42014-12-12 08:36:16 +000030#elif defined(_NEWLIB_VERSION)
31# include <support/newlib/xlocale.h>
Eric Fiseliercee57932017-08-03 04:28:10 +000032#elif (defined(__APPLE__) || defined(__FreeBSD__) \
Eric Fiselier7274c652014-11-25 21:57:41 +000033 || defined(__EMSCRIPTEN__) || defined(__IBMCPP__))
Howard Hinnantdd0d7022011-09-22 19:10:18 +000034# include <xlocale.h>
Petr Hosekfdb4a872017-04-13 21:29:21 +000035#elif defined(__Fuchsia__)
36# include <support/fuchsia/xlocale.h>
Vasileios Kalintiris281b4cf2015-11-09 10:21:04 +000037#elif defined(_LIBCPP_HAS_MUSL_LIBC)
38# include <support/musl/xlocale.h>
Petr Hosekfdb4a872017-04-13 21:29:21 +000039#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000040
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000041#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000042#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000043#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000044
45_LIBCPP_BEGIN_NAMESPACE_STD
46
Martin Storsjo5482ac62017-11-23 10:38:18 +000047#if !defined(_LIBCPP_LOCALE__L_EXTENSIONS)
Eric Fiselierebc2d2c2017-05-08 22:02:43 +000048struct __libcpp_locale_guard {
49 _LIBCPP_INLINE_VISIBILITY
50 __libcpp_locale_guard(locale_t& __loc) : __old_loc_(uselocale(__loc)) {}
51
52 _LIBCPP_INLINE_VISIBILITY
53 ~__libcpp_locale_guard() {
54 if (__old_loc_)
55 uselocale(__old_loc_);
56 }
57
58 locale_t __old_loc_;
59private:
60 __libcpp_locale_guard(__libcpp_locale_guard const&);
61 __libcpp_locale_guard& operator=(__libcpp_locale_guard const&);
62};
Martin Storsjo5482ac62017-11-23 10:38:18 +000063#elif defined(_LIBCPP_MSVCRT_LIKE)
64struct __libcpp_locale_guard {
65 __libcpp_locale_guard(locale_t __l) :
66 __status(_configthreadlocale(_ENABLE_PER_THREAD_LOCALE)),
67 __locale_collate(setlocale(LC_COLLATE, __l.__get_locale())),
68 __locale_ctype(setlocale(LC_CTYPE, __l.__get_locale())),
69 __locale_monetary(setlocale(LC_MONETARY, __l.__get_locale())),
70 __locale_numeric(setlocale(LC_NUMERIC, __l.__get_locale())),
71 __locale_time(setlocale(LC_TIME, __l.__get_locale()))
72 // LC_MESSAGES is not supported on Windows.
73 {}
74 ~__libcpp_locale_guard() {
75 setlocale(LC_COLLATE, __locale_collate);
76 setlocale(LC_CTYPE, __locale_ctype);
77 setlocale(LC_MONETARY, __locale_monetary);
78 setlocale(LC_NUMERIC, __locale_numeric);
79 setlocale(LC_TIME, __locale_time);
80 _configthreadlocale(__status);
81 }
82 int __status;
83 char* __locale_collate;
84 char* __locale_ctype;
85 char* __locale_monetary;
86 char* __locale_numeric;
87 char* __locale_time;
88};
Eric Fiselierebc2d2c2017-05-08 22:02:43 +000089#endif
90
91
Howard Hinnant8331b762013-03-06 23:30:19 +000092class _LIBCPP_TYPE_VIS locale;
Howard Hinnantc51e1022010-05-11 19:42:16 +000093
Howard Hinnanta54386e2012-09-14 00:39:16 +000094template <class _Facet>
95_LIBCPP_INLINE_VISIBILITY
96bool
97has_facet(const locale&) _NOEXCEPT;
98
99template <class _Facet>
100_LIBCPP_INLINE_VISIBILITY
101const _Facet&
102use_facet(const locale&);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000103
Howard Hinnant8331b762013-03-06 23:30:19 +0000104class _LIBCPP_TYPE_VIS locale
Howard Hinnantc51e1022010-05-11 19:42:16 +0000105{
106public:
107 // types:
Howard Hinnant8331b762013-03-06 23:30:19 +0000108 class _LIBCPP_TYPE_VIS facet;
109 class _LIBCPP_TYPE_VIS id;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000110
111 typedef int category;
Mehdi Amini228053d2017-05-04 17:08:54 +0000112 _LIBCPP_AVAILABILITY_LOCALE_CATEGORY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000113 static const category // values assigned here are for exposition only
114 none = 0,
115 collate = LC_COLLATE_MASK,
116 ctype = LC_CTYPE_MASK,
117 monetary = LC_MONETARY_MASK,
118 numeric = LC_NUMERIC_MASK,
119 time = LC_TIME_MASK,
120 messages = LC_MESSAGES_MASK,
121 all = collate | ctype | monetary | numeric | time | messages;
122
123 // construct/copy/destroy:
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000124 locale() _NOEXCEPT;
125 locale(const locale&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126 explicit locale(const char*);
127 explicit locale(const string&);
128 locale(const locale&, const char*, category);
129 locale(const locale&, const string&, category);
Howard Hinnantcf823322010-12-17 14:46:43 +0000130 template <class _Facet>
131 _LIBCPP_INLINE_VISIBILITY locale(const locale&, _Facet*);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132 locale(const locale&, const locale&, category);
133
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000134 ~locale();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000136 const locale& operator=(const locale&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000137
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000138 template <class _Facet>
139 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
140 locale combine(const locale&) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000141
142 // locale operations:
143 string name() const;
144 bool operator==(const locale&) const;
145 bool operator!=(const locale& __y) const {return !(*this == __y);}
146 template <class _CharT, class _Traits, class _Allocator>
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000147 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000148 bool operator()(const basic_string<_CharT, _Traits, _Allocator>&,
149 const basic_string<_CharT, _Traits, _Allocator>&) const;
150
151 // global locale objects:
152 static locale global(const locale&);
153 static const locale& classic();
154
155private:
156 class __imp;
157 __imp* __locale_;
158
159 void __install_ctor(const locale&, facet*, long);
160 static locale& __global();
161 bool has_facet(id&) const;
162 const facet* use_facet(id&) const;
163
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000164 template <class _Facet> friend bool has_facet(const locale&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000165 template <class _Facet> friend const _Facet& use_facet(const locale&);
166};
167
Howard Hinnant8331b762013-03-06 23:30:19 +0000168class _LIBCPP_TYPE_VIS locale::facet
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169 : public __shared_count
170{
171protected:
Howard Hinnant9833cad2010-09-21 18:58:51 +0000172 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000173 explicit facet(size_t __refs = 0)
174 : __shared_count(static_cast<long>(__refs)-1) {}
175
176 virtual ~facet();
177
178// facet(const facet&) = delete; // effectively done in __shared_count
179// void operator=(const facet&) = delete;
180private:
Howard Hinnant719bda32011-05-28 14:41:13 +0000181 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000182};
183
Howard Hinnant8331b762013-03-06 23:30:19 +0000184class _LIBCPP_TYPE_VIS locale::id
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185{
186 once_flag __flag_;
187 int32_t __id_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000188
Howard Hinnantc51e1022010-05-11 19:42:16 +0000189 static int32_t __next_id;
190public:
Howard Hinnantac7d9f02012-07-26 16:14:37 +0000191 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR id() :__id_(0) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000192private:
193 void __init();
194 void operator=(const id&); // = delete;
195 id(const id&); // = delete;
196public: // only needed for tests
197 long __get();
198
199 friend class locale;
200 friend class locale::__imp;
201};
202
203template <class _Facet>
204inline _LIBCPP_INLINE_VISIBILITY
205locale::locale(const locale& __other, _Facet* __f)
206{
207 __install_ctor(__other, __f, __f ? __f->id.__get() : 0);
208}
209
210template <class _Facet>
211locale
212locale::combine(const locale& __other) const
213{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000214 if (!_VSTD::has_facet<_Facet>(__other))
Marshall Clow8fea1612016-08-25 15:09:01 +0000215 __throw_runtime_error("locale::combine: locale missing facet");
216
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000217 return locale(*this, &const_cast<_Facet&>(_VSTD::use_facet<_Facet>(__other)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000218}
219
220template <class _Facet>
221inline _LIBCPP_INLINE_VISIBILITY
222bool
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000223has_facet(const locale& __l) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000224{
225 return __l.has_facet(_Facet::id);
226}
227
228template <class _Facet>
229inline _LIBCPP_INLINE_VISIBILITY
230const _Facet&
231use_facet(const locale& __l)
232{
233 return static_cast<const _Facet&>(*__l.use_facet(_Facet::id));
234}
235
236// template <class _CharT> class collate;
237
238template <class _CharT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000239class _LIBCPP_TEMPLATE_VIS collate
Howard Hinnantc51e1022010-05-11 19:42:16 +0000240 : public locale::facet
241{
242public:
243 typedef _CharT char_type;
244 typedef basic_string<char_type> string_type;
245
Howard Hinnant9833cad2010-09-21 18:58:51 +0000246 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247 explicit collate(size_t __refs = 0)
248 : locale::facet(__refs) {}
249
Howard Hinnant9833cad2010-09-21 18:58:51 +0000250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000251 int compare(const char_type* __lo1, const char_type* __hi1,
252 const char_type* __lo2, const char_type* __hi2) const
253 {
254 return do_compare(__lo1, __hi1, __lo2, __hi2);
255 }
256
Eric Fiselier39b86ea2019-03-08 23:59:29 +0000257 // FIXME(EricWF): The _LIBCPP_ALWAYS_INLINE is needed on Windows to work
258 // around a dllimport bug that expects an external instantiation.
Howard Hinnant9833cad2010-09-21 18:58:51 +0000259 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier39b86ea2019-03-08 23:59:29 +0000260 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +0000261 string_type transform(const char_type* __lo, const char_type* __hi) const
262 {
263 return do_transform(__lo, __hi);
264 }
265
Howard Hinnant9833cad2010-09-21 18:58:51 +0000266 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000267 long hash(const char_type* __lo, const char_type* __hi) const
268 {
269 return do_hash(__lo, __hi);
270 }
271
272 static locale::id id;
273
274protected:
275 ~collate();
276 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
277 const char_type* __lo2, const char_type* __hi2) const;
278 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const
279 {return string_type(__lo, __hi);}
280 virtual long do_hash(const char_type* __lo, const char_type* __hi) const;
281};
282
283template <class _CharT> locale::id collate<_CharT>::id;
284
285template <class _CharT>
286collate<_CharT>::~collate()
287{
288}
289
290template <class _CharT>
291int
292collate<_CharT>::do_compare(const char_type* __lo1, const char_type* __hi1,
293 const char_type* __lo2, const char_type* __hi2) const
294{
295 for (; __lo2 != __hi2; ++__lo1, ++__lo2)
296 {
297 if (__lo1 == __hi1 || *__lo1 < *__lo2)
298 return -1;
299 if (*__lo2 < *__lo1)
300 return 1;
301 }
302 return __lo1 != __hi1;
303}
304
305template <class _CharT>
306long
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000307collate<_CharT>::do_hash(const char_type* __lo, const char_type* __hi) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000308{
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000309 size_t __h = 0;
310 const size_t __sr = __CHAR_BIT__ * sizeof(size_t) - 8;
311 const size_t __mask = size_t(0xF) << (__sr + 4);
312 for(const char_type* __p = __lo; __p != __hi; ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000313 {
Howard Hinnant28b24882011-12-01 20:21:04 +0000314 __h = (__h << 4) + static_cast<size_t>(*__p);
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000315 size_t __g = __h & __mask;
316 __h ^= __g | (__g >> __sr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000317 }
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000318 return static_cast<long>(__h);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000319}
320
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000321_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<char>)
322_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000323
324// template <class CharT> class collate_byname;
325
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000326template <class _CharT> class _LIBCPP_TEMPLATE_VIS collate_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000327
328template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000329class _LIBCPP_TYPE_VIS collate_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000330 : public collate<char>
331{
332 locale_t __l;
333public:
334 typedef char char_type;
335 typedef basic_string<char_type> string_type;
336
337 explicit collate_byname(const char* __n, size_t __refs = 0);
338 explicit collate_byname(const string& __n, size_t __refs = 0);
339
340protected:
341 ~collate_byname();
342 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
343 const char_type* __lo2, const char_type* __hi2) const;
344 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
345};
346
347template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000348class _LIBCPP_TYPE_VIS collate_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000349 : public collate<wchar_t>
350{
351 locale_t __l;
352public:
353 typedef wchar_t char_type;
354 typedef basic_string<char_type> string_type;
355
356 explicit collate_byname(const char* __n, size_t __refs = 0);
357 explicit collate_byname(const string& __n, size_t __refs = 0);
358
359protected:
360 ~collate_byname();
361
362 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
363 const char_type* __lo2, const char_type* __hi2) const;
364 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
365};
366
367template <class _CharT, class _Traits, class _Allocator>
368bool
369locale::operator()(const basic_string<_CharT, _Traits, _Allocator>& __x,
370 const basic_string<_CharT, _Traits, _Allocator>& __y) const
371{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000372 return _VSTD::use_facet<_VSTD::collate<_CharT> >(*this).compare(
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373 __x.data(), __x.data() + __x.size(),
374 __y.data(), __y.data() + __y.size()) < 0;
375}
376
377// template <class charT> class ctype
378
Howard Hinnant8331b762013-03-06 23:30:19 +0000379class _LIBCPP_TYPE_VIS ctype_base
Howard Hinnant9833cad2010-09-21 18:58:51 +0000380{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381public:
Vasileios Kalintiris281b4cf2015-11-09 10:21:04 +0000382#if defined(__GLIBC__)
Alexis Hunt92b0c812011-07-09 00:56:23 +0000383 typedef unsigned short mask;
Howard Hinnant155c2af2010-05-24 17:49:41 +0000384 static const mask space = _ISspace;
385 static const mask print = _ISprint;
386 static const mask cntrl = _IScntrl;
387 static const mask upper = _ISupper;
388 static const mask lower = _ISlower;
389 static const mask alpha = _ISalpha;
390 static const mask digit = _ISdigit;
391 static const mask punct = _ISpunct;
392 static const mask xdigit = _ISxdigit;
393 static const mask blank = _ISblank;
Eric Fiselierbb999f92017-05-31 22:14:05 +0000394#elif defined(_LIBCPP_MSVCRT_LIKE)
Howard Hinnantd7a78632011-09-29 13:33:15 +0000395 typedef unsigned short mask;
Howard Hinnantdd0d7022011-09-22 19:10:18 +0000396 static const mask space = _SPACE;
397 static const mask print = _BLANK|_PUNCT|_ALPHA|_DIGIT;
398 static const mask cntrl = _CONTROL;
399 static const mask upper = _UPPER;
400 static const mask lower = _LOWER;
401 static const mask alpha = _ALPHA;
402 static const mask digit = _DIGIT;
403 static const mask punct = _PUNCT;
404 static const mask xdigit = _HEX;
405 static const mask blank = _BLANK;
Jonathan Roelofsae9ab252015-03-11 17:00:28 +0000406# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
Dan Albertebdf28b2015-03-11 00:51:06 +0000407#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__)
JF Bastien0c265d82015-02-25 22:16:46 +0000408# ifdef __APPLE__
David Chisnall1d581062011-09-21 08:39:44 +0000409 typedef __uint32_t mask;
JF Bastien0c265d82015-02-25 22:16:46 +0000410# elif defined(__FreeBSD__)
David Chisnall1d581062011-09-21 08:39:44 +0000411 typedef unsigned long mask;
Vasileios Kalintirisc5dd8942015-11-24 10:24:54 +0000412# elif defined(__EMSCRIPTEN__) || defined(__NetBSD__)
Howard Hinnant942dbd22013-03-29 18:27:28 +0000413 typedef unsigned short mask;
JF Bastien0c265d82015-02-25 22:16:46 +0000414# endif
David Chisnall1d581062011-09-21 08:39:44 +0000415 static const mask space = _CTYPE_S;
416 static const mask print = _CTYPE_R;
417 static const mask cntrl = _CTYPE_C;
418 static const mask upper = _CTYPE_U;
419 static const mask lower = _CTYPE_L;
420 static const mask alpha = _CTYPE_A;
421 static const mask digit = _CTYPE_D;
422 static const mask punct = _CTYPE_P;
423 static const mask xdigit = _CTYPE_X;
Dan Albert2dca4072014-07-23 19:32:03 +0000424
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000425# if defined(__NetBSD__)
426 static const mask blank = _CTYPE_BL;
427# else
David Chisnall1d581062011-09-21 08:39:44 +0000428 static const mask blank = _CTYPE_B;
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000429# endif
Howard Hinnanta47505d2013-08-30 14:42:39 +0000430#elif defined(__sun__) || defined(_AIX)
David Chisnall8074c342012-02-29 13:05:08 +0000431 typedef unsigned int mask;
432 static const mask space = _ISSPACE;
433 static const mask print = _ISPRINT;
434 static const mask cntrl = _ISCNTRL;
435 static const mask upper = _ISUPPER;
436 static const mask lower = _ISLOWER;
437 static const mask alpha = _ISALPHA;
438 static const mask digit = _ISDIGIT;
439 static const mask punct = _ISPUNCT;
440 static const mask xdigit = _ISXDIGIT;
441 static const mask blank = _ISBLANK;
JF Bastien0c265d82015-02-25 22:16:46 +0000442#elif defined(_NEWLIB_VERSION)
443 // Same type as Newlib's _ctype_ array in newlib/libc/include/ctype.h.
444 typedef char mask;
445 static const mask space = _S;
446 static const mask print = _P | _U | _L | _N | _B;
447 static const mask cntrl = _C;
448 static const mask upper = _U;
449 static const mask lower = _L;
450 static const mask alpha = _U | _L;
451 static const mask digit = _N;
452 static const mask punct = _P;
453 static const mask xdigit = _X | _N;
454 static const mask blank = _B;
Jonathan Roelofsae9ab252015-03-11 17:00:28 +0000455# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
456# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
457# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_XDIGIT
JF Bastien0c265d82015-02-25 22:16:46 +0000458#else
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000459 typedef unsigned long mask;
460 static const mask space = 1<<0;
461 static const mask print = 1<<1;
462 static const mask cntrl = 1<<2;
463 static const mask upper = 1<<3;
464 static const mask lower = 1<<4;
465 static const mask alpha = 1<<5;
466 static const mask digit = 1<<6;
467 static const mask punct = 1<<7;
468 static const mask xdigit = 1<<8;
469 static const mask blank = 1<<9;
JF Bastien0c265d82015-02-25 22:16:46 +0000470#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000471 static const mask alnum = alpha | digit;
472 static const mask graph = alnum | punct;
473
Louis Dionne16fe2952018-07-11 23:14:33 +0000474 _LIBCPP_INLINE_VISIBILITY ctype_base() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000475};
476
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000477template <class _CharT> class _LIBCPP_TEMPLATE_VIS ctype;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000478
479template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000480class _LIBCPP_TYPE_VIS ctype<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000481 : public locale::facet,
482 public ctype_base
483{
484public:
485 typedef wchar_t char_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000486
Louis Dionne16fe2952018-07-11 23:14:33 +0000487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000488 explicit ctype(size_t __refs = 0)
489 : locale::facet(__refs) {}
490
Louis Dionne16fe2952018-07-11 23:14:33 +0000491 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000492 bool is(mask __m, char_type __c) const
493 {
494 return do_is(__m, __c);
495 }
496
Louis Dionne16fe2952018-07-11 23:14:33 +0000497 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000498 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
499 {
500 return do_is(__low, __high, __vec);
501 }
502
Louis Dionne16fe2952018-07-11 23:14:33 +0000503 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000504 const char_type* scan_is(mask __m, const char_type* __low, const char_type* __high) const
505 {
506 return do_scan_is(__m, __low, __high);
507 }
508
Louis Dionne16fe2952018-07-11 23:14:33 +0000509 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000510 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
511 {
512 return do_scan_not(__m, __low, __high);
513 }
514
Louis Dionne16fe2952018-07-11 23:14:33 +0000515 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000516 char_type toupper(char_type __c) const
517 {
518 return do_toupper(__c);
519 }
520
Louis Dionne16fe2952018-07-11 23:14:33 +0000521 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000522 const char_type* toupper(char_type* __low, const char_type* __high) const
523 {
524 return do_toupper(__low, __high);
525 }
526
Louis Dionne16fe2952018-07-11 23:14:33 +0000527 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000528 char_type tolower(char_type __c) const
529 {
530 return do_tolower(__c);
531 }
532
Louis Dionne16fe2952018-07-11 23:14:33 +0000533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000534 const char_type* tolower(char_type* __low, const char_type* __high) const
535 {
536 return do_tolower(__low, __high);
537 }
538
Louis Dionne16fe2952018-07-11 23:14:33 +0000539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000540 char_type widen(char __c) const
541 {
542 return do_widen(__c);
543 }
544
Louis Dionne16fe2952018-07-11 23:14:33 +0000545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000546 const char* widen(const char* __low, const char* __high, char_type* __to) const
547 {
548 return do_widen(__low, __high, __to);
549 }
550
Louis Dionne16fe2952018-07-11 23:14:33 +0000551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000552 char narrow(char_type __c, char __dfault) const
553 {
554 return do_narrow(__c, __dfault);
555 }
556
Louis Dionne16fe2952018-07-11 23:14:33 +0000557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000558 const char_type* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
559 {
560 return do_narrow(__low, __high, __dfault, __to);
561 }
562
563 static locale::id id;
564
565protected:
566 ~ctype();
567 virtual bool do_is(mask __m, char_type __c) const;
568 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
569 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
570 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
571 virtual char_type do_toupper(char_type) const;
572 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
573 virtual char_type do_tolower(char_type) const;
574 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
575 virtual char_type do_widen(char) const;
576 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
577 virtual char do_narrow(char_type, char __dfault) const;
578 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
579};
580
581template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000582class _LIBCPP_TYPE_VIS ctype<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000583 : public locale::facet, public ctype_base
584{
585 const mask* __tab_;
586 bool __del_;
587public:
588 typedef char char_type;
589
590 explicit ctype(const mask* __tab = 0, bool __del = false, size_t __refs = 0);
591
Louis Dionne16fe2952018-07-11 23:14:33 +0000592 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593 bool is(mask __m, char_type __c) const
594 {
Marshall Clow11de4872013-10-21 14:41:05 +0000595 return isascii(__c) ? (__tab_[static_cast<int>(__c)] & __m) !=0 : false;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000596 }
597
Louis Dionne16fe2952018-07-11 23:14:33 +0000598 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000599 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
600 {
601 for (; __low != __high; ++__low, ++__vec)
Howard Hinnant28b24882011-12-01 20:21:04 +0000602 *__vec = isascii(*__low) ? __tab_[static_cast<int>(*__low)] : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000603 return __low;
604 }
605
Louis Dionne16fe2952018-07-11 23:14:33 +0000606 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000607 const char_type* scan_is (mask __m, const char_type* __low, const char_type* __high) const
608 {
609 for (; __low != __high; ++__low)
Howard Hinnant28b24882011-12-01 20:21:04 +0000610 if (isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000611 break;
612 return __low;
613 }
614
Louis Dionne16fe2952018-07-11 23:14:33 +0000615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000616 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
617 {
618 for (; __low != __high; ++__low)
Howard Hinnant28b24882011-12-01 20:21:04 +0000619 if (!(isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m)))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000620 break;
621 return __low;
622 }
623
Louis Dionne16fe2952018-07-11 23:14:33 +0000624 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000625 char_type toupper(char_type __c) const
626 {
627 return do_toupper(__c);
628 }
629
Louis Dionne16fe2952018-07-11 23:14:33 +0000630 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000631 const char_type* toupper(char_type* __low, const char_type* __high) const
632 {
633 return do_toupper(__low, __high);
634 }
635
Louis Dionne16fe2952018-07-11 23:14:33 +0000636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000637 char_type tolower(char_type __c) const
638 {
639 return do_tolower(__c);
640 }
641
Louis Dionne16fe2952018-07-11 23:14:33 +0000642 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000643 const char_type* tolower(char_type* __low, const char_type* __high) const
644 {
645 return do_tolower(__low, __high);
646 }
647
Louis Dionne16fe2952018-07-11 23:14:33 +0000648 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000649 char_type widen(char __c) const
650 {
651 return do_widen(__c);
652 }
653
Louis Dionne16fe2952018-07-11 23:14:33 +0000654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000655 const char* widen(const char* __low, const char* __high, char_type* __to) const
656 {
657 return do_widen(__low, __high, __to);
658 }
659
Louis Dionne16fe2952018-07-11 23:14:33 +0000660 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000661 char narrow(char_type __c, char __dfault) const
662 {
663 return do_narrow(__c, __dfault);
664 }
665
Louis Dionne16fe2952018-07-11 23:14:33 +0000666 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000667 const char* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
668 {
669 return do_narrow(__low, __high, __dfault, __to);
670 }
671
672 static locale::id id;
673
Howard Hinnant155c2af2010-05-24 17:49:41 +0000674#ifdef _CACHED_RUNES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000675 static const size_t table_size = _CACHED_RUNES;
Howard Hinnant155c2af2010-05-24 17:49:41 +0000676#else
677 static const size_t table_size = 256; // FIXME: Don't hardcode this.
678#endif
Louis Dionne16fe2952018-07-11 23:14:33 +0000679 _LIBCPP_INLINE_VISIBILITY const mask* table() const _NOEXCEPT {return __tab_;}
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000680 static const mask* classic_table() _NOEXCEPT;
Vasileios Kalintirisc5dd8942015-11-24 10:24:54 +0000681#if defined(__GLIBC__) || defined(__EMSCRIPTEN__)
Alexis Hunt92b0c812011-07-09 00:56:23 +0000682 static const int* __classic_upper_table() _NOEXCEPT;
683 static const int* __classic_lower_table() _NOEXCEPT;
Alexis Hunt5a4dd562011-07-09 01:09:31 +0000684#endif
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000685#if defined(__NetBSD__)
686 static const short* __classic_upper_table() _NOEXCEPT;
687 static const short* __classic_lower_table() _NOEXCEPT;
688#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000689
690protected:
691 ~ctype();
692 virtual char_type do_toupper(char_type __c) const;
693 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
694 virtual char_type do_tolower(char_type __c) const;
695 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
696 virtual char_type do_widen(char __c) const;
697 virtual const char* do_widen(const char* __low, const char* __high, char_type* __to) const;
698 virtual char do_narrow(char_type __c, char __dfault) const;
699 virtual const char* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const;
700};
701
702// template <class CharT> class ctype_byname;
703
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000704template <class _CharT> class _LIBCPP_TEMPLATE_VIS ctype_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000705
706template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000707class _LIBCPP_TYPE_VIS ctype_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000708 : public ctype<char>
709{
710 locale_t __l;
711
712public:
713 explicit ctype_byname(const char*, size_t = 0);
714 explicit ctype_byname(const string&, size_t = 0);
715
716protected:
717 ~ctype_byname();
718 virtual char_type do_toupper(char_type) const;
719 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
720 virtual char_type do_tolower(char_type) const;
721 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
722};
723
724template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000725class _LIBCPP_TYPE_VIS ctype_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726 : public ctype<wchar_t>
727{
728 locale_t __l;
729
730public:
731 explicit ctype_byname(const char*, size_t = 0);
732 explicit ctype_byname(const string&, size_t = 0);
733
734protected:
735 ~ctype_byname();
736 virtual bool do_is(mask __m, char_type __c) const;
737 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
738 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
739 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
740 virtual char_type do_toupper(char_type) const;
741 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
742 virtual char_type do_tolower(char_type) const;
743 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
744 virtual char_type do_widen(char) const;
745 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
746 virtual char do_narrow(char_type, char __dfault) const;
747 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
748};
749
750template <class _CharT>
751inline _LIBCPP_INLINE_VISIBILITY
752bool
753isspace(_CharT __c, const locale& __loc)
754{
755 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
756}
757
758template <class _CharT>
759inline _LIBCPP_INLINE_VISIBILITY
760bool
761isprint(_CharT __c, const locale& __loc)
762{
763 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c);
764}
765
766template <class _CharT>
767inline _LIBCPP_INLINE_VISIBILITY
768bool
769iscntrl(_CharT __c, const locale& __loc)
770{
771 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c);
772}
773
774template <class _CharT>
775inline _LIBCPP_INLINE_VISIBILITY
776bool
777isupper(_CharT __c, const locale& __loc)
778{
779 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c);
780}
781
782template <class _CharT>
783inline _LIBCPP_INLINE_VISIBILITY
784bool
785islower(_CharT __c, const locale& __loc)
786{
787 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c);
788}
789
790template <class _CharT>
791inline _LIBCPP_INLINE_VISIBILITY
792bool
793isalpha(_CharT __c, const locale& __loc)
794{
795 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c);
796}
797
798template <class _CharT>
799inline _LIBCPP_INLINE_VISIBILITY
800bool
801isdigit(_CharT __c, const locale& __loc)
802{
803 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c);
804}
805
806template <class _CharT>
807inline _LIBCPP_INLINE_VISIBILITY
808bool
809ispunct(_CharT __c, const locale& __loc)
810{
811 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c);
812}
813
814template <class _CharT>
815inline _LIBCPP_INLINE_VISIBILITY
816bool
817isxdigit(_CharT __c, const locale& __loc)
818{
819 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c);
820}
821
822template <class _CharT>
823inline _LIBCPP_INLINE_VISIBILITY
824bool
825isalnum(_CharT __c, const locale& __loc)
826{
827 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c);
828}
829
830template <class _CharT>
831inline _LIBCPP_INLINE_VISIBILITY
832bool
833isgraph(_CharT __c, const locale& __loc)
834{
835 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c);
836}
837
838template <class _CharT>
839inline _LIBCPP_INLINE_VISIBILITY
840_CharT
841toupper(_CharT __c, const locale& __loc)
842{
843 return use_facet<ctype<_CharT> >(__loc).toupper(__c);
844}
845
846template <class _CharT>
847inline _LIBCPP_INLINE_VISIBILITY
848_CharT
849tolower(_CharT __c, const locale& __loc)
850{
851 return use_facet<ctype<_CharT> >(__loc).tolower(__c);
852}
853
854// codecvt_base
855
Howard Hinnant8331b762013-03-06 23:30:19 +0000856class _LIBCPP_TYPE_VIS codecvt_base
Howard Hinnantc51e1022010-05-11 19:42:16 +0000857{
858public:
Louis Dionne16fe2952018-07-11 23:14:33 +0000859 _LIBCPP_INLINE_VISIBILITY codecvt_base() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000860 enum result {ok, partial, error, noconv};
861};
862
863// template <class internT, class externT, class stateT> class codecvt;
864
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000865template <class _InternT, class _ExternT, class _StateT> class _LIBCPP_TEMPLATE_VIS codecvt;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000866
867// template <> class codecvt<char, char, mbstate_t>
868
869template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000870class _LIBCPP_TYPE_VIS codecvt<char, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000871 : public locale::facet,
872 public codecvt_base
873{
874public:
875 typedef char intern_type;
876 typedef char extern_type;
877 typedef mbstate_t state_type;
878
Louis Dionne16fe2952018-07-11 23:14:33 +0000879 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000880 explicit codecvt(size_t __refs = 0)
881 : locale::facet(__refs) {}
882
Louis Dionne16fe2952018-07-11 23:14:33 +0000883 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000884 result out(state_type& __st,
885 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
886 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
887 {
888 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
889 }
890
Louis Dionne16fe2952018-07-11 23:14:33 +0000891 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000892 result unshift(state_type& __st,
893 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
894 {
895 return do_unshift(__st, __to, __to_end, __to_nxt);
896 }
897
Louis Dionne16fe2952018-07-11 23:14:33 +0000898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000899 result in(state_type& __st,
900 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
901 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
902 {
903 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
904 }
905
Louis Dionne16fe2952018-07-11 23:14:33 +0000906 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000907 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000908 {
909 return do_encoding();
910 }
911
Louis Dionne16fe2952018-07-11 23:14:33 +0000912 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000913 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914 {
915 return do_always_noconv();
916 }
917
Louis Dionne16fe2952018-07-11 23:14:33 +0000918 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000919 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
920 {
921 return do_length(__st, __frm, __end, __mx);
922 }
923
Louis Dionne16fe2952018-07-11 23:14:33 +0000924 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000925 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000926 {
927 return do_max_length();
928 }
929
930 static locale::id id;
931
932protected:
Louis Dionne16fe2952018-07-11 23:14:33 +0000933 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000934 explicit codecvt(const char*, size_t __refs = 0)
935 : locale::facet(__refs) {}
936
937 ~codecvt();
938
939 virtual result do_out(state_type& __st,
940 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
941 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
942 virtual result do_in(state_type& __st,
943 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
944 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
945 virtual result do_unshift(state_type& __st,
946 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000947 virtual int do_encoding() const _NOEXCEPT;
948 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949 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 +0000950 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000951};
952
953// template <> class codecvt<wchar_t, char, mbstate_t>
954
955template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000956class _LIBCPP_TYPE_VIS codecvt<wchar_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000957 : public locale::facet,
958 public codecvt_base
959{
960 locale_t __l;
961public:
962 typedef wchar_t intern_type;
963 typedef char extern_type;
964 typedef mbstate_t state_type;
965
966 explicit codecvt(size_t __refs = 0);
967
Louis Dionne16fe2952018-07-11 23:14:33 +0000968 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000969 result out(state_type& __st,
970 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
971 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
972 {
973 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
974 }
975
Louis Dionne16fe2952018-07-11 23:14:33 +0000976 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000977 result unshift(state_type& __st,
978 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
979 {
980 return do_unshift(__st, __to, __to_end, __to_nxt);
981 }
982
Louis Dionne16fe2952018-07-11 23:14:33 +0000983 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000984 result in(state_type& __st,
985 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
986 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
987 {
988 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
989 }
990
Louis Dionne16fe2952018-07-11 23:14:33 +0000991 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000992 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000993 {
994 return do_encoding();
995 }
996
Louis Dionne16fe2952018-07-11 23:14:33 +0000997 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000998 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000999 {
1000 return do_always_noconv();
1001 }
1002
Louis Dionne16fe2952018-07-11 23:14:33 +00001003 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1005 {
1006 return do_length(__st, __frm, __end, __mx);
1007 }
1008
Louis Dionne16fe2952018-07-11 23:14:33 +00001009 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001010 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001011 {
1012 return do_max_length();
1013 }
1014
1015 static locale::id id;
1016
1017protected:
1018 explicit codecvt(const char*, size_t __refs = 0);
1019
1020 ~codecvt();
1021
1022 virtual result do_out(state_type& __st,
1023 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1024 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1025 virtual result do_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 virtual result do_unshift(state_type& __st,
1029 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001030 virtual int do_encoding() const _NOEXCEPT;
1031 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032 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 +00001033 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001034};
1035
1036// template <> class codecvt<char16_t, char, mbstate_t>
1037
1038template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001039class _LIBCPP_TYPE_VIS codecvt<char16_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040 : public locale::facet,
1041 public codecvt_base
1042{
1043public:
1044 typedef char16_t intern_type;
1045 typedef char extern_type;
1046 typedef mbstate_t state_type;
1047
Louis Dionne16fe2952018-07-11 23:14:33 +00001048 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049 explicit codecvt(size_t __refs = 0)
1050 : locale::facet(__refs) {}
1051
Louis Dionne16fe2952018-07-11 23:14:33 +00001052 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053 result out(state_type& __st,
1054 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1055 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1056 {
1057 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1058 }
1059
Louis Dionne16fe2952018-07-11 23:14:33 +00001060 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001061 result unshift(state_type& __st,
1062 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1063 {
1064 return do_unshift(__st, __to, __to_end, __to_nxt);
1065 }
1066
Louis Dionne16fe2952018-07-11 23:14:33 +00001067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068 result in(state_type& __st,
1069 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1070 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1071 {
1072 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1073 }
1074
Louis Dionne16fe2952018-07-11 23:14:33 +00001075 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001076 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001077 {
1078 return do_encoding();
1079 }
1080
Louis Dionne16fe2952018-07-11 23:14:33 +00001081 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001082 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001083 {
1084 return do_always_noconv();
1085 }
1086
Louis Dionne16fe2952018-07-11 23:14:33 +00001087 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001088 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1089 {
1090 return do_length(__st, __frm, __end, __mx);
1091 }
1092
Louis Dionne16fe2952018-07-11 23:14:33 +00001093 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001094 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095 {
1096 return do_max_length();
1097 }
1098
1099 static locale::id id;
1100
1101protected:
Louis Dionne16fe2952018-07-11 23:14:33 +00001102 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001103 explicit codecvt(const char*, size_t __refs = 0)
1104 : locale::facet(__refs) {}
1105
1106 ~codecvt();
1107
1108 virtual result do_out(state_type& __st,
1109 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1110 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1111 virtual result do_in(state_type& __st,
1112 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1113 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1114 virtual result do_unshift(state_type& __st,
1115 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001116 virtual int do_encoding() const _NOEXCEPT;
1117 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118 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 +00001119 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001120};
1121
1122// template <> class codecvt<char32_t, char, mbstate_t>
1123
1124template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001125class _LIBCPP_TYPE_VIS codecvt<char32_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126 : public locale::facet,
1127 public codecvt_base
1128{
1129public:
1130 typedef char32_t intern_type;
1131 typedef char extern_type;
1132 typedef mbstate_t state_type;
1133
Louis Dionne16fe2952018-07-11 23:14:33 +00001134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001135 explicit codecvt(size_t __refs = 0)
1136 : locale::facet(__refs) {}
1137
Louis Dionne16fe2952018-07-11 23:14:33 +00001138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001139 result out(state_type& __st,
1140 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1141 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1142 {
1143 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1144 }
1145
Louis Dionne16fe2952018-07-11 23:14:33 +00001146 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001147 result unshift(state_type& __st,
1148 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1149 {
1150 return do_unshift(__st, __to, __to_end, __to_nxt);
1151 }
1152
Louis Dionne16fe2952018-07-11 23:14:33 +00001153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154 result in(state_type& __st,
1155 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1156 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1157 {
1158 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1159 }
1160
Louis Dionne16fe2952018-07-11 23:14:33 +00001161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001162 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163 {
1164 return do_encoding();
1165 }
1166
Louis Dionne16fe2952018-07-11 23:14:33 +00001167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001168 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169 {
1170 return do_always_noconv();
1171 }
1172
Louis Dionne16fe2952018-07-11 23:14:33 +00001173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1175 {
1176 return do_length(__st, __frm, __end, __mx);
1177 }
1178
Louis Dionne16fe2952018-07-11 23:14:33 +00001179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001180 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181 {
1182 return do_max_length();
1183 }
1184
1185 static locale::id id;
1186
1187protected:
Louis Dionne16fe2952018-07-11 23:14:33 +00001188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001189 explicit codecvt(const char*, size_t __refs = 0)
1190 : locale::facet(__refs) {}
1191
1192 ~codecvt();
1193
1194 virtual result do_out(state_type& __st,
1195 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1196 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1197 virtual result do_in(state_type& __st,
1198 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1199 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1200 virtual result do_unshift(state_type& __st,
1201 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001202 virtual int do_encoding() const _NOEXCEPT;
1203 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001204 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 +00001205 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206};
1207
Howard Hinnantc51e1022010-05-11 19:42:16 +00001208// template <class _InternT, class _ExternT, class _StateT> class codecvt_byname
1209
1210template <class _InternT, class _ExternT, class _StateT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001211class _LIBCPP_TEMPLATE_VIS codecvt_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212 : public codecvt<_InternT, _ExternT, _StateT>
1213{
1214public:
Louis Dionne16fe2952018-07-11 23:14:33 +00001215 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001216 explicit codecvt_byname(const char* __nm, size_t __refs = 0)
1217 : codecvt<_InternT, _ExternT, _StateT>(__nm, __refs) {}
Louis Dionne16fe2952018-07-11 23:14:33 +00001218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001219 explicit codecvt_byname(const string& __nm, size_t __refs = 0)
1220 : codecvt<_InternT, _ExternT, _StateT>(__nm.c_str(), __refs) {}
1221protected:
1222 ~codecvt_byname();
1223};
1224
1225template <class _InternT, class _ExternT, class _StateT>
1226codecvt_byname<_InternT, _ExternT, _StateT>::~codecvt_byname()
1227{
1228}
1229
Eric Fiselier1b57fa82016-09-15 22:27:07 +00001230_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char, char, mbstate_t>)
1231_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<wchar_t, char, mbstate_t>)
1232_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char16_t, char, mbstate_t>)
1233_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char32_t, char, mbstate_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001234
Howard Hinnantc834c512011-11-29 18:15:50 +00001235template <size_t _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236struct __narrow_to_utf8
1237{
1238 template <class _OutputIterator, class _CharT>
1239 _OutputIterator
1240 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const;
1241};
1242
1243template <>
1244struct __narrow_to_utf8<8>
1245{
1246 template <class _OutputIterator, class _CharT>
Louis Dionne16fe2952018-07-11 23:14:33 +00001247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248 _OutputIterator
1249 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1250 {
1251 for (; __wb < __we; ++__wb, ++__s)
1252 *__s = *__wb;
1253 return __s;
1254 }
1255};
1256
1257template <>
Louis Dionne5254b372018-10-25 12:13:43 +00001258struct _LIBCPP_TEMPLATE_VIS __narrow_to_utf8<16>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001259 : public codecvt<char16_t, char, mbstate_t>
1260{
Louis Dionne16fe2952018-07-11 23:14:33 +00001261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001262 __narrow_to_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1263
Louis Dionne5254b372018-10-25 12:13:43 +00001264 _LIBCPP_EXPORTED_FROM_ABI ~__narrow_to_utf8();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001265
1266 template <class _OutputIterator, class _CharT>
Louis Dionne16fe2952018-07-11 23:14:33 +00001267 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001268 _OutputIterator
1269 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1270 {
1271 result __r = ok;
1272 mbstate_t __mb;
1273 while (__wb < __we && __r != error)
1274 {
1275 const int __sz = 32;
1276 char __buf[__sz];
1277 char* __bn;
1278 const char16_t* __wn = (const char16_t*)__wb;
1279 __r = do_out(__mb, (const char16_t*)__wb, (const char16_t*)__we, __wn,
1280 __buf, __buf+__sz, __bn);
1281 if (__r == codecvt_base::error || __wn == (const char16_t*)__wb)
1282 __throw_runtime_error("locale not supported");
1283 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1284 *__s = *__p;
1285 __wb = (const _CharT*)__wn;
1286 }
1287 return __s;
1288 }
1289};
1290
1291template <>
Louis Dionne5254b372018-10-25 12:13:43 +00001292struct _LIBCPP_TEMPLATE_VIS __narrow_to_utf8<32>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001293 : public codecvt<char32_t, char, mbstate_t>
1294{
Louis Dionne16fe2952018-07-11 23:14:33 +00001295 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001296 __narrow_to_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1297
Louis Dionne5254b372018-10-25 12:13:43 +00001298 _LIBCPP_EXPORTED_FROM_ABI ~__narrow_to_utf8();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001299
1300 template <class _OutputIterator, class _CharT>
Louis Dionne16fe2952018-07-11 23:14:33 +00001301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302 _OutputIterator
1303 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1304 {
1305 result __r = ok;
1306 mbstate_t __mb;
1307 while (__wb < __we && __r != error)
1308 {
1309 const int __sz = 32;
1310 char __buf[__sz];
1311 char* __bn;
1312 const char32_t* __wn = (const char32_t*)__wb;
1313 __r = do_out(__mb, (const char32_t*)__wb, (const char32_t*)__we, __wn,
1314 __buf, __buf+__sz, __bn);
1315 if (__r == codecvt_base::error || __wn == (const char32_t*)__wb)
1316 __throw_runtime_error("locale not supported");
1317 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1318 *__s = *__p;
1319 __wb = (const _CharT*)__wn;
1320 }
1321 return __s;
1322 }
1323};
1324
Howard Hinnantc834c512011-11-29 18:15:50 +00001325template <size_t _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326struct __widen_from_utf8
1327{
1328 template <class _OutputIterator>
1329 _OutputIterator
1330 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const;
1331};
1332
1333template <>
1334struct __widen_from_utf8<8>
1335{
1336 template <class _OutputIterator>
Louis Dionne16fe2952018-07-11 23:14:33 +00001337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338 _OutputIterator
1339 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1340 {
1341 for (; __nb < __ne; ++__nb, ++__s)
1342 *__s = *__nb;
1343 return __s;
1344 }
1345};
1346
1347template <>
Louis Dionne5254b372018-10-25 12:13:43 +00001348struct _LIBCPP_TEMPLATE_VIS __widen_from_utf8<16>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001349 : public codecvt<char16_t, char, mbstate_t>
1350{
Louis Dionne16fe2952018-07-11 23:14:33 +00001351 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001352 __widen_from_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1353
Louis Dionne5254b372018-10-25 12:13:43 +00001354 _LIBCPP_EXPORTED_FROM_ABI ~__widen_from_utf8();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001355
1356 template <class _OutputIterator>
Louis Dionne16fe2952018-07-11 23:14:33 +00001357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358 _OutputIterator
1359 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1360 {
1361 result __r = ok;
1362 mbstate_t __mb;
1363 while (__nb < __ne && __r != error)
1364 {
1365 const int __sz = 32;
1366 char16_t __buf[__sz];
1367 char16_t* __bn;
1368 const char* __nn = __nb;
1369 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1370 __buf, __buf+__sz, __bn);
1371 if (__r == codecvt_base::error || __nn == __nb)
1372 __throw_runtime_error("locale not supported");
1373 for (const char16_t* __p = __buf; __p < __bn; ++__p, ++__s)
1374 *__s = (wchar_t)*__p;
1375 __nb = __nn;
1376 }
1377 return __s;
1378 }
1379};
1380
1381template <>
Louis Dionne5254b372018-10-25 12:13:43 +00001382struct _LIBCPP_TEMPLATE_VIS __widen_from_utf8<32>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001383 : public codecvt<char32_t, char, mbstate_t>
1384{
Louis Dionne16fe2952018-07-11 23:14:33 +00001385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001386 __widen_from_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1387
Louis Dionne5254b372018-10-25 12:13:43 +00001388 _LIBCPP_EXPORTED_FROM_ABI ~__widen_from_utf8();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389
1390 template <class _OutputIterator>
Louis Dionne16fe2952018-07-11 23:14:33 +00001391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001392 _OutputIterator
1393 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1394 {
1395 result __r = ok;
1396 mbstate_t __mb;
1397 while (__nb < __ne && __r != error)
1398 {
1399 const int __sz = 32;
1400 char32_t __buf[__sz];
1401 char32_t* __bn;
1402 const char* __nn = __nb;
1403 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1404 __buf, __buf+__sz, __bn);
1405 if (__r == codecvt_base::error || __nn == __nb)
1406 __throw_runtime_error("locale not supported");
1407 for (const char32_t* __p = __buf; __p < __bn; ++__p, ++__s)
1408 *__s = (wchar_t)*__p;
1409 __nb = __nn;
1410 }
1411 return __s;
1412 }
1413};
1414
1415// template <class charT> class numpunct
1416
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001417template <class _CharT> class _LIBCPP_TEMPLATE_VIS numpunct;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418
1419template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001420class _LIBCPP_TYPE_VIS numpunct<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001421 : public locale::facet
1422{
1423public:
1424 typedef char char_type;
1425 typedef basic_string<char_type> string_type;
1426
1427 explicit numpunct(size_t __refs = 0);
1428
Louis Dionne16fe2952018-07-11 23:14:33 +00001429 _LIBCPP_INLINE_VISIBILITY char_type decimal_point() const {return do_decimal_point();}
1430 _LIBCPP_INLINE_VISIBILITY char_type thousands_sep() const {return do_thousands_sep();}
1431 _LIBCPP_INLINE_VISIBILITY string grouping() const {return do_grouping();}
1432 _LIBCPP_INLINE_VISIBILITY string_type truename() const {return do_truename();}
1433 _LIBCPP_INLINE_VISIBILITY string_type falsename() const {return do_falsename();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001434
1435 static locale::id id;
1436
1437protected:
1438 ~numpunct();
1439 virtual char_type do_decimal_point() const;
1440 virtual char_type do_thousands_sep() const;
1441 virtual string do_grouping() const;
1442 virtual string_type do_truename() const;
1443 virtual string_type do_falsename() const;
1444
1445 char_type __decimal_point_;
1446 char_type __thousands_sep_;
1447 string __grouping_;
1448};
1449
1450template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001451class _LIBCPP_TYPE_VIS numpunct<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001452 : public locale::facet
1453{
1454public:
1455 typedef wchar_t char_type;
1456 typedef basic_string<char_type> string_type;
1457
1458 explicit numpunct(size_t __refs = 0);
1459
Louis Dionne16fe2952018-07-11 23:14:33 +00001460 _LIBCPP_INLINE_VISIBILITY char_type decimal_point() const {return do_decimal_point();}
1461 _LIBCPP_INLINE_VISIBILITY char_type thousands_sep() const {return do_thousands_sep();}
1462 _LIBCPP_INLINE_VISIBILITY string grouping() const {return do_grouping();}
1463 _LIBCPP_INLINE_VISIBILITY string_type truename() const {return do_truename();}
1464 _LIBCPP_INLINE_VISIBILITY string_type falsename() const {return do_falsename();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001465
1466 static locale::id id;
1467
1468protected:
1469 ~numpunct();
1470 virtual char_type do_decimal_point() const;
1471 virtual char_type do_thousands_sep() const;
1472 virtual string do_grouping() const;
1473 virtual string_type do_truename() const;
1474 virtual string_type do_falsename() const;
1475
1476 char_type __decimal_point_;
1477 char_type __thousands_sep_;
1478 string __grouping_;
1479};
1480
1481// template <class charT> class numpunct_byname
1482
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001483template <class _CharT> class _LIBCPP_TEMPLATE_VIS numpunct_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001484
1485template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001486class _LIBCPP_TYPE_VIS numpunct_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001487: public numpunct<char>
1488{
1489public:
1490 typedef char char_type;
1491 typedef basic_string<char_type> string_type;
1492
1493 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1494 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1495
1496protected:
1497 ~numpunct_byname();
1498
1499private:
1500 void __init(const char*);
1501};
1502
1503template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001504class _LIBCPP_TYPE_VIS numpunct_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001505: public numpunct<wchar_t>
1506{
1507public:
1508 typedef wchar_t char_type;
1509 typedef basic_string<char_type> string_type;
1510
1511 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1512 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1513
1514protected:
1515 ~numpunct_byname();
1516
1517private:
1518 void __init(const char*);
1519};
1520
1521_LIBCPP_END_NAMESPACE_STD
1522
1523#endif // _LIBCPP___LOCALE