blob: e718f11d6e6715eac95a477c44f8c5186d9b815c [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 Hinnantb1ad5a82011-06-30 21:18:19 +0000168 if (!_VSTD::has_facet<_Facet>(__other))
Marshall Clow8fea1612016-08-25 15:09:01 +0000169 __throw_runtime_error("locale::combine: locale missing facet");
170
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000171 return locale(*this, &const_cast<_Facet&>(_VSTD::use_facet<_Facet>(__other)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000172}
173
174template <class _Facet>
175inline _LIBCPP_INLINE_VISIBILITY
176bool
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000177has_facet(const locale& __l) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000178{
179 return __l.has_facet(_Facet::id);
180}
181
182template <class _Facet>
183inline _LIBCPP_INLINE_VISIBILITY
184const _Facet&
185use_facet(const locale& __l)
186{
187 return static_cast<const _Facet&>(*__l.use_facet(_Facet::id));
188}
189
190// template <class _CharT> class collate;
191
192template <class _CharT>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000193class _LIBCPP_TYPE_VIS_ONLY collate
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194 : public locale::facet
195{
196public:
197 typedef _CharT char_type;
198 typedef basic_string<char_type> string_type;
199
Howard Hinnant9833cad2010-09-21 18:58:51 +0000200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000201 explicit collate(size_t __refs = 0)
202 : locale::facet(__refs) {}
203
Howard Hinnant9833cad2010-09-21 18:58:51 +0000204 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000205 int compare(const char_type* __lo1, const char_type* __hi1,
206 const char_type* __lo2, const char_type* __hi2) const
207 {
208 return do_compare(__lo1, __hi1, __lo2, __hi2);
209 }
210
Howard Hinnant9833cad2010-09-21 18:58:51 +0000211 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212 string_type transform(const char_type* __lo, const char_type* __hi) const
213 {
214 return do_transform(__lo, __hi);
215 }
216
Howard Hinnant9833cad2010-09-21 18:58:51 +0000217 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000218 long hash(const char_type* __lo, const char_type* __hi) const
219 {
220 return do_hash(__lo, __hi);
221 }
222
223 static locale::id id;
224
225protected:
226 ~collate();
227 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
228 const char_type* __lo2, const char_type* __hi2) const;
229 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const
230 {return string_type(__lo, __hi);}
231 virtual long do_hash(const char_type* __lo, const char_type* __hi) const;
232};
233
234template <class _CharT> locale::id collate<_CharT>::id;
235
236template <class _CharT>
237collate<_CharT>::~collate()
238{
239}
240
241template <class _CharT>
242int
243collate<_CharT>::do_compare(const char_type* __lo1, const char_type* __hi1,
244 const char_type* __lo2, const char_type* __hi2) const
245{
246 for (; __lo2 != __hi2; ++__lo1, ++__lo2)
247 {
248 if (__lo1 == __hi1 || *__lo1 < *__lo2)
249 return -1;
250 if (*__lo2 < *__lo1)
251 return 1;
252 }
253 return __lo1 != __hi1;
254}
255
256template <class _CharT>
257long
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000258collate<_CharT>::do_hash(const char_type* __lo, const char_type* __hi) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259{
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000260 size_t __h = 0;
261 const size_t __sr = __CHAR_BIT__ * sizeof(size_t) - 8;
262 const size_t __mask = size_t(0xF) << (__sr + 4);
263 for(const char_type* __p = __lo; __p != __hi; ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264 {
Howard Hinnant28b24882011-12-01 20:21:04 +0000265 __h = (__h << 4) + static_cast<size_t>(*__p);
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000266 size_t __g = __h & __mask;
267 __h ^= __g | (__g >> __sr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000268 }
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000269 return static_cast<long>(__h);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270}
271
Howard Hinnant8ea98242013-08-23 17:37:05 +0000272_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS collate<char>)
273_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS collate<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000274
275// template <class CharT> class collate_byname;
276
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000277template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY collate_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000278
279template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000280class _LIBCPP_TYPE_VIS collate_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000281 : public collate<char>
282{
283 locale_t __l;
284public:
285 typedef char char_type;
286 typedef basic_string<char_type> string_type;
287
288 explicit collate_byname(const char* __n, size_t __refs = 0);
289 explicit collate_byname(const string& __n, size_t __refs = 0);
290
291protected:
292 ~collate_byname();
293 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
294 const char_type* __lo2, const char_type* __hi2) const;
295 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
296};
297
298template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000299class _LIBCPP_TYPE_VIS collate_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300 : public collate<wchar_t>
301{
302 locale_t __l;
303public:
304 typedef wchar_t char_type;
305 typedef basic_string<char_type> string_type;
306
307 explicit collate_byname(const char* __n, size_t __refs = 0);
308 explicit collate_byname(const string& __n, size_t __refs = 0);
309
310protected:
311 ~collate_byname();
312
313 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
314 const char_type* __lo2, const char_type* __hi2) const;
315 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
316};
317
318template <class _CharT, class _Traits, class _Allocator>
319bool
320locale::operator()(const basic_string<_CharT, _Traits, _Allocator>& __x,
321 const basic_string<_CharT, _Traits, _Allocator>& __y) const
322{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000323 return _VSTD::use_facet<_VSTD::collate<_CharT> >(*this).compare(
Howard Hinnantc51e1022010-05-11 19:42:16 +0000324 __x.data(), __x.data() + __x.size(),
325 __y.data(), __y.data() + __y.size()) < 0;
326}
327
328// template <class charT> class ctype
329
Howard Hinnant8331b762013-03-06 23:30:19 +0000330class _LIBCPP_TYPE_VIS ctype_base
Howard Hinnant9833cad2010-09-21 18:58:51 +0000331{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000332public:
Vasileios Kalintiris281b4cf2015-11-09 10:21:04 +0000333#if defined(__GLIBC__)
Alexis Hunt92b0c812011-07-09 00:56:23 +0000334 typedef unsigned short mask;
Howard Hinnant155c2af2010-05-24 17:49:41 +0000335 static const mask space = _ISspace;
336 static const mask print = _ISprint;
337 static const mask cntrl = _IScntrl;
338 static const mask upper = _ISupper;
339 static const mask lower = _ISlower;
340 static const mask alpha = _ISalpha;
341 static const mask digit = _ISdigit;
342 static const mask punct = _ISpunct;
343 static const mask xdigit = _ISxdigit;
344 static const mask blank = _ISblank;
Marshall Clow1f257322013-03-18 17:04:29 +0000345#elif defined(_WIN32)
Howard Hinnantd7a78632011-09-29 13:33:15 +0000346 typedef unsigned short mask;
Howard Hinnantdd0d7022011-09-22 19:10:18 +0000347 static const mask space = _SPACE;
348 static const mask print = _BLANK|_PUNCT|_ALPHA|_DIGIT;
349 static const mask cntrl = _CONTROL;
350 static const mask upper = _UPPER;
351 static const mask lower = _LOWER;
352 static const mask alpha = _ALPHA;
353 static const mask digit = _DIGIT;
354 static const mask punct = _PUNCT;
355 static const mask xdigit = _HEX;
356 static const mask blank = _BLANK;
Jonathan Roelofsae9ab252015-03-11 17:00:28 +0000357# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
Dan Albertebdf28b2015-03-11 00:51:06 +0000358#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__)
JF Bastien0c265d82015-02-25 22:16:46 +0000359# ifdef __APPLE__
David Chisnall1d581062011-09-21 08:39:44 +0000360 typedef __uint32_t mask;
JF Bastien0c265d82015-02-25 22:16:46 +0000361# elif defined(__FreeBSD__)
David Chisnall1d581062011-09-21 08:39:44 +0000362 typedef unsigned long mask;
Vasileios Kalintirisc5dd8942015-11-24 10:24:54 +0000363# elif defined(__EMSCRIPTEN__) || defined(__NetBSD__)
Howard Hinnant942dbd22013-03-29 18:27:28 +0000364 typedef unsigned short mask;
JF Bastien0c265d82015-02-25 22:16:46 +0000365# endif
David Chisnall1d581062011-09-21 08:39:44 +0000366 static const mask space = _CTYPE_S;
367 static const mask print = _CTYPE_R;
368 static const mask cntrl = _CTYPE_C;
369 static const mask upper = _CTYPE_U;
370 static const mask lower = _CTYPE_L;
371 static const mask alpha = _CTYPE_A;
372 static const mask digit = _CTYPE_D;
373 static const mask punct = _CTYPE_P;
374 static const mask xdigit = _CTYPE_X;
Dan Albert2dca4072014-07-23 19:32:03 +0000375
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000376# if defined(__NetBSD__)
377 static const mask blank = _CTYPE_BL;
378# else
David Chisnall1d581062011-09-21 08:39:44 +0000379 static const mask blank = _CTYPE_B;
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000380# endif
Howard Hinnanta47505d2013-08-30 14:42:39 +0000381#elif defined(__sun__) || defined(_AIX)
David Chisnall8074c342012-02-29 13:05:08 +0000382 typedef unsigned int mask;
383 static const mask space = _ISSPACE;
384 static const mask print = _ISPRINT;
385 static const mask cntrl = _ISCNTRL;
386 static const mask upper = _ISUPPER;
387 static const mask lower = _ISLOWER;
388 static const mask alpha = _ISALPHA;
389 static const mask digit = _ISDIGIT;
390 static const mask punct = _ISPUNCT;
391 static const mask xdigit = _ISXDIGIT;
392 static const mask blank = _ISBLANK;
JF Bastien0c265d82015-02-25 22:16:46 +0000393#elif defined(_NEWLIB_VERSION)
394 // Same type as Newlib's _ctype_ array in newlib/libc/include/ctype.h.
395 typedef char mask;
396 static const mask space = _S;
397 static const mask print = _P | _U | _L | _N | _B;
398 static const mask cntrl = _C;
399 static const mask upper = _U;
400 static const mask lower = _L;
401 static const mask alpha = _U | _L;
402 static const mask digit = _N;
403 static const mask punct = _P;
404 static const mask xdigit = _X | _N;
405 static const mask blank = _B;
Jonathan Roelofsae9ab252015-03-11 17:00:28 +0000406# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
407# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
408# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_XDIGIT
JF Bastien0c265d82015-02-25 22:16:46 +0000409#else
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000410 typedef unsigned long mask;
411 static const mask space = 1<<0;
412 static const mask print = 1<<1;
413 static const mask cntrl = 1<<2;
414 static const mask upper = 1<<3;
415 static const mask lower = 1<<4;
416 static const mask alpha = 1<<5;
417 static const mask digit = 1<<6;
418 static const mask punct = 1<<7;
419 static const mask xdigit = 1<<8;
420 static const mask blank = 1<<9;
JF Bastien0c265d82015-02-25 22:16:46 +0000421#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000422 static const mask alnum = alpha | digit;
423 static const mask graph = alnum | punct;
424
425 _LIBCPP_ALWAYS_INLINE ctype_base() {}
426};
427
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000428template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY ctype;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429
430template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000431class _LIBCPP_TYPE_VIS ctype<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000432 : public locale::facet,
433 public ctype_base
434{
435public:
436 typedef wchar_t char_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000437
Howard Hinnantc51e1022010-05-11 19:42:16 +0000438 _LIBCPP_ALWAYS_INLINE
439 explicit ctype(size_t __refs = 0)
440 : locale::facet(__refs) {}
441
442 _LIBCPP_ALWAYS_INLINE
443 bool is(mask __m, char_type __c) const
444 {
445 return do_is(__m, __c);
446 }
447
448 _LIBCPP_ALWAYS_INLINE
449 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
450 {
451 return do_is(__low, __high, __vec);
452 }
453
454 _LIBCPP_ALWAYS_INLINE
455 const char_type* scan_is(mask __m, const char_type* __low, const char_type* __high) const
456 {
457 return do_scan_is(__m, __low, __high);
458 }
459
460 _LIBCPP_ALWAYS_INLINE
461 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
462 {
463 return do_scan_not(__m, __low, __high);
464 }
465
466 _LIBCPP_ALWAYS_INLINE
467 char_type toupper(char_type __c) const
468 {
469 return do_toupper(__c);
470 }
471
472 _LIBCPP_ALWAYS_INLINE
473 const char_type* toupper(char_type* __low, const char_type* __high) const
474 {
475 return do_toupper(__low, __high);
476 }
477
478 _LIBCPP_ALWAYS_INLINE
479 char_type tolower(char_type __c) const
480 {
481 return do_tolower(__c);
482 }
483
484 _LIBCPP_ALWAYS_INLINE
485 const char_type* tolower(char_type* __low, const char_type* __high) const
486 {
487 return do_tolower(__low, __high);
488 }
489
490 _LIBCPP_ALWAYS_INLINE
491 char_type widen(char __c) const
492 {
493 return do_widen(__c);
494 }
495
496 _LIBCPP_ALWAYS_INLINE
497 const char* widen(const char* __low, const char* __high, char_type* __to) const
498 {
499 return do_widen(__low, __high, __to);
500 }
501
502 _LIBCPP_ALWAYS_INLINE
503 char narrow(char_type __c, char __dfault) const
504 {
505 return do_narrow(__c, __dfault);
506 }
507
508 _LIBCPP_ALWAYS_INLINE
509 const char_type* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
510 {
511 return do_narrow(__low, __high, __dfault, __to);
512 }
513
514 static locale::id id;
515
516protected:
517 ~ctype();
518 virtual bool do_is(mask __m, char_type __c) const;
519 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
520 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
521 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
522 virtual char_type do_toupper(char_type) const;
523 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
524 virtual char_type do_tolower(char_type) const;
525 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
526 virtual char_type do_widen(char) const;
527 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
528 virtual char do_narrow(char_type, char __dfault) const;
529 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
530};
531
532template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000533class _LIBCPP_TYPE_VIS ctype<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000534 : public locale::facet, public ctype_base
535{
536 const mask* __tab_;
537 bool __del_;
538public:
539 typedef char char_type;
540
541 explicit ctype(const mask* __tab = 0, bool __del = false, size_t __refs = 0);
542
543 _LIBCPP_ALWAYS_INLINE
544 bool is(mask __m, char_type __c) const
545 {
Marshall Clow11de4872013-10-21 14:41:05 +0000546 return isascii(__c) ? (__tab_[static_cast<int>(__c)] & __m) !=0 : false;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000547 }
548
549 _LIBCPP_ALWAYS_INLINE
550 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
551 {
552 for (; __low != __high; ++__low, ++__vec)
Howard Hinnant28b24882011-12-01 20:21:04 +0000553 *__vec = isascii(*__low) ? __tab_[static_cast<int>(*__low)] : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000554 return __low;
555 }
556
557 _LIBCPP_ALWAYS_INLINE
558 const char_type* scan_is (mask __m, const char_type* __low, const char_type* __high) const
559 {
560 for (; __low != __high; ++__low)
Howard Hinnant28b24882011-12-01 20:21:04 +0000561 if (isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000562 break;
563 return __low;
564 }
565
566 _LIBCPP_ALWAYS_INLINE
567 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
568 {
569 for (; __low != __high; ++__low)
Howard Hinnant28b24882011-12-01 20:21:04 +0000570 if (!(isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m)))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000571 break;
572 return __low;
573 }
574
575 _LIBCPP_ALWAYS_INLINE
576 char_type toupper(char_type __c) const
577 {
578 return do_toupper(__c);
579 }
580
581 _LIBCPP_ALWAYS_INLINE
582 const char_type* toupper(char_type* __low, const char_type* __high) const
583 {
584 return do_toupper(__low, __high);
585 }
586
587 _LIBCPP_ALWAYS_INLINE
588 char_type tolower(char_type __c) const
589 {
590 return do_tolower(__c);
591 }
592
593 _LIBCPP_ALWAYS_INLINE
594 const char_type* tolower(char_type* __low, const char_type* __high) const
595 {
596 return do_tolower(__low, __high);
597 }
598
599 _LIBCPP_ALWAYS_INLINE
600 char_type widen(char __c) const
601 {
602 return do_widen(__c);
603 }
604
605 _LIBCPP_ALWAYS_INLINE
606 const char* widen(const char* __low, const char* __high, char_type* __to) const
607 {
608 return do_widen(__low, __high, __to);
609 }
610
611 _LIBCPP_ALWAYS_INLINE
612 char narrow(char_type __c, char __dfault) const
613 {
614 return do_narrow(__c, __dfault);
615 }
616
617 _LIBCPP_ALWAYS_INLINE
618 const char* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
619 {
620 return do_narrow(__low, __high, __dfault, __to);
621 }
622
623 static locale::id id;
624
Howard Hinnant155c2af2010-05-24 17:49:41 +0000625#ifdef _CACHED_RUNES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000626 static const size_t table_size = _CACHED_RUNES;
Howard Hinnant155c2af2010-05-24 17:49:41 +0000627#else
628 static const size_t table_size = 256; // FIXME: Don't hardcode this.
629#endif
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000630 _LIBCPP_ALWAYS_INLINE const mask* table() const _NOEXCEPT {return __tab_;}
631 static const mask* classic_table() _NOEXCEPT;
Vasileios Kalintirisc5dd8942015-11-24 10:24:54 +0000632#if defined(__GLIBC__) || defined(__EMSCRIPTEN__)
Alexis Hunt92b0c812011-07-09 00:56:23 +0000633 static const int* __classic_upper_table() _NOEXCEPT;
634 static const int* __classic_lower_table() _NOEXCEPT;
Alexis Hunt5a4dd562011-07-09 01:09:31 +0000635#endif
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000636#if defined(__NetBSD__)
637 static const short* __classic_upper_table() _NOEXCEPT;
638 static const short* __classic_lower_table() _NOEXCEPT;
639#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000640
641protected:
642 ~ctype();
643 virtual char_type do_toupper(char_type __c) const;
644 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
645 virtual char_type do_tolower(char_type __c) const;
646 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
647 virtual char_type do_widen(char __c) const;
648 virtual const char* do_widen(const char* __low, const char* __high, char_type* __to) const;
649 virtual char do_narrow(char_type __c, char __dfault) const;
650 virtual const char* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const;
651};
652
653// template <class CharT> class ctype_byname;
654
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000655template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY ctype_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000656
657template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000658class _LIBCPP_TYPE_VIS ctype_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000659 : public ctype<char>
660{
661 locale_t __l;
662
663public:
664 explicit ctype_byname(const char*, size_t = 0);
665 explicit ctype_byname(const string&, size_t = 0);
666
667protected:
668 ~ctype_byname();
669 virtual char_type do_toupper(char_type) const;
670 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
671 virtual char_type do_tolower(char_type) const;
672 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
673};
674
675template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000676class _LIBCPP_TYPE_VIS ctype_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000677 : public ctype<wchar_t>
678{
679 locale_t __l;
680
681public:
682 explicit ctype_byname(const char*, size_t = 0);
683 explicit ctype_byname(const string&, size_t = 0);
684
685protected:
686 ~ctype_byname();
687 virtual bool do_is(mask __m, char_type __c) const;
688 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
689 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
690 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
691 virtual char_type do_toupper(char_type) const;
692 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
693 virtual char_type do_tolower(char_type) const;
694 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
695 virtual char_type do_widen(char) const;
696 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
697 virtual char do_narrow(char_type, char __dfault) const;
698 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
699};
700
701template <class _CharT>
702inline _LIBCPP_INLINE_VISIBILITY
703bool
704isspace(_CharT __c, const locale& __loc)
705{
706 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
707}
708
709template <class _CharT>
710inline _LIBCPP_INLINE_VISIBILITY
711bool
712isprint(_CharT __c, const locale& __loc)
713{
714 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c);
715}
716
717template <class _CharT>
718inline _LIBCPP_INLINE_VISIBILITY
719bool
720iscntrl(_CharT __c, const locale& __loc)
721{
722 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c);
723}
724
725template <class _CharT>
726inline _LIBCPP_INLINE_VISIBILITY
727bool
728isupper(_CharT __c, const locale& __loc)
729{
730 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c);
731}
732
733template <class _CharT>
734inline _LIBCPP_INLINE_VISIBILITY
735bool
736islower(_CharT __c, const locale& __loc)
737{
738 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c);
739}
740
741template <class _CharT>
742inline _LIBCPP_INLINE_VISIBILITY
743bool
744isalpha(_CharT __c, const locale& __loc)
745{
746 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c);
747}
748
749template <class _CharT>
750inline _LIBCPP_INLINE_VISIBILITY
751bool
752isdigit(_CharT __c, const locale& __loc)
753{
754 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c);
755}
756
757template <class _CharT>
758inline _LIBCPP_INLINE_VISIBILITY
759bool
760ispunct(_CharT __c, const locale& __loc)
761{
762 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c);
763}
764
765template <class _CharT>
766inline _LIBCPP_INLINE_VISIBILITY
767bool
768isxdigit(_CharT __c, const locale& __loc)
769{
770 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c);
771}
772
773template <class _CharT>
774inline _LIBCPP_INLINE_VISIBILITY
775bool
776isalnum(_CharT __c, const locale& __loc)
777{
778 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c);
779}
780
781template <class _CharT>
782inline _LIBCPP_INLINE_VISIBILITY
783bool
784isgraph(_CharT __c, const locale& __loc)
785{
786 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c);
787}
788
789template <class _CharT>
790inline _LIBCPP_INLINE_VISIBILITY
791_CharT
792toupper(_CharT __c, const locale& __loc)
793{
794 return use_facet<ctype<_CharT> >(__loc).toupper(__c);
795}
796
797template <class _CharT>
798inline _LIBCPP_INLINE_VISIBILITY
799_CharT
800tolower(_CharT __c, const locale& __loc)
801{
802 return use_facet<ctype<_CharT> >(__loc).tolower(__c);
803}
804
805// codecvt_base
806
Howard Hinnant8331b762013-03-06 23:30:19 +0000807class _LIBCPP_TYPE_VIS codecvt_base
Howard Hinnantc51e1022010-05-11 19:42:16 +0000808{
809public:
810 _LIBCPP_ALWAYS_INLINE codecvt_base() {}
811 enum result {ok, partial, error, noconv};
812};
813
814// template <class internT, class externT, class stateT> class codecvt;
815
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000816template <class _InternT, class _ExternT, class _StateT> class _LIBCPP_TYPE_VIS_ONLY codecvt;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000817
818// template <> class codecvt<char, char, mbstate_t>
819
820template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000821class _LIBCPP_TYPE_VIS codecvt<char, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000822 : public locale::facet,
823 public codecvt_base
824{
825public:
826 typedef char intern_type;
827 typedef char extern_type;
828 typedef mbstate_t state_type;
829
830 _LIBCPP_ALWAYS_INLINE
831 explicit codecvt(size_t __refs = 0)
832 : locale::facet(__refs) {}
833
834 _LIBCPP_ALWAYS_INLINE
835 result out(state_type& __st,
836 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
837 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
838 {
839 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
840 }
841
842 _LIBCPP_ALWAYS_INLINE
843 result unshift(state_type& __st,
844 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
845 {
846 return do_unshift(__st, __to, __to_end, __to_nxt);
847 }
848
849 _LIBCPP_ALWAYS_INLINE
850 result in(state_type& __st,
851 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
852 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
853 {
854 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
855 }
856
857 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000858 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000859 {
860 return do_encoding();
861 }
862
863 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000864 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000865 {
866 return do_always_noconv();
867 }
868
869 _LIBCPP_ALWAYS_INLINE
870 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
871 {
872 return do_length(__st, __frm, __end, __mx);
873 }
874
875 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000876 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000877 {
878 return do_max_length();
879 }
880
881 static locale::id id;
882
883protected:
884 _LIBCPP_ALWAYS_INLINE
885 explicit codecvt(const char*, size_t __refs = 0)
886 : locale::facet(__refs) {}
887
888 ~codecvt();
889
890 virtual result do_out(state_type& __st,
891 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
892 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
893 virtual result do_in(state_type& __st,
894 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
895 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
896 virtual result do_unshift(state_type& __st,
897 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000898 virtual int do_encoding() const _NOEXCEPT;
899 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000900 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 +0000901 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000902};
903
904// template <> class codecvt<wchar_t, char, mbstate_t>
905
906template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000907class _LIBCPP_TYPE_VIS codecvt<wchar_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000908 : public locale::facet,
909 public codecvt_base
910{
911 locale_t __l;
912public:
913 typedef wchar_t intern_type;
914 typedef char extern_type;
915 typedef mbstate_t state_type;
916
917 explicit codecvt(size_t __refs = 0);
918
919 _LIBCPP_ALWAYS_INLINE
920 result out(state_type& __st,
921 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
922 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
923 {
924 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
925 }
926
927 _LIBCPP_ALWAYS_INLINE
928 result unshift(state_type& __st,
929 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
930 {
931 return do_unshift(__st, __to, __to_end, __to_nxt);
932 }
933
934 _LIBCPP_ALWAYS_INLINE
935 result in(state_type& __st,
936 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
937 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
938 {
939 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
940 }
941
942 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000943 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000944 {
945 return do_encoding();
946 }
947
948 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000949 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950 {
951 return do_always_noconv();
952 }
953
954 _LIBCPP_ALWAYS_INLINE
955 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
956 {
957 return do_length(__st, __frm, __end, __mx);
958 }
959
960 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000961 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962 {
963 return do_max_length();
964 }
965
966 static locale::id id;
967
968protected:
969 explicit codecvt(const char*, size_t __refs = 0);
970
971 ~codecvt();
972
973 virtual result do_out(state_type& __st,
974 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
975 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
976 virtual result do_in(state_type& __st,
977 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
978 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
979 virtual result do_unshift(state_type& __st,
980 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000981 virtual int do_encoding() const _NOEXCEPT;
982 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983 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 +0000984 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985};
986
987// template <> class codecvt<char16_t, char, mbstate_t>
988
989template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000990class _LIBCPP_TYPE_VIS codecvt<char16_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991 : public locale::facet,
992 public codecvt_base
993{
994public:
995 typedef char16_t intern_type;
996 typedef char extern_type;
997 typedef mbstate_t state_type;
998
999 _LIBCPP_ALWAYS_INLINE
1000 explicit codecvt(size_t __refs = 0)
1001 : locale::facet(__refs) {}
1002
1003 _LIBCPP_ALWAYS_INLINE
1004 result out(state_type& __st,
1005 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1006 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1007 {
1008 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1009 }
1010
1011 _LIBCPP_ALWAYS_INLINE
1012 result unshift(state_type& __st,
1013 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1014 {
1015 return do_unshift(__st, __to, __to_end, __to_nxt);
1016 }
1017
1018 _LIBCPP_ALWAYS_INLINE
1019 result in(state_type& __st,
1020 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1021 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1022 {
1023 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1024 }
1025
1026 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001027 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001028 {
1029 return do_encoding();
1030 }
1031
1032 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001033 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001034 {
1035 return do_always_noconv();
1036 }
1037
1038 _LIBCPP_ALWAYS_INLINE
1039 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1040 {
1041 return do_length(__st, __frm, __end, __mx);
1042 }
1043
1044 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001045 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046 {
1047 return do_max_length();
1048 }
1049
1050 static locale::id id;
1051
1052protected:
1053 _LIBCPP_ALWAYS_INLINE
1054 explicit codecvt(const char*, size_t __refs = 0)
1055 : locale::facet(__refs) {}
1056
1057 ~codecvt();
1058
1059 virtual result do_out(state_type& __st,
1060 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1061 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1062 virtual result do_in(state_type& __st,
1063 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1064 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1065 virtual result do_unshift(state_type& __st,
1066 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001067 virtual int do_encoding() const _NOEXCEPT;
1068 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069 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 +00001070 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001071};
1072
1073// template <> class codecvt<char32_t, char, mbstate_t>
1074
1075template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001076class _LIBCPP_TYPE_VIS codecvt<char32_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001077 : public locale::facet,
1078 public codecvt_base
1079{
1080public:
1081 typedef char32_t intern_type;
1082 typedef char extern_type;
1083 typedef mbstate_t state_type;
1084
1085 _LIBCPP_ALWAYS_INLINE
1086 explicit codecvt(size_t __refs = 0)
1087 : locale::facet(__refs) {}
1088
1089 _LIBCPP_ALWAYS_INLINE
1090 result out(state_type& __st,
1091 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1092 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1093 {
1094 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1095 }
1096
1097 _LIBCPP_ALWAYS_INLINE
1098 result unshift(state_type& __st,
1099 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1100 {
1101 return do_unshift(__st, __to, __to_end, __to_nxt);
1102 }
1103
1104 _LIBCPP_ALWAYS_INLINE
1105 result in(state_type& __st,
1106 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1107 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1108 {
1109 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1110 }
1111
1112 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001113 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114 {
1115 return do_encoding();
1116 }
1117
1118 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001119 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001120 {
1121 return do_always_noconv();
1122 }
1123
1124 _LIBCPP_ALWAYS_INLINE
1125 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1126 {
1127 return do_length(__st, __frm, __end, __mx);
1128 }
1129
1130 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001131 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132 {
1133 return do_max_length();
1134 }
1135
1136 static locale::id id;
1137
1138protected:
1139 _LIBCPP_ALWAYS_INLINE
1140 explicit codecvt(const char*, size_t __refs = 0)
1141 : locale::facet(__refs) {}
1142
1143 ~codecvt();
1144
1145 virtual result do_out(state_type& __st,
1146 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1147 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1148 virtual result do_in(state_type& __st,
1149 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1150 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1151 virtual result do_unshift(state_type& __st,
1152 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001153 virtual int do_encoding() const _NOEXCEPT;
1154 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155 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 +00001156 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157};
1158
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159// template <class _InternT, class _ExternT, class _StateT> class codecvt_byname
1160
1161template <class _InternT, class _ExternT, class _StateT>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001162class _LIBCPP_TYPE_VIS_ONLY codecvt_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163 : public codecvt<_InternT, _ExternT, _StateT>
1164{
1165public:
Howard Hinnant9833cad2010-09-21 18:58:51 +00001166 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167 explicit codecvt_byname(const char* __nm, size_t __refs = 0)
1168 : codecvt<_InternT, _ExternT, _StateT>(__nm, __refs) {}
Howard Hinnant9833cad2010-09-21 18:58:51 +00001169 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170 explicit codecvt_byname(const string& __nm, size_t __refs = 0)
1171 : codecvt<_InternT, _ExternT, _StateT>(__nm.c_str(), __refs) {}
1172protected:
1173 ~codecvt_byname();
1174};
1175
1176template <class _InternT, class _ExternT, class _StateT>
1177codecvt_byname<_InternT, _ExternT, _StateT>::~codecvt_byname()
1178{
1179}
1180
Howard Hinnant8ea98242013-08-23 17:37:05 +00001181_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char, char, mbstate_t>)
1182_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<wchar_t, char, mbstate_t>)
1183_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char16_t, char, mbstate_t>)
1184_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char32_t, char, mbstate_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185
Howard Hinnantc834c512011-11-29 18:15:50 +00001186template <size_t _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001187struct __narrow_to_utf8
1188{
1189 template <class _OutputIterator, class _CharT>
1190 _OutputIterator
1191 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const;
1192};
1193
1194template <>
1195struct __narrow_to_utf8<8>
1196{
1197 template <class _OutputIterator, class _CharT>
1198 _LIBCPP_ALWAYS_INLINE
1199 _OutputIterator
1200 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1201 {
1202 for (; __wb < __we; ++__wb, ++__s)
1203 *__s = *__wb;
1204 return __s;
1205 }
1206};
1207
1208template <>
1209struct __narrow_to_utf8<16>
1210 : public codecvt<char16_t, char, mbstate_t>
1211{
1212 _LIBCPP_ALWAYS_INLINE
1213 __narrow_to_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1214
1215 ~__narrow_to_utf8();
1216
1217 template <class _OutputIterator, class _CharT>
1218 _LIBCPP_ALWAYS_INLINE
1219 _OutputIterator
1220 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1221 {
1222 result __r = ok;
1223 mbstate_t __mb;
1224 while (__wb < __we && __r != error)
1225 {
1226 const int __sz = 32;
1227 char __buf[__sz];
1228 char* __bn;
1229 const char16_t* __wn = (const char16_t*)__wb;
1230 __r = do_out(__mb, (const char16_t*)__wb, (const char16_t*)__we, __wn,
1231 __buf, __buf+__sz, __bn);
1232 if (__r == codecvt_base::error || __wn == (const char16_t*)__wb)
1233 __throw_runtime_error("locale not supported");
1234 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1235 *__s = *__p;
1236 __wb = (const _CharT*)__wn;
1237 }
1238 return __s;
1239 }
1240};
1241
1242template <>
1243struct __narrow_to_utf8<32>
1244 : public codecvt<char32_t, char, mbstate_t>
1245{
1246 _LIBCPP_ALWAYS_INLINE
1247 __narrow_to_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1248
1249 ~__narrow_to_utf8();
1250
1251 template <class _OutputIterator, class _CharT>
1252 _LIBCPP_ALWAYS_INLINE
1253 _OutputIterator
1254 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1255 {
1256 result __r = ok;
1257 mbstate_t __mb;
1258 while (__wb < __we && __r != error)
1259 {
1260 const int __sz = 32;
1261 char __buf[__sz];
1262 char* __bn;
1263 const char32_t* __wn = (const char32_t*)__wb;
1264 __r = do_out(__mb, (const char32_t*)__wb, (const char32_t*)__we, __wn,
1265 __buf, __buf+__sz, __bn);
1266 if (__r == codecvt_base::error || __wn == (const char32_t*)__wb)
1267 __throw_runtime_error("locale not supported");
1268 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1269 *__s = *__p;
1270 __wb = (const _CharT*)__wn;
1271 }
1272 return __s;
1273 }
1274};
1275
Howard Hinnantc834c512011-11-29 18:15:50 +00001276template <size_t _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001277struct __widen_from_utf8
1278{
1279 template <class _OutputIterator>
1280 _OutputIterator
1281 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const;
1282};
1283
1284template <>
1285struct __widen_from_utf8<8>
1286{
1287 template <class _OutputIterator>
1288 _LIBCPP_ALWAYS_INLINE
1289 _OutputIterator
1290 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1291 {
1292 for (; __nb < __ne; ++__nb, ++__s)
1293 *__s = *__nb;
1294 return __s;
1295 }
1296};
1297
1298template <>
1299struct __widen_from_utf8<16>
1300 : public codecvt<char16_t, char, mbstate_t>
1301{
1302 _LIBCPP_ALWAYS_INLINE
1303 __widen_from_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1304
1305 ~__widen_from_utf8();
1306
1307 template <class _OutputIterator>
1308 _LIBCPP_ALWAYS_INLINE
1309 _OutputIterator
1310 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1311 {
1312 result __r = ok;
1313 mbstate_t __mb;
1314 while (__nb < __ne && __r != error)
1315 {
1316 const int __sz = 32;
1317 char16_t __buf[__sz];
1318 char16_t* __bn;
1319 const char* __nn = __nb;
1320 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1321 __buf, __buf+__sz, __bn);
1322 if (__r == codecvt_base::error || __nn == __nb)
1323 __throw_runtime_error("locale not supported");
1324 for (const char16_t* __p = __buf; __p < __bn; ++__p, ++__s)
1325 *__s = (wchar_t)*__p;
1326 __nb = __nn;
1327 }
1328 return __s;
1329 }
1330};
1331
1332template <>
1333struct __widen_from_utf8<32>
1334 : public codecvt<char32_t, char, mbstate_t>
1335{
1336 _LIBCPP_ALWAYS_INLINE
1337 __widen_from_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1338
1339 ~__widen_from_utf8();
1340
1341 template <class _OutputIterator>
1342 _LIBCPP_ALWAYS_INLINE
1343 _OutputIterator
1344 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1345 {
1346 result __r = ok;
1347 mbstate_t __mb;
1348 while (__nb < __ne && __r != error)
1349 {
1350 const int __sz = 32;
1351 char32_t __buf[__sz];
1352 char32_t* __bn;
1353 const char* __nn = __nb;
1354 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1355 __buf, __buf+__sz, __bn);
1356 if (__r == codecvt_base::error || __nn == __nb)
1357 __throw_runtime_error("locale not supported");
1358 for (const char32_t* __p = __buf; __p < __bn; ++__p, ++__s)
1359 *__s = (wchar_t)*__p;
1360 __nb = __nn;
1361 }
1362 return __s;
1363 }
1364};
1365
1366// template <class charT> class numpunct
1367
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001368template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY numpunct;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001369
1370template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001371class _LIBCPP_TYPE_VIS numpunct<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001372 : public locale::facet
1373{
1374public:
1375 typedef char char_type;
1376 typedef basic_string<char_type> string_type;
1377
1378 explicit numpunct(size_t __refs = 0);
1379
1380 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
1381 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
1382 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
1383 _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();}
1384 _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();}
1385
1386 static locale::id id;
1387
1388protected:
1389 ~numpunct();
1390 virtual char_type do_decimal_point() const;
1391 virtual char_type do_thousands_sep() const;
1392 virtual string do_grouping() const;
1393 virtual string_type do_truename() const;
1394 virtual string_type do_falsename() const;
1395
1396 char_type __decimal_point_;
1397 char_type __thousands_sep_;
1398 string __grouping_;
1399};
1400
1401template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001402class _LIBCPP_TYPE_VIS numpunct<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001403 : public locale::facet
1404{
1405public:
1406 typedef wchar_t char_type;
1407 typedef basic_string<char_type> string_type;
1408
1409 explicit numpunct(size_t __refs = 0);
1410
1411 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
1412 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
1413 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
1414 _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();}
1415 _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();}
1416
1417 static locale::id id;
1418
1419protected:
1420 ~numpunct();
1421 virtual char_type do_decimal_point() const;
1422 virtual char_type do_thousands_sep() const;
1423 virtual string do_grouping() const;
1424 virtual string_type do_truename() const;
1425 virtual string_type do_falsename() const;
1426
1427 char_type __decimal_point_;
1428 char_type __thousands_sep_;
1429 string __grouping_;
1430};
1431
1432// template <class charT> class numpunct_byname
1433
Marshall Clow290eb3f2015-01-11 06:15:59 +00001434template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY numpunct_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001435
1436template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001437class _LIBCPP_TYPE_VIS numpunct_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001438: public numpunct<char>
1439{
1440public:
1441 typedef char char_type;
1442 typedef basic_string<char_type> string_type;
1443
1444 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1445 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1446
1447protected:
1448 ~numpunct_byname();
1449
1450private:
1451 void __init(const char*);
1452};
1453
1454template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001455class _LIBCPP_TYPE_VIS numpunct_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456: public numpunct<wchar_t>
1457{
1458public:
1459 typedef wchar_t char_type;
1460 typedef basic_string<char_type> string_type;
1461
1462 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1463 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1464
1465protected:
1466 ~numpunct_byname();
1467
1468private:
1469 void __init(const char*);
1470};
1471
1472_LIBCPP_END_NAMESPACE_STD
1473
1474#endif // _LIBCPP___LOCALE