blob: a878e259b800c23bd01dbdce57df433e927811b7 [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>
Vasileios Kalintiris281b4cf2015-11-09 10:21:04 +000040#elif defined(_LIBCPP_HAS_MUSL_LIBC)
41# include <support/musl/xlocale.h>
Marshall Clow3477ec92014-07-10 15:20:28 +000042#endif // __GLIBC__ || __APPLE__ || __FreeBSD__ || __sun__ || __EMSCRIPTEN__ || __IBMCPP__
Howard Hinnantc51e1022010-05-11 19:42:16 +000043
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000044#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000045#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000046#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000047
48_LIBCPP_BEGIN_NAMESPACE_STD
49
Howard Hinnant8331b762013-03-06 23:30:19 +000050class _LIBCPP_TYPE_VIS locale;
Howard Hinnantc51e1022010-05-11 19:42:16 +000051
Howard Hinnanta54386e2012-09-14 00:39:16 +000052template <class _Facet>
53_LIBCPP_INLINE_VISIBILITY
54bool
55has_facet(const locale&) _NOEXCEPT;
56
57template <class _Facet>
58_LIBCPP_INLINE_VISIBILITY
59const _Facet&
60use_facet(const locale&);
Howard Hinnantc51e1022010-05-11 19:42:16 +000061
Howard Hinnant8331b762013-03-06 23:30:19 +000062class _LIBCPP_TYPE_VIS locale
Howard Hinnantc51e1022010-05-11 19:42:16 +000063{
64public:
65 // types:
Howard Hinnant8331b762013-03-06 23:30:19 +000066 class _LIBCPP_TYPE_VIS facet;
67 class _LIBCPP_TYPE_VIS id;
Howard Hinnantc51e1022010-05-11 19:42:16 +000068
69 typedef int category;
70 static const category // values assigned here are for exposition only
71 none = 0,
72 collate = LC_COLLATE_MASK,
73 ctype = LC_CTYPE_MASK,
74 monetary = LC_MONETARY_MASK,
75 numeric = LC_NUMERIC_MASK,
76 time = LC_TIME_MASK,
77 messages = LC_MESSAGES_MASK,
78 all = collate | ctype | monetary | numeric | time | messages;
79
80 // construct/copy/destroy:
Howard Hinnant7c9e5732011-05-31 15:34:58 +000081 locale() _NOEXCEPT;
82 locale(const locale&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000083 explicit locale(const char*);
84 explicit locale(const string&);
85 locale(const locale&, const char*, category);
86 locale(const locale&, const string&, category);
Howard Hinnantcf823322010-12-17 14:46:43 +000087 template <class _Facet>
88 _LIBCPP_INLINE_VISIBILITY locale(const locale&, _Facet*);
Howard Hinnantc51e1022010-05-11 19:42:16 +000089 locale(const locale&, const locale&, category);
90
Howard Hinnant7c9e5732011-05-31 15:34:58 +000091 ~locale();
Howard Hinnantc51e1022010-05-11 19:42:16 +000092
Howard Hinnant7c9e5732011-05-31 15:34:58 +000093 const locale& operator=(const locale&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000094
Shoaib Meenai55f3a462017-03-02 03:22:18 +000095 template <class _Facet>
96 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
97 locale combine(const locale&) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +000098
99 // locale operations:
100 string name() const;
101 bool operator==(const locale&) const;
102 bool operator!=(const locale& __y) const {return !(*this == __y);}
103 template <class _CharT, class _Traits, class _Allocator>
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000104 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000105 bool operator()(const basic_string<_CharT, _Traits, _Allocator>&,
106 const basic_string<_CharT, _Traits, _Allocator>&) const;
107
108 // global locale objects:
109 static locale global(const locale&);
110 static const locale& classic();
111
112private:
113 class __imp;
114 __imp* __locale_;
115
116 void __install_ctor(const locale&, facet*, long);
117 static locale& __global();
118 bool has_facet(id&) const;
119 const facet* use_facet(id&) const;
120
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000121 template <class _Facet> friend bool has_facet(const locale&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000122 template <class _Facet> friend const _Facet& use_facet(const locale&);
123};
124
Howard Hinnant8331b762013-03-06 23:30:19 +0000125class _LIBCPP_TYPE_VIS locale::facet
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126 : public __shared_count
127{
128protected:
Howard Hinnant9833cad2010-09-21 18:58:51 +0000129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000130 explicit facet(size_t __refs = 0)
131 : __shared_count(static_cast<long>(__refs)-1) {}
132
133 virtual ~facet();
134
135// facet(const facet&) = delete; // effectively done in __shared_count
136// void operator=(const facet&) = delete;
137private:
Howard Hinnant719bda32011-05-28 14:41:13 +0000138 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000139};
140
Howard Hinnant8331b762013-03-06 23:30:19 +0000141class _LIBCPP_TYPE_VIS locale::id
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142{
143 once_flag __flag_;
144 int32_t __id_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000145
Howard Hinnantc51e1022010-05-11 19:42:16 +0000146 static int32_t __next_id;
147public:
Howard Hinnantac7d9f02012-07-26 16:14:37 +0000148 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR id() :__id_(0) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000149private:
150 void __init();
151 void operator=(const id&); // = delete;
152 id(const id&); // = delete;
153public: // only needed for tests
154 long __get();
155
156 friend class locale;
157 friend class locale::__imp;
158};
159
160template <class _Facet>
161inline _LIBCPP_INLINE_VISIBILITY
162locale::locale(const locale& __other, _Facet* __f)
163{
164 __install_ctor(__other, __f, __f ? __f->id.__get() : 0);
165}
166
167template <class _Facet>
168locale
169locale::combine(const locale& __other) const
170{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000171 if (!_VSTD::has_facet<_Facet>(__other))
Marshall Clow8fea1612016-08-25 15:09:01 +0000172 __throw_runtime_error("locale::combine: locale missing facet");
173
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000174 return locale(*this, &const_cast<_Facet&>(_VSTD::use_facet<_Facet>(__other)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000175}
176
177template <class _Facet>
178inline _LIBCPP_INLINE_VISIBILITY
179bool
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000180has_facet(const locale& __l) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000181{
182 return __l.has_facet(_Facet::id);
183}
184
185template <class _Facet>
186inline _LIBCPP_INLINE_VISIBILITY
187const _Facet&
188use_facet(const locale& __l)
189{
190 return static_cast<const _Facet&>(*__l.use_facet(_Facet::id));
191}
192
193// template <class _CharT> class collate;
194
195template <class _CharT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000196class _LIBCPP_TEMPLATE_VIS collate
Howard Hinnantc51e1022010-05-11 19:42:16 +0000197 : public locale::facet
198{
199public:
200 typedef _CharT char_type;
201 typedef basic_string<char_type> string_type;
202
Howard Hinnant9833cad2010-09-21 18:58:51 +0000203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000204 explicit collate(size_t __refs = 0)
205 : locale::facet(__refs) {}
206
Howard Hinnant9833cad2010-09-21 18:58:51 +0000207 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000208 int compare(const char_type* __lo1, const char_type* __hi1,
209 const char_type* __lo2, const char_type* __hi2) const
210 {
211 return do_compare(__lo1, __hi1, __lo2, __hi2);
212 }
213
Howard Hinnant9833cad2010-09-21 18:58:51 +0000214 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215 string_type transform(const char_type* __lo, const char_type* __hi) const
216 {
217 return do_transform(__lo, __hi);
218 }
219
Howard Hinnant9833cad2010-09-21 18:58:51 +0000220 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000221 long hash(const char_type* __lo, const char_type* __hi) const
222 {
223 return do_hash(__lo, __hi);
224 }
225
226 static locale::id id;
227
228protected:
229 ~collate();
230 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
231 const char_type* __lo2, const char_type* __hi2) const;
232 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const
233 {return string_type(__lo, __hi);}
234 virtual long do_hash(const char_type* __lo, const char_type* __hi) const;
235};
236
237template <class _CharT> locale::id collate<_CharT>::id;
238
239template <class _CharT>
240collate<_CharT>::~collate()
241{
242}
243
244template <class _CharT>
245int
246collate<_CharT>::do_compare(const char_type* __lo1, const char_type* __hi1,
247 const char_type* __lo2, const char_type* __hi2) const
248{
249 for (; __lo2 != __hi2; ++__lo1, ++__lo2)
250 {
251 if (__lo1 == __hi1 || *__lo1 < *__lo2)
252 return -1;
253 if (*__lo2 < *__lo1)
254 return 1;
255 }
256 return __lo1 != __hi1;
257}
258
259template <class _CharT>
260long
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000261collate<_CharT>::do_hash(const char_type* __lo, const char_type* __hi) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000262{
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000263 size_t __h = 0;
264 const size_t __sr = __CHAR_BIT__ * sizeof(size_t) - 8;
265 const size_t __mask = size_t(0xF) << (__sr + 4);
266 for(const char_type* __p = __lo; __p != __hi; ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000267 {
Howard Hinnant28b24882011-12-01 20:21:04 +0000268 __h = (__h << 4) + static_cast<size_t>(*__p);
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000269 size_t __g = __h & __mask;
270 __h ^= __g | (__g >> __sr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000271 }
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000272 return static_cast<long>(__h);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000273}
274
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000275_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<char>)
276_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277
278// template <class CharT> class collate_byname;
279
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000280template <class _CharT> class _LIBCPP_TEMPLATE_VIS collate_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000281
282template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000283class _LIBCPP_TYPE_VIS collate_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000284 : public collate<char>
285{
286 locale_t __l;
287public:
288 typedef char char_type;
289 typedef basic_string<char_type> string_type;
290
291 explicit collate_byname(const char* __n, size_t __refs = 0);
292 explicit collate_byname(const string& __n, size_t __refs = 0);
293
294protected:
295 ~collate_byname();
296 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
297 const char_type* __lo2, const char_type* __hi2) const;
298 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
299};
300
301template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000302class _LIBCPP_TYPE_VIS collate_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000303 : public collate<wchar_t>
304{
305 locale_t __l;
306public:
307 typedef wchar_t char_type;
308 typedef basic_string<char_type> string_type;
309
310 explicit collate_byname(const char* __n, size_t __refs = 0);
311 explicit collate_byname(const string& __n, size_t __refs = 0);
312
313protected:
314 ~collate_byname();
315
316 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
317 const char_type* __lo2, const char_type* __hi2) const;
318 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
319};
320
321template <class _CharT, class _Traits, class _Allocator>
322bool
323locale::operator()(const basic_string<_CharT, _Traits, _Allocator>& __x,
324 const basic_string<_CharT, _Traits, _Allocator>& __y) const
325{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000326 return _VSTD::use_facet<_VSTD::collate<_CharT> >(*this).compare(
Howard Hinnantc51e1022010-05-11 19:42:16 +0000327 __x.data(), __x.data() + __x.size(),
328 __y.data(), __y.data() + __y.size()) < 0;
329}
330
331// template <class charT> class ctype
332
Howard Hinnant8331b762013-03-06 23:30:19 +0000333class _LIBCPP_TYPE_VIS ctype_base
Howard Hinnant9833cad2010-09-21 18:58:51 +0000334{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000335public:
Vasileios Kalintiris281b4cf2015-11-09 10:21:04 +0000336#if defined(__GLIBC__)
Alexis Hunt92b0c812011-07-09 00:56:23 +0000337 typedef unsigned short mask;
Howard Hinnant155c2af2010-05-24 17:49:41 +0000338 static const mask space = _ISspace;
339 static const mask print = _ISprint;
340 static const mask cntrl = _IScntrl;
341 static const mask upper = _ISupper;
342 static const mask lower = _ISlower;
343 static const mask alpha = _ISalpha;
344 static const mask digit = _ISdigit;
345 static const mask punct = _ISpunct;
346 static const mask xdigit = _ISxdigit;
347 static const mask blank = _ISblank;
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +0000348#elif defined(_LIBCPP_MSVCRT)
Howard Hinnantd7a78632011-09-29 13:33:15 +0000349 typedef unsigned short mask;
Howard Hinnantdd0d7022011-09-22 19:10:18 +0000350 static const mask space = _SPACE;
351 static const mask print = _BLANK|_PUNCT|_ALPHA|_DIGIT;
352 static const mask cntrl = _CONTROL;
353 static const mask upper = _UPPER;
354 static const mask lower = _LOWER;
355 static const mask alpha = _ALPHA;
356 static const mask digit = _DIGIT;
357 static const mask punct = _PUNCT;
358 static const mask xdigit = _HEX;
359 static const mask blank = _BLANK;
Jonathan Roelofsae9ab252015-03-11 17:00:28 +0000360# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
Dan Albertebdf28b2015-03-11 00:51:06 +0000361#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__)
JF Bastien0c265d82015-02-25 22:16:46 +0000362# ifdef __APPLE__
David Chisnall1d581062011-09-21 08:39:44 +0000363 typedef __uint32_t mask;
JF Bastien0c265d82015-02-25 22:16:46 +0000364# elif defined(__FreeBSD__)
David Chisnall1d581062011-09-21 08:39:44 +0000365 typedef unsigned long mask;
Vasileios Kalintirisc5dd8942015-11-24 10:24:54 +0000366# elif defined(__EMSCRIPTEN__) || defined(__NetBSD__)
Howard Hinnant942dbd22013-03-29 18:27:28 +0000367 typedef unsigned short mask;
JF Bastien0c265d82015-02-25 22:16:46 +0000368# endif
David Chisnall1d581062011-09-21 08:39:44 +0000369 static const mask space = _CTYPE_S;
370 static const mask print = _CTYPE_R;
371 static const mask cntrl = _CTYPE_C;
372 static const mask upper = _CTYPE_U;
373 static const mask lower = _CTYPE_L;
374 static const mask alpha = _CTYPE_A;
375 static const mask digit = _CTYPE_D;
376 static const mask punct = _CTYPE_P;
377 static const mask xdigit = _CTYPE_X;
Dan Albert2dca4072014-07-23 19:32:03 +0000378
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000379# if defined(__NetBSD__)
380 static const mask blank = _CTYPE_BL;
381# else
David Chisnall1d581062011-09-21 08:39:44 +0000382 static const mask blank = _CTYPE_B;
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000383# endif
Howard Hinnanta47505d2013-08-30 14:42:39 +0000384#elif defined(__sun__) || defined(_AIX)
David Chisnall8074c342012-02-29 13:05:08 +0000385 typedef unsigned int mask;
386 static const mask space = _ISSPACE;
387 static const mask print = _ISPRINT;
388 static const mask cntrl = _ISCNTRL;
389 static const mask upper = _ISUPPER;
390 static const mask lower = _ISLOWER;
391 static const mask alpha = _ISALPHA;
392 static const mask digit = _ISDIGIT;
393 static const mask punct = _ISPUNCT;
394 static const mask xdigit = _ISXDIGIT;
395 static const mask blank = _ISBLANK;
JF Bastien0c265d82015-02-25 22:16:46 +0000396#elif defined(_NEWLIB_VERSION)
397 // Same type as Newlib's _ctype_ array in newlib/libc/include/ctype.h.
398 typedef char mask;
399 static const mask space = _S;
400 static const mask print = _P | _U | _L | _N | _B;
401 static const mask cntrl = _C;
402 static const mask upper = _U;
403 static const mask lower = _L;
404 static const mask alpha = _U | _L;
405 static const mask digit = _N;
406 static const mask punct = _P;
407 static const mask xdigit = _X | _N;
408 static const mask blank = _B;
Jonathan Roelofsae9ab252015-03-11 17:00:28 +0000409# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
410# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
411# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_XDIGIT
JF Bastien0c265d82015-02-25 22:16:46 +0000412#else
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000413 typedef unsigned long mask;
414 static const mask space = 1<<0;
415 static const mask print = 1<<1;
416 static const mask cntrl = 1<<2;
417 static const mask upper = 1<<3;
418 static const mask lower = 1<<4;
419 static const mask alpha = 1<<5;
420 static const mask digit = 1<<6;
421 static const mask punct = 1<<7;
422 static const mask xdigit = 1<<8;
423 static const mask blank = 1<<9;
JF Bastien0c265d82015-02-25 22:16:46 +0000424#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000425 static const mask alnum = alpha | digit;
426 static const mask graph = alnum | punct;
427
428 _LIBCPP_ALWAYS_INLINE ctype_base() {}
429};
430
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000431template <class _CharT> class _LIBCPP_TEMPLATE_VIS ctype;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000432
433template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000434class _LIBCPP_TYPE_VIS ctype<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000435 : public locale::facet,
436 public ctype_base
437{
438public:
439 typedef wchar_t char_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000440
Howard Hinnantc51e1022010-05-11 19:42:16 +0000441 _LIBCPP_ALWAYS_INLINE
442 explicit ctype(size_t __refs = 0)
443 : locale::facet(__refs) {}
444
445 _LIBCPP_ALWAYS_INLINE
446 bool is(mask __m, char_type __c) const
447 {
448 return do_is(__m, __c);
449 }
450
451 _LIBCPP_ALWAYS_INLINE
452 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
453 {
454 return do_is(__low, __high, __vec);
455 }
456
457 _LIBCPP_ALWAYS_INLINE
458 const char_type* scan_is(mask __m, const char_type* __low, const char_type* __high) const
459 {
460 return do_scan_is(__m, __low, __high);
461 }
462
463 _LIBCPP_ALWAYS_INLINE
464 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
465 {
466 return do_scan_not(__m, __low, __high);
467 }
468
469 _LIBCPP_ALWAYS_INLINE
470 char_type toupper(char_type __c) const
471 {
472 return do_toupper(__c);
473 }
474
475 _LIBCPP_ALWAYS_INLINE
476 const char_type* toupper(char_type* __low, const char_type* __high) const
477 {
478 return do_toupper(__low, __high);
479 }
480
481 _LIBCPP_ALWAYS_INLINE
482 char_type tolower(char_type __c) const
483 {
484 return do_tolower(__c);
485 }
486
487 _LIBCPP_ALWAYS_INLINE
488 const char_type* tolower(char_type* __low, const char_type* __high) const
489 {
490 return do_tolower(__low, __high);
491 }
492
493 _LIBCPP_ALWAYS_INLINE
494 char_type widen(char __c) const
495 {
496 return do_widen(__c);
497 }
498
499 _LIBCPP_ALWAYS_INLINE
500 const char* widen(const char* __low, const char* __high, char_type* __to) const
501 {
502 return do_widen(__low, __high, __to);
503 }
504
505 _LIBCPP_ALWAYS_INLINE
506 char narrow(char_type __c, char __dfault) const
507 {
508 return do_narrow(__c, __dfault);
509 }
510
511 _LIBCPP_ALWAYS_INLINE
512 const char_type* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
513 {
514 return do_narrow(__low, __high, __dfault, __to);
515 }
516
517 static locale::id id;
518
519protected:
520 ~ctype();
521 virtual bool do_is(mask __m, char_type __c) const;
522 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
523 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
524 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
525 virtual char_type do_toupper(char_type) const;
526 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
527 virtual char_type do_tolower(char_type) const;
528 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
529 virtual char_type do_widen(char) const;
530 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
531 virtual char do_narrow(char_type, char __dfault) const;
532 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
533};
534
535template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000536class _LIBCPP_TYPE_VIS ctype<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000537 : public locale::facet, public ctype_base
538{
539 const mask* __tab_;
540 bool __del_;
541public:
542 typedef char char_type;
543
544 explicit ctype(const mask* __tab = 0, bool __del = false, size_t __refs = 0);
545
546 _LIBCPP_ALWAYS_INLINE
547 bool is(mask __m, char_type __c) const
548 {
Marshall Clow11de4872013-10-21 14:41:05 +0000549 return isascii(__c) ? (__tab_[static_cast<int>(__c)] & __m) !=0 : false;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000550 }
551
552 _LIBCPP_ALWAYS_INLINE
553 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
554 {
555 for (; __low != __high; ++__low, ++__vec)
Howard Hinnant28b24882011-12-01 20:21:04 +0000556 *__vec = isascii(*__low) ? __tab_[static_cast<int>(*__low)] : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000557 return __low;
558 }
559
560 _LIBCPP_ALWAYS_INLINE
561 const char_type* scan_is (mask __m, const char_type* __low, const char_type* __high) const
562 {
563 for (; __low != __high; ++__low)
Howard Hinnant28b24882011-12-01 20:21:04 +0000564 if (isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000565 break;
566 return __low;
567 }
568
569 _LIBCPP_ALWAYS_INLINE
570 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
571 {
572 for (; __low != __high; ++__low)
Howard Hinnant28b24882011-12-01 20:21:04 +0000573 if (!(isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m)))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000574 break;
575 return __low;
576 }
577
578 _LIBCPP_ALWAYS_INLINE
579 char_type toupper(char_type __c) const
580 {
581 return do_toupper(__c);
582 }
583
584 _LIBCPP_ALWAYS_INLINE
585 const char_type* toupper(char_type* __low, const char_type* __high) const
586 {
587 return do_toupper(__low, __high);
588 }
589
590 _LIBCPP_ALWAYS_INLINE
591 char_type tolower(char_type __c) const
592 {
593 return do_tolower(__c);
594 }
595
596 _LIBCPP_ALWAYS_INLINE
597 const char_type* tolower(char_type* __low, const char_type* __high) const
598 {
599 return do_tolower(__low, __high);
600 }
601
602 _LIBCPP_ALWAYS_INLINE
603 char_type widen(char __c) const
604 {
605 return do_widen(__c);
606 }
607
608 _LIBCPP_ALWAYS_INLINE
609 const char* widen(const char* __low, const char* __high, char_type* __to) const
610 {
611 return do_widen(__low, __high, __to);
612 }
613
614 _LIBCPP_ALWAYS_INLINE
615 char narrow(char_type __c, char __dfault) const
616 {
617 return do_narrow(__c, __dfault);
618 }
619
620 _LIBCPP_ALWAYS_INLINE
621 const char* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
622 {
623 return do_narrow(__low, __high, __dfault, __to);
624 }
625
626 static locale::id id;
627
Howard Hinnant155c2af2010-05-24 17:49:41 +0000628#ifdef _CACHED_RUNES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000629 static const size_t table_size = _CACHED_RUNES;
Howard Hinnant155c2af2010-05-24 17:49:41 +0000630#else
631 static const size_t table_size = 256; // FIXME: Don't hardcode this.
632#endif
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000633 _LIBCPP_ALWAYS_INLINE const mask* table() const _NOEXCEPT {return __tab_;}
634 static const mask* classic_table() _NOEXCEPT;
Vasileios Kalintirisc5dd8942015-11-24 10:24:54 +0000635#if defined(__GLIBC__) || defined(__EMSCRIPTEN__)
Alexis Hunt92b0c812011-07-09 00:56:23 +0000636 static const int* __classic_upper_table() _NOEXCEPT;
637 static const int* __classic_lower_table() _NOEXCEPT;
Alexis Hunt5a4dd562011-07-09 01:09:31 +0000638#endif
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000639#if defined(__NetBSD__)
640 static const short* __classic_upper_table() _NOEXCEPT;
641 static const short* __classic_lower_table() _NOEXCEPT;
642#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000643
644protected:
645 ~ctype();
646 virtual char_type do_toupper(char_type __c) const;
647 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
648 virtual char_type do_tolower(char_type __c) const;
649 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
650 virtual char_type do_widen(char __c) const;
651 virtual const char* do_widen(const char* __low, const char* __high, char_type* __to) const;
652 virtual char do_narrow(char_type __c, char __dfault) const;
653 virtual const char* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const;
654};
655
656// template <class CharT> class ctype_byname;
657
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000658template <class _CharT> class _LIBCPP_TEMPLATE_VIS ctype_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000659
660template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000661class _LIBCPP_TYPE_VIS ctype_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000662 : public ctype<char>
663{
664 locale_t __l;
665
666public:
667 explicit ctype_byname(const char*, size_t = 0);
668 explicit ctype_byname(const string&, size_t = 0);
669
670protected:
671 ~ctype_byname();
672 virtual char_type do_toupper(char_type) const;
673 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
674 virtual char_type do_tolower(char_type) const;
675 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
676};
677
678template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000679class _LIBCPP_TYPE_VIS ctype_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000680 : public ctype<wchar_t>
681{
682 locale_t __l;
683
684public:
685 explicit ctype_byname(const char*, size_t = 0);
686 explicit ctype_byname(const string&, size_t = 0);
687
688protected:
689 ~ctype_byname();
690 virtual bool do_is(mask __m, char_type __c) const;
691 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
692 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
693 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
694 virtual char_type do_toupper(char_type) const;
695 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
696 virtual char_type do_tolower(char_type) const;
697 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
698 virtual char_type do_widen(char) const;
699 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
700 virtual char do_narrow(char_type, char __dfault) const;
701 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
702};
703
704template <class _CharT>
705inline _LIBCPP_INLINE_VISIBILITY
706bool
707isspace(_CharT __c, const locale& __loc)
708{
709 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
710}
711
712template <class _CharT>
713inline _LIBCPP_INLINE_VISIBILITY
714bool
715isprint(_CharT __c, const locale& __loc)
716{
717 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c);
718}
719
720template <class _CharT>
721inline _LIBCPP_INLINE_VISIBILITY
722bool
723iscntrl(_CharT __c, const locale& __loc)
724{
725 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c);
726}
727
728template <class _CharT>
729inline _LIBCPP_INLINE_VISIBILITY
730bool
731isupper(_CharT __c, const locale& __loc)
732{
733 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c);
734}
735
736template <class _CharT>
737inline _LIBCPP_INLINE_VISIBILITY
738bool
739islower(_CharT __c, const locale& __loc)
740{
741 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c);
742}
743
744template <class _CharT>
745inline _LIBCPP_INLINE_VISIBILITY
746bool
747isalpha(_CharT __c, const locale& __loc)
748{
749 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c);
750}
751
752template <class _CharT>
753inline _LIBCPP_INLINE_VISIBILITY
754bool
755isdigit(_CharT __c, const locale& __loc)
756{
757 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c);
758}
759
760template <class _CharT>
761inline _LIBCPP_INLINE_VISIBILITY
762bool
763ispunct(_CharT __c, const locale& __loc)
764{
765 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c);
766}
767
768template <class _CharT>
769inline _LIBCPP_INLINE_VISIBILITY
770bool
771isxdigit(_CharT __c, const locale& __loc)
772{
773 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c);
774}
775
776template <class _CharT>
777inline _LIBCPP_INLINE_VISIBILITY
778bool
779isalnum(_CharT __c, const locale& __loc)
780{
781 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c);
782}
783
784template <class _CharT>
785inline _LIBCPP_INLINE_VISIBILITY
786bool
787isgraph(_CharT __c, const locale& __loc)
788{
789 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c);
790}
791
792template <class _CharT>
793inline _LIBCPP_INLINE_VISIBILITY
794_CharT
795toupper(_CharT __c, const locale& __loc)
796{
797 return use_facet<ctype<_CharT> >(__loc).toupper(__c);
798}
799
800template <class _CharT>
801inline _LIBCPP_INLINE_VISIBILITY
802_CharT
803tolower(_CharT __c, const locale& __loc)
804{
805 return use_facet<ctype<_CharT> >(__loc).tolower(__c);
806}
807
808// codecvt_base
809
Howard Hinnant8331b762013-03-06 23:30:19 +0000810class _LIBCPP_TYPE_VIS codecvt_base
Howard Hinnantc51e1022010-05-11 19:42:16 +0000811{
812public:
813 _LIBCPP_ALWAYS_INLINE codecvt_base() {}
814 enum result {ok, partial, error, noconv};
815};
816
817// template <class internT, class externT, class stateT> class codecvt;
818
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000819template <class _InternT, class _ExternT, class _StateT> class _LIBCPP_TEMPLATE_VIS codecvt;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000820
821// template <> class codecvt<char, char, mbstate_t>
822
823template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000824class _LIBCPP_TYPE_VIS codecvt<char, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000825 : public locale::facet,
826 public codecvt_base
827{
828public:
829 typedef char intern_type;
830 typedef char extern_type;
831 typedef mbstate_t state_type;
832
833 _LIBCPP_ALWAYS_INLINE
834 explicit codecvt(size_t __refs = 0)
835 : locale::facet(__refs) {}
836
837 _LIBCPP_ALWAYS_INLINE
838 result out(state_type& __st,
839 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
840 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
841 {
842 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
843 }
844
845 _LIBCPP_ALWAYS_INLINE
846 result unshift(state_type& __st,
847 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
848 {
849 return do_unshift(__st, __to, __to_end, __to_nxt);
850 }
851
852 _LIBCPP_ALWAYS_INLINE
853 result in(state_type& __st,
854 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
855 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
856 {
857 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
858 }
859
860 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000861 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862 {
863 return do_encoding();
864 }
865
866 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000867 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000868 {
869 return do_always_noconv();
870 }
871
872 _LIBCPP_ALWAYS_INLINE
873 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
874 {
875 return do_length(__st, __frm, __end, __mx);
876 }
877
878 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000879 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000880 {
881 return do_max_length();
882 }
883
884 static locale::id id;
885
886protected:
887 _LIBCPP_ALWAYS_INLINE
888 explicit codecvt(const char*, size_t __refs = 0)
889 : locale::facet(__refs) {}
890
891 ~codecvt();
892
893 virtual result do_out(state_type& __st,
894 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
895 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
896 virtual result do_in(state_type& __st,
897 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
898 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
899 virtual result do_unshift(state_type& __st,
900 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000901 virtual int do_encoding() const _NOEXCEPT;
902 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000903 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 +0000904 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000905};
906
907// template <> class codecvt<wchar_t, char, mbstate_t>
908
909template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000910class _LIBCPP_TYPE_VIS codecvt<wchar_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000911 : public locale::facet,
912 public codecvt_base
913{
914 locale_t __l;
915public:
916 typedef wchar_t intern_type;
917 typedef char extern_type;
918 typedef mbstate_t state_type;
919
920 explicit codecvt(size_t __refs = 0);
921
922 _LIBCPP_ALWAYS_INLINE
923 result out(state_type& __st,
924 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
925 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
926 {
927 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
928 }
929
930 _LIBCPP_ALWAYS_INLINE
931 result unshift(state_type& __st,
932 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
933 {
934 return do_unshift(__st, __to, __to_end, __to_nxt);
935 }
936
937 _LIBCPP_ALWAYS_INLINE
938 result in(state_type& __st,
939 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
940 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
941 {
942 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
943 }
944
945 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000946 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947 {
948 return do_encoding();
949 }
950
951 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000952 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000953 {
954 return do_always_noconv();
955 }
956
957 _LIBCPP_ALWAYS_INLINE
958 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
959 {
960 return do_length(__st, __frm, __end, __mx);
961 }
962
963 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000964 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965 {
966 return do_max_length();
967 }
968
969 static locale::id id;
970
971protected:
972 explicit codecvt(const char*, size_t __refs = 0);
973
974 ~codecvt();
975
976 virtual result do_out(state_type& __st,
977 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
978 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
979 virtual result do_in(state_type& __st,
980 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
981 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
982 virtual result do_unshift(state_type& __st,
983 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000984 virtual int do_encoding() const _NOEXCEPT;
985 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986 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 +0000987 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988};
989
990// template <> class codecvt<char16_t, char, mbstate_t>
991
992template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000993class _LIBCPP_TYPE_VIS codecvt<char16_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000994 : public locale::facet,
995 public codecvt_base
996{
997public:
998 typedef char16_t intern_type;
999 typedef char extern_type;
1000 typedef mbstate_t state_type;
1001
1002 _LIBCPP_ALWAYS_INLINE
1003 explicit codecvt(size_t __refs = 0)
1004 : locale::facet(__refs) {}
1005
1006 _LIBCPP_ALWAYS_INLINE
1007 result out(state_type& __st,
1008 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1009 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1010 {
1011 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1012 }
1013
1014 _LIBCPP_ALWAYS_INLINE
1015 result unshift(state_type& __st,
1016 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1017 {
1018 return do_unshift(__st, __to, __to_end, __to_nxt);
1019 }
1020
1021 _LIBCPP_ALWAYS_INLINE
1022 result in(state_type& __st,
1023 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1024 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1025 {
1026 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1027 }
1028
1029 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001030 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001031 {
1032 return do_encoding();
1033 }
1034
1035 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001036 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037 {
1038 return do_always_noconv();
1039 }
1040
1041 _LIBCPP_ALWAYS_INLINE
1042 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1043 {
1044 return do_length(__st, __frm, __end, __mx);
1045 }
1046
1047 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001048 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049 {
1050 return do_max_length();
1051 }
1052
1053 static locale::id id;
1054
1055protected:
1056 _LIBCPP_ALWAYS_INLINE
1057 explicit codecvt(const char*, size_t __refs = 0)
1058 : locale::facet(__refs) {}
1059
1060 ~codecvt();
1061
1062 virtual result do_out(state_type& __st,
1063 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1064 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1065 virtual result do_in(state_type& __st,
1066 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1067 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1068 virtual result do_unshift(state_type& __st,
1069 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001070 virtual int do_encoding() const _NOEXCEPT;
1071 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072 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 +00001073 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074};
1075
1076// template <> class codecvt<char32_t, char, mbstate_t>
1077
1078template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001079class _LIBCPP_TYPE_VIS codecvt<char32_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001080 : public locale::facet,
1081 public codecvt_base
1082{
1083public:
1084 typedef char32_t intern_type;
1085 typedef char extern_type;
1086 typedef mbstate_t state_type;
1087
1088 _LIBCPP_ALWAYS_INLINE
1089 explicit codecvt(size_t __refs = 0)
1090 : locale::facet(__refs) {}
1091
1092 _LIBCPP_ALWAYS_INLINE
1093 result out(state_type& __st,
1094 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1095 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1096 {
1097 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1098 }
1099
1100 _LIBCPP_ALWAYS_INLINE
1101 result unshift(state_type& __st,
1102 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1103 {
1104 return do_unshift(__st, __to, __to_end, __to_nxt);
1105 }
1106
1107 _LIBCPP_ALWAYS_INLINE
1108 result in(state_type& __st,
1109 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1110 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1111 {
1112 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1113 }
1114
1115 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001116 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117 {
1118 return do_encoding();
1119 }
1120
1121 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001122 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001123 {
1124 return do_always_noconv();
1125 }
1126
1127 _LIBCPP_ALWAYS_INLINE
1128 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1129 {
1130 return do_length(__st, __frm, __end, __mx);
1131 }
1132
1133 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001134 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001135 {
1136 return do_max_length();
1137 }
1138
1139 static locale::id id;
1140
1141protected:
1142 _LIBCPP_ALWAYS_INLINE
1143 explicit codecvt(const char*, size_t __refs = 0)
1144 : locale::facet(__refs) {}
1145
1146 ~codecvt();
1147
1148 virtual result do_out(state_type& __st,
1149 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1150 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1151 virtual result do_in(state_type& __st,
1152 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1153 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1154 virtual result do_unshift(state_type& __st,
1155 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001156 virtual int do_encoding() const _NOEXCEPT;
1157 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158 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 +00001159 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160};
1161
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162// template <class _InternT, class _ExternT, class _StateT> class codecvt_byname
1163
1164template <class _InternT, class _ExternT, class _StateT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001165class _LIBCPP_TEMPLATE_VIS codecvt_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166 : public codecvt<_InternT, _ExternT, _StateT>
1167{
1168public:
Howard Hinnant9833cad2010-09-21 18:58:51 +00001169 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170 explicit codecvt_byname(const char* __nm, size_t __refs = 0)
1171 : codecvt<_InternT, _ExternT, _StateT>(__nm, __refs) {}
Howard Hinnant9833cad2010-09-21 18:58:51 +00001172 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173 explicit codecvt_byname(const string& __nm, size_t __refs = 0)
1174 : codecvt<_InternT, _ExternT, _StateT>(__nm.c_str(), __refs) {}
1175protected:
1176 ~codecvt_byname();
1177};
1178
1179template <class _InternT, class _ExternT, class _StateT>
1180codecvt_byname<_InternT, _ExternT, _StateT>::~codecvt_byname()
1181{
1182}
1183
Eric Fiselier1b57fa82016-09-15 22:27:07 +00001184_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char, char, mbstate_t>)
1185_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<wchar_t, char, mbstate_t>)
1186_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char16_t, char, mbstate_t>)
1187_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char32_t, char, mbstate_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001188
Aditya Kumare0f6a142016-08-27 02:26:42 +00001189_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_runtime_error(const char*);
1190
Howard Hinnantc834c512011-11-29 18:15:50 +00001191template <size_t _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192struct __narrow_to_utf8
1193{
1194 template <class _OutputIterator, class _CharT>
1195 _OutputIterator
1196 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const;
1197};
1198
1199template <>
1200struct __narrow_to_utf8<8>
1201{
1202 template <class _OutputIterator, class _CharT>
1203 _LIBCPP_ALWAYS_INLINE
1204 _OutputIterator
1205 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1206 {
1207 for (; __wb < __we; ++__wb, ++__s)
1208 *__s = *__wb;
1209 return __s;
1210 }
1211};
1212
1213template <>
1214struct __narrow_to_utf8<16>
1215 : public codecvt<char16_t, char, mbstate_t>
1216{
1217 _LIBCPP_ALWAYS_INLINE
1218 __narrow_to_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1219
1220 ~__narrow_to_utf8();
1221
1222 template <class _OutputIterator, class _CharT>
1223 _LIBCPP_ALWAYS_INLINE
1224 _OutputIterator
1225 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1226 {
1227 result __r = ok;
1228 mbstate_t __mb;
1229 while (__wb < __we && __r != error)
1230 {
1231 const int __sz = 32;
1232 char __buf[__sz];
1233 char* __bn;
1234 const char16_t* __wn = (const char16_t*)__wb;
1235 __r = do_out(__mb, (const char16_t*)__wb, (const char16_t*)__we, __wn,
1236 __buf, __buf+__sz, __bn);
1237 if (__r == codecvt_base::error || __wn == (const char16_t*)__wb)
1238 __throw_runtime_error("locale not supported");
1239 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1240 *__s = *__p;
1241 __wb = (const _CharT*)__wn;
1242 }
1243 return __s;
1244 }
1245};
1246
1247template <>
1248struct __narrow_to_utf8<32>
1249 : public codecvt<char32_t, char, mbstate_t>
1250{
1251 _LIBCPP_ALWAYS_INLINE
1252 __narrow_to_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1253
1254 ~__narrow_to_utf8();
1255
1256 template <class _OutputIterator, class _CharT>
1257 _LIBCPP_ALWAYS_INLINE
1258 _OutputIterator
1259 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1260 {
1261 result __r = ok;
1262 mbstate_t __mb;
1263 while (__wb < __we && __r != error)
1264 {
1265 const int __sz = 32;
1266 char __buf[__sz];
1267 char* __bn;
1268 const char32_t* __wn = (const char32_t*)__wb;
1269 __r = do_out(__mb, (const char32_t*)__wb, (const char32_t*)__we, __wn,
1270 __buf, __buf+__sz, __bn);
1271 if (__r == codecvt_base::error || __wn == (const char32_t*)__wb)
1272 __throw_runtime_error("locale not supported");
1273 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1274 *__s = *__p;
1275 __wb = (const _CharT*)__wn;
1276 }
1277 return __s;
1278 }
1279};
1280
Howard Hinnantc834c512011-11-29 18:15:50 +00001281template <size_t _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282struct __widen_from_utf8
1283{
1284 template <class _OutputIterator>
1285 _OutputIterator
1286 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const;
1287};
1288
1289template <>
1290struct __widen_from_utf8<8>
1291{
1292 template <class _OutputIterator>
1293 _LIBCPP_ALWAYS_INLINE
1294 _OutputIterator
1295 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1296 {
1297 for (; __nb < __ne; ++__nb, ++__s)
1298 *__s = *__nb;
1299 return __s;
1300 }
1301};
1302
1303template <>
1304struct __widen_from_utf8<16>
1305 : public codecvt<char16_t, char, mbstate_t>
1306{
1307 _LIBCPP_ALWAYS_INLINE
1308 __widen_from_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1309
1310 ~__widen_from_utf8();
1311
1312 template <class _OutputIterator>
1313 _LIBCPP_ALWAYS_INLINE
1314 _OutputIterator
1315 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1316 {
1317 result __r = ok;
1318 mbstate_t __mb;
1319 while (__nb < __ne && __r != error)
1320 {
1321 const int __sz = 32;
1322 char16_t __buf[__sz];
1323 char16_t* __bn;
1324 const char* __nn = __nb;
1325 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1326 __buf, __buf+__sz, __bn);
1327 if (__r == codecvt_base::error || __nn == __nb)
1328 __throw_runtime_error("locale not supported");
1329 for (const char16_t* __p = __buf; __p < __bn; ++__p, ++__s)
1330 *__s = (wchar_t)*__p;
1331 __nb = __nn;
1332 }
1333 return __s;
1334 }
1335};
1336
1337template <>
1338struct __widen_from_utf8<32>
1339 : public codecvt<char32_t, char, mbstate_t>
1340{
1341 _LIBCPP_ALWAYS_INLINE
1342 __widen_from_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1343
1344 ~__widen_from_utf8();
1345
1346 template <class _OutputIterator>
1347 _LIBCPP_ALWAYS_INLINE
1348 _OutputIterator
1349 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1350 {
1351 result __r = ok;
1352 mbstate_t __mb;
1353 while (__nb < __ne && __r != error)
1354 {
1355 const int __sz = 32;
1356 char32_t __buf[__sz];
1357 char32_t* __bn;
1358 const char* __nn = __nb;
1359 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1360 __buf, __buf+__sz, __bn);
1361 if (__r == codecvt_base::error || __nn == __nb)
1362 __throw_runtime_error("locale not supported");
1363 for (const char32_t* __p = __buf; __p < __bn; ++__p, ++__s)
1364 *__s = (wchar_t)*__p;
1365 __nb = __nn;
1366 }
1367 return __s;
1368 }
1369};
1370
1371// template <class charT> class numpunct
1372
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001373template <class _CharT> class _LIBCPP_TEMPLATE_VIS numpunct;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001374
1375template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001376class _LIBCPP_TYPE_VIS numpunct<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377 : public locale::facet
1378{
1379public:
1380 typedef char char_type;
1381 typedef basic_string<char_type> string_type;
1382
1383 explicit numpunct(size_t __refs = 0);
1384
1385 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
1386 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
1387 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
1388 _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();}
1389 _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();}
1390
1391 static locale::id id;
1392
1393protected:
1394 ~numpunct();
1395 virtual char_type do_decimal_point() const;
1396 virtual char_type do_thousands_sep() const;
1397 virtual string do_grouping() const;
1398 virtual string_type do_truename() const;
1399 virtual string_type do_falsename() const;
1400
1401 char_type __decimal_point_;
1402 char_type __thousands_sep_;
1403 string __grouping_;
1404};
1405
1406template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001407class _LIBCPP_TYPE_VIS numpunct<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001408 : public locale::facet
1409{
1410public:
1411 typedef wchar_t char_type;
1412 typedef basic_string<char_type> string_type;
1413
1414 explicit numpunct(size_t __refs = 0);
1415
1416 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
1417 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
1418 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
1419 _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();}
1420 _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();}
1421
1422 static locale::id id;
1423
1424protected:
1425 ~numpunct();
1426 virtual char_type do_decimal_point() const;
1427 virtual char_type do_thousands_sep() const;
1428 virtual string do_grouping() const;
1429 virtual string_type do_truename() const;
1430 virtual string_type do_falsename() const;
1431
1432 char_type __decimal_point_;
1433 char_type __thousands_sep_;
1434 string __grouping_;
1435};
1436
1437// template <class charT> class numpunct_byname
1438
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001439template <class _CharT> class _LIBCPP_TEMPLATE_VIS numpunct_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001440
1441template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001442class _LIBCPP_TYPE_VIS numpunct_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443: public numpunct<char>
1444{
1445public:
1446 typedef char char_type;
1447 typedef basic_string<char_type> string_type;
1448
1449 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1450 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1451
1452protected:
1453 ~numpunct_byname();
1454
1455private:
1456 void __init(const char*);
1457};
1458
1459template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001460class _LIBCPP_TYPE_VIS numpunct_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461: public numpunct<wchar_t>
1462{
1463public:
1464 typedef wchar_t char_type;
1465 typedef basic_string<char_type> string_type;
1466
1467 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1468 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1469
1470protected:
1471 ~numpunct_byname();
1472
1473private:
1474 void __init(const char*);
1475};
1476
1477_LIBCPP_END_NAMESPACE_STD
1478
1479#endif // _LIBCPP___LOCALE