blob: 7bc701dda6e3c7ccf68f823b58f042e2aba4f019 [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
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 Hinnant7c9e5732011-05-31 15:34:58 +0000118 template <class _Facet> friend bool has_facet(const locale&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000119 template <class _Facet> friend const _Facet& use_facet(const locale&);
120};
121
Howard Hinnant8331b762013-03-06 23:30:19 +0000122class _LIBCPP_TYPE_VIS locale::facet
Howard Hinnantc51e1022010-05-11 19:42:16 +0000123 : public __shared_count
124{
125protected:
Howard Hinnant9833cad2010-09-21 18:58:51 +0000126 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Hinnant719bda32011-05-28 14:41:13 +0000135 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000136};
137
Howard Hinnant8331b762013-03-06 23:30:19 +0000138class _LIBCPP_TYPE_VIS locale::id
Howard Hinnantc51e1022010-05-11 19:42:16 +0000139{
140 once_flag __flag_;
141 int32_t __id_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000142
Howard Hinnantc51e1022010-05-11 19:42:16 +0000143 static int32_t __next_id;
144public:
Howard Hinnantac7d9f02012-07-26 16:14:37 +0000145 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR id() :__id_(0) {}
Howard Hinnantc51e1022010-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 Hinnant72f73582010-08-11 17:04:31 +0000168#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000169 if (!_VSTD::has_facet<_Facet>(__other))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000170 throw runtime_error("locale::combine: locale missing facet");
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000171#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000172 return locale(*this, &const_cast<_Facet&>(_VSTD::use_facet<_Facet>(__other)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000173}
174
175template <class _Facet>
176inline _LIBCPP_INLINE_VISIBILITY
177bool
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000178has_facet(const locale& __l) _NOEXCEPT
Howard Hinnantc51e1022010-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 Hinnanta37d3cf2013-08-12 18:38:34 +0000194class _LIBCPP_TYPE_VIS_ONLY collate
Howard Hinnantc51e1022010-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 Hinnant9833cad2010-09-21 18:58:51 +0000201 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000202 explicit collate(size_t __refs = 0)
203 : locale::facet(__refs) {}
204
Howard Hinnant9833cad2010-09-21 18:58:51 +0000205 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Hinnant9833cad2010-09-21 18:58:51 +0000212 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Hinnant9833cad2010-09-21 18:58:51 +0000218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-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 Hinnant8c2bf6b2011-10-11 16:00:46 +0000259collate<_CharT>::do_hash(const char_type* __lo, const char_type* __hi) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000260{
Howard Hinnant8c2bf6b2011-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 Hinnantc51e1022010-05-11 19:42:16 +0000265 {
Howard Hinnant28b24882011-12-01 20:21:04 +0000266 __h = (__h << 4) + static_cast<size_t>(*__p);
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000267 size_t __g = __h & __mask;
268 __h ^= __g | (__g >> __sr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000269 }
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000270 return static_cast<long>(__h);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000271}
272
Howard Hinnant8ea98242013-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 Hinnantc51e1022010-05-11 19:42:16 +0000275
276// template <class CharT> class collate_byname;
277
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000278template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY collate_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000279
280template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000281class _LIBCPP_TYPE_VIS collate_byname<char>
Howard Hinnantc51e1022010-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 Hinnant8331b762013-03-06 23:30:19 +0000300class _LIBCPP_TYPE_VIS collate_byname<wchar_t>
Howard Hinnantc51e1022010-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 Hinnantb1ad5a82011-06-30 21:18:19 +0000324 return _VSTD::use_facet<_VSTD::collate<_CharT> >(*this).compare(
Howard Hinnantc51e1022010-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 Hinnant8331b762013-03-06 23:30:19 +0000331class _LIBCPP_TYPE_VIS ctype_base
Howard Hinnant9833cad2010-09-21 18:58:51 +0000332{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000333public:
Vasileios Kalintiris281b4cf2015-11-09 10:21:04 +0000334#if defined(__GLIBC__)
Alexis Hunt92b0c812011-07-09 00:56:23 +0000335 typedef unsigned short mask;
Howard Hinnant155c2af2010-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 Clow1f257322013-03-18 17:04:29 +0000346#elif defined(_WIN32)
Howard Hinnantd7a78632011-09-29 13:33:15 +0000347 typedef unsigned short mask;
Howard Hinnantdd0d7022011-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 Roelofsae9ab252015-03-11 17:00:28 +0000358# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
Dan Albertebdf28b2015-03-11 00:51:06 +0000359#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__)
JF Bastien0c265d82015-02-25 22:16:46 +0000360# ifdef __APPLE__
David Chisnall1d581062011-09-21 08:39:44 +0000361 typedef __uint32_t mask;
JF Bastien0c265d82015-02-25 22:16:46 +0000362# elif defined(__FreeBSD__)
David Chisnall1d581062011-09-21 08:39:44 +0000363 typedef unsigned long mask;
Vasileios Kalintirisc5dd8942015-11-24 10:24:54 +0000364# elif defined(__EMSCRIPTEN__) || defined(__NetBSD__)
Howard Hinnant942dbd22013-03-29 18:27:28 +0000365 typedef unsigned short mask;
JF Bastien0c265d82015-02-25 22:16:46 +0000366# endif
David Chisnall1d581062011-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 Albert2dca4072014-07-23 19:32:03 +0000376
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000377# if defined(__NetBSD__)
378 static const mask blank = _CTYPE_BL;
379# else
David Chisnall1d581062011-09-21 08:39:44 +0000380 static const mask blank = _CTYPE_B;
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000381# endif
Howard Hinnanta47505d2013-08-30 14:42:39 +0000382#elif defined(__sun__) || defined(_AIX)
David Chisnall8074c342012-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 Bastien0c265d82015-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 Roelofsae9ab252015-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 Bastien0c265d82015-02-25 22:16:46 +0000410#else
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000411 typedef unsigned long mask;
412 static const mask space = 1<<0;
413 static const mask print = 1<<1;
414 static const mask cntrl = 1<<2;
415 static const mask upper = 1<<3;
416 static const mask lower = 1<<4;
417 static const mask alpha = 1<<5;
418 static const mask digit = 1<<6;
419 static const mask punct = 1<<7;
420 static const mask xdigit = 1<<8;
421 static const mask blank = 1<<9;
JF Bastien0c265d82015-02-25 22:16:46 +0000422#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423 static const mask alnum = alpha | digit;
424 static const mask graph = alnum | punct;
425
426 _LIBCPP_ALWAYS_INLINE ctype_base() {}
427};
428
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000429template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY ctype;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000430
431template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000432class _LIBCPP_TYPE_VIS ctype<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000433 : public locale::facet,
434 public ctype_base
435{
436public:
437 typedef wchar_t char_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000438
Howard Hinnantc51e1022010-05-11 19:42:16 +0000439 _LIBCPP_ALWAYS_INLINE
440 explicit ctype(size_t __refs = 0)
441 : locale::facet(__refs) {}
442
443 _LIBCPP_ALWAYS_INLINE
444 bool is(mask __m, char_type __c) const
445 {
446 return do_is(__m, __c);
447 }
448
449 _LIBCPP_ALWAYS_INLINE
450 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
451 {
452 return do_is(__low, __high, __vec);
453 }
454
455 _LIBCPP_ALWAYS_INLINE
456 const char_type* scan_is(mask __m, const char_type* __low, const char_type* __high) const
457 {
458 return do_scan_is(__m, __low, __high);
459 }
460
461 _LIBCPP_ALWAYS_INLINE
462 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
463 {
464 return do_scan_not(__m, __low, __high);
465 }
466
467 _LIBCPP_ALWAYS_INLINE
468 char_type toupper(char_type __c) const
469 {
470 return do_toupper(__c);
471 }
472
473 _LIBCPP_ALWAYS_INLINE
474 const char_type* toupper(char_type* __low, const char_type* __high) const
475 {
476 return do_toupper(__low, __high);
477 }
478
479 _LIBCPP_ALWAYS_INLINE
480 char_type tolower(char_type __c) const
481 {
482 return do_tolower(__c);
483 }
484
485 _LIBCPP_ALWAYS_INLINE
486 const char_type* tolower(char_type* __low, const char_type* __high) const
487 {
488 return do_tolower(__low, __high);
489 }
490
491 _LIBCPP_ALWAYS_INLINE
492 char_type widen(char __c) const
493 {
494 return do_widen(__c);
495 }
496
497 _LIBCPP_ALWAYS_INLINE
498 const char* widen(const char* __low, const char* __high, char_type* __to) const
499 {
500 return do_widen(__low, __high, __to);
501 }
502
503 _LIBCPP_ALWAYS_INLINE
504 char narrow(char_type __c, char __dfault) const
505 {
506 return do_narrow(__c, __dfault);
507 }
508
509 _LIBCPP_ALWAYS_INLINE
510 const char_type* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
511 {
512 return do_narrow(__low, __high, __dfault, __to);
513 }
514
515 static locale::id id;
516
517protected:
518 ~ctype();
519 virtual bool do_is(mask __m, char_type __c) const;
520 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
521 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
522 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
523 virtual char_type do_toupper(char_type) const;
524 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
525 virtual char_type do_tolower(char_type) const;
526 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
527 virtual char_type do_widen(char) const;
528 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
529 virtual char do_narrow(char_type, char __dfault) const;
530 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
531};
532
533template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000534class _LIBCPP_TYPE_VIS ctype<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000535 : public locale::facet, public ctype_base
536{
537 const mask* __tab_;
538 bool __del_;
539public:
540 typedef char char_type;
541
542 explicit ctype(const mask* __tab = 0, bool __del = false, size_t __refs = 0);
543
544 _LIBCPP_ALWAYS_INLINE
545 bool is(mask __m, char_type __c) const
546 {
Marshall Clow11de4872013-10-21 14:41:05 +0000547 return isascii(__c) ? (__tab_[static_cast<int>(__c)] & __m) !=0 : false;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000548 }
549
550 _LIBCPP_ALWAYS_INLINE
551 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
552 {
553 for (; __low != __high; ++__low, ++__vec)
Howard Hinnant28b24882011-12-01 20:21:04 +0000554 *__vec = isascii(*__low) ? __tab_[static_cast<int>(*__low)] : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000555 return __low;
556 }
557
558 _LIBCPP_ALWAYS_INLINE
559 const char_type* scan_is (mask __m, const char_type* __low, const char_type* __high) const
560 {
561 for (; __low != __high; ++__low)
Howard Hinnant28b24882011-12-01 20:21:04 +0000562 if (isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000563 break;
564 return __low;
565 }
566
567 _LIBCPP_ALWAYS_INLINE
568 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
569 {
570 for (; __low != __high; ++__low)
Howard Hinnant28b24882011-12-01 20:21:04 +0000571 if (!(isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m)))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000572 break;
573 return __low;
574 }
575
576 _LIBCPP_ALWAYS_INLINE
577 char_type toupper(char_type __c) const
578 {
579 return do_toupper(__c);
580 }
581
582 _LIBCPP_ALWAYS_INLINE
583 const char_type* toupper(char_type* __low, const char_type* __high) const
584 {
585 return do_toupper(__low, __high);
586 }
587
588 _LIBCPP_ALWAYS_INLINE
589 char_type tolower(char_type __c) const
590 {
591 return do_tolower(__c);
592 }
593
594 _LIBCPP_ALWAYS_INLINE
595 const char_type* tolower(char_type* __low, const char_type* __high) const
596 {
597 return do_tolower(__low, __high);
598 }
599
600 _LIBCPP_ALWAYS_INLINE
601 char_type widen(char __c) const
602 {
603 return do_widen(__c);
604 }
605
606 _LIBCPP_ALWAYS_INLINE
607 const char* widen(const char* __low, const char* __high, char_type* __to) const
608 {
609 return do_widen(__low, __high, __to);
610 }
611
612 _LIBCPP_ALWAYS_INLINE
613 char narrow(char_type __c, char __dfault) const
614 {
615 return do_narrow(__c, __dfault);
616 }
617
618 _LIBCPP_ALWAYS_INLINE
619 const char* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
620 {
621 return do_narrow(__low, __high, __dfault, __to);
622 }
623
624 static locale::id id;
625
Howard Hinnant155c2af2010-05-24 17:49:41 +0000626#ifdef _CACHED_RUNES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000627 static const size_t table_size = _CACHED_RUNES;
Howard Hinnant155c2af2010-05-24 17:49:41 +0000628#else
629 static const size_t table_size = 256; // FIXME: Don't hardcode this.
630#endif
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000631 _LIBCPP_ALWAYS_INLINE const mask* table() const _NOEXCEPT {return __tab_;}
632 static const mask* classic_table() _NOEXCEPT;
Vasileios Kalintirisc5dd8942015-11-24 10:24:54 +0000633#if defined(__GLIBC__) || defined(__EMSCRIPTEN__)
Alexis Hunt92b0c812011-07-09 00:56:23 +0000634 static const int* __classic_upper_table() _NOEXCEPT;
635 static const int* __classic_lower_table() _NOEXCEPT;
Alexis Hunt5a4dd562011-07-09 01:09:31 +0000636#endif
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000637#if defined(__NetBSD__)
638 static const short* __classic_upper_table() _NOEXCEPT;
639 static const short* __classic_lower_table() _NOEXCEPT;
640#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000641
642protected:
643 ~ctype();
644 virtual char_type do_toupper(char_type __c) const;
645 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
646 virtual char_type do_tolower(char_type __c) const;
647 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
648 virtual char_type do_widen(char __c) const;
649 virtual const char* do_widen(const char* __low, const char* __high, char_type* __to) const;
650 virtual char do_narrow(char_type __c, char __dfault) const;
651 virtual const char* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const;
652};
653
654// template <class CharT> class ctype_byname;
655
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000656template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY ctype_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000657
658template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000659class _LIBCPP_TYPE_VIS ctype_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000660 : public ctype<char>
661{
662 locale_t __l;
663
664public:
665 explicit ctype_byname(const char*, size_t = 0);
666 explicit ctype_byname(const string&, size_t = 0);
667
668protected:
669 ~ctype_byname();
670 virtual char_type do_toupper(char_type) const;
671 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
672 virtual char_type do_tolower(char_type) const;
673 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
674};
675
676template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000677class _LIBCPP_TYPE_VIS ctype_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000678 : public ctype<wchar_t>
679{
680 locale_t __l;
681
682public:
683 explicit ctype_byname(const char*, size_t = 0);
684 explicit ctype_byname(const string&, size_t = 0);
685
686protected:
687 ~ctype_byname();
688 virtual bool do_is(mask __m, char_type __c) const;
689 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
690 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
691 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
692 virtual char_type do_toupper(char_type) const;
693 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
694 virtual char_type do_tolower(char_type) const;
695 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
696 virtual char_type do_widen(char) const;
697 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
698 virtual char do_narrow(char_type, char __dfault) const;
699 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
700};
701
702template <class _CharT>
703inline _LIBCPP_INLINE_VISIBILITY
704bool
705isspace(_CharT __c, const locale& __loc)
706{
707 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
708}
709
710template <class _CharT>
711inline _LIBCPP_INLINE_VISIBILITY
712bool
713isprint(_CharT __c, const locale& __loc)
714{
715 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c);
716}
717
718template <class _CharT>
719inline _LIBCPP_INLINE_VISIBILITY
720bool
721iscntrl(_CharT __c, const locale& __loc)
722{
723 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c);
724}
725
726template <class _CharT>
727inline _LIBCPP_INLINE_VISIBILITY
728bool
729isupper(_CharT __c, const locale& __loc)
730{
731 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c);
732}
733
734template <class _CharT>
735inline _LIBCPP_INLINE_VISIBILITY
736bool
737islower(_CharT __c, const locale& __loc)
738{
739 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c);
740}
741
742template <class _CharT>
743inline _LIBCPP_INLINE_VISIBILITY
744bool
745isalpha(_CharT __c, const locale& __loc)
746{
747 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c);
748}
749
750template <class _CharT>
751inline _LIBCPP_INLINE_VISIBILITY
752bool
753isdigit(_CharT __c, const locale& __loc)
754{
755 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c);
756}
757
758template <class _CharT>
759inline _LIBCPP_INLINE_VISIBILITY
760bool
761ispunct(_CharT __c, const locale& __loc)
762{
763 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c);
764}
765
766template <class _CharT>
767inline _LIBCPP_INLINE_VISIBILITY
768bool
769isxdigit(_CharT __c, const locale& __loc)
770{
771 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c);
772}
773
774template <class _CharT>
775inline _LIBCPP_INLINE_VISIBILITY
776bool
777isalnum(_CharT __c, const locale& __loc)
778{
779 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c);
780}
781
782template <class _CharT>
783inline _LIBCPP_INLINE_VISIBILITY
784bool
785isgraph(_CharT __c, const locale& __loc)
786{
787 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c);
788}
789
790template <class _CharT>
791inline _LIBCPP_INLINE_VISIBILITY
792_CharT
793toupper(_CharT __c, const locale& __loc)
794{
795 return use_facet<ctype<_CharT> >(__loc).toupper(__c);
796}
797
798template <class _CharT>
799inline _LIBCPP_INLINE_VISIBILITY
800_CharT
801tolower(_CharT __c, const locale& __loc)
802{
803 return use_facet<ctype<_CharT> >(__loc).tolower(__c);
804}
805
806// codecvt_base
807
Howard Hinnant8331b762013-03-06 23:30:19 +0000808class _LIBCPP_TYPE_VIS codecvt_base
Howard Hinnantc51e1022010-05-11 19:42:16 +0000809{
810public:
811 _LIBCPP_ALWAYS_INLINE codecvt_base() {}
812 enum result {ok, partial, error, noconv};
813};
814
815// template <class internT, class externT, class stateT> class codecvt;
816
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000817template <class _InternT, class _ExternT, class _StateT> class _LIBCPP_TYPE_VIS_ONLY codecvt;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000818
819// template <> class codecvt<char, char, mbstate_t>
820
821template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000822class _LIBCPP_TYPE_VIS codecvt<char, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000823 : public locale::facet,
824 public codecvt_base
825{
826public:
827 typedef char intern_type;
828 typedef char extern_type;
829 typedef mbstate_t state_type;
830
831 _LIBCPP_ALWAYS_INLINE
832 explicit codecvt(size_t __refs = 0)
833 : locale::facet(__refs) {}
834
835 _LIBCPP_ALWAYS_INLINE
836 result out(state_type& __st,
837 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
838 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
839 {
840 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
841 }
842
843 _LIBCPP_ALWAYS_INLINE
844 result unshift(state_type& __st,
845 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
846 {
847 return do_unshift(__st, __to, __to_end, __to_nxt);
848 }
849
850 _LIBCPP_ALWAYS_INLINE
851 result in(state_type& __st,
852 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
853 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
854 {
855 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
856 }
857
858 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000859 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000860 {
861 return do_encoding();
862 }
863
864 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000865 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000866 {
867 return do_always_noconv();
868 }
869
870 _LIBCPP_ALWAYS_INLINE
871 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
872 {
873 return do_length(__st, __frm, __end, __mx);
874 }
875
876 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000877 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000878 {
879 return do_max_length();
880 }
881
882 static locale::id id;
883
884protected:
885 _LIBCPP_ALWAYS_INLINE
886 explicit codecvt(const char*, size_t __refs = 0)
887 : locale::facet(__refs) {}
888
889 ~codecvt();
890
891 virtual result do_out(state_type& __st,
892 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
893 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
894 virtual result do_in(state_type& __st,
895 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
896 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
897 virtual result do_unshift(state_type& __st,
898 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000899 virtual int do_encoding() const _NOEXCEPT;
900 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000901 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 +0000902 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000903};
904
905// template <> class codecvt<wchar_t, char, mbstate_t>
906
907template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000908class _LIBCPP_TYPE_VIS codecvt<wchar_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000909 : public locale::facet,
910 public codecvt_base
911{
912 locale_t __l;
913public:
914 typedef wchar_t intern_type;
915 typedef char extern_type;
916 typedef mbstate_t state_type;
917
918 explicit codecvt(size_t __refs = 0);
919
920 _LIBCPP_ALWAYS_INLINE
921 result out(state_type& __st,
922 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
923 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
924 {
925 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
926 }
927
928 _LIBCPP_ALWAYS_INLINE
929 result unshift(state_type& __st,
930 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
931 {
932 return do_unshift(__st, __to, __to_end, __to_nxt);
933 }
934
935 _LIBCPP_ALWAYS_INLINE
936 result in(state_type& __st,
937 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
938 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
939 {
940 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
941 }
942
943 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000944 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945 {
946 return do_encoding();
947 }
948
949 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000950 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000951 {
952 return do_always_noconv();
953 }
954
955 _LIBCPP_ALWAYS_INLINE
956 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
957 {
958 return do_length(__st, __frm, __end, __mx);
959 }
960
961 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000962 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000963 {
964 return do_max_length();
965 }
966
967 static locale::id id;
968
969protected:
970 explicit codecvt(const char*, size_t __refs = 0);
971
972 ~codecvt();
973
974 virtual result do_out(state_type& __st,
975 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
976 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
977 virtual result do_in(state_type& __st,
978 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
979 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
980 virtual result do_unshift(state_type& __st,
981 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000982 virtual int do_encoding() const _NOEXCEPT;
983 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000984 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 +0000985 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986};
987
988// template <> class codecvt<char16_t, char, mbstate_t>
989
990template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000991class _LIBCPP_TYPE_VIS codecvt<char16_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000992 : public locale::facet,
993 public codecvt_base
994{
995public:
996 typedef char16_t intern_type;
997 typedef char extern_type;
998 typedef mbstate_t state_type;
999
1000 _LIBCPP_ALWAYS_INLINE
1001 explicit codecvt(size_t __refs = 0)
1002 : locale::facet(__refs) {}
1003
1004 _LIBCPP_ALWAYS_INLINE
1005 result out(state_type& __st,
1006 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1007 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1008 {
1009 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1010 }
1011
1012 _LIBCPP_ALWAYS_INLINE
1013 result unshift(state_type& __st,
1014 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1015 {
1016 return do_unshift(__st, __to, __to_end, __to_nxt);
1017 }
1018
1019 _LIBCPP_ALWAYS_INLINE
1020 result in(state_type& __st,
1021 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1022 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1023 {
1024 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1025 }
1026
1027 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001028 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029 {
1030 return do_encoding();
1031 }
1032
1033 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001034 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035 {
1036 return do_always_noconv();
1037 }
1038
1039 _LIBCPP_ALWAYS_INLINE
1040 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1041 {
1042 return do_length(__st, __frm, __end, __mx);
1043 }
1044
1045 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001046 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001047 {
1048 return do_max_length();
1049 }
1050
1051 static locale::id id;
1052
1053protected:
1054 _LIBCPP_ALWAYS_INLINE
1055 explicit codecvt(const char*, size_t __refs = 0)
1056 : locale::facet(__refs) {}
1057
1058 ~codecvt();
1059
1060 virtual result do_out(state_type& __st,
1061 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1062 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1063 virtual result do_in(state_type& __st,
1064 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1065 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1066 virtual result do_unshift(state_type& __st,
1067 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001068 virtual int do_encoding() const _NOEXCEPT;
1069 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001070 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 +00001071 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072};
1073
1074// template <> class codecvt<char32_t, char, mbstate_t>
1075
1076template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001077class _LIBCPP_TYPE_VIS codecvt<char32_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078 : public locale::facet,
1079 public codecvt_base
1080{
1081public:
1082 typedef char32_t intern_type;
1083 typedef char extern_type;
1084 typedef mbstate_t state_type;
1085
1086 _LIBCPP_ALWAYS_INLINE
1087 explicit codecvt(size_t __refs = 0)
1088 : locale::facet(__refs) {}
1089
1090 _LIBCPP_ALWAYS_INLINE
1091 result out(state_type& __st,
1092 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1093 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1094 {
1095 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1096 }
1097
1098 _LIBCPP_ALWAYS_INLINE
1099 result unshift(state_type& __st,
1100 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1101 {
1102 return do_unshift(__st, __to, __to_end, __to_nxt);
1103 }
1104
1105 _LIBCPP_ALWAYS_INLINE
1106 result in(state_type& __st,
1107 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1108 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1109 {
1110 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1111 }
1112
1113 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001114 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115 {
1116 return do_encoding();
1117 }
1118
1119 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001120 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121 {
1122 return do_always_noconv();
1123 }
1124
1125 _LIBCPP_ALWAYS_INLINE
1126 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1127 {
1128 return do_length(__st, __frm, __end, __mx);
1129 }
1130
1131 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001132 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133 {
1134 return do_max_length();
1135 }
1136
1137 static locale::id id;
1138
1139protected:
1140 _LIBCPP_ALWAYS_INLINE
1141 explicit codecvt(const char*, size_t __refs = 0)
1142 : locale::facet(__refs) {}
1143
1144 ~codecvt();
1145
1146 virtual result do_out(state_type& __st,
1147 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1148 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1149 virtual result do_in(state_type& __st,
1150 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1151 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1152 virtual result do_unshift(state_type& __st,
1153 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001154 virtual int do_encoding() const _NOEXCEPT;
1155 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156 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 +00001157 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158};
1159
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160// template <class _InternT, class _ExternT, class _StateT> class codecvt_byname
1161
1162template <class _InternT, class _ExternT, class _StateT>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001163class _LIBCPP_TYPE_VIS_ONLY codecvt_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164 : public codecvt<_InternT, _ExternT, _StateT>
1165{
1166public:
Howard Hinnant9833cad2010-09-21 18:58:51 +00001167 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168 explicit codecvt_byname(const char* __nm, size_t __refs = 0)
1169 : codecvt<_InternT, _ExternT, _StateT>(__nm, __refs) {}
Howard Hinnant9833cad2010-09-21 18:58:51 +00001170 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171 explicit codecvt_byname(const string& __nm, size_t __refs = 0)
1172 : codecvt<_InternT, _ExternT, _StateT>(__nm.c_str(), __refs) {}
1173protected:
1174 ~codecvt_byname();
1175};
1176
1177template <class _InternT, class _ExternT, class _StateT>
1178codecvt_byname<_InternT, _ExternT, _StateT>::~codecvt_byname()
1179{
1180}
1181
Howard Hinnant8ea98242013-08-23 17:37:05 +00001182_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char, char, mbstate_t>)
1183_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<wchar_t, char, mbstate_t>)
1184_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char16_t, char, mbstate_t>)
1185_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char32_t, char, mbstate_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186
Howard Hinnant8331b762013-03-06 23:30:19 +00001187_LIBCPP_FUNC_VIS void __throw_runtime_error(const char*);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001188
Howard Hinnantc834c512011-11-29 18:15:50 +00001189template <size_t _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190struct __narrow_to_utf8
1191{
1192 template <class _OutputIterator, class _CharT>
1193 _OutputIterator
1194 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const;
1195};
1196
1197template <>
1198struct __narrow_to_utf8<8>
1199{
1200 template <class _OutputIterator, class _CharT>
1201 _LIBCPP_ALWAYS_INLINE
1202 _OutputIterator
1203 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1204 {
1205 for (; __wb < __we; ++__wb, ++__s)
1206 *__s = *__wb;
1207 return __s;
1208 }
1209};
1210
1211template <>
1212struct __narrow_to_utf8<16>
1213 : public codecvt<char16_t, char, mbstate_t>
1214{
1215 _LIBCPP_ALWAYS_INLINE
1216 __narrow_to_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1217
1218 ~__narrow_to_utf8();
1219
1220 template <class _OutputIterator, class _CharT>
1221 _LIBCPP_ALWAYS_INLINE
1222 _OutputIterator
1223 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1224 {
1225 result __r = ok;
1226 mbstate_t __mb;
1227 while (__wb < __we && __r != error)
1228 {
1229 const int __sz = 32;
1230 char __buf[__sz];
1231 char* __bn;
1232 const char16_t* __wn = (const char16_t*)__wb;
1233 __r = do_out(__mb, (const char16_t*)__wb, (const char16_t*)__we, __wn,
1234 __buf, __buf+__sz, __bn);
1235 if (__r == codecvt_base::error || __wn == (const char16_t*)__wb)
1236 __throw_runtime_error("locale not supported");
1237 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1238 *__s = *__p;
1239 __wb = (const _CharT*)__wn;
1240 }
1241 return __s;
1242 }
1243};
1244
1245template <>
1246struct __narrow_to_utf8<32>
1247 : public codecvt<char32_t, char, mbstate_t>
1248{
1249 _LIBCPP_ALWAYS_INLINE
1250 __narrow_to_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1251
1252 ~__narrow_to_utf8();
1253
1254 template <class _OutputIterator, class _CharT>
1255 _LIBCPP_ALWAYS_INLINE
1256 _OutputIterator
1257 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1258 {
1259 result __r = ok;
1260 mbstate_t __mb;
1261 while (__wb < __we && __r != error)
1262 {
1263 const int __sz = 32;
1264 char __buf[__sz];
1265 char* __bn;
1266 const char32_t* __wn = (const char32_t*)__wb;
1267 __r = do_out(__mb, (const char32_t*)__wb, (const char32_t*)__we, __wn,
1268 __buf, __buf+__sz, __bn);
1269 if (__r == codecvt_base::error || __wn == (const char32_t*)__wb)
1270 __throw_runtime_error("locale not supported");
1271 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1272 *__s = *__p;
1273 __wb = (const _CharT*)__wn;
1274 }
1275 return __s;
1276 }
1277};
1278
Howard Hinnantc834c512011-11-29 18:15:50 +00001279template <size_t _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001280struct __widen_from_utf8
1281{
1282 template <class _OutputIterator>
1283 _OutputIterator
1284 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const;
1285};
1286
1287template <>
1288struct __widen_from_utf8<8>
1289{
1290 template <class _OutputIterator>
1291 _LIBCPP_ALWAYS_INLINE
1292 _OutputIterator
1293 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1294 {
1295 for (; __nb < __ne; ++__nb, ++__s)
1296 *__s = *__nb;
1297 return __s;
1298 }
1299};
1300
1301template <>
1302struct __widen_from_utf8<16>
1303 : public codecvt<char16_t, char, mbstate_t>
1304{
1305 _LIBCPP_ALWAYS_INLINE
1306 __widen_from_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1307
1308 ~__widen_from_utf8();
1309
1310 template <class _OutputIterator>
1311 _LIBCPP_ALWAYS_INLINE
1312 _OutputIterator
1313 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1314 {
1315 result __r = ok;
1316 mbstate_t __mb;
1317 while (__nb < __ne && __r != error)
1318 {
1319 const int __sz = 32;
1320 char16_t __buf[__sz];
1321 char16_t* __bn;
1322 const char* __nn = __nb;
1323 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1324 __buf, __buf+__sz, __bn);
1325 if (__r == codecvt_base::error || __nn == __nb)
1326 __throw_runtime_error("locale not supported");
1327 for (const char16_t* __p = __buf; __p < __bn; ++__p, ++__s)
1328 *__s = (wchar_t)*__p;
1329 __nb = __nn;
1330 }
1331 return __s;
1332 }
1333};
1334
1335template <>
1336struct __widen_from_utf8<32>
1337 : public codecvt<char32_t, char, mbstate_t>
1338{
1339 _LIBCPP_ALWAYS_INLINE
1340 __widen_from_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1341
1342 ~__widen_from_utf8();
1343
1344 template <class _OutputIterator>
1345 _LIBCPP_ALWAYS_INLINE
1346 _OutputIterator
1347 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1348 {
1349 result __r = ok;
1350 mbstate_t __mb;
1351 while (__nb < __ne && __r != error)
1352 {
1353 const int __sz = 32;
1354 char32_t __buf[__sz];
1355 char32_t* __bn;
1356 const char* __nn = __nb;
1357 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1358 __buf, __buf+__sz, __bn);
1359 if (__r == codecvt_base::error || __nn == __nb)
1360 __throw_runtime_error("locale not supported");
1361 for (const char32_t* __p = __buf; __p < __bn; ++__p, ++__s)
1362 *__s = (wchar_t)*__p;
1363 __nb = __nn;
1364 }
1365 return __s;
1366 }
1367};
1368
1369// template <class charT> class numpunct
1370
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001371template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY numpunct;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001372
1373template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001374class _LIBCPP_TYPE_VIS numpunct<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001375 : public locale::facet
1376{
1377public:
1378 typedef char char_type;
1379 typedef basic_string<char_type> string_type;
1380
1381 explicit numpunct(size_t __refs = 0);
1382
1383 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
1384 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
1385 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
1386 _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();}
1387 _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();}
1388
1389 static locale::id id;
1390
1391protected:
1392 ~numpunct();
1393 virtual char_type do_decimal_point() const;
1394 virtual char_type do_thousands_sep() const;
1395 virtual string do_grouping() const;
1396 virtual string_type do_truename() const;
1397 virtual string_type do_falsename() const;
1398
1399 char_type __decimal_point_;
1400 char_type __thousands_sep_;
1401 string __grouping_;
1402};
1403
1404template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001405class _LIBCPP_TYPE_VIS numpunct<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001406 : public locale::facet
1407{
1408public:
1409 typedef wchar_t char_type;
1410 typedef basic_string<char_type> string_type;
1411
1412 explicit numpunct(size_t __refs = 0);
1413
1414 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
1415 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
1416 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
1417 _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();}
1418 _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();}
1419
1420 static locale::id id;
1421
1422protected:
1423 ~numpunct();
1424 virtual char_type do_decimal_point() const;
1425 virtual char_type do_thousands_sep() const;
1426 virtual string do_grouping() const;
1427 virtual string_type do_truename() const;
1428 virtual string_type do_falsename() const;
1429
1430 char_type __decimal_point_;
1431 char_type __thousands_sep_;
1432 string __grouping_;
1433};
1434
1435// template <class charT> class numpunct_byname
1436
Marshall Clow290eb3f2015-01-11 06:15:59 +00001437template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY numpunct_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001438
1439template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001440class _LIBCPP_TYPE_VIS numpunct_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001441: public numpunct<char>
1442{
1443public:
1444 typedef char char_type;
1445 typedef basic_string<char_type> string_type;
1446
1447 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1448 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1449
1450protected:
1451 ~numpunct_byname();
1452
1453private:
1454 void __init(const char*);
1455};
1456
1457template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001458class _LIBCPP_TYPE_VIS numpunct_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001459: public numpunct<wchar_t>
1460{
1461public:
1462 typedef wchar_t char_type;
1463 typedef basic_string<char_type> string_type;
1464
1465 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1466 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1467
1468protected:
1469 ~numpunct_byname();
1470
1471private:
1472 void __init(const char*);
1473};
1474
1475_LIBCPP_END_NAMESPACE_STD
1476
1477#endif // _LIBCPP___LOCALE