blob: 47116200e26e704dce9c84cafc1f18f3f17a4173 [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__)
33# include <support/solaris/xlocale.h>
Marshall Clow3477ec92014-07-10 15:20:28 +000034#elif (defined(__GLIBC__) || defined(__APPLE__) || defined(__FreeBSD__) \
Eric Fiselier7274c652014-11-25 21:57:41 +000035 || defined(__EMSCRIPTEN__) || defined(__IBMCPP__))
Howard Hinnantdd0d7022011-09-22 19:10:18 +000036# include <xlocale.h>
Marshall Clow3477ec92014-07-10 15:20:28 +000037#endif // __GLIBC__ || __APPLE__ || __FreeBSD__ || __sun__ || __EMSCRIPTEN__ || __IBMCPP__
Howard Hinnantc51e1022010-05-11 19:42:16 +000038
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000039#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000040#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000041#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000042
43_LIBCPP_BEGIN_NAMESPACE_STD
44
Howard Hinnant8331b762013-03-06 23:30:19 +000045class _LIBCPP_TYPE_VIS locale;
Howard Hinnantc51e1022010-05-11 19:42:16 +000046
Howard Hinnanta54386e2012-09-14 00:39:16 +000047template <class _Facet>
48_LIBCPP_INLINE_VISIBILITY
49bool
50has_facet(const locale&) _NOEXCEPT;
51
52template <class _Facet>
53_LIBCPP_INLINE_VISIBILITY
54const _Facet&
55use_facet(const locale&);
Howard Hinnantc51e1022010-05-11 19:42:16 +000056
Howard Hinnant8331b762013-03-06 23:30:19 +000057class _LIBCPP_TYPE_VIS locale
Howard Hinnantc51e1022010-05-11 19:42:16 +000058{
59public:
60 // types:
Howard Hinnant8331b762013-03-06 23:30:19 +000061 class _LIBCPP_TYPE_VIS facet;
62 class _LIBCPP_TYPE_VIS id;
Howard Hinnantc51e1022010-05-11 19:42:16 +000063
64 typedef int category;
65 static const category // values assigned here are for exposition only
66 none = 0,
67 collate = LC_COLLATE_MASK,
68 ctype = LC_CTYPE_MASK,
69 monetary = LC_MONETARY_MASK,
70 numeric = LC_NUMERIC_MASK,
71 time = LC_TIME_MASK,
72 messages = LC_MESSAGES_MASK,
73 all = collate | ctype | monetary | numeric | time | messages;
74
75 // construct/copy/destroy:
Howard Hinnant7c9e5732011-05-31 15:34:58 +000076 locale() _NOEXCEPT;
77 locale(const locale&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000078 explicit locale(const char*);
79 explicit locale(const string&);
80 locale(const locale&, const char*, category);
81 locale(const locale&, const string&, category);
Howard Hinnantcf823322010-12-17 14:46:43 +000082 template <class _Facet>
83 _LIBCPP_INLINE_VISIBILITY locale(const locale&, _Facet*);
Howard Hinnantc51e1022010-05-11 19:42:16 +000084 locale(const locale&, const locale&, category);
85
Howard Hinnant7c9e5732011-05-31 15:34:58 +000086 ~locale();
Howard Hinnantc51e1022010-05-11 19:42:16 +000087
Howard Hinnant7c9e5732011-05-31 15:34:58 +000088 const locale& operator=(const locale&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000089
90 template <class _Facet> locale combine(const locale&) const;
91
92 // locale operations:
93 string name() const;
94 bool operator==(const locale&) const;
95 bool operator!=(const locale& __y) const {return !(*this == __y);}
96 template <class _CharT, class _Traits, class _Allocator>
97 bool operator()(const basic_string<_CharT, _Traits, _Allocator>&,
98 const basic_string<_CharT, _Traits, _Allocator>&) const;
99
100 // global locale objects:
101 static locale global(const locale&);
102 static const locale& classic();
103
104private:
105 class __imp;
106 __imp* __locale_;
107
108 void __install_ctor(const locale&, facet*, long);
109 static locale& __global();
110 bool has_facet(id&) const;
111 const facet* use_facet(id&) const;
112
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000113 template <class _Facet> friend bool has_facet(const locale&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114 template <class _Facet> friend const _Facet& use_facet(const locale&);
115};
116
Howard Hinnant8331b762013-03-06 23:30:19 +0000117class _LIBCPP_TYPE_VIS locale::facet
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118 : public __shared_count
119{
120protected:
Howard Hinnant9833cad2010-09-21 18:58:51 +0000121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000122 explicit facet(size_t __refs = 0)
123 : __shared_count(static_cast<long>(__refs)-1) {}
124
125 virtual ~facet();
126
127// facet(const facet&) = delete; // effectively done in __shared_count
128// void operator=(const facet&) = delete;
129private:
Howard Hinnant719bda32011-05-28 14:41:13 +0000130 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000131};
132
Howard Hinnant8331b762013-03-06 23:30:19 +0000133class _LIBCPP_TYPE_VIS locale::id
Howard Hinnantc51e1022010-05-11 19:42:16 +0000134{
135 once_flag __flag_;
136 int32_t __id_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000137
Howard Hinnantc51e1022010-05-11 19:42:16 +0000138 static int32_t __next_id;
139public:
Howard Hinnantac7d9f02012-07-26 16:14:37 +0000140 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR id() :__id_(0) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000141private:
142 void __init();
143 void operator=(const id&); // = delete;
144 id(const id&); // = delete;
145public: // only needed for tests
146 long __get();
147
148 friend class locale;
149 friend class locale::__imp;
150};
151
152template <class _Facet>
153inline _LIBCPP_INLINE_VISIBILITY
154locale::locale(const locale& __other, _Facet* __f)
155{
156 __install_ctor(__other, __f, __f ? __f->id.__get() : 0);
157}
158
159template <class _Facet>
160locale
161locale::combine(const locale& __other) const
162{
Howard Hinnant72f73582010-08-11 17:04:31 +0000163#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000164 if (!_VSTD::has_facet<_Facet>(__other))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000165 throw runtime_error("locale::combine: locale missing facet");
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000166#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000167 return locale(*this, &const_cast<_Facet&>(_VSTD::use_facet<_Facet>(__other)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000168}
169
170template <class _Facet>
171inline _LIBCPP_INLINE_VISIBILITY
172bool
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000173has_facet(const locale& __l) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000174{
175 return __l.has_facet(_Facet::id);
176}
177
178template <class _Facet>
179inline _LIBCPP_INLINE_VISIBILITY
180const _Facet&
181use_facet(const locale& __l)
182{
183 return static_cast<const _Facet&>(*__l.use_facet(_Facet::id));
184}
185
186// template <class _CharT> class collate;
187
188template <class _CharT>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000189class _LIBCPP_TYPE_VIS_ONLY collate
Howard Hinnantc51e1022010-05-11 19:42:16 +0000190 : public locale::facet
191{
192public:
193 typedef _CharT char_type;
194 typedef basic_string<char_type> string_type;
195
Howard Hinnant9833cad2010-09-21 18:58:51 +0000196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000197 explicit collate(size_t __refs = 0)
198 : locale::facet(__refs) {}
199
Howard Hinnant9833cad2010-09-21 18:58:51 +0000200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000201 int compare(const char_type* __lo1, const char_type* __hi1,
202 const char_type* __lo2, const char_type* __hi2) const
203 {
204 return do_compare(__lo1, __hi1, __lo2, __hi2);
205 }
206
Howard Hinnant9833cad2010-09-21 18:58:51 +0000207 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000208 string_type transform(const char_type* __lo, const char_type* __hi) const
209 {
210 return do_transform(__lo, __hi);
211 }
212
Howard Hinnant9833cad2010-09-21 18:58:51 +0000213 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000214 long hash(const char_type* __lo, const char_type* __hi) const
215 {
216 return do_hash(__lo, __hi);
217 }
218
219 static locale::id id;
220
221protected:
222 ~collate();
223 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
224 const char_type* __lo2, const char_type* __hi2) const;
225 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const
226 {return string_type(__lo, __hi);}
227 virtual long do_hash(const char_type* __lo, const char_type* __hi) const;
228};
229
230template <class _CharT> locale::id collate<_CharT>::id;
231
232template <class _CharT>
233collate<_CharT>::~collate()
234{
235}
236
237template <class _CharT>
238int
239collate<_CharT>::do_compare(const char_type* __lo1, const char_type* __hi1,
240 const char_type* __lo2, const char_type* __hi2) const
241{
242 for (; __lo2 != __hi2; ++__lo1, ++__lo2)
243 {
244 if (__lo1 == __hi1 || *__lo1 < *__lo2)
245 return -1;
246 if (*__lo2 < *__lo1)
247 return 1;
248 }
249 return __lo1 != __hi1;
250}
251
252template <class _CharT>
253long
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000254collate<_CharT>::do_hash(const char_type* __lo, const char_type* __hi) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000255{
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000256 size_t __h = 0;
257 const size_t __sr = __CHAR_BIT__ * sizeof(size_t) - 8;
258 const size_t __mask = size_t(0xF) << (__sr + 4);
259 for(const char_type* __p = __lo; __p != __hi; ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000260 {
Howard Hinnant28b24882011-12-01 20:21:04 +0000261 __h = (__h << 4) + static_cast<size_t>(*__p);
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000262 size_t __g = __h & __mask;
263 __h ^= __g | (__g >> __sr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264 }
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000265 return static_cast<long>(__h);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000266}
267
Howard Hinnant8ea98242013-08-23 17:37:05 +0000268_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS collate<char>)
269_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS collate<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270
271// template <class CharT> class collate_byname;
272
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000273template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY collate_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000274
275template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000276class _LIBCPP_TYPE_VIS collate_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277 : public collate<char>
278{
279 locale_t __l;
280public:
281 typedef char char_type;
282 typedef basic_string<char_type> string_type;
283
284 explicit collate_byname(const char* __n, size_t __refs = 0);
285 explicit collate_byname(const string& __n, size_t __refs = 0);
286
287protected:
288 ~collate_byname();
289 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
290 const char_type* __lo2, const char_type* __hi2) const;
291 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
292};
293
294template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000295class _LIBCPP_TYPE_VIS collate_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000296 : public collate<wchar_t>
297{
298 locale_t __l;
299public:
300 typedef wchar_t char_type;
301 typedef basic_string<char_type> string_type;
302
303 explicit collate_byname(const char* __n, size_t __refs = 0);
304 explicit collate_byname(const string& __n, size_t __refs = 0);
305
306protected:
307 ~collate_byname();
308
309 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
310 const char_type* __lo2, const char_type* __hi2) const;
311 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
312};
313
314template <class _CharT, class _Traits, class _Allocator>
315bool
316locale::operator()(const basic_string<_CharT, _Traits, _Allocator>& __x,
317 const basic_string<_CharT, _Traits, _Allocator>& __y) const
318{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000319 return _VSTD::use_facet<_VSTD::collate<_CharT> >(*this).compare(
Howard Hinnantc51e1022010-05-11 19:42:16 +0000320 __x.data(), __x.data() + __x.size(),
321 __y.data(), __y.data() + __y.size()) < 0;
322}
323
324// template <class charT> class ctype
325
Howard Hinnant8331b762013-03-06 23:30:19 +0000326class _LIBCPP_TYPE_VIS ctype_base
Howard Hinnant9833cad2010-09-21 18:58:51 +0000327{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000328public:
Marshall Clow82378c02013-03-18 19:34:07 +0000329#ifdef __GLIBC__
Alexis Hunt92b0c812011-07-09 00:56:23 +0000330 typedef unsigned short mask;
Howard Hinnant155c2af2010-05-24 17:49:41 +0000331 static const mask space = _ISspace;
332 static const mask print = _ISprint;
333 static const mask cntrl = _IScntrl;
334 static const mask upper = _ISupper;
335 static const mask lower = _ISlower;
336 static const mask alpha = _ISalpha;
337 static const mask digit = _ISdigit;
338 static const mask punct = _ISpunct;
339 static const mask xdigit = _ISxdigit;
340 static const mask blank = _ISblank;
Marshall Clow1f257322013-03-18 17:04:29 +0000341#elif defined(_WIN32)
Howard Hinnantd7a78632011-09-29 13:33:15 +0000342 typedef unsigned short mask;
Howard Hinnantdd0d7022011-09-22 19:10:18 +0000343 static const mask space = _SPACE;
344 static const mask print = _BLANK|_PUNCT|_ALPHA|_DIGIT;
345 static const mask cntrl = _CONTROL;
346 static const mask upper = _UPPER;
347 static const mask lower = _LOWER;
348 static const mask alpha = _ALPHA;
349 static const mask digit = _DIGIT;
350 static const mask punct = _PUNCT;
351 static const mask xdigit = _HEX;
352 static const mask blank = _BLANK;
Marshall Clow3477ec92014-07-10 15:20:28 +0000353#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__) || defined(__ANDROID__)
Marshall Clow82378c02013-03-18 19:34:07 +0000354#ifdef __APPLE__
David Chisnall1d581062011-09-21 08:39:44 +0000355 typedef __uint32_t mask;
Marshall Clow82378c02013-03-18 19:34:07 +0000356#elif defined(__FreeBSD__)
David Chisnall1d581062011-09-21 08:39:44 +0000357 typedef unsigned long mask;
Marshall Cloweae03f62013-11-19 18:05:03 +0000358#elif defined(__EMSCRIPTEN__) || defined(__NetBSD__)
Howard Hinnant942dbd22013-03-29 18:27:28 +0000359 typedef unsigned short mask;
Marshall Clow3477ec92014-07-10 15:20:28 +0000360#elif defined(__ANDROID__)
Dan Albertb380e8c2014-07-31 21:04:08 +0000361 typedef unsigned char mask;
David Chisnall1d581062011-09-21 08:39:44 +0000362#endif
363 static const mask space = _CTYPE_S;
364 static const mask print = _CTYPE_R;
365 static const mask cntrl = _CTYPE_C;
366 static const mask upper = _CTYPE_U;
367 static const mask lower = _CTYPE_L;
368 static const mask alpha = _CTYPE_A;
369 static const mask digit = _CTYPE_D;
370 static const mask punct = _CTYPE_P;
Dan Albert2dca4072014-07-23 19:32:03 +0000371# if defined(__ANDROID__)
372 static const mask xdigit = _CTYPE_X | _CTYPE_D;
373# else
David Chisnall1d581062011-09-21 08:39:44 +0000374 static const mask xdigit = _CTYPE_X;
Dan Albert2dca4072014-07-23 19:32:03 +0000375# endif
376
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000377# if defined(__NetBSD__)
378 static const mask blank = _CTYPE_BL;
379# else
David Chisnall1d581062011-09-21 08:39:44 +0000380 static const mask blank = _CTYPE_B;
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000381# endif
Howard Hinnanta47505d2013-08-30 14:42:39 +0000382#elif defined(__sun__) || defined(_AIX)
David Chisnall8074c342012-02-29 13:05:08 +0000383 typedef unsigned int mask;
384 static const mask space = _ISSPACE;
385 static const mask print = _ISPRINT;
386 static const mask cntrl = _ISCNTRL;
387 static const mask upper = _ISUPPER;
388 static const mask lower = _ISLOWER;
389 static const mask alpha = _ISALPHA;
390 static const mask digit = _ISDIGIT;
391 static const mask punct = _ISPUNCT;
392 static const mask xdigit = _ISXDIGIT;
393 static const mask blank = _ISBLANK;
Marshall Cloweae03f62013-11-19 18:05:03 +0000394#else // __GLIBC__ || _WIN32 || __APPLE__ || __FreeBSD__ || __EMSCRIPTEN__ || __sun__
Howard Hinnant8c2bf6b2011-10-11 16:00:46 +0000395 typedef unsigned long mask;
396 static const mask space = 1<<0;
397 static const mask print = 1<<1;
398 static const mask cntrl = 1<<2;
399 static const mask upper = 1<<3;
400 static const mask lower = 1<<4;
401 static const mask alpha = 1<<5;
402 static const mask digit = 1<<6;
403 static const mask punct = 1<<7;
404 static const mask xdigit = 1<<8;
405 static const mask blank = 1<<9;
406#endif // __GLIBC__ || _WIN32 || __APPLE__ || __FreeBSD__
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407 static const mask alnum = alpha | digit;
408 static const mask graph = alnum | punct;
409
410 _LIBCPP_ALWAYS_INLINE ctype_base() {}
411};
412
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000413template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY ctype;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000414
415template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000416class _LIBCPP_TYPE_VIS ctype<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000417 : public locale::facet,
418 public ctype_base
419{
420public:
421 typedef wchar_t char_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000422
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423 _LIBCPP_ALWAYS_INLINE
424 explicit ctype(size_t __refs = 0)
425 : locale::facet(__refs) {}
426
427 _LIBCPP_ALWAYS_INLINE
428 bool is(mask __m, char_type __c) const
429 {
430 return do_is(__m, __c);
431 }
432
433 _LIBCPP_ALWAYS_INLINE
434 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
435 {
436 return do_is(__low, __high, __vec);
437 }
438
439 _LIBCPP_ALWAYS_INLINE
440 const char_type* scan_is(mask __m, const char_type* __low, const char_type* __high) const
441 {
442 return do_scan_is(__m, __low, __high);
443 }
444
445 _LIBCPP_ALWAYS_INLINE
446 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
447 {
448 return do_scan_not(__m, __low, __high);
449 }
450
451 _LIBCPP_ALWAYS_INLINE
452 char_type toupper(char_type __c) const
453 {
454 return do_toupper(__c);
455 }
456
457 _LIBCPP_ALWAYS_INLINE
458 const char_type* toupper(char_type* __low, const char_type* __high) const
459 {
460 return do_toupper(__low, __high);
461 }
462
463 _LIBCPP_ALWAYS_INLINE
464 char_type tolower(char_type __c) const
465 {
466 return do_tolower(__c);
467 }
468
469 _LIBCPP_ALWAYS_INLINE
470 const char_type* tolower(char_type* __low, const char_type* __high) const
471 {
472 return do_tolower(__low, __high);
473 }
474
475 _LIBCPP_ALWAYS_INLINE
476 char_type widen(char __c) const
477 {
478 return do_widen(__c);
479 }
480
481 _LIBCPP_ALWAYS_INLINE
482 const char* widen(const char* __low, const char* __high, char_type* __to) const
483 {
484 return do_widen(__low, __high, __to);
485 }
486
487 _LIBCPP_ALWAYS_INLINE
488 char narrow(char_type __c, char __dfault) const
489 {
490 return do_narrow(__c, __dfault);
491 }
492
493 _LIBCPP_ALWAYS_INLINE
494 const char_type* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
495 {
496 return do_narrow(__low, __high, __dfault, __to);
497 }
498
499 static locale::id id;
500
501protected:
502 ~ctype();
503 virtual bool do_is(mask __m, char_type __c) const;
504 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
505 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
506 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
507 virtual char_type do_toupper(char_type) const;
508 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
509 virtual char_type do_tolower(char_type) const;
510 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
511 virtual char_type do_widen(char) const;
512 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
513 virtual char do_narrow(char_type, char __dfault) const;
514 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
515};
516
517template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000518class _LIBCPP_TYPE_VIS ctype<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000519 : public locale::facet, public ctype_base
520{
521 const mask* __tab_;
522 bool __del_;
523public:
524 typedef char char_type;
525
526 explicit ctype(const mask* __tab = 0, bool __del = false, size_t __refs = 0);
527
528 _LIBCPP_ALWAYS_INLINE
529 bool is(mask __m, char_type __c) const
530 {
Marshall Clow11de4872013-10-21 14:41:05 +0000531 return isascii(__c) ? (__tab_[static_cast<int>(__c)] & __m) !=0 : false;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000532 }
533
534 _LIBCPP_ALWAYS_INLINE
535 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
536 {
537 for (; __low != __high; ++__low, ++__vec)
Howard Hinnant28b24882011-12-01 20:21:04 +0000538 *__vec = isascii(*__low) ? __tab_[static_cast<int>(*__low)] : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000539 return __low;
540 }
541
542 _LIBCPP_ALWAYS_INLINE
543 const char_type* scan_is (mask __m, const char_type* __low, const char_type* __high) const
544 {
545 for (; __low != __high; ++__low)
Howard Hinnant28b24882011-12-01 20:21:04 +0000546 if (isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000547 break;
548 return __low;
549 }
550
551 _LIBCPP_ALWAYS_INLINE
552 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
553 {
554 for (; __low != __high; ++__low)
Howard Hinnant28b24882011-12-01 20:21:04 +0000555 if (!(isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m)))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000556 break;
557 return __low;
558 }
559
560 _LIBCPP_ALWAYS_INLINE
561 char_type toupper(char_type __c) const
562 {
563 return do_toupper(__c);
564 }
565
566 _LIBCPP_ALWAYS_INLINE
567 const char_type* toupper(char_type* __low, const char_type* __high) const
568 {
569 return do_toupper(__low, __high);
570 }
571
572 _LIBCPP_ALWAYS_INLINE
573 char_type tolower(char_type __c) const
574 {
575 return do_tolower(__c);
576 }
577
578 _LIBCPP_ALWAYS_INLINE
579 const char_type* tolower(char_type* __low, const char_type* __high) const
580 {
581 return do_tolower(__low, __high);
582 }
583
584 _LIBCPP_ALWAYS_INLINE
585 char_type widen(char __c) const
586 {
587 return do_widen(__c);
588 }
589
590 _LIBCPP_ALWAYS_INLINE
591 const char* widen(const char* __low, const char* __high, char_type* __to) const
592 {
593 return do_widen(__low, __high, __to);
594 }
595
596 _LIBCPP_ALWAYS_INLINE
597 char narrow(char_type __c, char __dfault) const
598 {
599 return do_narrow(__c, __dfault);
600 }
601
602 _LIBCPP_ALWAYS_INLINE
603 const char* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
604 {
605 return do_narrow(__low, __high, __dfault, __to);
606 }
607
608 static locale::id id;
609
Howard Hinnant155c2af2010-05-24 17:49:41 +0000610#ifdef _CACHED_RUNES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000611 static const size_t table_size = _CACHED_RUNES;
Howard Hinnant155c2af2010-05-24 17:49:41 +0000612#else
613 static const size_t table_size = 256; // FIXME: Don't hardcode this.
614#endif
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000615 _LIBCPP_ALWAYS_INLINE const mask* table() const _NOEXCEPT {return __tab_;}
616 static const mask* classic_table() _NOEXCEPT;
Marshall Cloweae03f62013-11-19 18:05:03 +0000617#if defined(__GLIBC__) || defined(__EMSCRIPTEN__)
Alexis Hunt92b0c812011-07-09 00:56:23 +0000618 static const int* __classic_upper_table() _NOEXCEPT;
619 static const int* __classic_lower_table() _NOEXCEPT;
Alexis Hunt5a4dd562011-07-09 01:09:31 +0000620#endif
Joerg Sonnenberger153e4162013-05-17 21:17:34 +0000621#if defined(__NetBSD__)
622 static const short* __classic_upper_table() _NOEXCEPT;
623 static const short* __classic_lower_table() _NOEXCEPT;
624#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000625
626protected:
627 ~ctype();
628 virtual char_type do_toupper(char_type __c) const;
629 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
630 virtual char_type do_tolower(char_type __c) const;
631 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
632 virtual char_type do_widen(char __c) const;
633 virtual const char* do_widen(const char* __low, const char* __high, char_type* __to) const;
634 virtual char do_narrow(char_type __c, char __dfault) const;
635 virtual const char* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const;
636};
637
638// template <class CharT> class ctype_byname;
639
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000640template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY ctype_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000641
642template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000643class _LIBCPP_TYPE_VIS ctype_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000644 : public ctype<char>
645{
646 locale_t __l;
647
648public:
649 explicit ctype_byname(const char*, size_t = 0);
650 explicit ctype_byname(const string&, size_t = 0);
651
652protected:
653 ~ctype_byname();
654 virtual char_type do_toupper(char_type) const;
655 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
656 virtual char_type do_tolower(char_type) const;
657 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
658};
659
660template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000661class _LIBCPP_TYPE_VIS ctype_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000662 : public ctype<wchar_t>
663{
664 locale_t __l;
665
666public:
667 explicit ctype_byname(const char*, size_t = 0);
668 explicit ctype_byname(const string&, size_t = 0);
669
670protected:
671 ~ctype_byname();
672 virtual bool do_is(mask __m, char_type __c) const;
673 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
674 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
675 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
676 virtual char_type do_toupper(char_type) const;
677 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
678 virtual char_type do_tolower(char_type) const;
679 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
680 virtual char_type do_widen(char) const;
681 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
682 virtual char do_narrow(char_type, char __dfault) const;
683 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
684};
685
686template <class _CharT>
687inline _LIBCPP_INLINE_VISIBILITY
688bool
689isspace(_CharT __c, const locale& __loc)
690{
691 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
692}
693
694template <class _CharT>
695inline _LIBCPP_INLINE_VISIBILITY
696bool
697isprint(_CharT __c, const locale& __loc)
698{
699 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c);
700}
701
702template <class _CharT>
703inline _LIBCPP_INLINE_VISIBILITY
704bool
705iscntrl(_CharT __c, const locale& __loc)
706{
707 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c);
708}
709
710template <class _CharT>
711inline _LIBCPP_INLINE_VISIBILITY
712bool
713isupper(_CharT __c, const locale& __loc)
714{
715 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c);
716}
717
718template <class _CharT>
719inline _LIBCPP_INLINE_VISIBILITY
720bool
721islower(_CharT __c, const locale& __loc)
722{
723 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c);
724}
725
726template <class _CharT>
727inline _LIBCPP_INLINE_VISIBILITY
728bool
729isalpha(_CharT __c, const locale& __loc)
730{
731 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c);
732}
733
734template <class _CharT>
735inline _LIBCPP_INLINE_VISIBILITY
736bool
737isdigit(_CharT __c, const locale& __loc)
738{
739 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c);
740}
741
742template <class _CharT>
743inline _LIBCPP_INLINE_VISIBILITY
744bool
745ispunct(_CharT __c, const locale& __loc)
746{
747 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c);
748}
749
750template <class _CharT>
751inline _LIBCPP_INLINE_VISIBILITY
752bool
753isxdigit(_CharT __c, const locale& __loc)
754{
755 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c);
756}
757
758template <class _CharT>
759inline _LIBCPP_INLINE_VISIBILITY
760bool
761isalnum(_CharT __c, const locale& __loc)
762{
763 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c);
764}
765
766template <class _CharT>
767inline _LIBCPP_INLINE_VISIBILITY
768bool
769isgraph(_CharT __c, const locale& __loc)
770{
771 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c);
772}
773
774template <class _CharT>
775inline _LIBCPP_INLINE_VISIBILITY
776_CharT
777toupper(_CharT __c, const locale& __loc)
778{
779 return use_facet<ctype<_CharT> >(__loc).toupper(__c);
780}
781
782template <class _CharT>
783inline _LIBCPP_INLINE_VISIBILITY
784_CharT
785tolower(_CharT __c, const locale& __loc)
786{
787 return use_facet<ctype<_CharT> >(__loc).tolower(__c);
788}
789
790// codecvt_base
791
Howard Hinnant8331b762013-03-06 23:30:19 +0000792class _LIBCPP_TYPE_VIS codecvt_base
Howard Hinnantc51e1022010-05-11 19:42:16 +0000793{
794public:
795 _LIBCPP_ALWAYS_INLINE codecvt_base() {}
796 enum result {ok, partial, error, noconv};
797};
798
799// template <class internT, class externT, class stateT> class codecvt;
800
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000801template <class _InternT, class _ExternT, class _StateT> class _LIBCPP_TYPE_VIS_ONLY codecvt;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000802
803// template <> class codecvt<char, char, mbstate_t>
804
805template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000806class _LIBCPP_TYPE_VIS codecvt<char, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000807 : public locale::facet,
808 public codecvt_base
809{
810public:
811 typedef char intern_type;
812 typedef char extern_type;
813 typedef mbstate_t state_type;
814
815 _LIBCPP_ALWAYS_INLINE
816 explicit codecvt(size_t __refs = 0)
817 : locale::facet(__refs) {}
818
819 _LIBCPP_ALWAYS_INLINE
820 result out(state_type& __st,
821 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
822 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
823 {
824 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
825 }
826
827 _LIBCPP_ALWAYS_INLINE
828 result unshift(state_type& __st,
829 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
830 {
831 return do_unshift(__st, __to, __to_end, __to_nxt);
832 }
833
834 _LIBCPP_ALWAYS_INLINE
835 result in(state_type& __st,
836 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
837 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
838 {
839 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
840 }
841
842 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000843 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000844 {
845 return do_encoding();
846 }
847
848 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000849 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000850 {
851 return do_always_noconv();
852 }
853
854 _LIBCPP_ALWAYS_INLINE
855 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
856 {
857 return do_length(__st, __frm, __end, __mx);
858 }
859
860 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000861 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862 {
863 return do_max_length();
864 }
865
866 static locale::id id;
867
868protected:
869 _LIBCPP_ALWAYS_INLINE
870 explicit codecvt(const char*, size_t __refs = 0)
871 : locale::facet(__refs) {}
872
873 ~codecvt();
874
875 virtual result do_out(state_type& __st,
876 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
877 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
878 virtual result do_in(state_type& __st,
879 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
880 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
881 virtual result do_unshift(state_type& __st,
882 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000883 virtual int do_encoding() const _NOEXCEPT;
884 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000885 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 +0000886 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000887};
888
889// template <> class codecvt<wchar_t, char, mbstate_t>
890
891template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000892class _LIBCPP_TYPE_VIS codecvt<wchar_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000893 : public locale::facet,
894 public codecvt_base
895{
896 locale_t __l;
897public:
898 typedef wchar_t intern_type;
899 typedef char extern_type;
900 typedef mbstate_t state_type;
901
902 explicit codecvt(size_t __refs = 0);
903
904 _LIBCPP_ALWAYS_INLINE
905 result out(state_type& __st,
906 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
907 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
908 {
909 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
910 }
911
912 _LIBCPP_ALWAYS_INLINE
913 result unshift(state_type& __st,
914 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
915 {
916 return do_unshift(__st, __to, __to_end, __to_nxt);
917 }
918
919 _LIBCPP_ALWAYS_INLINE
920 result in(state_type& __st,
921 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
922 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
923 {
924 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
925 }
926
927 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000928 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000929 {
930 return do_encoding();
931 }
932
933 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000934 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000935 {
936 return do_always_noconv();
937 }
938
939 _LIBCPP_ALWAYS_INLINE
940 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
941 {
942 return do_length(__st, __frm, __end, __mx);
943 }
944
945 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000946 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947 {
948 return do_max_length();
949 }
950
951 static locale::id id;
952
953protected:
954 explicit codecvt(const char*, size_t __refs = 0);
955
956 ~codecvt();
957
958 virtual result do_out(state_type& __st,
959 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
960 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
961 virtual result do_in(state_type& __st,
962 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
963 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
964 virtual result do_unshift(state_type& __st,
965 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +0000966 virtual int do_encoding() const _NOEXCEPT;
967 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968 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 +0000969 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970};
971
972// template <> class codecvt<char16_t, char, mbstate_t>
973
974template <>
Howard Hinnant8331b762013-03-06 23:30:19 +0000975class _LIBCPP_TYPE_VIS codecvt<char16_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976 : public locale::facet,
977 public codecvt_base
978{
979public:
980 typedef char16_t intern_type;
981 typedef char extern_type;
982 typedef mbstate_t state_type;
983
984 _LIBCPP_ALWAYS_INLINE
985 explicit codecvt(size_t __refs = 0)
986 : locale::facet(__refs) {}
987
988 _LIBCPP_ALWAYS_INLINE
989 result out(state_type& __st,
990 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
991 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
992 {
993 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
994 }
995
996 _LIBCPP_ALWAYS_INLINE
997 result unshift(state_type& __st,
998 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
999 {
1000 return do_unshift(__st, __to, __to_end, __to_nxt);
1001 }
1002
1003 _LIBCPP_ALWAYS_INLINE
1004 result in(state_type& __st,
1005 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1006 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1007 {
1008 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1009 }
1010
1011 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001012 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013 {
1014 return do_encoding();
1015 }
1016
1017 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001018 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019 {
1020 return do_always_noconv();
1021 }
1022
1023 _LIBCPP_ALWAYS_INLINE
1024 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1025 {
1026 return do_length(__st, __frm, __end, __mx);
1027 }
1028
1029 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001030 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001031 {
1032 return do_max_length();
1033 }
1034
1035 static locale::id id;
1036
1037protected:
1038 _LIBCPP_ALWAYS_INLINE
1039 explicit codecvt(const char*, size_t __refs = 0)
1040 : locale::facet(__refs) {}
1041
1042 ~codecvt();
1043
1044 virtual result do_out(state_type& __st,
1045 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1046 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1047 virtual result do_in(state_type& __st,
1048 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1049 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1050 virtual result do_unshift(state_type& __st,
1051 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001052 virtual int do_encoding() const _NOEXCEPT;
1053 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054 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 +00001055 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001056};
1057
1058// template <> class codecvt<char32_t, char, mbstate_t>
1059
1060template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001061class _LIBCPP_TYPE_VIS codecvt<char32_t, char, mbstate_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001062 : public locale::facet,
1063 public codecvt_base
1064{
1065public:
1066 typedef char32_t intern_type;
1067 typedef char extern_type;
1068 typedef mbstate_t state_type;
1069
1070 _LIBCPP_ALWAYS_INLINE
1071 explicit codecvt(size_t __refs = 0)
1072 : locale::facet(__refs) {}
1073
1074 _LIBCPP_ALWAYS_INLINE
1075 result out(state_type& __st,
1076 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1077 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1078 {
1079 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1080 }
1081
1082 _LIBCPP_ALWAYS_INLINE
1083 result unshift(state_type& __st,
1084 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1085 {
1086 return do_unshift(__st, __to, __to_end, __to_nxt);
1087 }
1088
1089 _LIBCPP_ALWAYS_INLINE
1090 result in(state_type& __st,
1091 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1092 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1093 {
1094 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1095 }
1096
1097 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001098 int encoding() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099 {
1100 return do_encoding();
1101 }
1102
1103 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001104 bool always_noconv() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001105 {
1106 return do_always_noconv();
1107 }
1108
1109 _LIBCPP_ALWAYS_INLINE
1110 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1111 {
1112 return do_length(__st, __frm, __end, __mx);
1113 }
1114
1115 _LIBCPP_ALWAYS_INLINE
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001116 int max_length() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117 {
1118 return do_max_length();
1119 }
1120
1121 static locale::id id;
1122
1123protected:
1124 _LIBCPP_ALWAYS_INLINE
1125 explicit codecvt(const char*, size_t __refs = 0)
1126 : locale::facet(__refs) {}
1127
1128 ~codecvt();
1129
1130 virtual result do_out(state_type& __st,
1131 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1132 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1133 virtual result do_in(state_type& __st,
1134 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1135 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1136 virtual result do_unshift(state_type& __st,
1137 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnant7c9e5732011-05-31 15:34:58 +00001138 virtual int do_encoding() const _NOEXCEPT;
1139 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140 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 +00001141 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142};
1143
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144// template <class _InternT, class _ExternT, class _StateT> class codecvt_byname
1145
1146template <class _InternT, class _ExternT, class _StateT>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001147class _LIBCPP_TYPE_VIS_ONLY codecvt_byname
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148 : public codecvt<_InternT, _ExternT, _StateT>
1149{
1150public:
Howard Hinnant9833cad2010-09-21 18:58:51 +00001151 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152 explicit codecvt_byname(const char* __nm, size_t __refs = 0)
1153 : codecvt<_InternT, _ExternT, _StateT>(__nm, __refs) {}
Howard Hinnant9833cad2010-09-21 18:58:51 +00001154 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155 explicit codecvt_byname(const string& __nm, size_t __refs = 0)
1156 : codecvt<_InternT, _ExternT, _StateT>(__nm.c_str(), __refs) {}
1157protected:
1158 ~codecvt_byname();
1159};
1160
1161template <class _InternT, class _ExternT, class _StateT>
1162codecvt_byname<_InternT, _ExternT, _StateT>::~codecvt_byname()
1163{
1164}
1165
Howard Hinnant8ea98242013-08-23 17:37:05 +00001166_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char, char, mbstate_t>)
1167_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<wchar_t, char, mbstate_t>)
1168_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char16_t, char, mbstate_t>)
1169_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char32_t, char, mbstate_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170
Howard Hinnant8331b762013-03-06 23:30:19 +00001171_LIBCPP_FUNC_VIS void __throw_runtime_error(const char*);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001172
Howard Hinnantc834c512011-11-29 18:15:50 +00001173template <size_t _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174struct __narrow_to_utf8
1175{
1176 template <class _OutputIterator, class _CharT>
1177 _OutputIterator
1178 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const;
1179};
1180
1181template <>
1182struct __narrow_to_utf8<8>
1183{
1184 template <class _OutputIterator, class _CharT>
1185 _LIBCPP_ALWAYS_INLINE
1186 _OutputIterator
1187 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1188 {
1189 for (; __wb < __we; ++__wb, ++__s)
1190 *__s = *__wb;
1191 return __s;
1192 }
1193};
1194
1195template <>
1196struct __narrow_to_utf8<16>
1197 : public codecvt<char16_t, char, mbstate_t>
1198{
1199 _LIBCPP_ALWAYS_INLINE
1200 __narrow_to_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1201
1202 ~__narrow_to_utf8();
1203
1204 template <class _OutputIterator, class _CharT>
1205 _LIBCPP_ALWAYS_INLINE
1206 _OutputIterator
1207 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1208 {
1209 result __r = ok;
1210 mbstate_t __mb;
1211 while (__wb < __we && __r != error)
1212 {
1213 const int __sz = 32;
1214 char __buf[__sz];
1215 char* __bn;
1216 const char16_t* __wn = (const char16_t*)__wb;
1217 __r = do_out(__mb, (const char16_t*)__wb, (const char16_t*)__we, __wn,
1218 __buf, __buf+__sz, __bn);
1219 if (__r == codecvt_base::error || __wn == (const char16_t*)__wb)
1220 __throw_runtime_error("locale not supported");
1221 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1222 *__s = *__p;
1223 __wb = (const _CharT*)__wn;
1224 }
1225 return __s;
1226 }
1227};
1228
1229template <>
1230struct __narrow_to_utf8<32>
1231 : public codecvt<char32_t, char, mbstate_t>
1232{
1233 _LIBCPP_ALWAYS_INLINE
1234 __narrow_to_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1235
1236 ~__narrow_to_utf8();
1237
1238 template <class _OutputIterator, class _CharT>
1239 _LIBCPP_ALWAYS_INLINE
1240 _OutputIterator
1241 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1242 {
1243 result __r = ok;
1244 mbstate_t __mb;
1245 while (__wb < __we && __r != error)
1246 {
1247 const int __sz = 32;
1248 char __buf[__sz];
1249 char* __bn;
1250 const char32_t* __wn = (const char32_t*)__wb;
1251 __r = do_out(__mb, (const char32_t*)__wb, (const char32_t*)__we, __wn,
1252 __buf, __buf+__sz, __bn);
1253 if (__r == codecvt_base::error || __wn == (const char32_t*)__wb)
1254 __throw_runtime_error("locale not supported");
1255 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1256 *__s = *__p;
1257 __wb = (const _CharT*)__wn;
1258 }
1259 return __s;
1260 }
1261};
1262
Howard Hinnantc834c512011-11-29 18:15:50 +00001263template <size_t _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001264struct __widen_from_utf8
1265{
1266 template <class _OutputIterator>
1267 _OutputIterator
1268 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const;
1269};
1270
1271template <>
1272struct __widen_from_utf8<8>
1273{
1274 template <class _OutputIterator>
1275 _LIBCPP_ALWAYS_INLINE
1276 _OutputIterator
1277 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1278 {
1279 for (; __nb < __ne; ++__nb, ++__s)
1280 *__s = *__nb;
1281 return __s;
1282 }
1283};
1284
1285template <>
1286struct __widen_from_utf8<16>
1287 : public codecvt<char16_t, char, mbstate_t>
1288{
1289 _LIBCPP_ALWAYS_INLINE
1290 __widen_from_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1291
1292 ~__widen_from_utf8();
1293
1294 template <class _OutputIterator>
1295 _LIBCPP_ALWAYS_INLINE
1296 _OutputIterator
1297 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1298 {
1299 result __r = ok;
1300 mbstate_t __mb;
1301 while (__nb < __ne && __r != error)
1302 {
1303 const int __sz = 32;
1304 char16_t __buf[__sz];
1305 char16_t* __bn;
1306 const char* __nn = __nb;
1307 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1308 __buf, __buf+__sz, __bn);
1309 if (__r == codecvt_base::error || __nn == __nb)
1310 __throw_runtime_error("locale not supported");
1311 for (const char16_t* __p = __buf; __p < __bn; ++__p, ++__s)
1312 *__s = (wchar_t)*__p;
1313 __nb = __nn;
1314 }
1315 return __s;
1316 }
1317};
1318
1319template <>
1320struct __widen_from_utf8<32>
1321 : public codecvt<char32_t, char, mbstate_t>
1322{
1323 _LIBCPP_ALWAYS_INLINE
1324 __widen_from_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1325
1326 ~__widen_from_utf8();
1327
1328 template <class _OutputIterator>
1329 _LIBCPP_ALWAYS_INLINE
1330 _OutputIterator
1331 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1332 {
1333 result __r = ok;
1334 mbstate_t __mb;
1335 while (__nb < __ne && __r != error)
1336 {
1337 const int __sz = 32;
1338 char32_t __buf[__sz];
1339 char32_t* __bn;
1340 const char* __nn = __nb;
1341 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1342 __buf, __buf+__sz, __bn);
1343 if (__r == codecvt_base::error || __nn == __nb)
1344 __throw_runtime_error("locale not supported");
1345 for (const char32_t* __p = __buf; __p < __bn; ++__p, ++__s)
1346 *__s = (wchar_t)*__p;
1347 __nb = __nn;
1348 }
1349 return __s;
1350 }
1351};
1352
1353// template <class charT> class numpunct
1354
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001355template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY numpunct;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001356
1357template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001358class _LIBCPP_TYPE_VIS numpunct<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001359 : public locale::facet
1360{
1361public:
1362 typedef char char_type;
1363 typedef basic_string<char_type> string_type;
1364
1365 explicit numpunct(size_t __refs = 0);
1366
1367 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
1368 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
1369 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
1370 _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();}
1371 _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();}
1372
1373 static locale::id id;
1374
1375protected:
1376 ~numpunct();
1377 virtual char_type do_decimal_point() const;
1378 virtual char_type do_thousands_sep() const;
1379 virtual string do_grouping() const;
1380 virtual string_type do_truename() const;
1381 virtual string_type do_falsename() const;
1382
1383 char_type __decimal_point_;
1384 char_type __thousands_sep_;
1385 string __grouping_;
1386};
1387
1388template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001389class _LIBCPP_TYPE_VIS numpunct<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001390 : public locale::facet
1391{
1392public:
1393 typedef wchar_t char_type;
1394 typedef basic_string<char_type> string_type;
1395
1396 explicit numpunct(size_t __refs = 0);
1397
1398 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
1399 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
1400 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
1401 _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();}
1402 _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();}
1403
1404 static locale::id id;
1405
1406protected:
1407 ~numpunct();
1408 virtual char_type do_decimal_point() const;
1409 virtual char_type do_thousands_sep() const;
1410 virtual string do_grouping() const;
1411 virtual string_type do_truename() const;
1412 virtual string_type do_falsename() const;
1413
1414 char_type __decimal_point_;
1415 char_type __thousands_sep_;
1416 string __grouping_;
1417};
1418
1419// template <class charT> class numpunct_byname
1420
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001421template <class charT> class _LIBCPP_TYPE_VIS_ONLY numpunct_byname;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001422
1423template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001424class _LIBCPP_TYPE_VIS numpunct_byname<char>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001425: public numpunct<char>
1426{
1427public:
1428 typedef char char_type;
1429 typedef basic_string<char_type> string_type;
1430
1431 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1432 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1433
1434protected:
1435 ~numpunct_byname();
1436
1437private:
1438 void __init(const char*);
1439};
1440
1441template <>
Howard Hinnant8331b762013-03-06 23:30:19 +00001442class _LIBCPP_TYPE_VIS numpunct_byname<wchar_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443: public numpunct<wchar_t>
1444{
1445public:
1446 typedef wchar_t char_type;
1447 typedef basic_string<char_type> string_type;
1448
1449 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1450 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1451
1452protected:
1453 ~numpunct_byname();
1454
1455private:
1456 void __init(const char*);
1457};
1458
1459_LIBCPP_END_NAMESPACE_STD
1460
1461#endif // _LIBCPP___LOCALE