blob: 4184e7e03489b900ec7d0148ada67c945ed56929 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:165//
Howard Hinnantb64f8b02010-11-16 22:09:026// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:168//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP___LOCALE
12#define _LIBCPP___LOCALE
13
14#include <__config>
15#include <string>
16#include <memory>
17#include <utility>
18#include <mutex>
19#include <cstdint>
20#include <cctype>
Howard Hinnantadff4892010-05-24 17:49:4121#include <locale.h>
Howard Hinnantef5aa932013-09-17 01:34:4722#if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
Howard Hinnant14fa9f92011-09-29 20:33:1023# include <support/win32/locale_win32.h>
Marshall Clowbe3d1172014-03-11 17:18:4724#elif defined(_AIX)
Howard Hinnant7f764502013-08-14 18:00:2025# include <support/ibm/xlocale.h>
Marshall Clowa6439262014-07-10 15:20:2826#elif defined(__ANDROID__)
27// Android gained the locale aware functions in L (API level 21)
28# include <android/api-level.h>
29# if __ANDROID_API__ <= 20
30# include <support/android/locale_bionic.h>
31# endif
Eric Fiselier6cb69ff2014-11-25 21:57:4132#elif defined(__sun__)
Eric Fiselierf51d6762015-01-23 22:22:3633# include <xlocale.h>
Eric Fiselier6cb69ff2014-11-25 21:57:4134# include <support/solaris/xlocale.h>
Sergey Dmitrouk984f8f62014-12-12 08:36:1635#elif defined(_NEWLIB_VERSION)
36# include <support/newlib/xlocale.h>
Marshall Clowa6439262014-07-10 15:20:2837#elif (defined(__GLIBC__) || defined(__APPLE__) || defined(__FreeBSD__) \
Eric Fiselier6cb69ff2014-11-25 21:57:4138 || defined(__EMSCRIPTEN__) || defined(__IBMCPP__))
Howard Hinnant92a07002011-09-22 19:10:1839# include <xlocale.h>
Petr Hosek82a50d62017-04-13 21:29:2140#elif defined(__Fuchsia__)
41# include <support/fuchsia/xlocale.h>
Vasileios Kalintiris579b42b2015-11-09 10:21:0442#elif defined(_LIBCPP_HAS_MUSL_LIBC)
43# include <support/musl/xlocale.h>
Petr Hosek82a50d62017-04-13 21:29:2144#endif
Howard Hinnantbc8d3f92010-05-11 19:42:1645
Howard Hinnant08e17472011-10-17 20:05:1046#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:1647#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:1048#endif
Howard Hinnantbc8d3f92010-05-11 19:42:1649
50_LIBCPP_BEGIN_NAMESPACE_STD
51
Howard Hinnant83eade62013-03-06 23:30:1952class _LIBCPP_TYPE_VIS locale;
Howard Hinnantbc8d3f92010-05-11 19:42:1653
Howard Hinnant33be35e2012-09-14 00:39:1654template <class _Facet>
55_LIBCPP_INLINE_VISIBILITY
56bool
57has_facet(const locale&) _NOEXCEPT;
58
59template <class _Facet>
60_LIBCPP_INLINE_VISIBILITY
61const _Facet&
62use_facet(const locale&);
Howard Hinnantbc8d3f92010-05-11 19:42:1663
Howard Hinnant83eade62013-03-06 23:30:1964class _LIBCPP_TYPE_VIS locale
Howard Hinnantbc8d3f92010-05-11 19:42:1665{
66public:
67 // types:
Howard Hinnant83eade62013-03-06 23:30:1968 class _LIBCPP_TYPE_VIS facet;
69 class _LIBCPP_TYPE_VIS id;
Howard Hinnantbc8d3f92010-05-11 19:42:1670
71 typedef int category;
Mehdi Amini907c1192017-05-04 17:08:5472 _LIBCPP_AVAILABILITY_LOCALE_CATEGORY
Howard Hinnantbc8d3f92010-05-11 19:42:1673 static const category // values assigned here are for exposition only
74 none = 0,
75 collate = LC_COLLATE_MASK,
76 ctype = LC_CTYPE_MASK,
77 monetary = LC_MONETARY_MASK,
78 numeric = LC_NUMERIC_MASK,
79 time = LC_TIME_MASK,
80 messages = LC_MESSAGES_MASK,
81 all = collate | ctype | monetary | numeric | time | messages;
82
83 // construct/copy/destroy:
Howard Hinnantc9834542011-05-31 15:34:5884 locale() _NOEXCEPT;
85 locale(const locale&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:1686 explicit locale(const char*);
87 explicit locale(const string&);
88 locale(const locale&, const char*, category);
89 locale(const locale&, const string&, category);
Howard Hinnant2d72b1e2010-12-17 14:46:4390 template <class _Facet>
91 _LIBCPP_INLINE_VISIBILITY locale(const locale&, _Facet*);
Howard Hinnantbc8d3f92010-05-11 19:42:1692 locale(const locale&, const locale&, category);
93
Howard Hinnantc9834542011-05-31 15:34:5894 ~locale();
Howard Hinnantbc8d3f92010-05-11 19:42:1695
Howard Hinnantc9834542011-05-31 15:34:5896 const locale& operator=(const locale&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:1697
Shoaib Meenai6b734922017-03-02 03:22:1898 template <class _Facet>
99 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
100 locale combine(const locale&) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16101
102 // locale operations:
103 string name() const;
104 bool operator==(const locale&) const;
105 bool operator!=(const locale& __y) const {return !(*this == __y);}
106 template <class _CharT, class _Traits, class _Allocator>
Shoaib Meenai6b734922017-03-02 03:22:18107 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantbc8d3f92010-05-11 19:42:16108 bool operator()(const basic_string<_CharT, _Traits, _Allocator>&,
109 const basic_string<_CharT, _Traits, _Allocator>&) const;
110
111 // global locale objects:
112 static locale global(const locale&);
113 static const locale& classic();
114
115private:
116 class __imp;
117 __imp* __locale_;
118
119 void __install_ctor(const locale&, facet*, long);
120 static locale& __global();
121 bool has_facet(id&) const;
122 const facet* use_facet(id&) const;
123
Howard Hinnantc9834542011-05-31 15:34:58124 template <class _Facet> friend bool has_facet(const locale&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16125 template <class _Facet> friend const _Facet& use_facet(const locale&);
126};
127
Howard Hinnant83eade62013-03-06 23:30:19128class _LIBCPP_TYPE_VIS locale::facet
Howard Hinnantbc8d3f92010-05-11 19:42:16129 : public __shared_count
130{
131protected:
Howard Hinnantb0be42b2010-09-21 18:58:51132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16133 explicit facet(size_t __refs = 0)
134 : __shared_count(static_cast<long>(__refs)-1) {}
135
136 virtual ~facet();
137
138// facet(const facet&) = delete; // effectively done in __shared_count
139// void operator=(const facet&) = delete;
140private:
Howard Hinnant1694d232011-05-28 14:41:13141 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16142};
143
Howard Hinnant83eade62013-03-06 23:30:19144class _LIBCPP_TYPE_VIS locale::id
Howard Hinnantbc8d3f92010-05-11 19:42:16145{
146 once_flag __flag_;
147 int32_t __id_;
Howard Hinnant324bb032010-08-22 00:02:43148
Howard Hinnantbc8d3f92010-05-11 19:42:16149 static int32_t __next_id;
150public:
Howard Hinnantf3d62ea2012-07-26 16:14:37151 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR id() :__id_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16152private:
153 void __init();
154 void operator=(const id&); // = delete;
155 id(const id&); // = delete;
156public: // only needed for tests
157 long __get();
158
159 friend class locale;
160 friend class locale::__imp;
161};
162
163template <class _Facet>
164inline _LIBCPP_INLINE_VISIBILITY
165locale::locale(const locale& __other, _Facet* __f)
166{
167 __install_ctor(__other, __f, __f ? __f->id.__get() : 0);
168}
169
170template <class _Facet>
171locale
172locale::combine(const locale& __other) const
173{
Howard Hinnant0949eed2011-06-30 21:18:19174 if (!_VSTD::has_facet<_Facet>(__other))
Marshall Clow14c09a22016-08-25 15:09:01175 __throw_runtime_error("locale::combine: locale missing facet");
176
Howard Hinnant0949eed2011-06-30 21:18:19177 return locale(*this, &const_cast<_Facet&>(_VSTD::use_facet<_Facet>(__other)));
Howard Hinnantbc8d3f92010-05-11 19:42:16178}
179
180template <class _Facet>
181inline _LIBCPP_INLINE_VISIBILITY
182bool
Howard Hinnantc9834542011-05-31 15:34:58183has_facet(const locale& __l) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16184{
185 return __l.has_facet(_Facet::id);
186}
187
188template <class _Facet>
189inline _LIBCPP_INLINE_VISIBILITY
190const _Facet&
191use_facet(const locale& __l)
192{
193 return static_cast<const _Facet&>(*__l.use_facet(_Facet::id));
194}
195
196// template <class _CharT> class collate;
197
198template <class _CharT>
Eric Fiselierc3589a82017-01-04 23:56:00199class _LIBCPP_TEMPLATE_VIS collate
Howard Hinnantbc8d3f92010-05-11 19:42:16200 : public locale::facet
201{
202public:
203 typedef _CharT char_type;
204 typedef basic_string<char_type> string_type;
205
Howard Hinnantb0be42b2010-09-21 18:58:51206 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16207 explicit collate(size_t __refs = 0)
208 : locale::facet(__refs) {}
209
Howard Hinnantb0be42b2010-09-21 18:58:51210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16211 int compare(const char_type* __lo1, const char_type* __hi1,
212 const char_type* __lo2, const char_type* __hi2) const
213 {
214 return do_compare(__lo1, __hi1, __lo2, __hi2);
215 }
216
Howard Hinnantb0be42b2010-09-21 18:58:51217 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16218 string_type transform(const char_type* __lo, const char_type* __hi) const
219 {
220 return do_transform(__lo, __hi);
221 }
222
Howard Hinnantb0be42b2010-09-21 18:58:51223 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16224 long hash(const char_type* __lo, const char_type* __hi) const
225 {
226 return do_hash(__lo, __hi);
227 }
228
229 static locale::id id;
230
231protected:
232 ~collate();
233 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
234 const char_type* __lo2, const char_type* __hi2) const;
235 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const
236 {return string_type(__lo, __hi);}
237 virtual long do_hash(const char_type* __lo, const char_type* __hi) const;
238};
239
240template <class _CharT> locale::id collate<_CharT>::id;
241
242template <class _CharT>
243collate<_CharT>::~collate()
244{
245}
246
247template <class _CharT>
248int
249collate<_CharT>::do_compare(const char_type* __lo1, const char_type* __hi1,
250 const char_type* __lo2, const char_type* __hi2) const
251{
252 for (; __lo2 != __hi2; ++__lo1, ++__lo2)
253 {
254 if (__lo1 == __hi1 || *__lo1 < *__lo2)
255 return -1;
256 if (*__lo2 < *__lo1)
257 return 1;
258 }
259 return __lo1 != __hi1;
260}
261
262template <class _CharT>
263long
Howard Hinnant11624452011-10-11 16:00:46264collate<_CharT>::do_hash(const char_type* __lo, const char_type* __hi) const
Howard Hinnantbc8d3f92010-05-11 19:42:16265{
Howard Hinnant11624452011-10-11 16:00:46266 size_t __h = 0;
267 const size_t __sr = __CHAR_BIT__ * sizeof(size_t) - 8;
268 const size_t __mask = size_t(0xF) << (__sr + 4);
269 for(const char_type* __p = __lo; __p != __hi; ++__p)
Howard Hinnantbc8d3f92010-05-11 19:42:16270 {
Howard Hinnantec3773c2011-12-01 20:21:04271 __h = (__h << 4) + static_cast<size_t>(*__p);
Howard Hinnant11624452011-10-11 16:00:46272 size_t __g = __h & __mask;
273 __h ^= __g | (__g >> __sr);
Howard Hinnantbc8d3f92010-05-11 19:42:16274 }
Howard Hinnant11624452011-10-11 16:00:46275 return static_cast<long>(__h);
Howard Hinnantbc8d3f92010-05-11 19:42:16276}
277
Eric Fiselier833d6442016-09-15 22:27:07278_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<char>)
279_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<wchar_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:16280
281// template <class CharT> class collate_byname;
282
Eric Fiselierc3589a82017-01-04 23:56:00283template <class _CharT> class _LIBCPP_TEMPLATE_VIS collate_byname;
Howard Hinnantbc8d3f92010-05-11 19:42:16284
285template <>
Howard Hinnant83eade62013-03-06 23:30:19286class _LIBCPP_TYPE_VIS collate_byname<char>
Howard Hinnantbc8d3f92010-05-11 19:42:16287 : public collate<char>
288{
289 locale_t __l;
290public:
291 typedef char char_type;
292 typedef basic_string<char_type> string_type;
293
294 explicit collate_byname(const char* __n, size_t __refs = 0);
295 explicit collate_byname(const string& __n, size_t __refs = 0);
296
297protected:
298 ~collate_byname();
299 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
300 const char_type* __lo2, const char_type* __hi2) const;
301 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
302};
303
304template <>
Howard Hinnant83eade62013-03-06 23:30:19305class _LIBCPP_TYPE_VIS collate_byname<wchar_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16306 : public collate<wchar_t>
307{
308 locale_t __l;
309public:
310 typedef wchar_t char_type;
311 typedef basic_string<char_type> string_type;
312
313 explicit collate_byname(const char* __n, size_t __refs = 0);
314 explicit collate_byname(const string& __n, size_t __refs = 0);
315
316protected:
317 ~collate_byname();
318
319 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
320 const char_type* __lo2, const char_type* __hi2) const;
321 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
322};
323
324template <class _CharT, class _Traits, class _Allocator>
325bool
326locale::operator()(const basic_string<_CharT, _Traits, _Allocator>& __x,
327 const basic_string<_CharT, _Traits, _Allocator>& __y) const
328{
Howard Hinnant0949eed2011-06-30 21:18:19329 return _VSTD::use_facet<_VSTD::collate<_CharT> >(*this).compare(
Howard Hinnantbc8d3f92010-05-11 19:42:16330 __x.data(), __x.data() + __x.size(),
331 __y.data(), __y.data() + __y.size()) < 0;
332}
333
334// template <class charT> class ctype
335
Howard Hinnant83eade62013-03-06 23:30:19336class _LIBCPP_TYPE_VIS ctype_base
Howard Hinnantb0be42b2010-09-21 18:58:51337{
Howard Hinnantbc8d3f92010-05-11 19:42:16338public:
Vasileios Kalintiris579b42b2015-11-09 10:21:04339#if defined(__GLIBC__)
Sean Hunt62a6ac32011-07-09 00:56:23340 typedef unsigned short mask;
Howard Hinnantadff4892010-05-24 17:49:41341 static const mask space = _ISspace;
342 static const mask print = _ISprint;
343 static const mask cntrl = _IScntrl;
344 static const mask upper = _ISupper;
345 static const mask lower = _ISlower;
346 static const mask alpha = _ISalpha;
347 static const mask digit = _ISdigit;
348 static const mask punct = _ISpunct;
349 static const mask xdigit = _ISxdigit;
350 static const mask blank = _ISblank;
Saleem Abdulrasoole34f9d52017-01-03 21:53:51351#elif defined(_LIBCPP_MSVCRT)
Howard Hinnant3c466fc2011-09-29 13:33:15352 typedef unsigned short mask;
Howard Hinnant92a07002011-09-22 19:10:18353 static const mask space = _SPACE;
354 static const mask print = _BLANK|_PUNCT|_ALPHA|_DIGIT;
355 static const mask cntrl = _CONTROL;
356 static const mask upper = _UPPER;
357 static const mask lower = _LOWER;
358 static const mask alpha = _ALPHA;
359 static const mask digit = _DIGIT;
360 static const mask punct = _PUNCT;
361 static const mask xdigit = _HEX;
362 static const mask blank = _BLANK;
Jonathan Roelofs6fb33ea2015-03-11 17:00:28363# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
Dan Albert6272ae52015-03-11 00:51:06364#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__)
JF Bastiena14f7cb2015-02-25 22:16:46365# ifdef __APPLE__
David Chisnallc512df12011-09-21 08:39:44366 typedef __uint32_t mask;
JF Bastiena14f7cb2015-02-25 22:16:46367# elif defined(__FreeBSD__)
David Chisnallc512df12011-09-21 08:39:44368 typedef unsigned long mask;
Vasileios Kalintiris5e00a712015-11-24 10:24:54369# elif defined(__EMSCRIPTEN__) || defined(__NetBSD__)
Howard Hinnantfc2f0212013-03-29 18:27:28370 typedef unsigned short mask;
JF Bastiena14f7cb2015-02-25 22:16:46371# endif
David Chisnallc512df12011-09-21 08:39:44372 static const mask space = _CTYPE_S;
373 static const mask print = _CTYPE_R;
374 static const mask cntrl = _CTYPE_C;
375 static const mask upper = _CTYPE_U;
376 static const mask lower = _CTYPE_L;
377 static const mask alpha = _CTYPE_A;
378 static const mask digit = _CTYPE_D;
379 static const mask punct = _CTYPE_P;
380 static const mask xdigit = _CTYPE_X;
Dan Albert2a52a322014-07-23 19:32:03381
Joerg Sonnenbergera71a9522013-05-17 21:17:34382# if defined(__NetBSD__)
383 static const mask blank = _CTYPE_BL;
384# else
David Chisnallc512df12011-09-21 08:39:44385 static const mask blank = _CTYPE_B;
Joerg Sonnenbergera71a9522013-05-17 21:17:34386# endif
Howard Hinnantcb0e6b62013-08-30 14:42:39387#elif defined(__sun__) || defined(_AIX)
David Chisnall997e4542012-02-29 13:05:08388 typedef unsigned int mask;
389 static const mask space = _ISSPACE;
390 static const mask print = _ISPRINT;
391 static const mask cntrl = _ISCNTRL;
392 static const mask upper = _ISUPPER;
393 static const mask lower = _ISLOWER;
394 static const mask alpha = _ISALPHA;
395 static const mask digit = _ISDIGIT;
396 static const mask punct = _ISPUNCT;
397 static const mask xdigit = _ISXDIGIT;
398 static const mask blank = _ISBLANK;
JF Bastiena14f7cb2015-02-25 22:16:46399#elif defined(_NEWLIB_VERSION)
400 // Same type as Newlib's _ctype_ array in newlib/libc/include/ctype.h.
401 typedef char mask;
402 static const mask space = _S;
403 static const mask print = _P | _U | _L | _N | _B;
404 static const mask cntrl = _C;
405 static const mask upper = _U;
406 static const mask lower = _L;
407 static const mask alpha = _U | _L;
408 static const mask digit = _N;
409 static const mask punct = _P;
410 static const mask xdigit = _X | _N;
411 static const mask blank = _B;
Jonathan Roelofs6fb33ea2015-03-11 17:00:28412# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
413# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
414# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_XDIGIT
JF Bastiena14f7cb2015-02-25 22:16:46415#else
Howard Hinnant11624452011-10-11 16:00:46416 typedef unsigned long mask;
417 static const mask space = 1<<0;
418 static const mask print = 1<<1;
419 static const mask cntrl = 1<<2;
420 static const mask upper = 1<<3;
421 static const mask lower = 1<<4;
422 static const mask alpha = 1<<5;
423 static const mask digit = 1<<6;
424 static const mask punct = 1<<7;
425 static const mask xdigit = 1<<8;
426 static const mask blank = 1<<9;
JF Bastiena14f7cb2015-02-25 22:16:46427#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16428 static const mask alnum = alpha | digit;
429 static const mask graph = alnum | punct;
430
431 _LIBCPP_ALWAYS_INLINE ctype_base() {}
432};
433
Eric Fiselierc3589a82017-01-04 23:56:00434template <class _CharT> class _LIBCPP_TEMPLATE_VIS ctype;
Howard Hinnantbc8d3f92010-05-11 19:42:16435
436template <>
Howard Hinnant83eade62013-03-06 23:30:19437class _LIBCPP_TYPE_VIS ctype<wchar_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16438 : public locale::facet,
439 public ctype_base
440{
441public:
442 typedef wchar_t char_type;
Howard Hinnant324bb032010-08-22 00:02:43443
Howard Hinnantbc8d3f92010-05-11 19:42:16444 _LIBCPP_ALWAYS_INLINE
445 explicit ctype(size_t __refs = 0)
446 : locale::facet(__refs) {}
447
448 _LIBCPP_ALWAYS_INLINE
449 bool is(mask __m, char_type __c) const
450 {
451 return do_is(__m, __c);
452 }
453
454 _LIBCPP_ALWAYS_INLINE
455 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
456 {
457 return do_is(__low, __high, __vec);
458 }
459
460 _LIBCPP_ALWAYS_INLINE
461 const char_type* scan_is(mask __m, const char_type* __low, const char_type* __high) const
462 {
463 return do_scan_is(__m, __low, __high);
464 }
465
466 _LIBCPP_ALWAYS_INLINE
467 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
468 {
469 return do_scan_not(__m, __low, __high);
470 }
471
472 _LIBCPP_ALWAYS_INLINE
473 char_type toupper(char_type __c) const
474 {
475 return do_toupper(__c);
476 }
477
478 _LIBCPP_ALWAYS_INLINE
479 const char_type* toupper(char_type* __low, const char_type* __high) const
480 {
481 return do_toupper(__low, __high);
482 }
483
484 _LIBCPP_ALWAYS_INLINE
485 char_type tolower(char_type __c) const
486 {
487 return do_tolower(__c);
488 }
489
490 _LIBCPP_ALWAYS_INLINE
491 const char_type* tolower(char_type* __low, const char_type* __high) const
492 {
493 return do_tolower(__low, __high);
494 }
495
496 _LIBCPP_ALWAYS_INLINE
497 char_type widen(char __c) const
498 {
499 return do_widen(__c);
500 }
501
502 _LIBCPP_ALWAYS_INLINE
503 const char* widen(const char* __low, const char* __high, char_type* __to) const
504 {
505 return do_widen(__low, __high, __to);
506 }
507
508 _LIBCPP_ALWAYS_INLINE
509 char narrow(char_type __c, char __dfault) const
510 {
511 return do_narrow(__c, __dfault);
512 }
513
514 _LIBCPP_ALWAYS_INLINE
515 const char_type* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
516 {
517 return do_narrow(__low, __high, __dfault, __to);
518 }
519
520 static locale::id id;
521
522protected:
523 ~ctype();
524 virtual bool do_is(mask __m, char_type __c) const;
525 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
526 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
527 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
528 virtual char_type do_toupper(char_type) const;
529 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
530 virtual char_type do_tolower(char_type) const;
531 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
532 virtual char_type do_widen(char) const;
533 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
534 virtual char do_narrow(char_type, char __dfault) const;
535 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
536};
537
538template <>
Howard Hinnant83eade62013-03-06 23:30:19539class _LIBCPP_TYPE_VIS ctype<char>
Howard Hinnantbc8d3f92010-05-11 19:42:16540 : public locale::facet, public ctype_base
541{
542 const mask* __tab_;
543 bool __del_;
544public:
545 typedef char char_type;
546
547 explicit ctype(const mask* __tab = 0, bool __del = false, size_t __refs = 0);
548
549 _LIBCPP_ALWAYS_INLINE
550 bool is(mask __m, char_type __c) const
551 {
Marshall Clow8a43fca2013-10-21 14:41:05552 return isascii(__c) ? (__tab_[static_cast<int>(__c)] & __m) !=0 : false;
Howard Hinnantbc8d3f92010-05-11 19:42:16553 }
554
555 _LIBCPP_ALWAYS_INLINE
556 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
557 {
558 for (; __low != __high; ++__low, ++__vec)
Howard Hinnantec3773c2011-12-01 20:21:04559 *__vec = isascii(*__low) ? __tab_[static_cast<int>(*__low)] : 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16560 return __low;
561 }
562
563 _LIBCPP_ALWAYS_INLINE
564 const char_type* scan_is (mask __m, const char_type* __low, const char_type* __high) const
565 {
566 for (; __low != __high; ++__low)
Howard Hinnantec3773c2011-12-01 20:21:04567 if (isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m))
Howard Hinnantbc8d3f92010-05-11 19:42:16568 break;
569 return __low;
570 }
571
572 _LIBCPP_ALWAYS_INLINE
573 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
574 {
575 for (; __low != __high; ++__low)
Howard Hinnantec3773c2011-12-01 20:21:04576 if (!(isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m)))
Howard Hinnantbc8d3f92010-05-11 19:42:16577 break;
578 return __low;
579 }
580
581 _LIBCPP_ALWAYS_INLINE
582 char_type toupper(char_type __c) const
583 {
584 return do_toupper(__c);
585 }
586
587 _LIBCPP_ALWAYS_INLINE
588 const char_type* toupper(char_type* __low, const char_type* __high) const
589 {
590 return do_toupper(__low, __high);
591 }
592
593 _LIBCPP_ALWAYS_INLINE
594 char_type tolower(char_type __c) const
595 {
596 return do_tolower(__c);
597 }
598
599 _LIBCPP_ALWAYS_INLINE
600 const char_type* tolower(char_type* __low, const char_type* __high) const
601 {
602 return do_tolower(__low, __high);
603 }
604
605 _LIBCPP_ALWAYS_INLINE
606 char_type widen(char __c) const
607 {
608 return do_widen(__c);
609 }
610
611 _LIBCPP_ALWAYS_INLINE
612 const char* widen(const char* __low, const char* __high, char_type* __to) const
613 {
614 return do_widen(__low, __high, __to);
615 }
616
617 _LIBCPP_ALWAYS_INLINE
618 char narrow(char_type __c, char __dfault) const
619 {
620 return do_narrow(__c, __dfault);
621 }
622
623 _LIBCPP_ALWAYS_INLINE
624 const char* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
625 {
626 return do_narrow(__low, __high, __dfault, __to);
627 }
628
629 static locale::id id;
630
Howard Hinnantadff4892010-05-24 17:49:41631#ifdef _CACHED_RUNES
Howard Hinnantbc8d3f92010-05-11 19:42:16632 static const size_t table_size = _CACHED_RUNES;
Howard Hinnantadff4892010-05-24 17:49:41633#else
634 static const size_t table_size = 256; // FIXME: Don't hardcode this.
635#endif
Howard Hinnantc9834542011-05-31 15:34:58636 _LIBCPP_ALWAYS_INLINE const mask* table() const _NOEXCEPT {return __tab_;}
637 static const mask* classic_table() _NOEXCEPT;
Vasileios Kalintiris5e00a712015-11-24 10:24:54638#if defined(__GLIBC__) || defined(__EMSCRIPTEN__)
Sean Hunt62a6ac32011-07-09 00:56:23639 static const int* __classic_upper_table() _NOEXCEPT;
640 static const int* __classic_lower_table() _NOEXCEPT;
Sean Hunte59f7242011-07-09 01:09:31641#endif
Joerg Sonnenbergera71a9522013-05-17 21:17:34642#if defined(__NetBSD__)
643 static const short* __classic_upper_table() _NOEXCEPT;
644 static const short* __classic_lower_table() _NOEXCEPT;
645#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16646
647protected:
648 ~ctype();
649 virtual char_type do_toupper(char_type __c) const;
650 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
651 virtual char_type do_tolower(char_type __c) const;
652 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
653 virtual char_type do_widen(char __c) const;
654 virtual const char* do_widen(const char* __low, const char* __high, char_type* __to) const;
655 virtual char do_narrow(char_type __c, char __dfault) const;
656 virtual const char* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const;
657};
658
659// template <class CharT> class ctype_byname;
660
Eric Fiselierc3589a82017-01-04 23:56:00661template <class _CharT> class _LIBCPP_TEMPLATE_VIS ctype_byname;
Howard Hinnantbc8d3f92010-05-11 19:42:16662
663template <>
Howard Hinnant83eade62013-03-06 23:30:19664class _LIBCPP_TYPE_VIS ctype_byname<char>
Howard Hinnantbc8d3f92010-05-11 19:42:16665 : public ctype<char>
666{
667 locale_t __l;
668
669public:
670 explicit ctype_byname(const char*, size_t = 0);
671 explicit ctype_byname(const string&, size_t = 0);
672
673protected:
674 ~ctype_byname();
675 virtual char_type do_toupper(char_type) const;
676 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
677 virtual char_type do_tolower(char_type) const;
678 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
679};
680
681template <>
Howard Hinnant83eade62013-03-06 23:30:19682class _LIBCPP_TYPE_VIS ctype_byname<wchar_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16683 : public ctype<wchar_t>
684{
685 locale_t __l;
686
687public:
688 explicit ctype_byname(const char*, size_t = 0);
689 explicit ctype_byname(const string&, size_t = 0);
690
691protected:
692 ~ctype_byname();
693 virtual bool do_is(mask __m, char_type __c) const;
694 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
695 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
696 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
697 virtual char_type do_toupper(char_type) const;
698 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
699 virtual char_type do_tolower(char_type) const;
700 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
701 virtual char_type do_widen(char) const;
702 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
703 virtual char do_narrow(char_type, char __dfault) const;
704 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
705};
706
707template <class _CharT>
708inline _LIBCPP_INLINE_VISIBILITY
709bool
710isspace(_CharT __c, const locale& __loc)
711{
712 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
713}
714
715template <class _CharT>
716inline _LIBCPP_INLINE_VISIBILITY
717bool
718isprint(_CharT __c, const locale& __loc)
719{
720 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c);
721}
722
723template <class _CharT>
724inline _LIBCPP_INLINE_VISIBILITY
725bool
726iscntrl(_CharT __c, const locale& __loc)
727{
728 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c);
729}
730
731template <class _CharT>
732inline _LIBCPP_INLINE_VISIBILITY
733bool
734isupper(_CharT __c, const locale& __loc)
735{
736 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c);
737}
738
739template <class _CharT>
740inline _LIBCPP_INLINE_VISIBILITY
741bool
742islower(_CharT __c, const locale& __loc)
743{
744 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c);
745}
746
747template <class _CharT>
748inline _LIBCPP_INLINE_VISIBILITY
749bool
750isalpha(_CharT __c, const locale& __loc)
751{
752 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c);
753}
754
755template <class _CharT>
756inline _LIBCPP_INLINE_VISIBILITY
757bool
758isdigit(_CharT __c, const locale& __loc)
759{
760 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c);
761}
762
763template <class _CharT>
764inline _LIBCPP_INLINE_VISIBILITY
765bool
766ispunct(_CharT __c, const locale& __loc)
767{
768 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c);
769}
770
771template <class _CharT>
772inline _LIBCPP_INLINE_VISIBILITY
773bool
774isxdigit(_CharT __c, const locale& __loc)
775{
776 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c);
777}
778
779template <class _CharT>
780inline _LIBCPP_INLINE_VISIBILITY
781bool
782isalnum(_CharT __c, const locale& __loc)
783{
784 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c);
785}
786
787template <class _CharT>
788inline _LIBCPP_INLINE_VISIBILITY
789bool
790isgraph(_CharT __c, const locale& __loc)
791{
792 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c);
793}
794
795template <class _CharT>
796inline _LIBCPP_INLINE_VISIBILITY
797_CharT
798toupper(_CharT __c, const locale& __loc)
799{
800 return use_facet<ctype<_CharT> >(__loc).toupper(__c);
801}
802
803template <class _CharT>
804inline _LIBCPP_INLINE_VISIBILITY
805_CharT
806tolower(_CharT __c, const locale& __loc)
807{
808 return use_facet<ctype<_CharT> >(__loc).tolower(__c);
809}
810
811// codecvt_base
812
Howard Hinnant83eade62013-03-06 23:30:19813class _LIBCPP_TYPE_VIS codecvt_base
Howard Hinnantbc8d3f92010-05-11 19:42:16814{
815public:
816 _LIBCPP_ALWAYS_INLINE codecvt_base() {}
817 enum result {ok, partial, error, noconv};
818};
819
820// template <class internT, class externT, class stateT> class codecvt;
821
Eric Fiselierc3589a82017-01-04 23:56:00822template <class _InternT, class _ExternT, class _StateT> class _LIBCPP_TEMPLATE_VIS codecvt;
Howard Hinnantbc8d3f92010-05-11 19:42:16823
824// template <> class codecvt<char, char, mbstate_t>
825
826template <>
Howard Hinnant83eade62013-03-06 23:30:19827class _LIBCPP_TYPE_VIS codecvt<char, char, mbstate_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16828 : public locale::facet,
829 public codecvt_base
830{
831public:
832 typedef char intern_type;
833 typedef char extern_type;
834 typedef mbstate_t state_type;
835
836 _LIBCPP_ALWAYS_INLINE
837 explicit codecvt(size_t __refs = 0)
838 : locale::facet(__refs) {}
839
840 _LIBCPP_ALWAYS_INLINE
841 result out(state_type& __st,
842 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
843 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
844 {
845 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
846 }
847
848 _LIBCPP_ALWAYS_INLINE
849 result unshift(state_type& __st,
850 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
851 {
852 return do_unshift(__st, __to, __to_end, __to_nxt);
853 }
854
855 _LIBCPP_ALWAYS_INLINE
856 result in(state_type& __st,
857 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
858 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
859 {
860 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
861 }
862
863 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:58864 int encoding() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16865 {
866 return do_encoding();
867 }
868
869 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:58870 bool always_noconv() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16871 {
872 return do_always_noconv();
873 }
874
875 _LIBCPP_ALWAYS_INLINE
876 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
877 {
878 return do_length(__st, __frm, __end, __mx);
879 }
880
881 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:58882 int max_length() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16883 {
884 return do_max_length();
885 }
886
887 static locale::id id;
888
889protected:
890 _LIBCPP_ALWAYS_INLINE
891 explicit codecvt(const char*, size_t __refs = 0)
892 : locale::facet(__refs) {}
893
894 ~codecvt();
895
896 virtual result do_out(state_type& __st,
897 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
898 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
899 virtual result do_in(state_type& __st,
900 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
901 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
902 virtual result do_unshift(state_type& __st,
903 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnantc9834542011-05-31 15:34:58904 virtual int do_encoding() const _NOEXCEPT;
905 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16906 virtual int do_length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
Howard Hinnantc9834542011-05-31 15:34:58907 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16908};
909
910// template <> class codecvt<wchar_t, char, mbstate_t>
911
912template <>
Howard Hinnant83eade62013-03-06 23:30:19913class _LIBCPP_TYPE_VIS codecvt<wchar_t, char, mbstate_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16914 : public locale::facet,
915 public codecvt_base
916{
917 locale_t __l;
918public:
919 typedef wchar_t intern_type;
920 typedef char extern_type;
921 typedef mbstate_t state_type;
922
923 explicit codecvt(size_t __refs = 0);
924
925 _LIBCPP_ALWAYS_INLINE
926 result out(state_type& __st,
927 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
928 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
929 {
930 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
931 }
932
933 _LIBCPP_ALWAYS_INLINE
934 result unshift(state_type& __st,
935 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
936 {
937 return do_unshift(__st, __to, __to_end, __to_nxt);
938 }
939
940 _LIBCPP_ALWAYS_INLINE
941 result in(state_type& __st,
942 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
943 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
944 {
945 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
946 }
947
948 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:58949 int encoding() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16950 {
951 return do_encoding();
952 }
953
954 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:58955 bool always_noconv() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16956 {
957 return do_always_noconv();
958 }
959
960 _LIBCPP_ALWAYS_INLINE
961 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
962 {
963 return do_length(__st, __frm, __end, __mx);
964 }
965
966 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:58967 int max_length() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16968 {
969 return do_max_length();
970 }
971
972 static locale::id id;
973
974protected:
975 explicit codecvt(const char*, size_t __refs = 0);
976
977 ~codecvt();
978
979 virtual result do_out(state_type& __st,
980 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
981 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
982 virtual result do_in(state_type& __st,
983 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
984 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
985 virtual result do_unshift(state_type& __st,
986 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnantc9834542011-05-31 15:34:58987 virtual int do_encoding() const _NOEXCEPT;
988 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16989 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
Howard Hinnantc9834542011-05-31 15:34:58990 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16991};
992
993// template <> class codecvt<char16_t, char, mbstate_t>
994
995template <>
Howard Hinnant83eade62013-03-06 23:30:19996class _LIBCPP_TYPE_VIS codecvt<char16_t, char, mbstate_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16997 : public locale::facet,
998 public codecvt_base
999{
1000public:
1001 typedef char16_t intern_type;
1002 typedef char extern_type;
1003 typedef mbstate_t state_type;
1004
1005 _LIBCPP_ALWAYS_INLINE
1006 explicit codecvt(size_t __refs = 0)
1007 : locale::facet(__refs) {}
1008
1009 _LIBCPP_ALWAYS_INLINE
1010 result out(state_type& __st,
1011 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1012 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1013 {
1014 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1015 }
1016
1017 _LIBCPP_ALWAYS_INLINE
1018 result unshift(state_type& __st,
1019 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1020 {
1021 return do_unshift(__st, __to, __to_end, __to_nxt);
1022 }
1023
1024 _LIBCPP_ALWAYS_INLINE
1025 result in(state_type& __st,
1026 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1027 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1028 {
1029 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1030 }
1031
1032 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:581033 int encoding() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161034 {
1035 return do_encoding();
1036 }
1037
1038 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:581039 bool always_noconv() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161040 {
1041 return do_always_noconv();
1042 }
1043
1044 _LIBCPP_ALWAYS_INLINE
1045 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1046 {
1047 return do_length(__st, __frm, __end, __mx);
1048 }
1049
1050 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:581051 int max_length() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161052 {
1053 return do_max_length();
1054 }
1055
1056 static locale::id id;
1057
1058protected:
1059 _LIBCPP_ALWAYS_INLINE
1060 explicit codecvt(const char*, size_t __refs = 0)
1061 : locale::facet(__refs) {}
1062
1063 ~codecvt();
1064
1065 virtual result do_out(state_type& __st,
1066 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1067 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1068 virtual result do_in(state_type& __st,
1069 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1070 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1071 virtual result do_unshift(state_type& __st,
1072 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnantc9834542011-05-31 15:34:581073 virtual int do_encoding() const _NOEXCEPT;
1074 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:161075 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
Howard Hinnantc9834542011-05-31 15:34:581076 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:161077};
1078
1079// template <> class codecvt<char32_t, char, mbstate_t>
1080
1081template <>
Howard Hinnant83eade62013-03-06 23:30:191082class _LIBCPP_TYPE_VIS codecvt<char32_t, char, mbstate_t>
Howard Hinnantbc8d3f92010-05-11 19:42:161083 : public locale::facet,
1084 public codecvt_base
1085{
1086public:
1087 typedef char32_t intern_type;
1088 typedef char extern_type;
1089 typedef mbstate_t state_type;
1090
1091 _LIBCPP_ALWAYS_INLINE
1092 explicit codecvt(size_t __refs = 0)
1093 : locale::facet(__refs) {}
1094
1095 _LIBCPP_ALWAYS_INLINE
1096 result out(state_type& __st,
1097 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1098 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1099 {
1100 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1101 }
1102
1103 _LIBCPP_ALWAYS_INLINE
1104 result unshift(state_type& __st,
1105 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1106 {
1107 return do_unshift(__st, __to, __to_end, __to_nxt);
1108 }
1109
1110 _LIBCPP_ALWAYS_INLINE
1111 result in(state_type& __st,
1112 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1113 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1114 {
1115 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1116 }
1117
1118 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:581119 int encoding() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161120 {
1121 return do_encoding();
1122 }
1123
1124 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:581125 bool always_noconv() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161126 {
1127 return do_always_noconv();
1128 }
1129
1130 _LIBCPP_ALWAYS_INLINE
1131 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1132 {
1133 return do_length(__st, __frm, __end, __mx);
1134 }
1135
1136 _LIBCPP_ALWAYS_INLINE
Howard Hinnantc9834542011-05-31 15:34:581137 int max_length() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161138 {
1139 return do_max_length();
1140 }
1141
1142 static locale::id id;
1143
1144protected:
1145 _LIBCPP_ALWAYS_INLINE
1146 explicit codecvt(const char*, size_t __refs = 0)
1147 : locale::facet(__refs) {}
1148
1149 ~codecvt();
1150
1151 virtual result do_out(state_type& __st,
1152 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1153 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1154 virtual result do_in(state_type& __st,
1155 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1156 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1157 virtual result do_unshift(state_type& __st,
1158 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
Howard Hinnantc9834542011-05-31 15:34:581159 virtual int do_encoding() const _NOEXCEPT;
1160 virtual bool do_always_noconv() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:161161 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
Howard Hinnantc9834542011-05-31 15:34:581162 virtual int do_max_length() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:161163};
1164
Howard Hinnantbc8d3f92010-05-11 19:42:161165// template <class _InternT, class _ExternT, class _StateT> class codecvt_byname
1166
1167template <class _InternT, class _ExternT, class _StateT>
Eric Fiselierc3589a82017-01-04 23:56:001168class _LIBCPP_TEMPLATE_VIS codecvt_byname
Howard Hinnantbc8d3f92010-05-11 19:42:161169 : public codecvt<_InternT, _ExternT, _StateT>
1170{
1171public:
Howard Hinnantb0be42b2010-09-21 18:58:511172 _LIBCPP_ALWAYS_INLINE
Howard Hinnantbc8d3f92010-05-11 19:42:161173 explicit codecvt_byname(const char* __nm, size_t __refs = 0)
1174 : codecvt<_InternT, _ExternT, _StateT>(__nm, __refs) {}
Howard Hinnantb0be42b2010-09-21 18:58:511175 _LIBCPP_ALWAYS_INLINE
Howard Hinnantbc8d3f92010-05-11 19:42:161176 explicit codecvt_byname(const string& __nm, size_t __refs = 0)
1177 : codecvt<_InternT, _ExternT, _StateT>(__nm.c_str(), __refs) {}
1178protected:
1179 ~codecvt_byname();
1180};
1181
1182template <class _InternT, class _ExternT, class _StateT>
1183codecvt_byname<_InternT, _ExternT, _StateT>::~codecvt_byname()
1184{
1185}
1186
Eric Fiselier833d6442016-09-15 22:27:071187_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char, char, mbstate_t>)
1188_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<wchar_t, char, mbstate_t>)
1189_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char16_t, char, mbstate_t>)
1190_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char32_t, char, mbstate_t>)
Howard Hinnantbc8d3f92010-05-11 19:42:161191
Aditya Kumar5db67372016-08-27 02:26:421192_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_runtime_error(const char*);
1193
Howard Hinnant99968442011-11-29 18:15:501194template <size_t _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:161195struct __narrow_to_utf8
1196{
1197 template <class _OutputIterator, class _CharT>
1198 _OutputIterator
1199 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const;
1200};
1201
1202template <>
1203struct __narrow_to_utf8<8>
1204{
1205 template <class _OutputIterator, class _CharT>
1206 _LIBCPP_ALWAYS_INLINE
1207 _OutputIterator
1208 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1209 {
1210 for (; __wb < __we; ++__wb, ++__s)
1211 *__s = *__wb;
1212 return __s;
1213 }
1214};
1215
1216template <>
1217struct __narrow_to_utf8<16>
1218 : public codecvt<char16_t, char, mbstate_t>
1219{
1220 _LIBCPP_ALWAYS_INLINE
1221 __narrow_to_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1222
1223 ~__narrow_to_utf8();
1224
1225 template <class _OutputIterator, class _CharT>
1226 _LIBCPP_ALWAYS_INLINE
1227 _OutputIterator
1228 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1229 {
1230 result __r = ok;
1231 mbstate_t __mb;
1232 while (__wb < __we && __r != error)
1233 {
1234 const int __sz = 32;
1235 char __buf[__sz];
1236 char* __bn;
1237 const char16_t* __wn = (const char16_t*)__wb;
1238 __r = do_out(__mb, (const char16_t*)__wb, (const char16_t*)__we, __wn,
1239 __buf, __buf+__sz, __bn);
1240 if (__r == codecvt_base::error || __wn == (const char16_t*)__wb)
1241 __throw_runtime_error("locale not supported");
1242 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1243 *__s = *__p;
1244 __wb = (const _CharT*)__wn;
1245 }
1246 return __s;
1247 }
1248};
1249
1250template <>
1251struct __narrow_to_utf8<32>
1252 : public codecvt<char32_t, char, mbstate_t>
1253{
1254 _LIBCPP_ALWAYS_INLINE
1255 __narrow_to_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1256
1257 ~__narrow_to_utf8();
1258
1259 template <class _OutputIterator, class _CharT>
1260 _LIBCPP_ALWAYS_INLINE
1261 _OutputIterator
1262 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1263 {
1264 result __r = ok;
1265 mbstate_t __mb;
1266 while (__wb < __we && __r != error)
1267 {
1268 const int __sz = 32;
1269 char __buf[__sz];
1270 char* __bn;
1271 const char32_t* __wn = (const char32_t*)__wb;
1272 __r = do_out(__mb, (const char32_t*)__wb, (const char32_t*)__we, __wn,
1273 __buf, __buf+__sz, __bn);
1274 if (__r == codecvt_base::error || __wn == (const char32_t*)__wb)
1275 __throw_runtime_error("locale not supported");
1276 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1277 *__s = *__p;
1278 __wb = (const _CharT*)__wn;
1279 }
1280 return __s;
1281 }
1282};
1283
Howard Hinnant99968442011-11-29 18:15:501284template <size_t _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:161285struct __widen_from_utf8
1286{
1287 template <class _OutputIterator>
1288 _OutputIterator
1289 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const;
1290};
1291
1292template <>
1293struct __widen_from_utf8<8>
1294{
1295 template <class _OutputIterator>
1296 _LIBCPP_ALWAYS_INLINE
1297 _OutputIterator
1298 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1299 {
1300 for (; __nb < __ne; ++__nb, ++__s)
1301 *__s = *__nb;
1302 return __s;
1303 }
1304};
1305
1306template <>
1307struct __widen_from_utf8<16>
1308 : public codecvt<char16_t, char, mbstate_t>
1309{
1310 _LIBCPP_ALWAYS_INLINE
1311 __widen_from_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1312
1313 ~__widen_from_utf8();
1314
1315 template <class _OutputIterator>
1316 _LIBCPP_ALWAYS_INLINE
1317 _OutputIterator
1318 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1319 {
1320 result __r = ok;
1321 mbstate_t __mb;
1322 while (__nb < __ne && __r != error)
1323 {
1324 const int __sz = 32;
1325 char16_t __buf[__sz];
1326 char16_t* __bn;
1327 const char* __nn = __nb;
1328 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1329 __buf, __buf+__sz, __bn);
1330 if (__r == codecvt_base::error || __nn == __nb)
1331 __throw_runtime_error("locale not supported");
1332 for (const char16_t* __p = __buf; __p < __bn; ++__p, ++__s)
1333 *__s = (wchar_t)*__p;
1334 __nb = __nn;
1335 }
1336 return __s;
1337 }
1338};
1339
1340template <>
1341struct __widen_from_utf8<32>
1342 : public codecvt<char32_t, char, mbstate_t>
1343{
1344 _LIBCPP_ALWAYS_INLINE
1345 __widen_from_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1346
1347 ~__widen_from_utf8();
1348
1349 template <class _OutputIterator>
1350 _LIBCPP_ALWAYS_INLINE
1351 _OutputIterator
1352 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1353 {
1354 result __r = ok;
1355 mbstate_t __mb;
1356 while (__nb < __ne && __r != error)
1357 {
1358 const int __sz = 32;
1359 char32_t __buf[__sz];
1360 char32_t* __bn;
1361 const char* __nn = __nb;
1362 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1363 __buf, __buf+__sz, __bn);
1364 if (__r == codecvt_base::error || __nn == __nb)
1365 __throw_runtime_error("locale not supported");
1366 for (const char32_t* __p = __buf; __p < __bn; ++__p, ++__s)
1367 *__s = (wchar_t)*__p;
1368 __nb = __nn;
1369 }
1370 return __s;
1371 }
1372};
1373
1374// template <class charT> class numpunct
1375
Eric Fiselierc3589a82017-01-04 23:56:001376template <class _CharT> class _LIBCPP_TEMPLATE_VIS numpunct;
Howard Hinnantbc8d3f92010-05-11 19:42:161377
1378template <>
Howard Hinnant83eade62013-03-06 23:30:191379class _LIBCPP_TYPE_VIS numpunct<char>
Howard Hinnantbc8d3f92010-05-11 19:42:161380 : public locale::facet
1381{
1382public:
1383 typedef char char_type;
1384 typedef basic_string<char_type> string_type;
1385
1386 explicit numpunct(size_t __refs = 0);
1387
1388 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
1389 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
1390 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
1391 _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();}
1392 _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();}
1393
1394 static locale::id id;
1395
1396protected:
1397 ~numpunct();
1398 virtual char_type do_decimal_point() const;
1399 virtual char_type do_thousands_sep() const;
1400 virtual string do_grouping() const;
1401 virtual string_type do_truename() const;
1402 virtual string_type do_falsename() const;
1403
1404 char_type __decimal_point_;
1405 char_type __thousands_sep_;
1406 string __grouping_;
1407};
1408
1409template <>
Howard Hinnant83eade62013-03-06 23:30:191410class _LIBCPP_TYPE_VIS numpunct<wchar_t>
Howard Hinnantbc8d3f92010-05-11 19:42:161411 : public locale::facet
1412{
1413public:
1414 typedef wchar_t char_type;
1415 typedef basic_string<char_type> string_type;
1416
1417 explicit numpunct(size_t __refs = 0);
1418
1419 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
1420 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
1421 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
1422 _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();}
1423 _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();}
1424
1425 static locale::id id;
1426
1427protected:
1428 ~numpunct();
1429 virtual char_type do_decimal_point() const;
1430 virtual char_type do_thousands_sep() const;
1431 virtual string do_grouping() const;
1432 virtual string_type do_truename() const;
1433 virtual string_type do_falsename() const;
1434
1435 char_type __decimal_point_;
1436 char_type __thousands_sep_;
1437 string __grouping_;
1438};
1439
1440// template <class charT> class numpunct_byname
1441
Eric Fiselierc3589a82017-01-04 23:56:001442template <class _CharT> class _LIBCPP_TEMPLATE_VIS numpunct_byname;
Howard Hinnantbc8d3f92010-05-11 19:42:161443
1444template <>
Howard Hinnant83eade62013-03-06 23:30:191445class _LIBCPP_TYPE_VIS numpunct_byname<char>
Howard Hinnantbc8d3f92010-05-11 19:42:161446: public numpunct<char>
1447{
1448public:
1449 typedef char char_type;
1450 typedef basic_string<char_type> string_type;
1451
1452 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1453 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1454
1455protected:
1456 ~numpunct_byname();
1457
1458private:
1459 void __init(const char*);
1460};
1461
1462template <>
Howard Hinnant83eade62013-03-06 23:30:191463class _LIBCPP_TYPE_VIS numpunct_byname<wchar_t>
Howard Hinnantbc8d3f92010-05-11 19:42:161464: public numpunct<wchar_t>
1465{
1466public:
1467 typedef wchar_t char_type;
1468 typedef basic_string<char_type> string_type;
1469
1470 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1471 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1472
1473protected:
1474 ~numpunct_byname();
1475
1476private:
1477 void __init(const char*);
1478};
1479
1480_LIBCPP_END_NAMESPACE_STD
1481
1482#endif // _LIBCPP___LOCALE