blob: 4184e7e03489b900ec7d0148ada67c945ed56929 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP___LOCALE
12#define _LIBCPP___LOCALE
13
14#include <__config>
15#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>
Howard Hinnant8ad70912013-09-17 01:34:47 +000022#if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
Howard Hinnantae0f80b2011-09-29 20:33:10 +000023# include <support/win32/locale_win32.h>
Marshall Clow2f96ec62014-03-11 17:18:47 +000024#elif defined(_AIX)
Howard Hinnantea382952013-08-14 18:00:20 +000025# include <support/ibm/xlocale.h>
Marshall Clow3477ec92014-07-10 15:20:28 +000026#elif defined(__ANDROID__)
27// Android gained the locale aware functions in L (API level 21)
28# include <android/api-level.h>
29# if __ANDROID_API__ <= 20
30# include <support/android/locale_bionic.h>
31# endif
Eric Fiselier7274c652014-11-25 21:57:41 +000032#elif defined(__sun__)
Eric Fiselier90adc202015-01-23 22:22:36 +000033# include <xlocale.h>
Eric Fiselier7274c652014-11-25 21:57:41 +000034# include <support/solaris/xlocale.h>
Sergey Dmitrouk9935bd42014-12-12 08:36:16 +000035#elif defined(_NEWLIB_VERSION)
36# include <support/newlib/xlocale.h>
Marshall Clow3477ec92014-07-10 15:20:28 +000037#elif (defined(__GLIBC__) || defined(__APPLE__) || defined(__FreeBSD__) \
Eric Fiselier7274c652014-11-25 21:57:41 +000038 || defined(__EMSCRIPTEN__) || defined(__IBMCPP__))
Howard Hinnantdd0d7022011-09-22 19:10:18 +000039# include <xlocale.h>
Petr Hosekfdb4a872017-04-13 21:29:21 +000040#elif defined(__Fuchsia__)
41# include <support/fuchsia/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
Howard Hinnant8331b762013-03-06 23:30:19 +000052class _LIBCPP_TYPE_VIS locale;
Howard Hinnantc51e1022010-05-11 19:42:16 +000053
Howard Hinnanta54386e2012-09-14 00:39:16 +000054template <class _Facet>
55_LIBCPP_INLINE_VISIBILITY
56bool
57has_facet(const locale&) _NOEXCEPT;
58
59template <class _Facet>
60_LIBCPP_INLINE_VISIBILITY
61const _Facet&
62use_facet(const locale&);
Howard Hinnantc51e1022010-05-11 19:42:16 +000063
Howard Hinnant8331b762013-03-06 23:30:19 +000064class _LIBCPP_TYPE_VIS locale
Howard Hinnantc51e1022010-05-11 19:42:16 +000065{
66public:
67 // types:
Howard Hinnant8331b762013-03-06 23:30:19 +000068 class _LIBCPP_TYPE_VIS facet;
69 class _LIBCPP_TYPE_VIS id;
Howard Hinnantc51e1022010-05-11 19:42:16 +000070
71 typedef int category;
Mehdi Amini228053d2017-05-04 17:08:54 +000072 _LIBCPP_AVAILABILITY_LOCALE_CATEGORY
Howard Hinnantc51e1022010-05-11 19:42:16 +000073 static const category // values assigned here are for exposition only
74 none = 0,
75 collate = LC_COLLATE_MASK,
76 ctype = LC_CTYPE_MASK,
77 monetary = LC_MONETARY_MASK,
78 numeric = LC_NUMERIC_MASK,
79 time = LC_TIME_MASK,
80 messages = LC_MESSAGES_MASK,
81 all = collate | ctype | monetary | numeric | time | messages;
82
83 // construct/copy/destroy:
Howard Hinnant7c9e5732011-05-31 15:34:58 +000084 locale() _NOEXCEPT;
85 locale(const locale&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000086 explicit locale(const char*);
87 explicit locale(const string&);
88 locale(const locale&, const char*, category);
89 locale(const locale&, const string&, category);
Howard Hinnantcf823322010-12-17 14:46:43 +000090 template <class _Facet>
91 _LIBCPP_INLINE_VISIBILITY locale(const locale&, _Facet*);
Howard Hinnantc51e1022010-05-11 19:42:16 +000092 locale(const locale&, const locale&, category);
93
Howard Hinnant7c9e5732011-05-31 15:34:58 +000094 ~locale();
Howard Hinnantc51e1022010-05-11 19:42:16 +000095
Howard Hinnant7c9e5732011-05-31 15:34:58 +000096 const locale& operator=(const locale&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000097
Shoaib Meenai55f3a462017-03-02 03:22:18 +000098 template <class _Facet>
99 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
100 locale combine(const locale&) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000101
102 // locale operations:
103 string name() const;
104 bool operator==(const locale&) const;
105 bool operator!=(const locale& __y) const {return !(*this == __y);}
106 template <class _CharT, class _Traits, class _Allocator>
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000107 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000108 bool operator()(const basic_string<_CharT, _Traits, _Allocator>&,
109 const basic_string<_CharT, _Traits, _Allocator>&) const;
110
111 // global locale objects:
112 static locale global(const locale&);
113 static const locale& classic();
114
115private:
116 class __imp;
117 __imp* __locale_;
118
119 void __install_ctor(const locale&, facet*, long);
120 static locale& __global();
121 bool has_facet(id&) const;
122 const facet* use_facet(id&) const;
123
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000124 template <class _Facet> friend bool has_facet(const locale&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000125 template <class _Facet> friend const _Facet& use_facet(const locale&);
126};
127
Howard Hinnant8331b762013-03-06 23:30:19 +0000128class _LIBCPP_TYPE_VIS locale::facet
Howard Hinnantc51e1022010-05-11 19:42:16 +0000129 : public __shared_count
130{
131protected:
Howard Hinnant9833cad2010-09-21 18:58:51 +0000132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133 explicit facet(size_t __refs = 0)
134 : __shared_count(static_cast<long>(__refs)-1) {}
135
136 virtual ~facet();
137
138// facet(const facet&) = delete; // effectively done in __shared_count
139// void operator=(const facet&) = delete;
140private:
Howard Hinnant719bda32011-05-28 14:41:13 +0000141 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142};
143
Howard Hinnant8331b762013-03-06 23:30:19 +0000144class _LIBCPP_TYPE_VIS locale::id
Howard Hinnantc51e1022010-05-11 19:42:16 +0000145{
146 once_flag __flag_;
147 int32_t __id_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000148
Howard Hinnantc51e1022010-05-11 19:42:16 +0000149 static int32_t __next_id;
150public:
Howard Hinnantac7d9f02012-07-26 16:14:37 +0000151 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR id() :__id_(0) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152private:
153 void __init();
154 void operator=(const id&); // = delete;
155 id(const id&); // = delete;
156public: // only needed for tests
157 long __get();
158
159 friend class locale;
160 friend class locale::__imp;
161};
162
163template <class _Facet>
164inline _LIBCPP_INLINE_VISIBILITY
165locale::locale(const locale& __other, _Facet* __f)
166{
167 __install_ctor(__other, __f, __f ? __f->id.__get() : 0);
168}
169
170template <class _Facet>
171locale
172locale::combine(const locale& __other) const
173{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000174 if (!_VSTD::has_facet<_Facet>(__other))
Marshall Clow8fea1612016-08-25 15:09:01 +0000175 __throw_runtime_error("locale::combine: locale missing facet");
176
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000177 return locale(*this, &const_cast<_Facet&>(_VSTD::use_facet<_Facet>(__other)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000178}
179
180template <class _Facet>
181inline _LIBCPP_INLINE_VISIBILITY
182bool
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000183has_facet(const locale& __l) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000184{
185 return __l.has_facet(_Facet::id);
186}
187
188template <class _Facet>
189inline _LIBCPP_INLINE_VISIBILITY
190const _Facet&
191use_facet(const locale& __l)
192{
193 return static_cast<const _Facet&>(*__l.use_facet(_Facet::id));
194}
195
196// template <class _CharT> class collate;
197
198template <class _CharT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000199class _LIBCPP_TEMPLATE_VIS collate
Howard Hinnantc51e1022010-05-11 19:42:16 +0000200 : public locale::facet
201{
202public:
203 typedef _CharT char_type;
204 typedef basic_string<char_type> string_type;
205
Howard Hinnant9833cad2010-09-21 18:58:51 +0000206 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207 explicit collate(size_t __refs = 0)
208 : locale::facet(__refs) {}
209
Howard Hinnant9833cad2010-09-21 18:58:51 +0000210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000211 int compare(const char_type* __lo1, const char_type* __hi1,
212 const char_type* __lo2, const char_type* __hi2) const
213 {
214 return do_compare(__lo1, __hi1, __lo2, __hi2);
215 }
216
Howard Hinnant9833cad2010-09-21 18:58:51 +0000217 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000218 string_type transform(const char_type* __lo, const char_type* __hi) const
219 {
220 return do_transform(__lo, __hi);
221 }
222
Howard Hinnant9833cad2010-09-21 18:58:51 +0000223 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000224 long hash(const char_type* __lo, const char_type* __hi) const
225 {
226 return do_hash(__lo, __hi);
227 }
228
229 static locale::id id;
230
231protected:
232 ~collate();
233 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
234 const char_type* __lo2, const char_type* __hi2) const;
235 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const
236 {return string_type(__lo, __hi);}
237 virtual long do_hash(const char_type* __lo, const char_type* __hi) const;
238};
239
240template <class _CharT> locale::id collate<_CharT>::id;
241
242template <class _CharT>
243collate<_CharT>::~collate()
244{
245}
246
247template <class _CharT>
248int
249collate<_CharT>::do_compare(const char_type* __lo1, const char_type* __hi1,
250 const char_type* __lo2, const char_type* __hi2) const
251{
252 for (; __lo2 != __hi2; ++__lo1, ++__lo2)
253 {
254 if (__lo1 == __hi1 || *__lo1 < *__lo2)
255 return -1;
256 if (*__lo2 < *__lo1)
257 return 1;
258 }
259 return __lo1 != __hi1;
260}
261
262template <class _CharT>
263long
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000264collate<_CharT>::do_hash(const char_type* __lo, const char_type* __hi) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000265{
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000266 size_t __h = 0;
267 const size_t __sr = __CHAR_BIT__ * sizeof(size_t) - 8;
268 const size_t __mask = size_t(0xF) << (__sr + 4);
269 for(const char_type* __p = __lo; __p != __hi; ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270 {
Howard Hinnant28b24882011-12-01 20:21:04 +0000271 __h = (__h << 4) + static_cast<size_t>(*__p);
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000272 size_t __g = __h & __mask;
273 __h ^= __g | (__g >> __sr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000274 }
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000275 return static_cast<long>(__h);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000276}
277
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000278_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<char>)
279_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000280
281// template <class CharT> class collate_byname;
282
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000283template <class _CharT> class _LIBCPP_TEMPLATE_VIS collate_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000284
285template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000286class _LIBCPP_TYPE_VIS collate_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000287 : public collate<char>
288{
289 locale_t __l;
290public:
291 typedef char char_type;
292 typedef basic_string<char_type> string_type;
293
294 explicit collate_byname(const char* __n, size_t __refs = 0);
295 explicit collate_byname(const string& __n, size_t __refs = 0);
296
297protected:
298 ~collate_byname();
299 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
300 const char_type* __lo2, const char_type* __hi2) const;
301 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
302};
303
304template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000305class _LIBCPP_TYPE_VIS collate_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000306 : public collate<wchar_t>
307{
308 locale_t __l;
309public:
310 typedef wchar_t char_type;
311 typedef basic_string<char_type> string_type;
312
313 explicit collate_byname(const char* __n, size_t __refs = 0);
314 explicit collate_byname(const string& __n, size_t __refs = 0);
315
316protected:
317 ~collate_byname();
318
319 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
320 const char_type* __lo2, const char_type* __hi2) const;
321 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
322};
323
324template <class _CharT, class _Traits, class _Allocator>
325bool
326locale::operator()(const basic_string<_CharT, _Traits, _Allocator>& __x,
327 const basic_string<_CharT, _Traits, _Allocator>& __y) const
328{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000329 return _VSTD::use_facet<_VSTD::collate<_CharT> >(*this).compare(
Howard Hinnantc51e1022010-05-11 19:42:16 +0000330 __x.data(), __x.data() + __x.size(),
331 __y.data(), __y.data() + __y.size()) < 0;
332}
333
334// template <class charT> class ctype
335
Howard Hinnant8331b762013-03-06 23:30:19 +0000336class _LIBCPP_TYPE_VIS ctype_base
Howard Hinnant9833cad2010-09-21 18:58:51 +0000337{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000338public:
Vasileios Kalintiris281b4cf2015-11-09 10:21:04 +0000339#if defined(__GLIBC__)
Alexis Hunt92b0c812011-07-09 00:56:23 +0000340 typedef unsigned short mask;
Howard Hinnant155c2af2010-05-24 17:49:41 +0000341 static const mask space = _ISspace;
342 static const mask print = _ISprint;
343 static const mask cntrl = _IScntrl;
344 static const mask upper = _ISupper;
345 static const mask lower = _ISlower;
346 static const mask alpha = _ISalpha;
347 static const mask digit = _ISdigit;
348 static const mask punct = _ISpunct;
349 static const mask xdigit = _ISxdigit;
350 static const mask blank = _ISblank;
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +0000351#elif defined(_LIBCPP_MSVCRT)
Howard Hinnantd7a78632011-09-29 13:33:15 +0000352 typedef unsigned short mask;
Howard Hinnantdd0d7022011-09-22 19:10:18 +0000353 static const mask space = _SPACE;
354 static const mask print = _BLANK|_PUNCT|_ALPHA|_DIGIT;
355 static const mask cntrl = _CONTROL;
356 static const mask upper = _UPPER;
357 static const mask lower = _LOWER;
358 static const mask alpha = _ALPHA;
359 static const mask digit = _DIGIT;
360 static const mask punct = _PUNCT;
361 static const mask xdigit = _HEX;
362 static const mask blank = _BLANK;
Jonathan Roelofsae9ab252015-03-11 17:00:28 +0000363# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
Dan Albertebdf28b2015-03-11 00:51:06 +0000364#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__)
JF Bastien0c265d82015-02-25 22:16:46 +0000365# ifdef __APPLE__
David Chisnall1d581062011-09-21 08:39:44 +0000366 typedef __uint32_t mask;
JF Bastien0c265d82015-02-25 22:16:46 +0000367# elif defined(__FreeBSD__)
David Chisnall1d581062011-09-21 08:39:44 +0000368 typedef unsigned long mask;
Vasileios Kalintirisc5dd8942015-11-24 10:24:54 +0000369# elif defined(__EMSCRIPTEN__) || defined(__NetBSD__)
Howard Hinnant942dbd22013-03-29 18:27:28 +0000370 typedef unsigned short mask;
JF Bastien0c265d82015-02-25 22:16:46 +0000371# endif
David Chisnall1d581062011-09-21 08:39:44 +0000372 static const mask space = _CTYPE_S;
373 static const mask print = _CTYPE_R;
374 static const mask cntrl = _CTYPE_C;
375 static const mask upper = _CTYPE_U;
376 static const mask lower = _CTYPE_L;
377 static const mask alpha = _CTYPE_A;
378 static const mask digit = _CTYPE_D;
379 static const mask punct = _CTYPE_P;
380 static const mask xdigit = _CTYPE_X;
Dan Albert2dca4072014-07-23 19:32:03 +0000381
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000382# if defined(__NetBSD__)
383 static const mask blank = _CTYPE_BL;
384# else
David Chisnall1d581062011-09-21 08:39:44 +0000385 static const mask blank = _CTYPE_B;
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000386# endif
Howard Hinnanta47505d2013-08-30 14:42:39 +0000387#elif defined(__sun__) || defined(_AIX)
David Chisnall8074c342012-02-29 13:05:08 +0000388 typedef unsigned int mask;
389 static const mask space = _ISSPACE;
390 static const mask print = _ISPRINT;
391 static const mask cntrl = _ISCNTRL;
392 static const mask upper = _ISUPPER;
393 static const mask lower = _ISLOWER;
394 static const mask alpha = _ISALPHA;
395 static const mask digit = _ISDIGIT;
396 static const mask punct = _ISPUNCT;
397 static const mask xdigit = _ISXDIGIT;
398 static const mask blank = _ISBLANK;
JF Bastien0c265d82015-02-25 22:16:46 +0000399#elif defined(_NEWLIB_VERSION)
400 // Same type as Newlib's _ctype_ array in newlib/libc/include/ctype.h.
401 typedef char mask;
402 static const mask space = _S;
403 static const mask print = _P | _U | _L | _N | _B;
404 static const mask cntrl = _C;
405 static const mask upper = _U;
406 static const mask lower = _L;
407 static const mask alpha = _U | _L;
408 static const mask digit = _N;
409 static const mask punct = _P;
410 static const mask xdigit = _X | _N;
411 static const mask blank = _B;
Jonathan Roelofsae9ab252015-03-11 17:00:28 +0000412# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
413# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
414# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_XDIGIT
JF Bastien0c265d82015-02-25 22:16:46 +0000415#else
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000416 typedef unsigned long mask;
417 static const mask space = 1<<0;
418 static const mask print = 1<<1;
419 static const mask cntrl = 1<<2;
420 static const mask upper = 1<<3;
421 static const mask lower = 1<<4;
422 static const mask alpha = 1<<5;
423 static const mask digit = 1<<6;
424 static const mask punct = 1<<7;
425 static const mask xdigit = 1<<8;
426 static const mask blank = 1<<9;
JF Bastien0c265d82015-02-25 22:16:46 +0000427#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000428 static const mask alnum = alpha | digit;
429 static const mask graph = alnum | punct;
430
431 _LIBCPP_ALWAYS_INLINE ctype_base() {}
432};
433
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000434template <class _CharT> class _LIBCPP_TEMPLATE_VIS ctype;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000435
436template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000437class _LIBCPP_TYPE_VIS ctype<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000438 : public locale::facet,
439 public ctype_base
440{
441public:
442 typedef wchar_t char_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000443
Howard Hinnantc51e1022010-05-11 19:42:16 +0000444 _LIBCPP_ALWAYS_INLINE
445 explicit ctype(size_t __refs = 0)
446 : locale::facet(__refs) {}
447
448 _LIBCPP_ALWAYS_INLINE
449 bool is(mask __m, char_type __c) const
450 {
451 return do_is(__m, __c);
452 }
453
454 _LIBCPP_ALWAYS_INLINE
455 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
456 {
457 return do_is(__low, __high, __vec);
458 }
459
460 _LIBCPP_ALWAYS_INLINE
461 const char_type* scan_is(mask __m, const char_type* __low, const char_type* __high) const
462 {
463 return do_scan_is(__m, __low, __high);
464 }
465
466 _LIBCPP_ALWAYS_INLINE
467 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
468 {
469 return do_scan_not(__m, __low, __high);
470 }
471
472 _LIBCPP_ALWAYS_INLINE
473 char_type toupper(char_type __c) const
474 {
475 return do_toupper(__c);
476 }
477
478 _LIBCPP_ALWAYS_INLINE
479 const char_type* toupper(char_type* __low, const char_type* __high) const
480 {
481 return do_toupper(__low, __high);
482 }
483
484 _LIBCPP_ALWAYS_INLINE
485 char_type tolower(char_type __c) const
486 {
487 return do_tolower(__c);
488 }
489
490 _LIBCPP_ALWAYS_INLINE
491 const char_type* tolower(char_type* __low, const char_type* __high) const
492 {
493 return do_tolower(__low, __high);
494 }
495
496 _LIBCPP_ALWAYS_INLINE
497 char_type widen(char __c) const
498 {
499 return do_widen(__c);
500 }
501
502 _LIBCPP_ALWAYS_INLINE
503 const char* widen(const char* __low, const char* __high, char_type* __to) const
504 {
505 return do_widen(__low, __high, __to);
506 }
507
508 _LIBCPP_ALWAYS_INLINE
509 char narrow(char_type __c, char __dfault) const
510 {
511 return do_narrow(__c, __dfault);
512 }
513
514 _LIBCPP_ALWAYS_INLINE
515 const char_type* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
516 {
517 return do_narrow(__low, __high, __dfault, __to);
518 }
519
520 static locale::id id;
521
522protected:
523 ~ctype();
524 virtual bool do_is(mask __m, char_type __c) const;
525 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
526 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
527 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
528 virtual char_type do_toupper(char_type) const;
529 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
530 virtual char_type do_tolower(char_type) const;
531 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
532 virtual char_type do_widen(char) const;
533 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
534 virtual char do_narrow(char_type, char __dfault) const;
535 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
536};
537
538template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000539class _LIBCPP_TYPE_VIS ctype<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000540 : public locale::facet, public ctype_base
541{
542 const mask* __tab_;
543 bool __del_;
544public:
545 typedef char char_type;
546
547 explicit ctype(const mask* __tab = 0, bool __del = false, size_t __refs = 0);
548
549 _LIBCPP_ALWAYS_INLINE
550 bool is(mask __m, char_type __c) const
551 {
Marshall Clow11de4872013-10-21 14:41:05 +0000552 return isascii(__c) ? (__tab_[static_cast<int>(__c)] & __m) !=0 : false;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000553 }
554
555 _LIBCPP_ALWAYS_INLINE
556 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
557 {
558 for (; __low != __high; ++__low, ++__vec)
Howard Hinnant28b24882011-12-01 20:21:04 +0000559 *__vec = isascii(*__low) ? __tab_[static_cast<int>(*__low)] : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000560 return __low;
561 }
562
563 _LIBCPP_ALWAYS_INLINE
564 const char_type* scan_is (mask __m, const char_type* __low, const char_type* __high) const
565 {
566 for (; __low != __high; ++__low)
Howard Hinnant28b24882011-12-01 20:21:04 +0000567 if (isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000568 break;
569 return __low;
570 }
571
572 _LIBCPP_ALWAYS_INLINE
573 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
574 {
575 for (; __low != __high; ++__low)
Howard Hinnant28b24882011-12-01 20:21:04 +0000576 if (!(isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m)))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000577 break;
578 return __low;
579 }
580
581 _LIBCPP_ALWAYS_INLINE
582 char_type toupper(char_type __c) const
583 {
584 return do_toupper(__c);
585 }
586
587 _LIBCPP_ALWAYS_INLINE
588 const char_type* toupper(char_type* __low, const char_type* __high) const
589 {
590 return do_toupper(__low, __high);
591 }
592
593 _LIBCPP_ALWAYS_INLINE
594 char_type tolower(char_type __c) const
595 {
596 return do_tolower(__c);
597 }
598
599 _LIBCPP_ALWAYS_INLINE
600 const char_type* tolower(char_type* __low, const char_type* __high) const
601 {
602 return do_tolower(__low, __high);
603 }
604
605 _LIBCPP_ALWAYS_INLINE
606 char_type widen(char __c) const
607 {
608 return do_widen(__c);
609 }
610
611 _LIBCPP_ALWAYS_INLINE
612 const char* widen(const char* __low, const char* __high, char_type* __to) const
613 {
614 return do_widen(__low, __high, __to);
615 }
616
617 _LIBCPP_ALWAYS_INLINE
618 char narrow(char_type __c, char __dfault) const
619 {
620 return do_narrow(__c, __dfault);
621 }
622
623 _LIBCPP_ALWAYS_INLINE
624 const char* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
625 {
626 return do_narrow(__low, __high, __dfault, __to);
627 }
628
629 static locale::id id;
630
Howard Hinnant155c2af2010-05-24 17:49:41 +0000631#ifdef _CACHED_RUNES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000632 static const size_t table_size = _CACHED_RUNES;
Howard Hinnant155c2af2010-05-24 17:49:41 +0000633#else
634 static const size_t table_size = 256; // FIXME: Don't hardcode this.
635#endif
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000636 _LIBCPP_ALWAYS_INLINE const mask* table() const _NOEXCEPT {return __tab_;}
637 static const mask* classic_table() _NOEXCEPT;
Vasileios Kalintirisc5dd8942015-11-24 10:24:54 +0000638#if defined(__GLIBC__) || defined(__EMSCRIPTEN__)
Alexis Hunt92b0c812011-07-09 00:56:23 +0000639 static const int* __classic_upper_table() _NOEXCEPT;
640 static const int* __classic_lower_table() _NOEXCEPT;
Alexis Hunt5a4dd562011-07-09 01:09:31 +0000641#endif
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000642#if defined(__NetBSD__)
643 static const short* __classic_upper_table() _NOEXCEPT;
644 static const short* __classic_lower_table() _NOEXCEPT;
645#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000646
647protected:
648 ~ctype();
649 virtual char_type do_toupper(char_type __c) const;
650 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
651 virtual char_type do_tolower(char_type __c) const;
652 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
653 virtual char_type do_widen(char __c) const;
654 virtual const char* do_widen(const char* __low, const char* __high, char_type* __to) const;
655 virtual char do_narrow(char_type __c, char __dfault) const;
656 virtual const char* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const;
657};
658
659// template <class CharT> class ctype_byname;
660
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000661template <class _CharT> class _LIBCPP_TEMPLATE_VIS ctype_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000662
663template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000664class _LIBCPP_TYPE_VIS ctype_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000665 : public ctype<char>
666{
667 locale_t __l;
668
669public:
670 explicit ctype_byname(const char*, size_t = 0);
671 explicit ctype_byname(const string&, size_t = 0);
672
673protected:
674 ~ctype_byname();
675 virtual char_type do_toupper(char_type) const;
676 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
677 virtual char_type do_tolower(char_type) const;
678 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
679};
680
681template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000682class _LIBCPP_TYPE_VIS ctype_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000683 : public ctype<wchar_t>
684{
685 locale_t __l;
686
687public:
688 explicit ctype_byname(const char*, size_t = 0);
689 explicit ctype_byname(const string&, size_t = 0);
690
691protected:
692 ~ctype_byname();
693 virtual bool do_is(mask __m, char_type __c) const;
694 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
695 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
696 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
697 virtual char_type do_toupper(char_type) const;
698 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
699 virtual char_type do_tolower(char_type) const;
700 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
701 virtual char_type do_widen(char) const;
702 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
703 virtual char do_narrow(char_type, char __dfault) const;
704 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
705};
706
707template <class _CharT>
708inline _LIBCPP_INLINE_VISIBILITY
709bool
710isspace(_CharT __c, const locale& __loc)
711{
712 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
713}
714
715template <class _CharT>
716inline _LIBCPP_INLINE_VISIBILITY
717bool
718isprint(_CharT __c, const locale& __loc)
719{
720 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c);
721}
722
723template <class _CharT>
724inline _LIBCPP_INLINE_VISIBILITY
725bool
726iscntrl(_CharT __c, const locale& __loc)
727{
728 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c);
729}
730
731template <class _CharT>
732inline _LIBCPP_INLINE_VISIBILITY
733bool
734isupper(_CharT __c, const locale& __loc)
735{
736 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c);
737}
738
739template <class _CharT>
740inline _LIBCPP_INLINE_VISIBILITY
741bool
742islower(_CharT __c, const locale& __loc)
743{
744 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c);
745}
746
747template <class _CharT>
748inline _LIBCPP_INLINE_VISIBILITY
749bool
750isalpha(_CharT __c, const locale& __loc)
751{
752 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c);
753}
754
755template <class _CharT>
756inline _LIBCPP_INLINE_VISIBILITY
757bool
758isdigit(_CharT __c, const locale& __loc)
759{
760 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c);
761}
762
763template <class _CharT>
764inline _LIBCPP_INLINE_VISIBILITY
765bool
766ispunct(_CharT __c, const locale& __loc)
767{
768 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c);
769}
770
771template <class _CharT>
772inline _LIBCPP_INLINE_VISIBILITY
773bool
774isxdigit(_CharT __c, const locale& __loc)
775{
776 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c);
777}
778
779template <class _CharT>
780inline _LIBCPP_INLINE_VISIBILITY
781bool
782isalnum(_CharT __c, const locale& __loc)
783{
784 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c);
785}
786
787template <class _CharT>
788inline _LIBCPP_INLINE_VISIBILITY
789bool
790isgraph(_CharT __c, const locale& __loc)
791{
792 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c);
793}
794
795template <class _CharT>
796inline _LIBCPP_INLINE_VISIBILITY
797_CharT
798toupper(_CharT __c, const locale& __loc)
799{
800 return use_facet<ctype<_CharT> >(__loc).toupper(__c);
801}
802
803template <class _CharT>
804inline _LIBCPP_INLINE_VISIBILITY
805_CharT
806tolower(_CharT __c, const locale& __loc)
807{
808 return use_facet<ctype<_CharT> >(__loc).tolower(__c);
809}
810
811// codecvt_base
812
Howard Hinnant8331b762013-03-06 23:30:19 +0000813class _LIBCPP_TYPE_VIS codecvt_base
Howard Hinnantc51e1022010-05-11 19:42:16 +0000814{
815public:
816 _LIBCPP_ALWAYS_INLINE codecvt_base() {}
817 enum result {ok, partial, error, noconv};
818};
819
820// template <class internT, class externT, class stateT> class codecvt;
821
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000822template <class _InternT, class _ExternT, class _StateT> class _LIBCPP_TEMPLATE_VIS codecvt;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000823
824// template <> class codecvt<char, char, mbstate_t>
825
826template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000827class _LIBCPP_TYPE_VIS codecvt<char, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000828 : public locale::facet,
829 public codecvt_base
830{
831public:
832 typedef char intern_type;
833 typedef char extern_type;
834 typedef mbstate_t state_type;
835
836 _LIBCPP_ALWAYS_INLINE
837 explicit codecvt(size_t __refs = 0)
838 : locale::facet(__refs) {}
839
840 _LIBCPP_ALWAYS_INLINE
841 result out(state_type& __st,
842 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
843 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
844 {
845 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
846 }
847
848 _LIBCPP_ALWAYS_INLINE
849 result unshift(state_type& __st,
850 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
851 {
852 return do_unshift(__st, __to, __to_end, __to_nxt);
853 }
854
855 _LIBCPP_ALWAYS_INLINE
856 result in(state_type& __st,
857 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
858 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
859 {
860 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
861 }
862
863 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000864 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000865 {
866 return do_encoding();
867 }
868
869 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000870 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000871 {
872 return do_always_noconv();
873 }
874
875 _LIBCPP_ALWAYS_INLINE
876 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
877 {
878 return do_length(__st, __frm, __end, __mx);
879 }
880
881 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000882 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000883 {
884 return do_max_length();
885 }
886
887 static locale::id id;
888
889protected:
890 _LIBCPP_ALWAYS_INLINE
891 explicit codecvt(const char*, size_t __refs = 0)
892 : locale::facet(__refs) {}
893
894 ~codecvt();
895
896 virtual result do_out(state_type& __st,
897 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
898 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
899 virtual result do_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 virtual result do_unshift(state_type& __st,
903 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000904 virtual int do_encoding() const _NOEXCEPT;
905 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000906 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 +0000907 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000908};
909
910// template <> class codecvt<wchar_t, char, mbstate_t>
911
912template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000913class _LIBCPP_TYPE_VIS codecvt<wchar_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914 : public locale::facet,
915 public codecvt_base
916{
917 locale_t __l;
918public:
919 typedef wchar_t intern_type;
920 typedef char extern_type;
921 typedef mbstate_t state_type;
922
923 explicit codecvt(size_t __refs = 0);
924
925 _LIBCPP_ALWAYS_INLINE
926 result out(state_type& __st,
927 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
928 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
929 {
930 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
931 }
932
933 _LIBCPP_ALWAYS_INLINE
934 result unshift(state_type& __st,
935 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
936 {
937 return do_unshift(__st, __to, __to_end, __to_nxt);
938 }
939
940 _LIBCPP_ALWAYS_INLINE
941 result in(state_type& __st,
942 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
943 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
944 {
945 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
946 }
947
948 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000949 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950 {
951 return do_encoding();
952 }
953
954 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000955 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956 {
957 return do_always_noconv();
958 }
959
960 _LIBCPP_ALWAYS_INLINE
961 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
962 {
963 return do_length(__st, __frm, __end, __mx);
964 }
965
966 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000967 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968 {
969 return do_max_length();
970 }
971
972 static locale::id id;
973
974protected:
975 explicit codecvt(const char*, size_t __refs = 0);
976
977 ~codecvt();
978
979 virtual result do_out(state_type& __st,
980 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
981 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
982 virtual result do_in(state_type& __st,
983 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
984 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
985 virtual result do_unshift(state_type& __st,
986 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000987 virtual int do_encoding() const _NOEXCEPT;
988 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000989 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 +0000990 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991};
992
993// template <> class codecvt<char16_t, char, mbstate_t>
994
995template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000996class _LIBCPP_TYPE_VIS codecvt<char16_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997 : public locale::facet,
998 public codecvt_base
999{
1000public:
1001 typedef char16_t intern_type;
1002 typedef char extern_type;
1003 typedef mbstate_t state_type;
1004
1005 _LIBCPP_ALWAYS_INLINE
1006 explicit codecvt(size_t __refs = 0)
1007 : locale::facet(__refs) {}
1008
1009 _LIBCPP_ALWAYS_INLINE
1010 result out(state_type& __st,
1011 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1012 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1013 {
1014 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1015 }
1016
1017 _LIBCPP_ALWAYS_INLINE
1018 result unshift(state_type& __st,
1019 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1020 {
1021 return do_unshift(__st, __to, __to_end, __to_nxt);
1022 }
1023
1024 _LIBCPP_ALWAYS_INLINE
1025 result in(state_type& __st,
1026 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1027 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1028 {
1029 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1030 }
1031
1032 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001033 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001034 {
1035 return do_encoding();
1036 }
1037
1038 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001039 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040 {
1041 return do_always_noconv();
1042 }
1043
1044 _LIBCPP_ALWAYS_INLINE
1045 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1046 {
1047 return do_length(__st, __frm, __end, __mx);
1048 }
1049
1050 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001051 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001052 {
1053 return do_max_length();
1054 }
1055
1056 static locale::id id;
1057
1058protected:
1059 _LIBCPP_ALWAYS_INLINE
1060 explicit codecvt(const char*, size_t __refs = 0)
1061 : locale::facet(__refs) {}
1062
1063 ~codecvt();
1064
1065 virtual result do_out(state_type& __st,
1066 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1067 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1068 virtual result do_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 virtual result do_unshift(state_type& __st,
1072 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001073 virtual int do_encoding() const _NOEXCEPT;
1074 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075 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 +00001076 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001077};
1078
1079// template <> class codecvt<char32_t, char, mbstate_t>
1080
1081template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001082class _LIBCPP_TYPE_VIS codecvt<char32_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001083 : public locale::facet,
1084 public codecvt_base
1085{
1086public:
1087 typedef char32_t intern_type;
1088 typedef char extern_type;
1089 typedef mbstate_t state_type;
1090
1091 _LIBCPP_ALWAYS_INLINE
1092 explicit codecvt(size_t __refs = 0)
1093 : locale::facet(__refs) {}
1094
1095 _LIBCPP_ALWAYS_INLINE
1096 result out(state_type& __st,
1097 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1098 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1099 {
1100 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1101 }
1102
1103 _LIBCPP_ALWAYS_INLINE
1104 result unshift(state_type& __st,
1105 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1106 {
1107 return do_unshift(__st, __to, __to_end, __to_nxt);
1108 }
1109
1110 _LIBCPP_ALWAYS_INLINE
1111 result 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 {
1115 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1116 }
1117
1118 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001119 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001120 {
1121 return do_encoding();
1122 }
1123
1124 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001125 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126 {
1127 return do_always_noconv();
1128 }
1129
1130 _LIBCPP_ALWAYS_INLINE
1131 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1132 {
1133 return do_length(__st, __frm, __end, __mx);
1134 }
1135
1136 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001137 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001138 {
1139 return do_max_length();
1140 }
1141
1142 static locale::id id;
1143
1144protected:
1145 _LIBCPP_ALWAYS_INLINE
1146 explicit codecvt(const char*, size_t __refs = 0)
1147 : locale::facet(__refs) {}
1148
1149 ~codecvt();
1150
1151 virtual result do_out(state_type& __st,
1152 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1153 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1154 virtual result do_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 virtual result do_unshift(state_type& __st,
1158 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001159 virtual int do_encoding() const _NOEXCEPT;
1160 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161 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 +00001162 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163};
1164
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165// template <class _InternT, class _ExternT, class _StateT> class codecvt_byname
1166
1167template <class _InternT, class _ExternT, class _StateT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001168class _LIBCPP_TEMPLATE_VIS codecvt_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169 : public codecvt<_InternT, _ExternT, _StateT>
1170{
1171public:
Howard Hinnant9833cad2010-09-21 18:58:51 +00001172 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173 explicit codecvt_byname(const char* __nm, size_t __refs = 0)
1174 : codecvt<_InternT, _ExternT, _StateT>(__nm, __refs) {}
Howard Hinnant9833cad2010-09-21 18:58:51 +00001175 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176 explicit codecvt_byname(const string& __nm, size_t __refs = 0)
1177 : codecvt<_InternT, _ExternT, _StateT>(__nm.c_str(), __refs) {}
1178protected:
1179 ~codecvt_byname();
1180};
1181
1182template <class _InternT, class _ExternT, class _StateT>
1183codecvt_byname<_InternT, _ExternT, _StateT>::~codecvt_byname()
1184{
1185}
1186
Eric Fiselier1b57fa82016-09-15 22:27:07 +00001187_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char, char, mbstate_t>)
1188_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<wchar_t, char, mbstate_t>)
1189_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char16_t, char, mbstate_t>)
1190_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char32_t, char, mbstate_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191
Aditya Kumare0f6a142016-08-27 02:26:42 +00001192_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_runtime_error(const char*);
1193
Howard Hinnantc834c512011-11-29 18:15:50 +00001194template <size_t _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195struct __narrow_to_utf8
1196{
1197 template <class _OutputIterator, class _CharT>
1198 _OutputIterator
1199 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const;
1200};
1201
1202template <>
1203struct __narrow_to_utf8<8>
1204{
1205 template <class _OutputIterator, class _CharT>
1206 _LIBCPP_ALWAYS_INLINE
1207 _OutputIterator
1208 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1209 {
1210 for (; __wb < __we; ++__wb, ++__s)
1211 *__s = *__wb;
1212 return __s;
1213 }
1214};
1215
1216template <>
1217struct __narrow_to_utf8<16>
1218 : public codecvt<char16_t, char, mbstate_t>
1219{
1220 _LIBCPP_ALWAYS_INLINE
1221 __narrow_to_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1222
1223 ~__narrow_to_utf8();
1224
1225 template <class _OutputIterator, class _CharT>
1226 _LIBCPP_ALWAYS_INLINE
1227 _OutputIterator
1228 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1229 {
1230 result __r = ok;
1231 mbstate_t __mb;
1232 while (__wb < __we && __r != error)
1233 {
1234 const int __sz = 32;
1235 char __buf[__sz];
1236 char* __bn;
1237 const char16_t* __wn = (const char16_t*)__wb;
1238 __r = do_out(__mb, (const char16_t*)__wb, (const char16_t*)__we, __wn,
1239 __buf, __buf+__sz, __bn);
1240 if (__r == codecvt_base::error || __wn == (const char16_t*)__wb)
1241 __throw_runtime_error("locale not supported");
1242 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1243 *__s = *__p;
1244 __wb = (const _CharT*)__wn;
1245 }
1246 return __s;
1247 }
1248};
1249
1250template <>
1251struct __narrow_to_utf8<32>
1252 : public codecvt<char32_t, char, mbstate_t>
1253{
1254 _LIBCPP_ALWAYS_INLINE
1255 __narrow_to_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1256
1257 ~__narrow_to_utf8();
1258
1259 template <class _OutputIterator, class _CharT>
1260 _LIBCPP_ALWAYS_INLINE
1261 _OutputIterator
1262 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1263 {
1264 result __r = ok;
1265 mbstate_t __mb;
1266 while (__wb < __we && __r != error)
1267 {
1268 const int __sz = 32;
1269 char __buf[__sz];
1270 char* __bn;
1271 const char32_t* __wn = (const char32_t*)__wb;
1272 __r = do_out(__mb, (const char32_t*)__wb, (const char32_t*)__we, __wn,
1273 __buf, __buf+__sz, __bn);
1274 if (__r == codecvt_base::error || __wn == (const char32_t*)__wb)
1275 __throw_runtime_error("locale not supported");
1276 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1277 *__s = *__p;
1278 __wb = (const _CharT*)__wn;
1279 }
1280 return __s;
1281 }
1282};
1283
Howard Hinnantc834c512011-11-29 18:15:50 +00001284template <size_t _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285struct __widen_from_utf8
1286{
1287 template <class _OutputIterator>
1288 _OutputIterator
1289 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const;
1290};
1291
1292template <>
1293struct __widen_from_utf8<8>
1294{
1295 template <class _OutputIterator>
1296 _LIBCPP_ALWAYS_INLINE
1297 _OutputIterator
1298 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1299 {
1300 for (; __nb < __ne; ++__nb, ++__s)
1301 *__s = *__nb;
1302 return __s;
1303 }
1304};
1305
1306template <>
1307struct __widen_from_utf8<16>
1308 : public codecvt<char16_t, char, mbstate_t>
1309{
1310 _LIBCPP_ALWAYS_INLINE
1311 __widen_from_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1312
1313 ~__widen_from_utf8();
1314
1315 template <class _OutputIterator>
1316 _LIBCPP_ALWAYS_INLINE
1317 _OutputIterator
1318 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1319 {
1320 result __r = ok;
1321 mbstate_t __mb;
1322 while (__nb < __ne && __r != error)
1323 {
1324 const int __sz = 32;
1325 char16_t __buf[__sz];
1326 char16_t* __bn;
1327 const char* __nn = __nb;
1328 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1329 __buf, __buf+__sz, __bn);
1330 if (__r == codecvt_base::error || __nn == __nb)
1331 __throw_runtime_error("locale not supported");
1332 for (const char16_t* __p = __buf; __p < __bn; ++__p, ++__s)
1333 *__s = (wchar_t)*__p;
1334 __nb = __nn;
1335 }
1336 return __s;
1337 }
1338};
1339
1340template <>
1341struct __widen_from_utf8<32>
1342 : public codecvt<char32_t, char, mbstate_t>
1343{
1344 _LIBCPP_ALWAYS_INLINE
1345 __widen_from_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1346
1347 ~__widen_from_utf8();
1348
1349 template <class _OutputIterator>
1350 _LIBCPP_ALWAYS_INLINE
1351 _OutputIterator
1352 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1353 {
1354 result __r = ok;
1355 mbstate_t __mb;
1356 while (__nb < __ne && __r != error)
1357 {
1358 const int __sz = 32;
1359 char32_t __buf[__sz];
1360 char32_t* __bn;
1361 const char* __nn = __nb;
1362 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1363 __buf, __buf+__sz, __bn);
1364 if (__r == codecvt_base::error || __nn == __nb)
1365 __throw_runtime_error("locale not supported");
1366 for (const char32_t* __p = __buf; __p < __bn; ++__p, ++__s)
1367 *__s = (wchar_t)*__p;
1368 __nb = __nn;
1369 }
1370 return __s;
1371 }
1372};
1373
1374// template <class charT> class numpunct
1375
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001376template <class _CharT> class _LIBCPP_TEMPLATE_VIS numpunct;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377
1378template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001379class _LIBCPP_TYPE_VIS numpunct<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001380 : public locale::facet
1381{
1382public:
1383 typedef char char_type;
1384 typedef basic_string<char_type> string_type;
1385
1386 explicit numpunct(size_t __refs = 0);
1387
1388 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
1389 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
1390 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
1391 _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();}
1392 _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();}
1393
1394 static locale::id id;
1395
1396protected:
1397 ~numpunct();
1398 virtual char_type do_decimal_point() const;
1399 virtual char_type do_thousands_sep() const;
1400 virtual string do_grouping() const;
1401 virtual string_type do_truename() const;
1402 virtual string_type do_falsename() const;
1403
1404 char_type __decimal_point_;
1405 char_type __thousands_sep_;
1406 string __grouping_;
1407};
1408
1409template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001410class _LIBCPP_TYPE_VIS numpunct<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001411 : public locale::facet
1412{
1413public:
1414 typedef wchar_t char_type;
1415 typedef basic_string<char_type> string_type;
1416
1417 explicit numpunct(size_t __refs = 0);
1418
1419 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
1420 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
1421 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
1422 _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();}
1423 _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();}
1424
1425 static locale::id id;
1426
1427protected:
1428 ~numpunct();
1429 virtual char_type do_decimal_point() const;
1430 virtual char_type do_thousands_sep() const;
1431 virtual string do_grouping() const;
1432 virtual string_type do_truename() const;
1433 virtual string_type do_falsename() const;
1434
1435 char_type __decimal_point_;
1436 char_type __thousands_sep_;
1437 string __grouping_;
1438};
1439
1440// template <class charT> class numpunct_byname
1441
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001442template <class _CharT> class _LIBCPP_TEMPLATE_VIS numpunct_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443
1444template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001445class _LIBCPP_TYPE_VIS numpunct_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001446: public numpunct<char>
1447{
1448public:
1449 typedef char char_type;
1450 typedef basic_string<char_type> string_type;
1451
1452 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1453 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1454
1455protected:
1456 ~numpunct_byname();
1457
1458private:
1459 void __init(const char*);
1460};
1461
1462template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001463class _LIBCPP_TYPE_VIS numpunct_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001464: public numpunct<wchar_t>
1465{
1466public:
1467 typedef wchar_t char_type;
1468 typedef basic_string<char_type> string_type;
1469
1470 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1471 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1472
1473protected:
1474 ~numpunct_byname();
1475
1476private:
1477 void __init(const char*);
1478};
1479
1480_LIBCPP_END_NAMESPACE_STD
1481
1482#endif // _LIBCPP___LOCALE