2 //===----------------------------------------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP___LOCALE
12 #define _LIBCPP___LOCALE
22 #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
23 # include <support/win32/locale_win32.h>
25 # include <support/ibm/xlocale.h>
26 #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>
32 #elif defined(__sun__)
34 # include <support/solaris/xlocale.h>
35 #elif defined(_NEWLIB_VERSION)
36 # include <support/newlib/xlocale.h>
37 #elif (defined(__GLIBC__) || defined(__APPLE__) || defined(__FreeBSD__) \
38 || defined(__EMSCRIPTEN__) || defined(__IBMCPP__))
40 #elif defined(__Fuchsia__)
41 # include <support/fuchsia/xlocale.h>
42 #elif defined(_LIBCPP_HAS_MUSL_LIBC)
43 # include <support/musl/xlocale.h>
46 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
47 #pragma GCC system_header
50 _LIBCPP_BEGIN_NAMESPACE_STD
52 class _LIBCPP_TYPE_VIS locale;
54 template <class _Facet>
55 _LIBCPP_INLINE_VISIBILITY
57 has_facet(const locale&) _NOEXCEPT;
59 template <class _Facet>
60 _LIBCPP_INLINE_VISIBILITY
62 use_facet(const locale&);
64 class _LIBCPP_TYPE_VIS locale
68 class _LIBCPP_TYPE_VIS facet;
69 class _LIBCPP_TYPE_VIS id;
72 static const category // values assigned here are for exposition only
74 collate = LC_COLLATE_MASK,
75 ctype = LC_CTYPE_MASK,
76 monetary = LC_MONETARY_MASK,
77 numeric = LC_NUMERIC_MASK,
79 messages = LC_MESSAGES_MASK,
80 all = collate | ctype | monetary | numeric | time | messages;
82 // construct/copy/destroy:
84 locale(const locale&) _NOEXCEPT;
85 explicit locale(const char*);
86 explicit locale(const string&);
87 locale(const locale&, const char*, category);
88 locale(const locale&, const string&, category);
89 template <class _Facet>
90 _LIBCPP_INLINE_VISIBILITY locale(const locale&, _Facet*);
91 locale(const locale&, const locale&, category);
95 const locale& operator=(const locale&) _NOEXCEPT;
97 template <class _Facet>
98 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
99 locale combine(const locale&) const;
101 // locale operations:
103 bool operator==(const locale&) const;
104 bool operator!=(const locale& __y) const {return !(*this == __y);}
105 template <class _CharT, class _Traits, class _Allocator>
106 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
107 bool operator()(const basic_string<_CharT, _Traits, _Allocator>&,
108 const basic_string<_CharT, _Traits, _Allocator>&) const;
110 // global locale objects:
111 static locale global(const locale&);
112 static const locale& classic();
118 void __install_ctor(const locale&, facet*, long);
119 static locale& __global();
120 bool has_facet(id&) const;
121 const facet* use_facet(id&) const;
123 template <class _Facet> friend bool has_facet(const locale&) _NOEXCEPT;
124 template <class _Facet> friend const _Facet& use_facet(const locale&);
127 class _LIBCPP_TYPE_VIS locale::facet
128 : public __shared_count
131 _LIBCPP_INLINE_VISIBILITY
132 explicit facet(size_t __refs = 0)
133 : __shared_count(static_cast<long>(__refs)-1) {}
137 // facet(const facet&) = delete; // effectively done in __shared_count
138 // void operator=(const facet&) = delete;
140 virtual void __on_zero_shared() _NOEXCEPT;
143 class _LIBCPP_TYPE_VIS locale::id
148 static int32_t __next_id;
150 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR id() :__id_(0) {}
153 void operator=(const id&); // = delete;
154 id(const id&); // = delete;
155 public: // only needed for tests
159 friend class locale::__imp;
162 template <class _Facet>
163 inline _LIBCPP_INLINE_VISIBILITY
164 locale::locale(const locale& __other, _Facet* __f)
166 __install_ctor(__other, __f, __f ? __f->id.__get() : 0);
169 template <class _Facet>
171 locale::combine(const locale& __other) const
173 if (!_VSTD::has_facet<_Facet>(__other))
174 __throw_runtime_error("locale::combine: locale missing facet");
176 return locale(*this, &const_cast<_Facet&>(_VSTD::use_facet<_Facet>(__other)));
179 template <class _Facet>
180 inline _LIBCPP_INLINE_VISIBILITY
182 has_facet(const locale& __l) _NOEXCEPT
184 return __l.has_facet(_Facet::id);
187 template <class _Facet>
188 inline _LIBCPP_INLINE_VISIBILITY
190 use_facet(const locale& __l)
192 return static_cast<const _Facet&>(*__l.use_facet(_Facet::id));
195 // template <class _CharT> class collate;
197 template <class _CharT>
198 class _LIBCPP_TEMPLATE_VIS collate
199 : public locale::facet
202 typedef _CharT char_type;
203 typedef basic_string<char_type> string_type;
205 _LIBCPP_INLINE_VISIBILITY
206 explicit collate(size_t __refs = 0)
207 : locale::facet(__refs) {}
209 _LIBCPP_INLINE_VISIBILITY
210 int compare(const char_type* __lo1, const char_type* __hi1,
211 const char_type* __lo2, const char_type* __hi2) const
213 return do_compare(__lo1, __hi1, __lo2, __hi2);
216 _LIBCPP_INLINE_VISIBILITY
217 string_type transform(const char_type* __lo, const char_type* __hi) const
219 return do_transform(__lo, __hi);
222 _LIBCPP_INLINE_VISIBILITY
223 long hash(const char_type* __lo, const char_type* __hi) const
225 return do_hash(__lo, __hi);
228 static locale::id id;
232 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
233 const char_type* __lo2, const char_type* __hi2) const;
234 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const
235 {return string_type(__lo, __hi);}
236 virtual long do_hash(const char_type* __lo, const char_type* __hi) const;
239 template <class _CharT> locale::id collate<_CharT>::id;
241 template <class _CharT>
242 collate<_CharT>::~collate()
246 template <class _CharT>
248 collate<_CharT>::do_compare(const char_type* __lo1, const char_type* __hi1,
249 const char_type* __lo2, const char_type* __hi2) const
251 for (; __lo2 != __hi2; ++__lo1, ++__lo2)
253 if (__lo1 == __hi1 || *__lo1 < *__lo2)
258 return __lo1 != __hi1;
261 template <class _CharT>
263 collate<_CharT>::do_hash(const char_type* __lo, const char_type* __hi) const
266 const size_t __sr = __CHAR_BIT__ * sizeof(size_t) - 8;
267 const size_t __mask = size_t(0xF) << (__sr + 4);
268 for(const char_type* __p = __lo; __p != __hi; ++__p)
270 __h = (__h << 4) + static_cast<size_t>(*__p);
271 size_t __g = __h & __mask;
272 __h ^= __g | (__g >> __sr);
274 return static_cast<long>(__h);
277 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<char>)
278 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<wchar_t>)
280 // template <class CharT> class collate_byname;
282 template <class _CharT> class _LIBCPP_TEMPLATE_VIS collate_byname;
285 class _LIBCPP_TYPE_VIS collate_byname<char>
286 : public collate<char>
290 typedef char char_type;
291 typedef basic_string<char_type> string_type;
293 explicit collate_byname(const char* __n, size_t __refs = 0);
294 explicit collate_byname(const string& __n, size_t __refs = 0);
298 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
299 const char_type* __lo2, const char_type* __hi2) const;
300 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
304 class _LIBCPP_TYPE_VIS collate_byname<wchar_t>
305 : public collate<wchar_t>
309 typedef wchar_t char_type;
310 typedef basic_string<char_type> string_type;
312 explicit collate_byname(const char* __n, size_t __refs = 0);
313 explicit collate_byname(const string& __n, size_t __refs = 0);
318 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
319 const char_type* __lo2, const char_type* __hi2) const;
320 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
323 template <class _CharT, class _Traits, class _Allocator>
325 locale::operator()(const basic_string<_CharT, _Traits, _Allocator>& __x,
326 const basic_string<_CharT, _Traits, _Allocator>& __y) const
328 return _VSTD::use_facet<_VSTD::collate<_CharT> >(*this).compare(
329 __x.data(), __x.data() + __x.size(),
330 __y.data(), __y.data() + __y.size()) < 0;
333 // template <class charT> class ctype
335 class _LIBCPP_TYPE_VIS ctype_base
338 #if defined(__GLIBC__)
339 typedef unsigned short mask;
340 static const mask space = _ISspace;
341 static const mask print = _ISprint;
342 static const mask cntrl = _IScntrl;
343 static const mask upper = _ISupper;
344 static const mask lower = _ISlower;
345 static const mask alpha = _ISalpha;
346 static const mask digit = _ISdigit;
347 static const mask punct = _ISpunct;
348 static const mask xdigit = _ISxdigit;
349 static const mask blank = _ISblank;
350 #elif defined(_LIBCPP_MSVCRT)
351 typedef unsigned short mask;
352 static const mask space = _SPACE;
353 static const mask print = _BLANK|_PUNCT|_ALPHA|_DIGIT;
354 static const mask cntrl = _CONTROL;
355 static const mask upper = _UPPER;
356 static const mask lower = _LOWER;
357 static const mask alpha = _ALPHA;
358 static const mask digit = _DIGIT;
359 static const mask punct = _PUNCT;
360 static const mask xdigit = _HEX;
361 static const mask blank = _BLANK;
362 # define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
363 #elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__)
365 typedef __uint32_t mask;
366 # elif defined(__FreeBSD__)
367 typedef unsigned long mask;
368 # elif defined(__EMSCRIPTEN__) || defined(__NetBSD__)
369 typedef unsigned short mask;
371 static const mask space = _CTYPE_S;
372 static const mask print = _CTYPE_R;
373 static const mask cntrl = _CTYPE_C;
374 static const mask upper = _CTYPE_U;
375 static const mask lower = _CTYPE_L;
376 static const mask alpha = _CTYPE_A;
377 static const mask digit = _CTYPE_D;
378 static const mask punct = _CTYPE_P;
379 static const mask xdigit = _CTYPE_X;
381 # if defined(__NetBSD__)
382 static const mask blank = _CTYPE_BL;
384 static const mask blank = _CTYPE_B;
386 #elif defined(__sun__) || defined(_AIX)
387 typedef unsigned int mask;
388 static const mask space = _ISSPACE;
389 static const mask print = _ISPRINT;
390 static const mask cntrl = _ISCNTRL;
391 static const mask upper = _ISUPPER;
392 static const mask lower = _ISLOWER;
393 static const mask alpha = _ISALPHA;
394 static const mask digit = _ISDIGIT;
395 static const mask punct = _ISPUNCT;
396 static const mask xdigit = _ISXDIGIT;
397 static const mask blank = _ISBLANK;
398 #elif defined(_NEWLIB_VERSION)
399 // Same type as Newlib's _ctype_ array in newlib/libc/include/ctype.h.
401 static const mask space = _S;
402 static const mask print = _P | _U | _L | _N | _B;
403 static const mask cntrl = _C;
404 static const mask upper = _U;
405 static const mask lower = _L;
406 static const mask alpha = _U | _L;
407 static const mask digit = _N;
408 static const mask punct = _P;
409 static const mask xdigit = _X | _N;
410 static const mask blank = _B;
411 # define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
412 # define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
413 # define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_XDIGIT
415 typedef unsigned long mask;
416 static const mask space = 1<<0;
417 static const mask print = 1<<1;
418 static const mask cntrl = 1<<2;
419 static const mask upper = 1<<3;
420 static const mask lower = 1<<4;
421 static const mask alpha = 1<<5;
422 static const mask digit = 1<<6;
423 static const mask punct = 1<<7;
424 static const mask xdigit = 1<<8;
425 static const mask blank = 1<<9;
427 static const mask alnum = alpha | digit;
428 static const mask graph = alnum | punct;
430 _LIBCPP_ALWAYS_INLINE ctype_base() {}
433 template <class _CharT> class _LIBCPP_TEMPLATE_VIS ctype;
436 class _LIBCPP_TYPE_VIS ctype<wchar_t>
437 : public locale::facet,
441 typedef wchar_t char_type;
443 _LIBCPP_ALWAYS_INLINE
444 explicit ctype(size_t __refs = 0)
445 : locale::facet(__refs) {}
447 _LIBCPP_ALWAYS_INLINE
448 bool is(mask __m, char_type __c) const
450 return do_is(__m, __c);
453 _LIBCPP_ALWAYS_INLINE
454 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
456 return do_is(__low, __high, __vec);
459 _LIBCPP_ALWAYS_INLINE
460 const char_type* scan_is(mask __m, const char_type* __low, const char_type* __high) const
462 return do_scan_is(__m, __low, __high);
465 _LIBCPP_ALWAYS_INLINE
466 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
468 return do_scan_not(__m, __low, __high);
471 _LIBCPP_ALWAYS_INLINE
472 char_type toupper(char_type __c) const
474 return do_toupper(__c);
477 _LIBCPP_ALWAYS_INLINE
478 const char_type* toupper(char_type* __low, const char_type* __high) const
480 return do_toupper(__low, __high);
483 _LIBCPP_ALWAYS_INLINE
484 char_type tolower(char_type __c) const
486 return do_tolower(__c);
489 _LIBCPP_ALWAYS_INLINE
490 const char_type* tolower(char_type* __low, const char_type* __high) const
492 return do_tolower(__low, __high);
495 _LIBCPP_ALWAYS_INLINE
496 char_type widen(char __c) const
498 return do_widen(__c);
501 _LIBCPP_ALWAYS_INLINE
502 const char* widen(const char* __low, const char* __high, char_type* __to) const
504 return do_widen(__low, __high, __to);
507 _LIBCPP_ALWAYS_INLINE
508 char narrow(char_type __c, char __dfault) const
510 return do_narrow(__c, __dfault);
513 _LIBCPP_ALWAYS_INLINE
514 const char_type* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
516 return do_narrow(__low, __high, __dfault, __to);
519 static locale::id id;
523 virtual bool do_is(mask __m, char_type __c) const;
524 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
525 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
526 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
527 virtual char_type do_toupper(char_type) const;
528 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
529 virtual char_type do_tolower(char_type) const;
530 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
531 virtual char_type do_widen(char) const;
532 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
533 virtual char do_narrow(char_type, char __dfault) const;
534 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
538 class _LIBCPP_TYPE_VIS ctype<char>
539 : public locale::facet, public ctype_base
544 typedef char char_type;
546 explicit ctype(const mask* __tab = 0, bool __del = false, size_t __refs = 0);
548 _LIBCPP_ALWAYS_INLINE
549 bool is(mask __m, char_type __c) const
551 return isascii(__c) ? (__tab_[static_cast<int>(__c)] & __m) !=0 : false;
554 _LIBCPP_ALWAYS_INLINE
555 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
557 for (; __low != __high; ++__low, ++__vec)
558 *__vec = isascii(*__low) ? __tab_[static_cast<int>(*__low)] : 0;
562 _LIBCPP_ALWAYS_INLINE
563 const char_type* scan_is (mask __m, const char_type* __low, const char_type* __high) const
565 for (; __low != __high; ++__low)
566 if (isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m))
571 _LIBCPP_ALWAYS_INLINE
572 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
574 for (; __low != __high; ++__low)
575 if (!(isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m)))
580 _LIBCPP_ALWAYS_INLINE
581 char_type toupper(char_type __c) const
583 return do_toupper(__c);
586 _LIBCPP_ALWAYS_INLINE
587 const char_type* toupper(char_type* __low, const char_type* __high) const
589 return do_toupper(__low, __high);
592 _LIBCPP_ALWAYS_INLINE
593 char_type tolower(char_type __c) const
595 return do_tolower(__c);
598 _LIBCPP_ALWAYS_INLINE
599 const char_type* tolower(char_type* __low, const char_type* __high) const
601 return do_tolower(__low, __high);
604 _LIBCPP_ALWAYS_INLINE
605 char_type widen(char __c) const
607 return do_widen(__c);
610 _LIBCPP_ALWAYS_INLINE
611 const char* widen(const char* __low, const char* __high, char_type* __to) const
613 return do_widen(__low, __high, __to);
616 _LIBCPP_ALWAYS_INLINE
617 char narrow(char_type __c, char __dfault) const
619 return do_narrow(__c, __dfault);
622 _LIBCPP_ALWAYS_INLINE
623 const char* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
625 return do_narrow(__low, __high, __dfault, __to);
628 static locale::id id;
631 static const size_t table_size = _CACHED_RUNES;
633 static const size_t table_size = 256; // FIXME: Don't hardcode this.
635 _LIBCPP_ALWAYS_INLINE const mask* table() const _NOEXCEPT {return __tab_;}
636 static const mask* classic_table() _NOEXCEPT;
637 #if defined(__GLIBC__) || defined(__EMSCRIPTEN__)
638 static const int* __classic_upper_table() _NOEXCEPT;
639 static const int* __classic_lower_table() _NOEXCEPT;
641 #if defined(__NetBSD__)
642 static const short* __classic_upper_table() _NOEXCEPT;
643 static const short* __classic_lower_table() _NOEXCEPT;
648 virtual char_type do_toupper(char_type __c) const;
649 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
650 virtual char_type do_tolower(char_type __c) const;
651 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
652 virtual char_type do_widen(char __c) const;
653 virtual const char* do_widen(const char* __low, const char* __high, char_type* __to) const;
654 virtual char do_narrow(char_type __c, char __dfault) const;
655 virtual const char* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const;
658 // template <class CharT> class ctype_byname;
660 template <class _CharT> class _LIBCPP_TEMPLATE_VIS ctype_byname;
663 class _LIBCPP_TYPE_VIS ctype_byname<char>
669 explicit ctype_byname(const char*, size_t = 0);
670 explicit ctype_byname(const string&, size_t = 0);
674 virtual char_type do_toupper(char_type) const;
675 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
676 virtual char_type do_tolower(char_type) const;
677 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
681 class _LIBCPP_TYPE_VIS ctype_byname<wchar_t>
682 : public ctype<wchar_t>
687 explicit ctype_byname(const char*, size_t = 0);
688 explicit ctype_byname(const string&, size_t = 0);
692 virtual bool do_is(mask __m, char_type __c) const;
693 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
694 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
695 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
696 virtual char_type do_toupper(char_type) const;
697 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
698 virtual char_type do_tolower(char_type) const;
699 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
700 virtual char_type do_widen(char) const;
701 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
702 virtual char do_narrow(char_type, char __dfault) const;
703 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
706 template <class _CharT>
707 inline _LIBCPP_INLINE_VISIBILITY
709 isspace(_CharT __c, const locale& __loc)
711 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
714 template <class _CharT>
715 inline _LIBCPP_INLINE_VISIBILITY
717 isprint(_CharT __c, const locale& __loc)
719 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c);
722 template <class _CharT>
723 inline _LIBCPP_INLINE_VISIBILITY
725 iscntrl(_CharT __c, const locale& __loc)
727 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c);
730 template <class _CharT>
731 inline _LIBCPP_INLINE_VISIBILITY
733 isupper(_CharT __c, const locale& __loc)
735 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c);
738 template <class _CharT>
739 inline _LIBCPP_INLINE_VISIBILITY
741 islower(_CharT __c, const locale& __loc)
743 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c);
746 template <class _CharT>
747 inline _LIBCPP_INLINE_VISIBILITY
749 isalpha(_CharT __c, const locale& __loc)
751 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c);
754 template <class _CharT>
755 inline _LIBCPP_INLINE_VISIBILITY
757 isdigit(_CharT __c, const locale& __loc)
759 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c);
762 template <class _CharT>
763 inline _LIBCPP_INLINE_VISIBILITY
765 ispunct(_CharT __c, const locale& __loc)
767 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c);
770 template <class _CharT>
771 inline _LIBCPP_INLINE_VISIBILITY
773 isxdigit(_CharT __c, const locale& __loc)
775 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c);
778 template <class _CharT>
779 inline _LIBCPP_INLINE_VISIBILITY
781 isalnum(_CharT __c, const locale& __loc)
783 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c);
786 template <class _CharT>
787 inline _LIBCPP_INLINE_VISIBILITY
789 isgraph(_CharT __c, const locale& __loc)
791 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c);
794 template <class _CharT>
795 inline _LIBCPP_INLINE_VISIBILITY
797 toupper(_CharT __c, const locale& __loc)
799 return use_facet<ctype<_CharT> >(__loc).toupper(__c);
802 template <class _CharT>
803 inline _LIBCPP_INLINE_VISIBILITY
805 tolower(_CharT __c, const locale& __loc)
807 return use_facet<ctype<_CharT> >(__loc).tolower(__c);
812 class _LIBCPP_TYPE_VIS codecvt_base
815 _LIBCPP_ALWAYS_INLINE codecvt_base() {}
816 enum result {ok, partial, error, noconv};
819 // template <class internT, class externT, class stateT> class codecvt;
821 template <class _InternT, class _ExternT, class _StateT> class _LIBCPP_TEMPLATE_VIS codecvt;
823 // template <> class codecvt<char, char, mbstate_t>
826 class _LIBCPP_TYPE_VIS codecvt<char, char, mbstate_t>
827 : public locale::facet,
831 typedef char intern_type;
832 typedef char extern_type;
833 typedef mbstate_t state_type;
835 _LIBCPP_ALWAYS_INLINE
836 explicit codecvt(size_t __refs = 0)
837 : locale::facet(__refs) {}
839 _LIBCPP_ALWAYS_INLINE
840 result out(state_type& __st,
841 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
842 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
844 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
847 _LIBCPP_ALWAYS_INLINE
848 result unshift(state_type& __st,
849 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
851 return do_unshift(__st, __to, __to_end, __to_nxt);
854 _LIBCPP_ALWAYS_INLINE
855 result in(state_type& __st,
856 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
857 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
859 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
862 _LIBCPP_ALWAYS_INLINE
863 int encoding() const _NOEXCEPT
865 return do_encoding();
868 _LIBCPP_ALWAYS_INLINE
869 bool always_noconv() const _NOEXCEPT
871 return do_always_noconv();
874 _LIBCPP_ALWAYS_INLINE
875 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
877 return do_length(__st, __frm, __end, __mx);
880 _LIBCPP_ALWAYS_INLINE
881 int max_length() const _NOEXCEPT
883 return do_max_length();
886 static locale::id id;
889 _LIBCPP_ALWAYS_INLINE
890 explicit codecvt(const char*, size_t __refs = 0)
891 : locale::facet(__refs) {}
895 virtual result do_out(state_type& __st,
896 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
897 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
898 virtual result do_in(state_type& __st,
899 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
900 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
901 virtual result do_unshift(state_type& __st,
902 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
903 virtual int do_encoding() const _NOEXCEPT;
904 virtual bool do_always_noconv() const _NOEXCEPT;
905 virtual int do_length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
906 virtual int do_max_length() const _NOEXCEPT;
909 // template <> class codecvt<wchar_t, char, mbstate_t>
912 class _LIBCPP_TYPE_VIS codecvt<wchar_t, char, mbstate_t>
913 : public locale::facet,
918 typedef wchar_t intern_type;
919 typedef char extern_type;
920 typedef mbstate_t state_type;
922 explicit codecvt(size_t __refs = 0);
924 _LIBCPP_ALWAYS_INLINE
925 result out(state_type& __st,
926 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
927 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
929 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
932 _LIBCPP_ALWAYS_INLINE
933 result unshift(state_type& __st,
934 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
936 return do_unshift(__st, __to, __to_end, __to_nxt);
939 _LIBCPP_ALWAYS_INLINE
940 result in(state_type& __st,
941 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
942 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
944 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
947 _LIBCPP_ALWAYS_INLINE
948 int encoding() const _NOEXCEPT
950 return do_encoding();
953 _LIBCPP_ALWAYS_INLINE
954 bool always_noconv() const _NOEXCEPT
956 return do_always_noconv();
959 _LIBCPP_ALWAYS_INLINE
960 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
962 return do_length(__st, __frm, __end, __mx);
965 _LIBCPP_ALWAYS_INLINE
966 int max_length() const _NOEXCEPT
968 return do_max_length();
971 static locale::id id;
974 explicit codecvt(const char*, size_t __refs = 0);
978 virtual result do_out(state_type& __st,
979 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
980 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
981 virtual result do_in(state_type& __st,
982 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
983 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
984 virtual result do_unshift(state_type& __st,
985 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
986 virtual int do_encoding() const _NOEXCEPT;
987 virtual bool do_always_noconv() const _NOEXCEPT;
988 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
989 virtual int do_max_length() const _NOEXCEPT;
992 // template <> class codecvt<char16_t, char, mbstate_t>
995 class _LIBCPP_TYPE_VIS codecvt<char16_t, char, mbstate_t>
996 : public locale::facet,
1000 typedef char16_t intern_type;
1001 typedef char extern_type;
1002 typedef mbstate_t state_type;
1004 _LIBCPP_ALWAYS_INLINE
1005 explicit codecvt(size_t __refs = 0)
1006 : locale::facet(__refs) {}
1008 _LIBCPP_ALWAYS_INLINE
1009 result out(state_type& __st,
1010 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1011 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1013 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1016 _LIBCPP_ALWAYS_INLINE
1017 result unshift(state_type& __st,
1018 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1020 return do_unshift(__st, __to, __to_end, __to_nxt);
1023 _LIBCPP_ALWAYS_INLINE
1024 result in(state_type& __st,
1025 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1026 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1028 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1031 _LIBCPP_ALWAYS_INLINE
1032 int encoding() const _NOEXCEPT
1034 return do_encoding();
1037 _LIBCPP_ALWAYS_INLINE
1038 bool always_noconv() const _NOEXCEPT
1040 return do_always_noconv();
1043 _LIBCPP_ALWAYS_INLINE
1044 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1046 return do_length(__st, __frm, __end, __mx);
1049 _LIBCPP_ALWAYS_INLINE
1050 int max_length() const _NOEXCEPT
1052 return do_max_length();
1055 static locale::id id;
1058 _LIBCPP_ALWAYS_INLINE
1059 explicit codecvt(const char*, size_t __refs = 0)
1060 : locale::facet(__refs) {}
1064 virtual result do_out(state_type& __st,
1065 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1066 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1067 virtual result do_in(state_type& __st,
1068 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1069 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1070 virtual result do_unshift(state_type& __st,
1071 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1072 virtual int do_encoding() const _NOEXCEPT;
1073 virtual bool do_always_noconv() const _NOEXCEPT;
1074 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
1075 virtual int do_max_length() const _NOEXCEPT;
1078 // template <> class codecvt<char32_t, char, mbstate_t>
1081 class _LIBCPP_TYPE_VIS codecvt<char32_t, char, mbstate_t>
1082 : public locale::facet,
1086 typedef char32_t intern_type;
1087 typedef char extern_type;
1088 typedef mbstate_t state_type;
1090 _LIBCPP_ALWAYS_INLINE
1091 explicit codecvt(size_t __refs = 0)
1092 : locale::facet(__refs) {}
1094 _LIBCPP_ALWAYS_INLINE
1095 result out(state_type& __st,
1096 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1097 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1099 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1102 _LIBCPP_ALWAYS_INLINE
1103 result unshift(state_type& __st,
1104 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1106 return do_unshift(__st, __to, __to_end, __to_nxt);
1109 _LIBCPP_ALWAYS_INLINE
1110 result in(state_type& __st,
1111 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1112 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1114 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1117 _LIBCPP_ALWAYS_INLINE
1118 int encoding() const _NOEXCEPT
1120 return do_encoding();
1123 _LIBCPP_ALWAYS_INLINE
1124 bool always_noconv() const _NOEXCEPT
1126 return do_always_noconv();
1129 _LIBCPP_ALWAYS_INLINE
1130 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1132 return do_length(__st, __frm, __end, __mx);
1135 _LIBCPP_ALWAYS_INLINE
1136 int max_length() const _NOEXCEPT
1138 return do_max_length();
1141 static locale::id id;
1144 _LIBCPP_ALWAYS_INLINE
1145 explicit codecvt(const char*, size_t __refs = 0)
1146 : locale::facet(__refs) {}
1150 virtual result do_out(state_type& __st,
1151 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1152 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1153 virtual result do_in(state_type& __st,
1154 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1155 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1156 virtual result do_unshift(state_type& __st,
1157 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1158 virtual int do_encoding() const _NOEXCEPT;
1159 virtual bool do_always_noconv() const _NOEXCEPT;
1160 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
1161 virtual int do_max_length() const _NOEXCEPT;
1164 // template <class _InternT, class _ExternT, class _StateT> class codecvt_byname
1166 template <class _InternT, class _ExternT, class _StateT>
1167 class _LIBCPP_TEMPLATE_VIS codecvt_byname
1168 : public codecvt<_InternT, _ExternT, _StateT>
1171 _LIBCPP_ALWAYS_INLINE
1172 explicit codecvt_byname(const char* __nm, size_t __refs = 0)
1173 : codecvt<_InternT, _ExternT, _StateT>(__nm, __refs) {}
1174 _LIBCPP_ALWAYS_INLINE
1175 explicit codecvt_byname(const string& __nm, size_t __refs = 0)
1176 : codecvt<_InternT, _ExternT, _StateT>(__nm.c_str(), __refs) {}
1181 template <class _InternT, class _ExternT, class _StateT>
1182 codecvt_byname<_InternT, _ExternT, _StateT>::~codecvt_byname()
1186 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char, char, mbstate_t>)
1187 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<wchar_t, char, mbstate_t>)
1188 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char16_t, char, mbstate_t>)
1189 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char32_t, char, mbstate_t>)
1191 _LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_runtime_error(const char*);
1193 template <size_t _Np>
1194 struct __narrow_to_utf8
1196 template <class _OutputIterator, class _CharT>
1198 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const;
1202 struct __narrow_to_utf8<8>
1204 template <class _OutputIterator, class _CharT>
1205 _LIBCPP_ALWAYS_INLINE
1207 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1209 for (; __wb < __we; ++__wb, ++__s)
1216 struct __narrow_to_utf8<16>
1217 : public codecvt<char16_t, char, mbstate_t>
1219 _LIBCPP_ALWAYS_INLINE
1220 __narrow_to_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1222 ~__narrow_to_utf8();
1224 template <class _OutputIterator, class _CharT>
1225 _LIBCPP_ALWAYS_INLINE
1227 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1231 while (__wb < __we && __r != error)
1233 const int __sz = 32;
1236 const char16_t* __wn = (const char16_t*)__wb;
1237 __r = do_out(__mb, (const char16_t*)__wb, (const char16_t*)__we, __wn,
1238 __buf, __buf+__sz, __bn);
1239 if (__r == codecvt_base::error || __wn == (const char16_t*)__wb)
1240 __throw_runtime_error("locale not supported");
1241 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1243 __wb = (const _CharT*)__wn;
1250 struct __narrow_to_utf8<32>
1251 : public codecvt<char32_t, char, mbstate_t>
1253 _LIBCPP_ALWAYS_INLINE
1254 __narrow_to_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1256 ~__narrow_to_utf8();
1258 template <class _OutputIterator, class _CharT>
1259 _LIBCPP_ALWAYS_INLINE
1261 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1265 while (__wb < __we && __r != error)
1267 const int __sz = 32;
1270 const char32_t* __wn = (const char32_t*)__wb;
1271 __r = do_out(__mb, (const char32_t*)__wb, (const char32_t*)__we, __wn,
1272 __buf, __buf+__sz, __bn);
1273 if (__r == codecvt_base::error || __wn == (const char32_t*)__wb)
1274 __throw_runtime_error("locale not supported");
1275 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1277 __wb = (const _CharT*)__wn;
1283 template <size_t _Np>
1284 struct __widen_from_utf8
1286 template <class _OutputIterator>
1288 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const;
1292 struct __widen_from_utf8<8>
1294 template <class _OutputIterator>
1295 _LIBCPP_ALWAYS_INLINE
1297 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1299 for (; __nb < __ne; ++__nb, ++__s)
1306 struct __widen_from_utf8<16>
1307 : public codecvt<char16_t, char, mbstate_t>
1309 _LIBCPP_ALWAYS_INLINE
1310 __widen_from_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1312 ~__widen_from_utf8();
1314 template <class _OutputIterator>
1315 _LIBCPP_ALWAYS_INLINE
1317 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1321 while (__nb < __ne && __r != error)
1323 const int __sz = 32;
1324 char16_t __buf[__sz];
1326 const char* __nn = __nb;
1327 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1328 __buf, __buf+__sz, __bn);
1329 if (__r == codecvt_base::error || __nn == __nb)
1330 __throw_runtime_error("locale not supported");
1331 for (const char16_t* __p = __buf; __p < __bn; ++__p, ++__s)
1332 *__s = (wchar_t)*__p;
1340 struct __widen_from_utf8<32>
1341 : public codecvt<char32_t, char, mbstate_t>
1343 _LIBCPP_ALWAYS_INLINE
1344 __widen_from_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1346 ~__widen_from_utf8();
1348 template <class _OutputIterator>
1349 _LIBCPP_ALWAYS_INLINE
1351 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1355 while (__nb < __ne && __r != error)
1357 const int __sz = 32;
1358 char32_t __buf[__sz];
1360 const char* __nn = __nb;
1361 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1362 __buf, __buf+__sz, __bn);
1363 if (__r == codecvt_base::error || __nn == __nb)
1364 __throw_runtime_error("locale not supported");
1365 for (const char32_t* __p = __buf; __p < __bn; ++__p, ++__s)
1366 *__s = (wchar_t)*__p;
1373 // template <class charT> class numpunct
1375 template <class _CharT> class _LIBCPP_TEMPLATE_VIS numpunct;
1378 class _LIBCPP_TYPE_VIS numpunct<char>
1379 : public locale::facet
1382 typedef char char_type;
1383 typedef basic_string<char_type> string_type;
1385 explicit numpunct(size_t __refs = 0);
1387 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
1388 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
1389 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
1390 _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();}
1391 _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();}
1393 static locale::id id;
1397 virtual char_type do_decimal_point() const;
1398 virtual char_type do_thousands_sep() const;
1399 virtual string do_grouping() const;
1400 virtual string_type do_truename() const;
1401 virtual string_type do_falsename() const;
1403 char_type __decimal_point_;
1404 char_type __thousands_sep_;
1409 class _LIBCPP_TYPE_VIS numpunct<wchar_t>
1410 : public locale::facet
1413 typedef wchar_t char_type;
1414 typedef basic_string<char_type> string_type;
1416 explicit numpunct(size_t __refs = 0);
1418 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
1419 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
1420 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
1421 _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();}
1422 _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();}
1424 static locale::id id;
1428 virtual char_type do_decimal_point() const;
1429 virtual char_type do_thousands_sep() const;
1430 virtual string do_grouping() const;
1431 virtual string_type do_truename() const;
1432 virtual string_type do_falsename() const;
1434 char_type __decimal_point_;
1435 char_type __thousands_sep_;
1439 // template <class charT> class numpunct_byname
1441 template <class _CharT> class _LIBCPP_TEMPLATE_VIS numpunct_byname;
1444 class _LIBCPP_TYPE_VIS numpunct_byname<char>
1445 : public numpunct<char>
1448 typedef char char_type;
1449 typedef basic_string<char_type> string_type;
1451 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1452 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1458 void __init(const char*);
1462 class _LIBCPP_TYPE_VIS numpunct_byname<wchar_t>
1463 : public numpunct<wchar_t>
1466 typedef wchar_t char_type;
1467 typedef basic_string<char_type> string_type;
1469 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1470 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1476 void __init(const char*);
1479 _LIBCPP_END_NAMESPACE_STD
1481 #endif // _LIBCPP___LOCALE