blob: e525053ca3f6c974c451b8f2a24b2392337e69c4 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-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 Hinnantbc8d3f92010-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 Hinnantadff4892010-05-24 17:49:41 +000021#include <locale.h>
Howard Hinnantef5aa932013-09-17 01:34:47 +000022#if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
Howard Hinnant14fa9f92011-09-29 20:33:10 +000023# include <support/win32/locale_win32.h>
Marshall Clowbe3d1172014-03-11 17:18:47 +000024#elif defined(_AIX)
Howard Hinnant7f764502013-08-14 18:00:20 +000025# include <support/ibm/xlocale.h>
Marshall Clowa6439262014-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 Fiselier6cb69ff2014-11-25 21:57:41 +000032#elif defined(__sun__)
Eric Fiselierf51d6762015-01-23 22:22:36 +000033# include <xlocale.h>
Eric Fiselier6cb69ff2014-11-25 21:57:41 +000034# include <support/solaris/xlocale.h>
Sergey Dmitrouk984f8f62014-12-12 08:36:16 +000035#elif defined(_NEWLIB_VERSION)
36# include <support/newlib/xlocale.h>
Marshall Clowa6439262014-07-10 15:20:28 +000037#elif (defined(__GLIBC__) || defined(__APPLE__) || defined(__FreeBSD__) \
Eric Fiselier6cb69ff2014-11-25 21:57:41 +000038 || defined(__EMSCRIPTEN__) || defined(__IBMCPP__))
Howard Hinnant92a07002011-09-22 19:10:18 +000039# include <xlocale.h>
Vasileios Kalintiris579b42b2015-11-09 10:21:04 +000040#elif defined(_LIBCPP_HAS_MUSL_LIBC)
41# include <support/musl/xlocale.h>
Marshall Clowa6439262014-07-10 15:20:28 +000042#endif // __GLIBC__ || __APPLE__ || __FreeBSD__ || __sun__ || __EMSCRIPTEN__ || __IBMCPP__
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000043
Howard Hinnant08e17472011-10-17 20:05:10 +000044#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000045#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000046#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000047
48_LIBCPP_BEGIN_NAMESPACE_STD
49
Howard Hinnant83eade62013-03-06 23:30:19 +000050class _LIBCPP_TYPE_VIS locale;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000051
Howard Hinnant33be35e2012-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 Hinnantbc8d3f92010-05-11 19:42:16 +000061
Howard Hinnant83eade62013-03-06 23:30:19 +000062class _LIBCPP_TYPE_VIS locale
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063{
64public:
65 // types:
Howard Hinnant83eade62013-03-06 23:30:19 +000066 class _LIBCPP_TYPE_VIS facet;
67 class _LIBCPP_TYPE_VIS id;
Howard Hinnantbc8d3f92010-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 Hinnantc9834542011-05-31 15:34:58 +000081 locale() _NOEXCEPT;
82 locale(const locale&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-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 Hinnant2d72b1e2010-12-17 14:46:43 +000087 template <class _Facet>
88 _LIBCPP_INLINE_VISIBILITY locale(const locale&, _Facet*);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000089 locale(const locale&, const locale&, category);
90
Howard Hinnantc9834542011-05-31 15:34:58 +000091 ~locale();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000092
Howard Hinnantc9834542011-05-31 15:34:58 +000093 const locale& operator=(const locale&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000094
95 template <class _Facet> locale combine(const locale&) const;
96
97 // locale operations:
98 string name() const;
99 bool operator==(const locale&) const;
100 bool operator!=(const locale& __y) const {return !(*this == __y);}
101 template <class _CharT, class _Traits, class _Allocator>
102 bool operator()(const basic_string<_CharT, _Traits, _Allocator>&,
103 const basic_string<_CharT, _Traits, _Allocator>&) const;
104
105 // global locale objects:
106 static locale global(const locale&);
107 static const locale& classic();
108
109private:
110 class __imp;
111 __imp* __locale_;
112
113 void __install_ctor(const locale&, facet*, long);
114 static locale& __global();
115 bool has_facet(id&) const;
116 const facet* use_facet(id&) const;
117
Howard Hinnantc9834542011-05-31 15:34:58 +0000118 template <class _Facet> friend bool has_facet(const locale&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000119 template <class _Facet> friend const _Facet& use_facet(const locale&);
120};
121
Howard Hinnant83eade62013-03-06 23:30:19 +0000122class _LIBCPP_TYPE_VIS locale::facet
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000123 : public __shared_count
124{
125protected:
Howard Hinnantb0be42b2010-09-21 18:58:51 +0000126 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000127 explicit facet(size_t __refs = 0)
128 : __shared_count(static_cast<long>(__refs)-1) {}
129
130 virtual ~facet();
131
132// facet(const facet&) = delete; // effectively done in __shared_count
133// void operator=(const facet&) = delete;
134private:
Howard Hinnant1694d232011-05-28 14:41:13 +0000135 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000136};
137
Howard Hinnant83eade62013-03-06 23:30:19 +0000138class _LIBCPP_TYPE_VIS locale::id
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000139{
140 once_flag __flag_;
141 int32_t __id_;
Howard Hinnant324bb032010-08-22 00:02:43 +0000142
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000143 static int32_t __next_id;
144public:
Howard Hinnantf3d62ea2012-07-26 16:14:37 +0000145 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR id() :__id_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000146private:
147 void __init();
148 void operator=(const id&); // = delete;
149 id(const id&); // = delete;
150public: // only needed for tests
151 long __get();
152
153 friend class locale;
154 friend class locale::__imp;
155};
156
157template <class _Facet>
158inline _LIBCPP_INLINE_VISIBILITY
159locale::locale(const locale& __other, _Facet* __f)
160{
161 __install_ctor(__other, __f, __f ? __f->id.__get() : 0);
162}
163
164template <class _Facet>
165locale
166locale::combine(const locale& __other) const
167{
Howard Hinnantd4444702010-08-11 17:04:31 +0000168#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +0000169 if (!_VSTD::has_facet<_Facet>(__other))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000170 throw runtime_error("locale::combine: locale missing facet");
Howard Hinnant324bb032010-08-22 00:02:43 +0000171#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +0000172 return locale(*this, &const_cast<_Facet&>(_VSTD::use_facet<_Facet>(__other)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000173}
174
175template <class _Facet>
176inline _LIBCPP_INLINE_VISIBILITY
177bool
Howard Hinnantc9834542011-05-31 15:34:58 +0000178has_facet(const locale& __l) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000179{
180 return __l.has_facet(_Facet::id);
181}
182
183template <class _Facet>
184inline _LIBCPP_INLINE_VISIBILITY
185const _Facet&
186use_facet(const locale& __l)
187{
188 return static_cast<const _Facet&>(*__l.use_facet(_Facet::id));
189}
190
191// template <class _CharT> class collate;
192
193template <class _CharT>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000194class _LIBCPP_TYPE_VIS_ONLY collate
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000195 : public locale::facet
196{
197public:
198 typedef _CharT char_type;
199 typedef basic_string<char_type> string_type;
200
Howard Hinnantb0be42b2010-09-21 18:58:51 +0000201 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000202 explicit collate(size_t __refs = 0)
203 : locale::facet(__refs) {}
204
Howard Hinnantb0be42b2010-09-21 18:58:51 +0000205 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000206 int compare(const char_type* __lo1, const char_type* __hi1,
207 const char_type* __lo2, const char_type* __hi2) const
208 {
209 return do_compare(__lo1, __hi1, __lo2, __hi2);
210 }
211
Howard Hinnantb0be42b2010-09-21 18:58:51 +0000212 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000213 string_type transform(const char_type* __lo, const char_type* __hi) const
214 {
215 return do_transform(__lo, __hi);
216 }
217
Howard Hinnantb0be42b2010-09-21 18:58:51 +0000218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000219 long hash(const char_type* __lo, const char_type* __hi) const
220 {
221 return do_hash(__lo, __hi);
222 }
223
224 static locale::id id;
225
226protected:
227 ~collate();
228 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
229 const char_type* __lo2, const char_type* __hi2) const;
230 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const
231 {return string_type(__lo, __hi);}
232 virtual long do_hash(const char_type* __lo, const char_type* __hi) const;
233};
234
235template <class _CharT> locale::id collate<_CharT>::id;
236
237template <class _CharT>
238collate<_CharT>::~collate()
239{
240}
241
242template <class _CharT>
243int
244collate<_CharT>::do_compare(const char_type* __lo1, const char_type* __hi1,
245 const char_type* __lo2, const char_type* __hi2) const
246{
247 for (; __lo2 != __hi2; ++__lo1, ++__lo2)
248 {
249 if (__lo1 == __hi1 || *__lo1 < *__lo2)
250 return -1;
251 if (*__lo2 < *__lo1)
252 return 1;
253 }
254 return __lo1 != __hi1;
255}
256
257template <class _CharT>
258long
Howard Hinnant11624452011-10-11 16:00:46 +0000259collate<_CharT>::do_hash(const char_type* __lo, const char_type* __hi) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000260{
Howard Hinnant11624452011-10-11 16:00:46 +0000261 size_t __h = 0;
262 const size_t __sr = __CHAR_BIT__ * sizeof(size_t) - 8;
263 const size_t __mask = size_t(0xF) << (__sr + 4);
264 for(const char_type* __p = __lo; __p != __hi; ++__p)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000265 {
Howard Hinnantec3773c2011-12-01 20:21:04 +0000266 __h = (__h << 4) + static_cast<size_t>(*__p);
Howard Hinnant11624452011-10-11 16:00:46 +0000267 size_t __g = __h & __mask;
268 __h ^= __g | (__g >> __sr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000269 }
Howard Hinnant11624452011-10-11 16:00:46 +0000270 return static_cast<long>(__h);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000271}
272
Howard Hinnant499cea12013-08-23 17:37:05 +0000273_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS collate<char>)
274_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS collate<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000275
276// template <class CharT> class collate_byname;
277
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000278template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY collate_byname;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000279
280template <>
Howard Hinnant83eade62013-03-06 23:30:19 +0000281class _LIBCPP_TYPE_VIS collate_byname<char>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000282 : public collate<char>
283{
284 locale_t __l;
285public:
286 typedef char char_type;
287 typedef basic_string<char_type> string_type;
288
289 explicit collate_byname(const char* __n, size_t __refs = 0);
290 explicit collate_byname(const string& __n, size_t __refs = 0);
291
292protected:
293 ~collate_byname();
294 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
295 const char_type* __lo2, const char_type* __hi2) const;
296 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
297};
298
299template <>
Howard Hinnant83eade62013-03-06 23:30:19 +0000300class _LIBCPP_TYPE_VIS collate_byname<wchar_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000301 : public collate<wchar_t>
302{
303 locale_t __l;
304public:
305 typedef wchar_t char_type;
306 typedef basic_string<char_type> string_type;
307
308 explicit collate_byname(const char* __n, size_t __refs = 0);
309 explicit collate_byname(const string& __n, size_t __refs = 0);
310
311protected:
312 ~collate_byname();
313
314 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
315 const char_type* __lo2, const char_type* __hi2) const;
316 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
317};
318
319template <class _CharT, class _Traits, class _Allocator>
320bool
321locale::operator()(const basic_string<_CharT, _Traits, _Allocator>& __x,
322 const basic_string<_CharT, _Traits, _Allocator>& __y) const
323{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000324 return _VSTD::use_facet<_VSTD::collate<_CharT> >(*this).compare(
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000325 __x.data(), __x.data() + __x.size(),
326 __y.data(), __y.data() + __y.size()) < 0;
327}
328
329// template <class charT> class ctype
330
Howard Hinnant83eade62013-03-06 23:30:19 +0000331class _LIBCPP_TYPE_VIS ctype_base
Howard Hinnantb0be42b2010-09-21 18:58:51 +0000332{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000333public:
Vasileios Kalintiris579b42b2015-11-09 10:21:04 +0000334#if defined(__GLIBC__)
Sean Hunt62a6ac32011-07-09 00:56:23 +0000335 typedef unsigned short mask;
Howard Hinnantadff4892010-05-24 17:49:41 +0000336 static const mask space = _ISspace;
337 static const mask print = _ISprint;
338 static const mask cntrl = _IScntrl;
339 static const mask upper = _ISupper;
340 static const mask lower = _ISlower;
341 static const mask alpha = _ISalpha;
342 static const mask digit = _ISdigit;
343 static const mask punct = _ISpunct;
344 static const mask xdigit = _ISxdigit;
345 static const mask blank = _ISblank;
Marshall Clowa22d2ad2013-03-18 17:04:29 +0000346#elif defined(_WIN32)
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000347 typedef unsigned short mask;
Howard Hinnant92a07002011-09-22 19:10:18 +0000348 static const mask space = _SPACE;
349 static const mask print = _BLANK|_PUNCT|_ALPHA|_DIGIT;
350 static const mask cntrl = _CONTROL;
351 static const mask upper = _UPPER;
352 static const mask lower = _LOWER;
353 static const mask alpha = _ALPHA;
354 static const mask digit = _DIGIT;
355 static const mask punct = _PUNCT;
356 static const mask xdigit = _HEX;
357 static const mask blank = _BLANK;
Jonathan Roelofs6fb33ea2015-03-11 17:00:28 +0000358# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
Dan Albert6272ae52015-03-11 00:51:06 +0000359#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__)
JF Bastiena14f7cb2015-02-25 22:16:46 +0000360# ifdef __APPLE__
David Chisnallc512df12011-09-21 08:39:44 +0000361 typedef __uint32_t mask;
JF Bastiena14f7cb2015-02-25 22:16:46 +0000362# elif defined(__FreeBSD__)
David Chisnallc512df12011-09-21 08:39:44 +0000363 typedef unsigned long mask;
Vasileios Kalintiris579b42b2015-11-09 10:21:04 +0000364# elif defined(__EMSCRIPTEN__) || defined(__NetBSD__) || defined(_LIBCPP_HAS_MUSL_LIBC)
Howard Hinnantfc2f0212013-03-29 18:27:28 +0000365 typedef unsigned short mask;
JF Bastiena14f7cb2015-02-25 22:16:46 +0000366# endif
David Chisnallc512df12011-09-21 08:39:44 +0000367 static const mask space = _CTYPE_S;
368 static const mask print = _CTYPE_R;
369 static const mask cntrl = _CTYPE_C;
370 static const mask upper = _CTYPE_U;
371 static const mask lower = _CTYPE_L;
372 static const mask alpha = _CTYPE_A;
373 static const mask digit = _CTYPE_D;
374 static const mask punct = _CTYPE_P;
375 static const mask xdigit = _CTYPE_X;
Dan Albert2a52a322014-07-23 19:32:03 +0000376
Joerg Sonnenbergera71a9522013-05-17 21:17:34 +0000377# if defined(__NetBSD__)
378 static const mask blank = _CTYPE_BL;
379# else
David Chisnallc512df12011-09-21 08:39:44 +0000380 static const mask blank = _CTYPE_B;
Joerg Sonnenbergera71a9522013-05-17 21:17:34 +0000381# endif
Howard Hinnantcb0e6b62013-08-30 14:42:39 +0000382#elif defined(__sun__) || defined(_AIX)
David Chisnall997e4542012-02-29 13:05:08 +0000383 typedef unsigned int mask;
384 static const mask space = _ISSPACE;
385 static const mask print = _ISPRINT;
386 static const mask cntrl = _ISCNTRL;
387 static const mask upper = _ISUPPER;
388 static const mask lower = _ISLOWER;
389 static const mask alpha = _ISALPHA;
390 static const mask digit = _ISDIGIT;
391 static const mask punct = _ISPUNCT;
392 static const mask xdigit = _ISXDIGIT;
393 static const mask blank = _ISBLANK;
JF Bastiena14f7cb2015-02-25 22:16:46 +0000394#elif defined(_NEWLIB_VERSION)
395 // Same type as Newlib's _ctype_ array in newlib/libc/include/ctype.h.
396 typedef char mask;
397 static const mask space = _S;
398 static const mask print = _P | _U | _L | _N | _B;
399 static const mask cntrl = _C;
400 static const mask upper = _U;
401 static const mask lower = _L;
402 static const mask alpha = _U | _L;
403 static const mask digit = _N;
404 static const mask punct = _P;
405 static const mask xdigit = _X | _N;
406 static const mask blank = _B;
Jonathan Roelofs6fb33ea2015-03-11 17:00:28 +0000407# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
408# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
409# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_XDIGIT
JF Bastiena14f7cb2015-02-25 22:16:46 +0000410#else
Vasileios Kalintiris579b42b2015-11-09 10:21:04 +0000411#if defined(_LIBCPP_HAS_MUSL_LIBC)
412 typedef unsigned short mask;
413#else
Howard Hinnant11624452011-10-11 16:00:46 +0000414 typedef unsigned long mask;
Vasileios Kalintiris579b42b2015-11-09 10:21:04 +0000415#endif
Howard Hinnant11624452011-10-11 16:00:46 +0000416 static const mask space = 1<<0;
417 static const mask print = 1<<1;
418 static const mask cntrl = 1<<2;
419 static const mask upper = 1<<3;
420 static const mask lower = 1<<4;
421 static const mask alpha = 1<<5;
422 static const mask digit = 1<<6;
423 static const mask punct = 1<<7;
424 static const mask xdigit = 1<<8;
425 static const mask blank = 1<<9;
JF Bastiena14f7cb2015-02-25 22:16:46 +0000426#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000427 static const mask alnum = alpha | digit;
428 static const mask graph = alnum | punct;
429
430 _LIBCPP_ALWAYS_INLINE ctype_base() {}
431};
432
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000433template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY ctype;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000434
435template <>
Howard Hinnant83eade62013-03-06 23:30:19 +0000436class _LIBCPP_TYPE_VIS ctype<wchar_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000437 : public locale::facet,
438 public ctype_base
439{
440public:
441 typedef wchar_t char_type;
Howard Hinnant324bb032010-08-22 00:02:43 +0000442
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000443 _LIBCPP_ALWAYS_INLINE
444 explicit ctype(size_t __refs = 0)
445 : locale::facet(__refs) {}
446
447 _LIBCPP_ALWAYS_INLINE
448 bool is(mask __m, char_type __c) const
449 {
450 return do_is(__m, __c);
451 }
452
453 _LIBCPP_ALWAYS_INLINE
454 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
455 {
456 return do_is(__low, __high, __vec);
457 }
458
459 _LIBCPP_ALWAYS_INLINE
460 const char_type* scan_is(mask __m, const char_type* __low, const char_type* __high) const
461 {
462 return do_scan_is(__m, __low, __high);
463 }
464
465 _LIBCPP_ALWAYS_INLINE
466 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
467 {
468 return do_scan_not(__m, __low, __high);
469 }
470
471 _LIBCPP_ALWAYS_INLINE
472 char_type toupper(char_type __c) const
473 {
474 return do_toupper(__c);
475 }
476
477 _LIBCPP_ALWAYS_INLINE
478 const char_type* toupper(char_type* __low, const char_type* __high) const
479 {
480 return do_toupper(__low, __high);
481 }
482
483 _LIBCPP_ALWAYS_INLINE
484 char_type tolower(char_type __c) const
485 {
486 return do_tolower(__c);
487 }
488
489 _LIBCPP_ALWAYS_INLINE
490 const char_type* tolower(char_type* __low, const char_type* __high) const
491 {
492 return do_tolower(__low, __high);
493 }
494
495 _LIBCPP_ALWAYS_INLINE
496 char_type widen(char __c) const
497 {
498 return do_widen(__c);
499 }
500
501 _LIBCPP_ALWAYS_INLINE
502 const char* widen(const char* __low, const char* __high, char_type* __to) const
503 {
504 return do_widen(__low, __high, __to);
505 }
506
507 _LIBCPP_ALWAYS_INLINE
508 char narrow(char_type __c, char __dfault) const
509 {
510 return do_narrow(__c, __dfault);
511 }
512
513 _LIBCPP_ALWAYS_INLINE
514 const char_type* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
515 {
516 return do_narrow(__low, __high, __dfault, __to);
517 }
518
519 static locale::id id;
520
521protected:
522 ~ctype();
523 virtual bool do_is(mask __m, char_type __c) const;
524 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
525 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
526 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
527 virtual char_type do_toupper(char_type) const;
528 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
529 virtual char_type do_tolower(char_type) const;
530 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
531 virtual char_type do_widen(char) const;
532 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
533 virtual char do_narrow(char_type, char __dfault) const;
534 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
535};
536
537template <>
Howard Hinnant83eade62013-03-06 23:30:19 +0000538class _LIBCPP_TYPE_VIS ctype<char>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000539 : public locale::facet, public ctype_base
540{
541 const mask* __tab_;
542 bool __del_;
543public:
544 typedef char char_type;
545
546 explicit ctype(const mask* __tab = 0, bool __del = false, size_t __refs = 0);
547
548 _LIBCPP_ALWAYS_INLINE
549 bool is(mask __m, char_type __c) const
550 {
Marshall Clow8a43fca2013-10-21 14:41:05 +0000551 return isascii(__c) ? (__tab_[static_cast<int>(__c)] & __m) !=0 : false;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000552 }
553
554 _LIBCPP_ALWAYS_INLINE
555 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
556 {
557 for (; __low != __high; ++__low, ++__vec)
Howard Hinnantec3773c2011-12-01 20:21:04 +0000558 *__vec = isascii(*__low) ? __tab_[static_cast<int>(*__low)] : 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000559 return __low;
560 }
561
562 _LIBCPP_ALWAYS_INLINE
563 const char_type* scan_is (mask __m, const char_type* __low, const char_type* __high) const
564 {
565 for (; __low != __high; ++__low)
Howard Hinnantec3773c2011-12-01 20:21:04 +0000566 if (isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000567 break;
568 return __low;
569 }
570
571 _LIBCPP_ALWAYS_INLINE
572 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
573 {
574 for (; __low != __high; ++__low)
Howard Hinnantec3773c2011-12-01 20:21:04 +0000575 if (!(isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000576 break;
577 return __low;
578 }
579
580 _LIBCPP_ALWAYS_INLINE
581 char_type toupper(char_type __c) const
582 {
583 return do_toupper(__c);
584 }
585
586 _LIBCPP_ALWAYS_INLINE
587 const char_type* toupper(char_type* __low, const char_type* __high) const
588 {
589 return do_toupper(__low, __high);
590 }
591
592 _LIBCPP_ALWAYS_INLINE
593 char_type tolower(char_type __c) const
594 {
595 return do_tolower(__c);
596 }
597
598 _LIBCPP_ALWAYS_INLINE
599 const char_type* tolower(char_type* __low, const char_type* __high) const
600 {
601 return do_tolower(__low, __high);
602 }
603
604 _LIBCPP_ALWAYS_INLINE
605 char_type widen(char __c) const
606 {
607 return do_widen(__c);
608 }
609
610 _LIBCPP_ALWAYS_INLINE
611 const char* widen(const char* __low, const char* __high, char_type* __to) const
612 {
613 return do_widen(__low, __high, __to);
614 }
615
616 _LIBCPP_ALWAYS_INLINE
617 char narrow(char_type __c, char __dfault) const
618 {
619 return do_narrow(__c, __dfault);
620 }
621
622 _LIBCPP_ALWAYS_INLINE
623 const char* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
624 {
625 return do_narrow(__low, __high, __dfault, __to);
626 }
627
628 static locale::id id;
629
Howard Hinnantadff4892010-05-24 17:49:41 +0000630#ifdef _CACHED_RUNES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000631 static const size_t table_size = _CACHED_RUNES;
Howard Hinnantadff4892010-05-24 17:49:41 +0000632#else
633 static const size_t table_size = 256; // FIXME: Don't hardcode this.
634#endif
Howard Hinnantc9834542011-05-31 15:34:58 +0000635 _LIBCPP_ALWAYS_INLINE const mask* table() const _NOEXCEPT {return __tab_;}
636 static const mask* classic_table() _NOEXCEPT;
Vasileios Kalintiris579b42b2015-11-09 10:21:04 +0000637#if defined(__GLIBC__) || defined(__EMSCRIPTEN__) || defined(_LIBCPP_HAS_MUSL_LIBC)
Sean Hunt62a6ac32011-07-09 00:56:23 +0000638 static const int* __classic_upper_table() _NOEXCEPT;
639 static const int* __classic_lower_table() _NOEXCEPT;
Sean Hunte59f7242011-07-09 01:09:31 +0000640#endif
Joerg Sonnenbergera71a9522013-05-17 21:17:34 +0000641#if defined(__NetBSD__)
642 static const short* __classic_upper_table() _NOEXCEPT;
643 static const short* __classic_lower_table() _NOEXCEPT;
644#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000645
646protected:
647 ~ctype();
648 virtual char_type do_toupper(char_type __c) const;
649 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
650 virtual char_type do_tolower(char_type __c) const;
651 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
652 virtual char_type do_widen(char __c) const;
653 virtual const char* do_widen(const char* __low, const char* __high, char_type* __to) const;
654 virtual char do_narrow(char_type __c, char __dfault) const;
655 virtual const char* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const;
656};
657
658// template <class CharT> class ctype_byname;
659
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000660template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY ctype_byname;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000661
662template <>
Howard Hinnant83eade62013-03-06 23:30:19 +0000663class _LIBCPP_TYPE_VIS ctype_byname<char>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000664 : public ctype<char>
665{
666 locale_t __l;
667
668public:
669 explicit ctype_byname(const char*, size_t = 0);
670 explicit ctype_byname(const string&, size_t = 0);
671
672protected:
673 ~ctype_byname();
674 virtual char_type do_toupper(char_type) const;
675 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
676 virtual char_type do_tolower(char_type) const;
677 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
678};
679
680template <>
Howard Hinnant83eade62013-03-06 23:30:19 +0000681class _LIBCPP_TYPE_VIS ctype_byname<wchar_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000682 : public ctype<wchar_t>
683{
684 locale_t __l;
685
686public:
687 explicit ctype_byname(const char*, size_t = 0);
688 explicit ctype_byname(const string&, size_t = 0);
689
690protected:
691 ~ctype_byname();
692 virtual bool do_is(mask __m, char_type __c) const;
693 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
694 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
695 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
696 virtual char_type do_toupper(char_type) const;
697 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
698 virtual char_type do_tolower(char_type) const;
699 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
700 virtual char_type do_widen(char) const;
701 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
702 virtual char do_narrow(char_type, char __dfault) const;
703 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
704};
705
706template <class _CharT>
707inline _LIBCPP_INLINE_VISIBILITY
708bool
709isspace(_CharT __c, const locale& __loc)
710{
711 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
712}
713
714template <class _CharT>
715inline _LIBCPP_INLINE_VISIBILITY
716bool
717isprint(_CharT __c, const locale& __loc)
718{
719 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c);
720}
721
722template <class _CharT>
723inline _LIBCPP_INLINE_VISIBILITY
724bool
725iscntrl(_CharT __c, const locale& __loc)
726{
727 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c);
728}
729
730template <class _CharT>
731inline _LIBCPP_INLINE_VISIBILITY
732bool
733isupper(_CharT __c, const locale& __loc)
734{
735 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c);
736}
737
738template <class _CharT>
739inline _LIBCPP_INLINE_VISIBILITY
740bool
741islower(_CharT __c, const locale& __loc)
742{
743 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c);
744}
745
746template <class _CharT>
747inline _LIBCPP_INLINE_VISIBILITY
748bool
749isalpha(_CharT __c, const locale& __loc)
750{
751 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c);
752}
753
754template <class _CharT>
755inline _LIBCPP_INLINE_VISIBILITY
756bool
757isdigit(_CharT __c, const locale& __loc)
758{
759 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c);
760}
761
762template <class _CharT>
763inline _LIBCPP_INLINE_VISIBILITY
764bool
765ispunct(_CharT __c, const locale& __loc)
766{
767 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c);
768}
769
770template <class _CharT>
771inline _LIBCPP_INLINE_VISIBILITY
772bool
773isxdigit(_CharT __c, const locale& __loc)
774{
775 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c);
776}
777
778template <class _CharT>
779inline _LIBCPP_INLINE_VISIBILITY
780bool
781isalnum(_CharT __c, const locale& __loc)
782{
783 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c);
784}
785
786template <class _CharT>
787inline _LIBCPP_INLINE_VISIBILITY
788bool
789isgraph(_CharT __c, const locale& __loc)
790{
791 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c);
792}
793
794template <class _CharT>
795inline _LIBCPP_INLINE_VISIBILITY
796_CharT
797toupper(_CharT __c, const locale& __loc)
798{
799 return use_facet<ctype<_CharT> >(__loc).toupper(__c);
800}
801
802template <class _CharT>
803inline _LIBCPP_INLINE_VISIBILITY
804_CharT
805tolower(_CharT __c, const locale& __loc)
806{
807 return use_facet<ctype<_CharT> >(__loc).tolower(__c);
808}
809
810// codecvt_base
811
Howard Hinnant83eade62013-03-06 23:30:19 +0000812class _LIBCPP_TYPE_VIS codecvt_base
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000813{
814public:
815 _LIBCPP_ALWAYS_INLINE codecvt_base() {}
816 enum result {ok, partial, error, noconv};
817};
818
819// template <class internT, class externT, class stateT> class codecvt;
820
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000821template <class _InternT, class _ExternT, class _StateT> class _LIBCPP_TYPE_VIS_ONLY codecvt;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000822
823// template <> class codecvt<char, char, mbstate_t>
824
825template <>
Howard Hinnant83eade62013-03-06 23:30:19 +0000826class _LIBCPP_TYPE_VIS codecvt<char, char, mbstate_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000827 : public locale::facet,
828 public codecvt_base
829{
830public:
831 typedef char intern_type;
832 typedef char extern_type;
833 typedef mbstate_t state_type;
834
835 _LIBCPP_ALWAYS_INLINE
836 explicit codecvt(size_t __refs = 0)
837 : locale::facet(__refs) {}
838
839 _LIBCPP_ALWAYS_INLINE
840 result out(state_type& __st,
841 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
842 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
843 {
844 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
845 }
846
847 _LIBCPP_ALWAYS_INLINE
848 result unshift(state_type& __st,
849 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
850 {
851 return do_unshift(__st, __to, __to_end, __to_nxt);
852 }
853
854 _LIBCPP_ALWAYS_INLINE
855 result in(state_type& __st,
856 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
857 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
858 {
859 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
860 }
861
862 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:58 +0000863 int encoding() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000864 {
865 return do_encoding();
866 }
867
868 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:58 +0000869 bool always_noconv() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000870 {
871 return do_always_noconv();
872 }
873
874 _LIBCPP_ALWAYS_INLINE
875 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
876 {
877 return do_length(__st, __frm, __end, __mx);
878 }
879
880 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:58 +0000881 int max_length() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000882 {
883 return do_max_length();
884 }
885
886 static locale::id id;
887
888protected:
889 _LIBCPP_ALWAYS_INLINE
890 explicit codecvt(const char*, size_t __refs = 0)
891 : locale::facet(__refs) {}
892
893 ~codecvt();
894
895 virtual result do_out(state_type& __st,
896 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
897 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
898 virtual result do_in(state_type& __st,
899 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
900 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
901 virtual result do_unshift(state_type& __st,
902 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnantc9834542011-05-31 15:34:58 +0000903 virtual int do_encoding() const _NOEXCEPT;
904 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000905 virtual int do_length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
Howard Hinnantc9834542011-05-31 15:34:58 +0000906 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000907};
908
909// template <> class codecvt<wchar_t, char, mbstate_t>
910
911template <>
Howard Hinnant83eade62013-03-06 23:30:19 +0000912class _LIBCPP_TYPE_VIS codecvt<wchar_t, char, mbstate_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000913 : public locale::facet,
914 public codecvt_base
915{
916 locale_t __l;
917public:
918 typedef wchar_t intern_type;
919 typedef char extern_type;
920 typedef mbstate_t state_type;
921
922 explicit codecvt(size_t __refs = 0);
923
924 _LIBCPP_ALWAYS_INLINE
925 result out(state_type& __st,
926 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
927 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
928 {
929 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
930 }
931
932 _LIBCPP_ALWAYS_INLINE
933 result unshift(state_type& __st,
934 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
935 {
936 return do_unshift(__st, __to, __to_end, __to_nxt);
937 }
938
939 _LIBCPP_ALWAYS_INLINE
940 result in(state_type& __st,
941 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
942 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
943 {
944 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
945 }
946
947 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:58 +0000948 int encoding() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000949 {
950 return do_encoding();
951 }
952
953 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:58 +0000954 bool always_noconv() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000955 {
956 return do_always_noconv();
957 }
958
959 _LIBCPP_ALWAYS_INLINE
960 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
961 {
962 return do_length(__st, __frm, __end, __mx);
963 }
964
965 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:58 +0000966 int max_length() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000967 {
968 return do_max_length();
969 }
970
971 static locale::id id;
972
973protected:
974 explicit codecvt(const char*, size_t __refs = 0);
975
976 ~codecvt();
977
978 virtual result do_out(state_type& __st,
979 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
980 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
981 virtual result do_in(state_type& __st,
982 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
983 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
984 virtual result do_unshift(state_type& __st,
985 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnantc9834542011-05-31 15:34:58 +0000986 virtual int do_encoding() const _NOEXCEPT;
987 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000988 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
Howard Hinnantc9834542011-05-31 15:34:58 +0000989 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000990};
991
992// template <> class codecvt<char16_t, char, mbstate_t>
993
994template <>
Howard Hinnant83eade62013-03-06 23:30:19 +0000995class _LIBCPP_TYPE_VIS codecvt<char16_t, char, mbstate_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000996 : public locale::facet,
997 public codecvt_base
998{
999public:
1000 typedef char16_t intern_type;
1001 typedef char extern_type;
1002 typedef mbstate_t state_type;
1003
1004 _LIBCPP_ALWAYS_INLINE
1005 explicit codecvt(size_t __refs = 0)
1006 : locale::facet(__refs) {}
1007
1008 _LIBCPP_ALWAYS_INLINE
1009 result out(state_type& __st,
1010 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1011 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1012 {
1013 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1014 }
1015
1016 _LIBCPP_ALWAYS_INLINE
1017 result unshift(state_type& __st,
1018 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1019 {
1020 return do_unshift(__st, __to, __to_end, __to_nxt);
1021 }
1022
1023 _LIBCPP_ALWAYS_INLINE
1024 result in(state_type& __st,
1025 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1026 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1027 {
1028 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1029 }
1030
1031 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:58 +00001032 int encoding() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001033 {
1034 return do_encoding();
1035 }
1036
1037 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:58 +00001038 bool always_noconv() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001039 {
1040 return do_always_noconv();
1041 }
1042
1043 _LIBCPP_ALWAYS_INLINE
1044 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1045 {
1046 return do_length(__st, __frm, __end, __mx);
1047 }
1048
1049 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:58 +00001050 int max_length() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001051 {
1052 return do_max_length();
1053 }
1054
1055 static locale::id id;
1056
1057protected:
1058 _LIBCPP_ALWAYS_INLINE
1059 explicit codecvt(const char*, size_t __refs = 0)
1060 : locale::facet(__refs) {}
1061
1062 ~codecvt();
1063
1064 virtual result do_out(state_type& __st,
1065 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1066 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1067 virtual result do_in(state_type& __st,
1068 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1069 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1070 virtual result do_unshift(state_type& __st,
1071 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnantc9834542011-05-31 15:34:58 +00001072 virtual int do_encoding() const _NOEXCEPT;
1073 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
Howard Hinnantc9834542011-05-31 15:34:58 +00001075 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001076};
1077
1078// template <> class codecvt<char32_t, char, mbstate_t>
1079
1080template <>
Howard Hinnant83eade62013-03-06 23:30:19 +00001081class _LIBCPP_TYPE_VIS codecvt<char32_t, char, mbstate_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001082 : public locale::facet,
1083 public codecvt_base
1084{
1085public:
1086 typedef char32_t intern_type;
1087 typedef char extern_type;
1088 typedef mbstate_t state_type;
1089
1090 _LIBCPP_ALWAYS_INLINE
1091 explicit codecvt(size_t __refs = 0)
1092 : locale::facet(__refs) {}
1093
1094 _LIBCPP_ALWAYS_INLINE
1095 result out(state_type& __st,
1096 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1097 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1098 {
1099 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1100 }
1101
1102 _LIBCPP_ALWAYS_INLINE
1103 result unshift(state_type& __st,
1104 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1105 {
1106 return do_unshift(__st, __to, __to_end, __to_nxt);
1107 }
1108
1109 _LIBCPP_ALWAYS_INLINE
1110 result in(state_type& __st,
1111 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1112 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1113 {
1114 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1115 }
1116
1117 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:58 +00001118 int encoding() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001119 {
1120 return do_encoding();
1121 }
1122
1123 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:58 +00001124 bool always_noconv() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001125 {
1126 return do_always_noconv();
1127 }
1128
1129 _LIBCPP_ALWAYS_INLINE
1130 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1131 {
1132 return do_length(__st, __frm, __end, __mx);
1133 }
1134
1135 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:58 +00001136 int max_length() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001137 {
1138 return do_max_length();
1139 }
1140
1141 static locale::id id;
1142
1143protected:
1144 _LIBCPP_ALWAYS_INLINE
1145 explicit codecvt(const char*, size_t __refs = 0)
1146 : locale::facet(__refs) {}
1147
1148 ~codecvt();
1149
1150 virtual result do_out(state_type& __st,
1151 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1152 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1153 virtual result do_in(state_type& __st,
1154 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1155 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1156 virtual result do_unshift(state_type& __st,
1157 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnantc9834542011-05-31 15:34:58 +00001158 virtual int do_encoding() const _NOEXCEPT;
1159 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001160 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
Howard Hinnantc9834542011-05-31 15:34:58 +00001161 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001162};
1163
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001164// template <class _InternT, class _ExternT, class _StateT> class codecvt_byname
1165
1166template <class _InternT, class _ExternT, class _StateT>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001167class _LIBCPP_TYPE_VIS_ONLY codecvt_byname
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001168 : public codecvt<_InternT, _ExternT, _StateT>
1169{
1170public:
Howard Hinnantb0be42b2010-09-21 18:58:51 +00001171 _LIBCPP_ALWAYS_INLINE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001172 explicit codecvt_byname(const char* __nm, size_t __refs = 0)
1173 : codecvt<_InternT, _ExternT, _StateT>(__nm, __refs) {}
Howard Hinnantb0be42b2010-09-21 18:58:51 +00001174 _LIBCPP_ALWAYS_INLINE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001175 explicit codecvt_byname(const string& __nm, size_t __refs = 0)
1176 : codecvt<_InternT, _ExternT, _StateT>(__nm.c_str(), __refs) {}
1177protected:
1178 ~codecvt_byname();
1179};
1180
1181template <class _InternT, class _ExternT, class _StateT>
1182codecvt_byname<_InternT, _ExternT, _StateT>::~codecvt_byname()
1183{
1184}
1185
Howard Hinnant499cea12013-08-23 17:37:05 +00001186_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char, char, mbstate_t>)
1187_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<wchar_t, char, mbstate_t>)
1188_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char16_t, char, mbstate_t>)
1189_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char32_t, char, mbstate_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001190
Howard Hinnant83eade62013-03-06 23:30:19 +00001191_LIBCPP_FUNC_VIS void __throw_runtime_error(const char*);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001192
Howard Hinnant99968442011-11-29 18:15:50 +00001193template <size_t _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001194struct __narrow_to_utf8
1195{
1196 template <class _OutputIterator, class _CharT>
1197 _OutputIterator
1198 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const;
1199};
1200
1201template <>
1202struct __narrow_to_utf8<8>
1203{
1204 template <class _OutputIterator, class _CharT>
1205 _LIBCPP_ALWAYS_INLINE
1206 _OutputIterator
1207 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1208 {
1209 for (; __wb < __we; ++__wb, ++__s)
1210 *__s = *__wb;
1211 return __s;
1212 }
1213};
1214
1215template <>
1216struct __narrow_to_utf8<16>
1217 : public codecvt<char16_t, char, mbstate_t>
1218{
1219 _LIBCPP_ALWAYS_INLINE
1220 __narrow_to_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1221
1222 ~__narrow_to_utf8();
1223
1224 template <class _OutputIterator, class _CharT>
1225 _LIBCPP_ALWAYS_INLINE
1226 _OutputIterator
1227 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1228 {
1229 result __r = ok;
1230 mbstate_t __mb;
1231 while (__wb < __we && __r != error)
1232 {
1233 const int __sz = 32;
1234 char __buf[__sz];
1235 char* __bn;
1236 const char16_t* __wn = (const char16_t*)__wb;
1237 __r = do_out(__mb, (const char16_t*)__wb, (const char16_t*)__we, __wn,
1238 __buf, __buf+__sz, __bn);
1239 if (__r == codecvt_base::error || __wn == (const char16_t*)__wb)
1240 __throw_runtime_error("locale not supported");
1241 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1242 *__s = *__p;
1243 __wb = (const _CharT*)__wn;
1244 }
1245 return __s;
1246 }
1247};
1248
1249template <>
1250struct __narrow_to_utf8<32>
1251 : public codecvt<char32_t, char, mbstate_t>
1252{
1253 _LIBCPP_ALWAYS_INLINE
1254 __narrow_to_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1255
1256 ~__narrow_to_utf8();
1257
1258 template <class _OutputIterator, class _CharT>
1259 _LIBCPP_ALWAYS_INLINE
1260 _OutputIterator
1261 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1262 {
1263 result __r = ok;
1264 mbstate_t __mb;
1265 while (__wb < __we && __r != error)
1266 {
1267 const int __sz = 32;
1268 char __buf[__sz];
1269 char* __bn;
1270 const char32_t* __wn = (const char32_t*)__wb;
1271 __r = do_out(__mb, (const char32_t*)__wb, (const char32_t*)__we, __wn,
1272 __buf, __buf+__sz, __bn);
1273 if (__r == codecvt_base::error || __wn == (const char32_t*)__wb)
1274 __throw_runtime_error("locale not supported");
1275 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1276 *__s = *__p;
1277 __wb = (const _CharT*)__wn;
1278 }
1279 return __s;
1280 }
1281};
1282
Howard Hinnant99968442011-11-29 18:15:50 +00001283template <size_t _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001284struct __widen_from_utf8
1285{
1286 template <class _OutputIterator>
1287 _OutputIterator
1288 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const;
1289};
1290
1291template <>
1292struct __widen_from_utf8<8>
1293{
1294 template <class _OutputIterator>
1295 _LIBCPP_ALWAYS_INLINE
1296 _OutputIterator
1297 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1298 {
1299 for (; __nb < __ne; ++__nb, ++__s)
1300 *__s = *__nb;
1301 return __s;
1302 }
1303};
1304
1305template <>
1306struct __widen_from_utf8<16>
1307 : public codecvt<char16_t, char, mbstate_t>
1308{
1309 _LIBCPP_ALWAYS_INLINE
1310 __widen_from_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1311
1312 ~__widen_from_utf8();
1313
1314 template <class _OutputIterator>
1315 _LIBCPP_ALWAYS_INLINE
1316 _OutputIterator
1317 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1318 {
1319 result __r = ok;
1320 mbstate_t __mb;
1321 while (__nb < __ne && __r != error)
1322 {
1323 const int __sz = 32;
1324 char16_t __buf[__sz];
1325 char16_t* __bn;
1326 const char* __nn = __nb;
1327 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1328 __buf, __buf+__sz, __bn);
1329 if (__r == codecvt_base::error || __nn == __nb)
1330 __throw_runtime_error("locale not supported");
1331 for (const char16_t* __p = __buf; __p < __bn; ++__p, ++__s)
1332 *__s = (wchar_t)*__p;
1333 __nb = __nn;
1334 }
1335 return __s;
1336 }
1337};
1338
1339template <>
1340struct __widen_from_utf8<32>
1341 : public codecvt<char32_t, char, mbstate_t>
1342{
1343 _LIBCPP_ALWAYS_INLINE
1344 __widen_from_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1345
1346 ~__widen_from_utf8();
1347
1348 template <class _OutputIterator>
1349 _LIBCPP_ALWAYS_INLINE
1350 _OutputIterator
1351 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1352 {
1353 result __r = ok;
1354 mbstate_t __mb;
1355 while (__nb < __ne && __r != error)
1356 {
1357 const int __sz = 32;
1358 char32_t __buf[__sz];
1359 char32_t* __bn;
1360 const char* __nn = __nb;
1361 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1362 __buf, __buf+__sz, __bn);
1363 if (__r == codecvt_base::error || __nn == __nb)
1364 __throw_runtime_error("locale not supported");
1365 for (const char32_t* __p = __buf; __p < __bn; ++__p, ++__s)
1366 *__s = (wchar_t)*__p;
1367 __nb = __nn;
1368 }
1369 return __s;
1370 }
1371};
1372
1373// template <class charT> class numpunct
1374
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001375template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY numpunct;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001376
1377template <>
Howard Hinnant83eade62013-03-06 23:30:19 +00001378class _LIBCPP_TYPE_VIS numpunct<char>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001379 : public locale::facet
1380{
1381public:
1382 typedef char char_type;
1383 typedef basic_string<char_type> string_type;
1384
1385 explicit numpunct(size_t __refs = 0);
1386
1387 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
1388 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
1389 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
1390 _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();}
1391 _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();}
1392
1393 static locale::id id;
1394
1395protected:
1396 ~numpunct();
1397 virtual char_type do_decimal_point() const;
1398 virtual char_type do_thousands_sep() const;
1399 virtual string do_grouping() const;
1400 virtual string_type do_truename() const;
1401 virtual string_type do_falsename() const;
1402
1403 char_type __decimal_point_;
1404 char_type __thousands_sep_;
1405 string __grouping_;
1406};
1407
1408template <>
Howard Hinnant83eade62013-03-06 23:30:19 +00001409class _LIBCPP_TYPE_VIS numpunct<wchar_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001410 : public locale::facet
1411{
1412public:
1413 typedef wchar_t char_type;
1414 typedef basic_string<char_type> string_type;
1415
1416 explicit numpunct(size_t __refs = 0);
1417
1418 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
1419 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
1420 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
1421 _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();}
1422 _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();}
1423
1424 static locale::id id;
1425
1426protected:
1427 ~numpunct();
1428 virtual char_type do_decimal_point() const;
1429 virtual char_type do_thousands_sep() const;
1430 virtual string do_grouping() const;
1431 virtual string_type do_truename() const;
1432 virtual string_type do_falsename() const;
1433
1434 char_type __decimal_point_;
1435 char_type __thousands_sep_;
1436 string __grouping_;
1437};
1438
1439// template <class charT> class numpunct_byname
1440
Marshall Clowe4220212015-01-11 06:15:59 +00001441template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY numpunct_byname;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001442
1443template <>
Howard Hinnant83eade62013-03-06 23:30:19 +00001444class _LIBCPP_TYPE_VIS numpunct_byname<char>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001445: public numpunct<char>
1446{
1447public:
1448 typedef char char_type;
1449 typedef basic_string<char_type> string_type;
1450
1451 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1452 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1453
1454protected:
1455 ~numpunct_byname();
1456
1457private:
1458 void __init(const char*);
1459};
1460
1461template <>
Howard Hinnant83eade62013-03-06 23:30:19 +00001462class _LIBCPP_TYPE_VIS numpunct_byname<wchar_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001463: public numpunct<wchar_t>
1464{
1465public:
1466 typedef wchar_t char_type;
1467 typedef basic_string<char_type> string_type;
1468
1469 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1470 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1471
1472protected:
1473 ~numpunct_byname();
1474
1475private:
1476 void __init(const char*);
1477};
1478
1479_LIBCPP_END_NAMESPACE_STD
1480
1481#endif // _LIBCPP___LOCALE