blob: 625d0f85e1d0ff4a1c90c84afd424a1840ba79d9 [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>
Marshall Clow3477ec92014-07-10 15:20:28 +000040#endif // __GLIBC__ || __APPLE__ || __FreeBSD__ || __sun__ || __EMSCRIPTEN__ || __IBMCPP__
Howard Hinnantc51e1022010-05-11 19:42:16 +000041
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000042#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000043#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000044#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000045
46_LIBCPP_BEGIN_NAMESPACE_STD
47
Howard Hinnant8331b762013-03-06 23:30:19 +000048class _LIBCPP_TYPE_VIS locale;
Howard Hinnantc51e1022010-05-11 19:42:16 +000049
Howard Hinnanta54386e2012-09-14 00:39:16 +000050template <class _Facet>
51_LIBCPP_INLINE_VISIBILITY
52bool
53has_facet(const locale&) _NOEXCEPT;
54
55template <class _Facet>
56_LIBCPP_INLINE_VISIBILITY
57const _Facet&
58use_facet(const locale&);
Howard Hinnantc51e1022010-05-11 19:42:16 +000059
Howard Hinnant8331b762013-03-06 23:30:19 +000060class _LIBCPP_TYPE_VIS locale
Howard Hinnantc51e1022010-05-11 19:42:16 +000061{
62public:
63 // types:
Howard Hinnant8331b762013-03-06 23:30:19 +000064 class _LIBCPP_TYPE_VIS facet;
65 class _LIBCPP_TYPE_VIS id;
Howard Hinnantc51e1022010-05-11 19:42:16 +000066
67 typedef int category;
68 static const category // values assigned here are for exposition only
69 none = 0,
70 collate = LC_COLLATE_MASK,
71 ctype = LC_CTYPE_MASK,
72 monetary = LC_MONETARY_MASK,
73 numeric = LC_NUMERIC_MASK,
74 time = LC_TIME_MASK,
75 messages = LC_MESSAGES_MASK,
76 all = collate | ctype | monetary | numeric | time | messages;
77
78 // construct/copy/destroy:
Howard Hinnant7c9e5732011-05-31 15:34:58 +000079 locale() _NOEXCEPT;
80 locale(const locale&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000081 explicit locale(const char*);
82 explicit locale(const string&);
83 locale(const locale&, const char*, category);
84 locale(const locale&, const string&, category);
Howard Hinnantcf823322010-12-17 14:46:43 +000085 template <class _Facet>
86 _LIBCPP_INLINE_VISIBILITY locale(const locale&, _Facet*);
Howard Hinnantc51e1022010-05-11 19:42:16 +000087 locale(const locale&, const locale&, category);
88
Howard Hinnant7c9e5732011-05-31 15:34:58 +000089 ~locale();
Howard Hinnantc51e1022010-05-11 19:42:16 +000090
Howard Hinnant7c9e5732011-05-31 15:34:58 +000091 const locale& operator=(const locale&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000092
93 template <class _Facet> locale combine(const locale&) const;
94
95 // locale operations:
96 string name() const;
97 bool operator==(const locale&) const;
98 bool operator!=(const locale& __y) const {return !(*this == __y);}
99 template <class _CharT, class _Traits, class _Allocator>
100 bool operator()(const basic_string<_CharT, _Traits, _Allocator>&,
101 const basic_string<_CharT, _Traits, _Allocator>&) const;
102
103 // global locale objects:
104 static locale global(const locale&);
105 static const locale& classic();
106
107private:
108 class __imp;
109 __imp* __locale_;
110
111 void __install_ctor(const locale&, facet*, long);
112 static locale& __global();
113 bool has_facet(id&) const;
114 const facet* use_facet(id&) const;
115
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000116 template <class _Facet> friend bool has_facet(const locale&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000117 template <class _Facet> friend const _Facet& use_facet(const locale&);
118};
119
Howard Hinnant8331b762013-03-06 23:30:19 +0000120class _LIBCPP_TYPE_VIS locale::facet
Howard Hinnantc51e1022010-05-11 19:42:16 +0000121 : public __shared_count
122{
123protected:
Howard Hinnant9833cad2010-09-21 18:58:51 +0000124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000125 explicit facet(size_t __refs = 0)
126 : __shared_count(static_cast<long>(__refs)-1) {}
127
128 virtual ~facet();
129
130// facet(const facet&) = delete; // effectively done in __shared_count
131// void operator=(const facet&) = delete;
132private:
Howard Hinnant719bda32011-05-28 14:41:13 +0000133 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000134};
135
Howard Hinnant8331b762013-03-06 23:30:19 +0000136class _LIBCPP_TYPE_VIS locale::id
Howard Hinnantc51e1022010-05-11 19:42:16 +0000137{
138 once_flag __flag_;
139 int32_t __id_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000140
Howard Hinnantc51e1022010-05-11 19:42:16 +0000141 static int32_t __next_id;
142public:
Howard Hinnantac7d9f02012-07-26 16:14:37 +0000143 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR id() :__id_(0) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000144private:
145 void __init();
146 void operator=(const id&); // = delete;
147 id(const id&); // = delete;
148public: // only needed for tests
149 long __get();
150
151 friend class locale;
152 friend class locale::__imp;
153};
154
155template <class _Facet>
156inline _LIBCPP_INLINE_VISIBILITY
157locale::locale(const locale& __other, _Facet* __f)
158{
159 __install_ctor(__other, __f, __f ? __f->id.__get() : 0);
160}
161
162template <class _Facet>
163locale
164locale::combine(const locale& __other) const
165{
Howard Hinnant72f73582010-08-11 17:04:31 +0000166#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000167 if (!_VSTD::has_facet<_Facet>(__other))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000168 throw runtime_error("locale::combine: locale missing facet");
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000169#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000170 return locale(*this, &const_cast<_Facet&>(_VSTD::use_facet<_Facet>(__other)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171}
172
173template <class _Facet>
174inline _LIBCPP_INLINE_VISIBILITY
175bool
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000176has_facet(const locale& __l) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000177{
178 return __l.has_facet(_Facet::id);
179}
180
181template <class _Facet>
182inline _LIBCPP_INLINE_VISIBILITY
183const _Facet&
184use_facet(const locale& __l)
185{
186 return static_cast<const _Facet&>(*__l.use_facet(_Facet::id));
187}
188
189// template <class _CharT> class collate;
190
191template <class _CharT>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000192class _LIBCPP_TYPE_VIS_ONLY collate
Howard Hinnantc51e1022010-05-11 19:42:16 +0000193 : public locale::facet
194{
195public:
196 typedef _CharT char_type;
197 typedef basic_string<char_type> string_type;
198
Howard Hinnant9833cad2010-09-21 18:58:51 +0000199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000200 explicit collate(size_t __refs = 0)
201 : locale::facet(__refs) {}
202
Howard Hinnant9833cad2010-09-21 18:58:51 +0000203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000204 int compare(const char_type* __lo1, const char_type* __hi1,
205 const char_type* __lo2, const char_type* __hi2) const
206 {
207 return do_compare(__lo1, __hi1, __lo2, __hi2);
208 }
209
Howard Hinnant9833cad2010-09-21 18:58:51 +0000210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000211 string_type transform(const char_type* __lo, const char_type* __hi) const
212 {
213 return do_transform(__lo, __hi);
214 }
215
Howard Hinnant9833cad2010-09-21 18:58:51 +0000216 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000217 long hash(const char_type* __lo, const char_type* __hi) const
218 {
219 return do_hash(__lo, __hi);
220 }
221
222 static locale::id id;
223
224protected:
225 ~collate();
226 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
227 const char_type* __lo2, const char_type* __hi2) const;
228 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const
229 {return string_type(__lo, __hi);}
230 virtual long do_hash(const char_type* __lo, const char_type* __hi) const;
231};
232
233template <class _CharT> locale::id collate<_CharT>::id;
234
235template <class _CharT>
236collate<_CharT>::~collate()
237{
238}
239
240template <class _CharT>
241int
242collate<_CharT>::do_compare(const char_type* __lo1, const char_type* __hi1,
243 const char_type* __lo2, const char_type* __hi2) const
244{
245 for (; __lo2 != __hi2; ++__lo1, ++__lo2)
246 {
247 if (__lo1 == __hi1 || *__lo1 < *__lo2)
248 return -1;
249 if (*__lo2 < *__lo1)
250 return 1;
251 }
252 return __lo1 != __hi1;
253}
254
255template <class _CharT>
256long
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000257collate<_CharT>::do_hash(const char_type* __lo, const char_type* __hi) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000258{
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000259 size_t __h = 0;
260 const size_t __sr = __CHAR_BIT__ * sizeof(size_t) - 8;
261 const size_t __mask = size_t(0xF) << (__sr + 4);
262 for(const char_type* __p = __lo; __p != __hi; ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000263 {
Howard Hinnant28b24882011-12-01 20:21:04 +0000264 __h = (__h << 4) + static_cast<size_t>(*__p);
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000265 size_t __g = __h & __mask;
266 __h ^= __g | (__g >> __sr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000267 }
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000268 return static_cast<long>(__h);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000269}
270
Howard Hinnant8ea98242013-08-23 17:37:05 +0000271_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS collate<char>)
272_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS collate<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000273
274// template <class CharT> class collate_byname;
275
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000276template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY collate_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277
278template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000279class _LIBCPP_TYPE_VIS collate_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000280 : public collate<char>
281{
282 locale_t __l;
283public:
284 typedef char char_type;
285 typedef basic_string<char_type> string_type;
286
287 explicit collate_byname(const char* __n, size_t __refs = 0);
288 explicit collate_byname(const string& __n, size_t __refs = 0);
289
290protected:
291 ~collate_byname();
292 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
293 const char_type* __lo2, const char_type* __hi2) const;
294 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
295};
296
297template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000298class _LIBCPP_TYPE_VIS collate_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000299 : public collate<wchar_t>
300{
301 locale_t __l;
302public:
303 typedef wchar_t char_type;
304 typedef basic_string<char_type> string_type;
305
306 explicit collate_byname(const char* __n, size_t __refs = 0);
307 explicit collate_byname(const string& __n, size_t __refs = 0);
308
309protected:
310 ~collate_byname();
311
312 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
313 const char_type* __lo2, const char_type* __hi2) const;
314 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
315};
316
317template <class _CharT, class _Traits, class _Allocator>
318bool
319locale::operator()(const basic_string<_CharT, _Traits, _Allocator>& __x,
320 const basic_string<_CharT, _Traits, _Allocator>& __y) const
321{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000322 return _VSTD::use_facet<_VSTD::collate<_CharT> >(*this).compare(
Howard Hinnantc51e1022010-05-11 19:42:16 +0000323 __x.data(), __x.data() + __x.size(),
324 __y.data(), __y.data() + __y.size()) < 0;
325}
326
327// template <class charT> class ctype
328
Howard Hinnant8331b762013-03-06 23:30:19 +0000329class _LIBCPP_TYPE_VIS ctype_base
Howard Hinnant9833cad2010-09-21 18:58:51 +0000330{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000331public:
Marshall Clow82378c02013-03-18 19:34:07 +0000332#ifdef __GLIBC__
Alexis Hunt92b0c812011-07-09 00:56:23 +0000333 typedef unsigned short mask;
Howard Hinnant155c2af2010-05-24 17:49:41 +0000334 static const mask space = _ISspace;
335 static const mask print = _ISprint;
336 static const mask cntrl = _IScntrl;
337 static const mask upper = _ISupper;
338 static const mask lower = _ISlower;
339 static const mask alpha = _ISalpha;
340 static const mask digit = _ISdigit;
341 static const mask punct = _ISpunct;
342 static const mask xdigit = _ISxdigit;
343 static const mask blank = _ISblank;
Marshall Clow1f257322013-03-18 17:04:29 +0000344#elif defined(_WIN32)
Howard Hinnantd7a78632011-09-29 13:33:15 +0000345 typedef unsigned short mask;
Howard Hinnantdd0d7022011-09-22 19:10:18 +0000346 static const mask space = _SPACE;
347 static const mask print = _BLANK|_PUNCT|_ALPHA|_DIGIT;
348 static const mask cntrl = _CONTROL;
349 static const mask upper = _UPPER;
350 static const mask lower = _LOWER;
351 static const mask alpha = _ALPHA;
352 static const mask digit = _DIGIT;
353 static const mask punct = _PUNCT;
354 static const mask xdigit = _HEX;
355 static const mask blank = _BLANK;
Dan Albertebdf28b2015-03-11 00:51:06 +0000356#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__)
JF Bastien0c265d82015-02-25 22:16:46 +0000357# ifdef __APPLE__
David Chisnall1d581062011-09-21 08:39:44 +0000358 typedef __uint32_t mask;
JF Bastien0c265d82015-02-25 22:16:46 +0000359# elif defined(__FreeBSD__)
David Chisnall1d581062011-09-21 08:39:44 +0000360 typedef unsigned long mask;
JF Bastien0c265d82015-02-25 22:16:46 +0000361# elif defined(__EMSCRIPTEN__) || defined(__NetBSD__)
Howard Hinnant942dbd22013-03-29 18:27:28 +0000362 typedef unsigned short mask;
JF Bastien0c265d82015-02-25 22:16:46 +0000363# endif
David Chisnall1d581062011-09-21 08:39:44 +0000364 static const mask space = _CTYPE_S;
365 static const mask print = _CTYPE_R;
366 static const mask cntrl = _CTYPE_C;
367 static const mask upper = _CTYPE_U;
368 static const mask lower = _CTYPE_L;
369 static const mask alpha = _CTYPE_A;
370 static const mask digit = _CTYPE_D;
371 static const mask punct = _CTYPE_P;
372 static const mask xdigit = _CTYPE_X;
Dan Albert2dca4072014-07-23 19:32:03 +0000373
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000374# if defined(__NetBSD__)
375 static const mask blank = _CTYPE_BL;
376# else
David Chisnall1d581062011-09-21 08:39:44 +0000377 static const mask blank = _CTYPE_B;
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000378# endif
Howard Hinnanta47505d2013-08-30 14:42:39 +0000379#elif defined(__sun__) || defined(_AIX)
David Chisnall8074c342012-02-29 13:05:08 +0000380 typedef unsigned int mask;
381 static const mask space = _ISSPACE;
382 static const mask print = _ISPRINT;
383 static const mask cntrl = _ISCNTRL;
384 static const mask upper = _ISUPPER;
385 static const mask lower = _ISLOWER;
386 static const mask alpha = _ISALPHA;
387 static const mask digit = _ISDIGIT;
388 static const mask punct = _ISPUNCT;
389 static const mask xdigit = _ISXDIGIT;
390 static const mask blank = _ISBLANK;
JF Bastien0c265d82015-02-25 22:16:46 +0000391#elif defined(_NEWLIB_VERSION)
392 // Same type as Newlib's _ctype_ array in newlib/libc/include/ctype.h.
393 typedef char mask;
394 static const mask space = _S;
395 static const mask print = _P | _U | _L | _N | _B;
396 static const mask cntrl = _C;
397 static const mask upper = _U;
398 static const mask lower = _L;
399 static const mask alpha = _U | _L;
400 static const mask digit = _N;
401 static const mask punct = _P;
402 static const mask xdigit = _X | _N;
403 static const mask blank = _B;
404#else
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000405 typedef unsigned long mask;
406 static const mask space = 1<<0;
407 static const mask print = 1<<1;
408 static const mask cntrl = 1<<2;
409 static const mask upper = 1<<3;
410 static const mask lower = 1<<4;
411 static const mask alpha = 1<<5;
412 static const mask digit = 1<<6;
413 static const mask punct = 1<<7;
414 static const mask xdigit = 1<<8;
415 static const mask blank = 1<<9;
JF Bastien0c265d82015-02-25 22:16:46 +0000416#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000417 static const mask alnum = alpha | digit;
418 static const mask graph = alnum | punct;
419
420 _LIBCPP_ALWAYS_INLINE ctype_base() {}
421};
422
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000423template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY ctype;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000424
425template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000426class _LIBCPP_TYPE_VIS ctype<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000427 : public locale::facet,
428 public ctype_base
429{
430public:
431 typedef wchar_t char_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000432
Howard Hinnantc51e1022010-05-11 19:42:16 +0000433 _LIBCPP_ALWAYS_INLINE
434 explicit ctype(size_t __refs = 0)
435 : locale::facet(__refs) {}
436
437 _LIBCPP_ALWAYS_INLINE
438 bool is(mask __m, char_type __c) const
439 {
440 return do_is(__m, __c);
441 }
442
443 _LIBCPP_ALWAYS_INLINE
444 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
445 {
446 return do_is(__low, __high, __vec);
447 }
448
449 _LIBCPP_ALWAYS_INLINE
450 const char_type* scan_is(mask __m, const char_type* __low, const char_type* __high) const
451 {
452 return do_scan_is(__m, __low, __high);
453 }
454
455 _LIBCPP_ALWAYS_INLINE
456 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
457 {
458 return do_scan_not(__m, __low, __high);
459 }
460
461 _LIBCPP_ALWAYS_INLINE
462 char_type toupper(char_type __c) const
463 {
464 return do_toupper(__c);
465 }
466
467 _LIBCPP_ALWAYS_INLINE
468 const char_type* toupper(char_type* __low, const char_type* __high) const
469 {
470 return do_toupper(__low, __high);
471 }
472
473 _LIBCPP_ALWAYS_INLINE
474 char_type tolower(char_type __c) const
475 {
476 return do_tolower(__c);
477 }
478
479 _LIBCPP_ALWAYS_INLINE
480 const char_type* tolower(char_type* __low, const char_type* __high) const
481 {
482 return do_tolower(__low, __high);
483 }
484
485 _LIBCPP_ALWAYS_INLINE
486 char_type widen(char __c) const
487 {
488 return do_widen(__c);
489 }
490
491 _LIBCPP_ALWAYS_INLINE
492 const char* widen(const char* __low, const char* __high, char_type* __to) const
493 {
494 return do_widen(__low, __high, __to);
495 }
496
497 _LIBCPP_ALWAYS_INLINE
498 char narrow(char_type __c, char __dfault) const
499 {
500 return do_narrow(__c, __dfault);
501 }
502
503 _LIBCPP_ALWAYS_INLINE
504 const char_type* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
505 {
506 return do_narrow(__low, __high, __dfault, __to);
507 }
508
509 static locale::id id;
510
511protected:
512 ~ctype();
513 virtual bool do_is(mask __m, char_type __c) const;
514 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
515 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
516 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
517 virtual char_type do_toupper(char_type) const;
518 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
519 virtual char_type do_tolower(char_type) const;
520 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
521 virtual char_type do_widen(char) const;
522 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
523 virtual char do_narrow(char_type, char __dfault) const;
524 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
525};
526
527template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000528class _LIBCPP_TYPE_VIS ctype<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000529 : public locale::facet, public ctype_base
530{
531 const mask* __tab_;
532 bool __del_;
533public:
534 typedef char char_type;
535
536 explicit ctype(const mask* __tab = 0, bool __del = false, size_t __refs = 0);
537
538 _LIBCPP_ALWAYS_INLINE
539 bool is(mask __m, char_type __c) const
540 {
Marshall Clow11de4872013-10-21 14:41:05 +0000541 return isascii(__c) ? (__tab_[static_cast<int>(__c)] & __m) !=0 : false;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000542 }
543
544 _LIBCPP_ALWAYS_INLINE
545 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
546 {
547 for (; __low != __high; ++__low, ++__vec)
Howard Hinnant28b24882011-12-01 20:21:04 +0000548 *__vec = isascii(*__low) ? __tab_[static_cast<int>(*__low)] : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000549 return __low;
550 }
551
552 _LIBCPP_ALWAYS_INLINE
553 const char_type* scan_is (mask __m, const char_type* __low, const char_type* __high) const
554 {
555 for (; __low != __high; ++__low)
Howard Hinnant28b24882011-12-01 20:21:04 +0000556 if (isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000557 break;
558 return __low;
559 }
560
561 _LIBCPP_ALWAYS_INLINE
562 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
563 {
564 for (; __low != __high; ++__low)
Howard Hinnant28b24882011-12-01 20:21:04 +0000565 if (!(isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m)))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000566 break;
567 return __low;
568 }
569
570 _LIBCPP_ALWAYS_INLINE
571 char_type toupper(char_type __c) const
572 {
573 return do_toupper(__c);
574 }
575
576 _LIBCPP_ALWAYS_INLINE
577 const char_type* toupper(char_type* __low, const char_type* __high) const
578 {
579 return do_toupper(__low, __high);
580 }
581
582 _LIBCPP_ALWAYS_INLINE
583 char_type tolower(char_type __c) const
584 {
585 return do_tolower(__c);
586 }
587
588 _LIBCPP_ALWAYS_INLINE
589 const char_type* tolower(char_type* __low, const char_type* __high) const
590 {
591 return do_tolower(__low, __high);
592 }
593
594 _LIBCPP_ALWAYS_INLINE
595 char_type widen(char __c) const
596 {
597 return do_widen(__c);
598 }
599
600 _LIBCPP_ALWAYS_INLINE
601 const char* widen(const char* __low, const char* __high, char_type* __to) const
602 {
603 return do_widen(__low, __high, __to);
604 }
605
606 _LIBCPP_ALWAYS_INLINE
607 char narrow(char_type __c, char __dfault) const
608 {
609 return do_narrow(__c, __dfault);
610 }
611
612 _LIBCPP_ALWAYS_INLINE
613 const char* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
614 {
615 return do_narrow(__low, __high, __dfault, __to);
616 }
617
618 static locale::id id;
619
Howard Hinnant155c2af2010-05-24 17:49:41 +0000620#ifdef _CACHED_RUNES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000621 static const size_t table_size = _CACHED_RUNES;
Howard Hinnant155c2af2010-05-24 17:49:41 +0000622#else
623 static const size_t table_size = 256; // FIXME: Don't hardcode this.
624#endif
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000625 _LIBCPP_ALWAYS_INLINE const mask* table() const _NOEXCEPT {return __tab_;}
626 static const mask* classic_table() _NOEXCEPT;
Marshall Cloweae03f62013-11-19 18:05:03 +0000627#if defined(__GLIBC__) || defined(__EMSCRIPTEN__)
Alexis Hunt92b0c812011-07-09 00:56:23 +0000628 static const int* __classic_upper_table() _NOEXCEPT;
629 static const int* __classic_lower_table() _NOEXCEPT;
Alexis Hunt5a4dd562011-07-09 01:09:31 +0000630#endif
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000631#if defined(__NetBSD__)
632 static const short* __classic_upper_table() _NOEXCEPT;
633 static const short* __classic_lower_table() _NOEXCEPT;
634#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000635
636protected:
637 ~ctype();
638 virtual char_type do_toupper(char_type __c) const;
639 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
640 virtual char_type do_tolower(char_type __c) const;
641 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
642 virtual char_type do_widen(char __c) const;
643 virtual const char* do_widen(const char* __low, const char* __high, char_type* __to) const;
644 virtual char do_narrow(char_type __c, char __dfault) const;
645 virtual const char* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const;
646};
647
648// template <class CharT> class ctype_byname;
649
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000650template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY ctype_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000651
652template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000653class _LIBCPP_TYPE_VIS ctype_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000654 : public ctype<char>
655{
656 locale_t __l;
657
658public:
659 explicit ctype_byname(const char*, size_t = 0);
660 explicit ctype_byname(const string&, size_t = 0);
661
662protected:
663 ~ctype_byname();
664 virtual char_type do_toupper(char_type) const;
665 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
666 virtual char_type do_tolower(char_type) const;
667 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
668};
669
670template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000671class _LIBCPP_TYPE_VIS ctype_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672 : public ctype<wchar_t>
673{
674 locale_t __l;
675
676public:
677 explicit ctype_byname(const char*, size_t = 0);
678 explicit ctype_byname(const string&, size_t = 0);
679
680protected:
681 ~ctype_byname();
682 virtual bool do_is(mask __m, char_type __c) const;
683 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
684 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
685 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
686 virtual char_type do_toupper(char_type) const;
687 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
688 virtual char_type do_tolower(char_type) const;
689 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
690 virtual char_type do_widen(char) const;
691 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
692 virtual char do_narrow(char_type, char __dfault) const;
693 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
694};
695
696template <class _CharT>
697inline _LIBCPP_INLINE_VISIBILITY
698bool
699isspace(_CharT __c, const locale& __loc)
700{
701 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
702}
703
704template <class _CharT>
705inline _LIBCPP_INLINE_VISIBILITY
706bool
707isprint(_CharT __c, const locale& __loc)
708{
709 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c);
710}
711
712template <class _CharT>
713inline _LIBCPP_INLINE_VISIBILITY
714bool
715iscntrl(_CharT __c, const locale& __loc)
716{
717 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c);
718}
719
720template <class _CharT>
721inline _LIBCPP_INLINE_VISIBILITY
722bool
723isupper(_CharT __c, const locale& __loc)
724{
725 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c);
726}
727
728template <class _CharT>
729inline _LIBCPP_INLINE_VISIBILITY
730bool
731islower(_CharT __c, const locale& __loc)
732{
733 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c);
734}
735
736template <class _CharT>
737inline _LIBCPP_INLINE_VISIBILITY
738bool
739isalpha(_CharT __c, const locale& __loc)
740{
741 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c);
742}
743
744template <class _CharT>
745inline _LIBCPP_INLINE_VISIBILITY
746bool
747isdigit(_CharT __c, const locale& __loc)
748{
749 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c);
750}
751
752template <class _CharT>
753inline _LIBCPP_INLINE_VISIBILITY
754bool
755ispunct(_CharT __c, const locale& __loc)
756{
757 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c);
758}
759
760template <class _CharT>
761inline _LIBCPP_INLINE_VISIBILITY
762bool
763isxdigit(_CharT __c, const locale& __loc)
764{
765 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c);
766}
767
768template <class _CharT>
769inline _LIBCPP_INLINE_VISIBILITY
770bool
771isalnum(_CharT __c, const locale& __loc)
772{
773 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c);
774}
775
776template <class _CharT>
777inline _LIBCPP_INLINE_VISIBILITY
778bool
779isgraph(_CharT __c, const locale& __loc)
780{
781 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c);
782}
783
784template <class _CharT>
785inline _LIBCPP_INLINE_VISIBILITY
786_CharT
787toupper(_CharT __c, const locale& __loc)
788{
789 return use_facet<ctype<_CharT> >(__loc).toupper(__c);
790}
791
792template <class _CharT>
793inline _LIBCPP_INLINE_VISIBILITY
794_CharT
795tolower(_CharT __c, const locale& __loc)
796{
797 return use_facet<ctype<_CharT> >(__loc).tolower(__c);
798}
799
800// codecvt_base
801
Howard Hinnant8331b762013-03-06 23:30:19 +0000802class _LIBCPP_TYPE_VIS codecvt_base
Howard Hinnantc51e1022010-05-11 19:42:16 +0000803{
804public:
805 _LIBCPP_ALWAYS_INLINE codecvt_base() {}
806 enum result {ok, partial, error, noconv};
807};
808
809// template <class internT, class externT, class stateT> class codecvt;
810
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000811template <class _InternT, class _ExternT, class _StateT> class _LIBCPP_TYPE_VIS_ONLY codecvt;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000812
813// template <> class codecvt<char, char, mbstate_t>
814
815template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000816class _LIBCPP_TYPE_VIS codecvt<char, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000817 : public locale::facet,
818 public codecvt_base
819{
820public:
821 typedef char intern_type;
822 typedef char extern_type;
823 typedef mbstate_t state_type;
824
825 _LIBCPP_ALWAYS_INLINE
826 explicit codecvt(size_t __refs = 0)
827 : locale::facet(__refs) {}
828
829 _LIBCPP_ALWAYS_INLINE
830 result out(state_type& __st,
831 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
832 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
833 {
834 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
835 }
836
837 _LIBCPP_ALWAYS_INLINE
838 result unshift(state_type& __st,
839 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
840 {
841 return do_unshift(__st, __to, __to_end, __to_nxt);
842 }
843
844 _LIBCPP_ALWAYS_INLINE
845 result in(state_type& __st,
846 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
847 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
848 {
849 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
850 }
851
852 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000853 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000854 {
855 return do_encoding();
856 }
857
858 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000859 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000860 {
861 return do_always_noconv();
862 }
863
864 _LIBCPP_ALWAYS_INLINE
865 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
866 {
867 return do_length(__st, __frm, __end, __mx);
868 }
869
870 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000871 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872 {
873 return do_max_length();
874 }
875
876 static locale::id id;
877
878protected:
879 _LIBCPP_ALWAYS_INLINE
880 explicit codecvt(const char*, size_t __refs = 0)
881 : locale::facet(__refs) {}
882
883 ~codecvt();
884
885 virtual result do_out(state_type& __st,
886 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
887 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
888 virtual result do_in(state_type& __st,
889 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
890 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
891 virtual result do_unshift(state_type& __st,
892 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000893 virtual int do_encoding() const _NOEXCEPT;
894 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000895 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 +0000896 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000897};
898
899// template <> class codecvt<wchar_t, char, mbstate_t>
900
901template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000902class _LIBCPP_TYPE_VIS codecvt<wchar_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000903 : public locale::facet,
904 public codecvt_base
905{
906 locale_t __l;
907public:
908 typedef wchar_t intern_type;
909 typedef char extern_type;
910 typedef mbstate_t state_type;
911
912 explicit codecvt(size_t __refs = 0);
913
914 _LIBCPP_ALWAYS_INLINE
915 result out(state_type& __st,
916 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
917 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
918 {
919 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
920 }
921
922 _LIBCPP_ALWAYS_INLINE
923 result unshift(state_type& __st,
924 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
925 {
926 return do_unshift(__st, __to, __to_end, __to_nxt);
927 }
928
929 _LIBCPP_ALWAYS_INLINE
930 result in(state_type& __st,
931 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
932 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
933 {
934 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
935 }
936
937 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000938 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000939 {
940 return do_encoding();
941 }
942
943 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000944 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945 {
946 return do_always_noconv();
947 }
948
949 _LIBCPP_ALWAYS_INLINE
950 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
951 {
952 return do_length(__st, __frm, __end, __mx);
953 }
954
955 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000956 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000957 {
958 return do_max_length();
959 }
960
961 static locale::id id;
962
963protected:
964 explicit codecvt(const char*, size_t __refs = 0);
965
966 ~codecvt();
967
968 virtual result do_out(state_type& __st,
969 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
970 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
971 virtual result do_in(state_type& __st,
972 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
973 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
974 virtual result do_unshift(state_type& __st,
975 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000976 virtual int do_encoding() const _NOEXCEPT;
977 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000978 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 +0000979 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980};
981
982// template <> class codecvt<char16_t, char, mbstate_t>
983
984template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000985class _LIBCPP_TYPE_VIS codecvt<char16_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986 : public locale::facet,
987 public codecvt_base
988{
989public:
990 typedef char16_t intern_type;
991 typedef char extern_type;
992 typedef mbstate_t state_type;
993
994 _LIBCPP_ALWAYS_INLINE
995 explicit codecvt(size_t __refs = 0)
996 : locale::facet(__refs) {}
997
998 _LIBCPP_ALWAYS_INLINE
999 result out(state_type& __st,
1000 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1001 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1002 {
1003 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1004 }
1005
1006 _LIBCPP_ALWAYS_INLINE
1007 result unshift(state_type& __st,
1008 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1009 {
1010 return do_unshift(__st, __to, __to_end, __to_nxt);
1011 }
1012
1013 _LIBCPP_ALWAYS_INLINE
1014 result in(state_type& __st,
1015 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1016 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1017 {
1018 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1019 }
1020
1021 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001022 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023 {
1024 return do_encoding();
1025 }
1026
1027 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001028 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029 {
1030 return do_always_noconv();
1031 }
1032
1033 _LIBCPP_ALWAYS_INLINE
1034 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1035 {
1036 return do_length(__st, __frm, __end, __mx);
1037 }
1038
1039 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001040 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041 {
1042 return do_max_length();
1043 }
1044
1045 static locale::id id;
1046
1047protected:
1048 _LIBCPP_ALWAYS_INLINE
1049 explicit codecvt(const char*, size_t __refs = 0)
1050 : locale::facet(__refs) {}
1051
1052 ~codecvt();
1053
1054 virtual result do_out(state_type& __st,
1055 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1056 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1057 virtual result do_in(state_type& __st,
1058 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1059 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1060 virtual result do_unshift(state_type& __st,
1061 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001062 virtual int do_encoding() const _NOEXCEPT;
1063 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001064 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 +00001065 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066};
1067
1068// template <> class codecvt<char32_t, char, mbstate_t>
1069
1070template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001071class _LIBCPP_TYPE_VIS codecvt<char32_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072 : public locale::facet,
1073 public codecvt_base
1074{
1075public:
1076 typedef char32_t intern_type;
1077 typedef char extern_type;
1078 typedef mbstate_t state_type;
1079
1080 _LIBCPP_ALWAYS_INLINE
1081 explicit codecvt(size_t __refs = 0)
1082 : locale::facet(__refs) {}
1083
1084 _LIBCPP_ALWAYS_INLINE
1085 result out(state_type& __st,
1086 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1087 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1088 {
1089 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1090 }
1091
1092 _LIBCPP_ALWAYS_INLINE
1093 result unshift(state_type& __st,
1094 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1095 {
1096 return do_unshift(__st, __to, __to_end, __to_nxt);
1097 }
1098
1099 _LIBCPP_ALWAYS_INLINE
1100 result in(state_type& __st,
1101 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1102 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1103 {
1104 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1105 }
1106
1107 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001108 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001109 {
1110 return do_encoding();
1111 }
1112
1113 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001114 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115 {
1116 return do_always_noconv();
1117 }
1118
1119 _LIBCPP_ALWAYS_INLINE
1120 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1121 {
1122 return do_length(__st, __frm, __end, __mx);
1123 }
1124
1125 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001126 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127 {
1128 return do_max_length();
1129 }
1130
1131 static locale::id id;
1132
1133protected:
1134 _LIBCPP_ALWAYS_INLINE
1135 explicit codecvt(const char*, size_t __refs = 0)
1136 : locale::facet(__refs) {}
1137
1138 ~codecvt();
1139
1140 virtual result do_out(state_type& __st,
1141 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1142 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1143 virtual result do_in(state_type& __st,
1144 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1145 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1146 virtual result do_unshift(state_type& __st,
1147 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001148 virtual int do_encoding() const _NOEXCEPT;
1149 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150 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 +00001151 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152};
1153
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154// template <class _InternT, class _ExternT, class _StateT> class codecvt_byname
1155
1156template <class _InternT, class _ExternT, class _StateT>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001157class _LIBCPP_TYPE_VIS_ONLY codecvt_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158 : public codecvt<_InternT, _ExternT, _StateT>
1159{
1160public:
Howard Hinnant9833cad2010-09-21 18:58:51 +00001161 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162 explicit codecvt_byname(const char* __nm, size_t __refs = 0)
1163 : codecvt<_InternT, _ExternT, _StateT>(__nm, __refs) {}
Howard Hinnant9833cad2010-09-21 18:58:51 +00001164 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165 explicit codecvt_byname(const string& __nm, size_t __refs = 0)
1166 : codecvt<_InternT, _ExternT, _StateT>(__nm.c_str(), __refs) {}
1167protected:
1168 ~codecvt_byname();
1169};
1170
1171template <class _InternT, class _ExternT, class _StateT>
1172codecvt_byname<_InternT, _ExternT, _StateT>::~codecvt_byname()
1173{
1174}
1175
Howard Hinnant8ea98242013-08-23 17:37:05 +00001176_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char, char, mbstate_t>)
1177_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<wchar_t, char, mbstate_t>)
1178_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char16_t, char, mbstate_t>)
1179_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char32_t, char, mbstate_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001180
Howard Hinnant8331b762013-03-06 23:30:19 +00001181_LIBCPP_FUNC_VIS void __throw_runtime_error(const char*);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182
Howard Hinnantc834c512011-11-29 18:15:50 +00001183template <size_t _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001184struct __narrow_to_utf8
1185{
1186 template <class _OutputIterator, class _CharT>
1187 _OutputIterator
1188 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const;
1189};
1190
1191template <>
1192struct __narrow_to_utf8<8>
1193{
1194 template <class _OutputIterator, class _CharT>
1195 _LIBCPP_ALWAYS_INLINE
1196 _OutputIterator
1197 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1198 {
1199 for (; __wb < __we; ++__wb, ++__s)
1200 *__s = *__wb;
1201 return __s;
1202 }
1203};
1204
1205template <>
1206struct __narrow_to_utf8<16>
1207 : public codecvt<char16_t, char, mbstate_t>
1208{
1209 _LIBCPP_ALWAYS_INLINE
1210 __narrow_to_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1211
1212 ~__narrow_to_utf8();
1213
1214 template <class _OutputIterator, class _CharT>
1215 _LIBCPP_ALWAYS_INLINE
1216 _OutputIterator
1217 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1218 {
1219 result __r = ok;
1220 mbstate_t __mb;
1221 while (__wb < __we && __r != error)
1222 {
1223 const int __sz = 32;
1224 char __buf[__sz];
1225 char* __bn;
1226 const char16_t* __wn = (const char16_t*)__wb;
1227 __r = do_out(__mb, (const char16_t*)__wb, (const char16_t*)__we, __wn,
1228 __buf, __buf+__sz, __bn);
1229 if (__r == codecvt_base::error || __wn == (const char16_t*)__wb)
1230 __throw_runtime_error("locale not supported");
1231 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1232 *__s = *__p;
1233 __wb = (const _CharT*)__wn;
1234 }
1235 return __s;
1236 }
1237};
1238
1239template <>
1240struct __narrow_to_utf8<32>
1241 : public codecvt<char32_t, char, mbstate_t>
1242{
1243 _LIBCPP_ALWAYS_INLINE
1244 __narrow_to_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1245
1246 ~__narrow_to_utf8();
1247
1248 template <class _OutputIterator, class _CharT>
1249 _LIBCPP_ALWAYS_INLINE
1250 _OutputIterator
1251 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1252 {
1253 result __r = ok;
1254 mbstate_t __mb;
1255 while (__wb < __we && __r != error)
1256 {
1257 const int __sz = 32;
1258 char __buf[__sz];
1259 char* __bn;
1260 const char32_t* __wn = (const char32_t*)__wb;
1261 __r = do_out(__mb, (const char32_t*)__wb, (const char32_t*)__we, __wn,
1262 __buf, __buf+__sz, __bn);
1263 if (__r == codecvt_base::error || __wn == (const char32_t*)__wb)
1264 __throw_runtime_error("locale not supported");
1265 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1266 *__s = *__p;
1267 __wb = (const _CharT*)__wn;
1268 }
1269 return __s;
1270 }
1271};
1272
Howard Hinnantc834c512011-11-29 18:15:50 +00001273template <size_t _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001274struct __widen_from_utf8
1275{
1276 template <class _OutputIterator>
1277 _OutputIterator
1278 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const;
1279};
1280
1281template <>
1282struct __widen_from_utf8<8>
1283{
1284 template <class _OutputIterator>
1285 _LIBCPP_ALWAYS_INLINE
1286 _OutputIterator
1287 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1288 {
1289 for (; __nb < __ne; ++__nb, ++__s)
1290 *__s = *__nb;
1291 return __s;
1292 }
1293};
1294
1295template <>
1296struct __widen_from_utf8<16>
1297 : public codecvt<char16_t, char, mbstate_t>
1298{
1299 _LIBCPP_ALWAYS_INLINE
1300 __widen_from_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1301
1302 ~__widen_from_utf8();
1303
1304 template <class _OutputIterator>
1305 _LIBCPP_ALWAYS_INLINE
1306 _OutputIterator
1307 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1308 {
1309 result __r = ok;
1310 mbstate_t __mb;
1311 while (__nb < __ne && __r != error)
1312 {
1313 const int __sz = 32;
1314 char16_t __buf[__sz];
1315 char16_t* __bn;
1316 const char* __nn = __nb;
1317 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1318 __buf, __buf+__sz, __bn);
1319 if (__r == codecvt_base::error || __nn == __nb)
1320 __throw_runtime_error("locale not supported");
1321 for (const char16_t* __p = __buf; __p < __bn; ++__p, ++__s)
1322 *__s = (wchar_t)*__p;
1323 __nb = __nn;
1324 }
1325 return __s;
1326 }
1327};
1328
1329template <>
1330struct __widen_from_utf8<32>
1331 : public codecvt<char32_t, char, mbstate_t>
1332{
1333 _LIBCPP_ALWAYS_INLINE
1334 __widen_from_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1335
1336 ~__widen_from_utf8();
1337
1338 template <class _OutputIterator>
1339 _LIBCPP_ALWAYS_INLINE
1340 _OutputIterator
1341 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1342 {
1343 result __r = ok;
1344 mbstate_t __mb;
1345 while (__nb < __ne && __r != error)
1346 {
1347 const int __sz = 32;
1348 char32_t __buf[__sz];
1349 char32_t* __bn;
1350 const char* __nn = __nb;
1351 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1352 __buf, __buf+__sz, __bn);
1353 if (__r == codecvt_base::error || __nn == __nb)
1354 __throw_runtime_error("locale not supported");
1355 for (const char32_t* __p = __buf; __p < __bn; ++__p, ++__s)
1356 *__s = (wchar_t)*__p;
1357 __nb = __nn;
1358 }
1359 return __s;
1360 }
1361};
1362
1363// template <class charT> class numpunct
1364
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001365template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY numpunct;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001366
1367template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001368class _LIBCPP_TYPE_VIS numpunct<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001369 : public locale::facet
1370{
1371public:
1372 typedef char char_type;
1373 typedef basic_string<char_type> string_type;
1374
1375 explicit numpunct(size_t __refs = 0);
1376
1377 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
1378 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
1379 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
1380 _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();}
1381 _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();}
1382
1383 static locale::id id;
1384
1385protected:
1386 ~numpunct();
1387 virtual char_type do_decimal_point() const;
1388 virtual char_type do_thousands_sep() const;
1389 virtual string do_grouping() const;
1390 virtual string_type do_truename() const;
1391 virtual string_type do_falsename() const;
1392
1393 char_type __decimal_point_;
1394 char_type __thousands_sep_;
1395 string __grouping_;
1396};
1397
1398template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001399class _LIBCPP_TYPE_VIS numpunct<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400 : public locale::facet
1401{
1402public:
1403 typedef wchar_t char_type;
1404 typedef basic_string<char_type> string_type;
1405
1406 explicit numpunct(size_t __refs = 0);
1407
1408 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
1409 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
1410 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
1411 _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();}
1412 _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();}
1413
1414 static locale::id id;
1415
1416protected:
1417 ~numpunct();
1418 virtual char_type do_decimal_point() const;
1419 virtual char_type do_thousands_sep() const;
1420 virtual string do_grouping() const;
1421 virtual string_type do_truename() const;
1422 virtual string_type do_falsename() const;
1423
1424 char_type __decimal_point_;
1425 char_type __thousands_sep_;
1426 string __grouping_;
1427};
1428
1429// template <class charT> class numpunct_byname
1430
Marshall Clow290eb3f2015-01-11 06:15:59 +00001431template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY numpunct_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001432
1433template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001434class _LIBCPP_TYPE_VIS numpunct_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001435: public numpunct<char>
1436{
1437public:
1438 typedef char char_type;
1439 typedef basic_string<char_type> string_type;
1440
1441 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1442 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1443
1444protected:
1445 ~numpunct_byname();
1446
1447private:
1448 void __init(const char*);
1449};
1450
1451template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001452class _LIBCPP_TYPE_VIS numpunct_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453: public numpunct<wchar_t>
1454{
1455public:
1456 typedef wchar_t char_type;
1457 typedef basic_string<char_type> string_type;
1458
1459 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1460 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1461
1462protected:
1463 ~numpunct_byname();
1464
1465private:
1466 void __init(const char*);
1467};
1468
1469_LIBCPP_END_NAMESPACE_STD
1470
1471#endif // _LIBCPP___LOCALE