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(_LIBCPP_HAS_MUSL_LIBC)
41 # include <support/musl/xlocale.h>
42 #endif // __GLIBC__ || __APPLE__ || __FreeBSD__ || __sun__ || __EMSCRIPTEN__ || __IBMCPP__
44 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
45 #pragma GCC system_header
48 _LIBCPP_BEGIN_NAMESPACE_STD
50 class _LIBCPP_TYPE_VIS locale;
52 template <class _Facet>
53 _LIBCPP_INLINE_VISIBILITY
55 has_facet(const locale&) _NOEXCEPT;
57 template <class _Facet>
58 _LIBCPP_INLINE_VISIBILITY
60 use_facet(const locale&);
62 class _LIBCPP_TYPE_VIS locale
66 class _LIBCPP_TYPE_VIS facet;
67 class _LIBCPP_TYPE_VIS id;
70 static const category // values assigned here are for exposition only
72 collate = LC_COLLATE_MASK,
73 ctype = LC_CTYPE_MASK,
74 monetary = LC_MONETARY_MASK,
75 numeric = LC_NUMERIC_MASK,
77 messages = LC_MESSAGES_MASK,
78 all = collate | ctype | monetary | numeric | time | messages;
80 // construct/copy/destroy:
82 locale(const locale&) _NOEXCEPT;
83 explicit locale(const char*);
84 explicit locale(const string&);
85 locale(const locale&, const char*, category);
86 locale(const locale&, const string&, category);
87 template <class _Facet>
88 _LIBCPP_INLINE_VISIBILITY locale(const locale&, _Facet*);
89 locale(const locale&, const locale&, category);
93 const locale& operator=(const locale&) _NOEXCEPT;
95 template <class _Facet> locale combine(const locale&) const;
99 bool operator==(const locale&) const;
100 bool operator!=(const locale& __y) const {return !(*this == __y);}
101 template <class _CharT, class _Traits, class _Allocator>
102 bool operator()(const basic_string<_CharT, _Traits, _Allocator>&,
103 const basic_string<_CharT, _Traits, _Allocator>&) const;
105 // global locale objects:
106 static locale global(const locale&);
107 static const locale& classic();
113 void __install_ctor(const locale&, facet*, long);
114 static locale& __global();
115 bool has_facet(id&) const;
116 const facet* use_facet(id&) const;
118 template <class _Facet> friend bool has_facet(const locale&) _NOEXCEPT;
119 template <class _Facet> friend const _Facet& use_facet(const locale&);
122 class _LIBCPP_TYPE_VIS locale::facet
123 : public __shared_count
126 _LIBCPP_INLINE_VISIBILITY
127 explicit facet(size_t __refs = 0)
128 : __shared_count(static_cast<long>(__refs)-1) {}
132 // facet(const facet&) = delete; // effectively done in __shared_count
133 // void operator=(const facet&) = delete;
135 virtual void __on_zero_shared() _NOEXCEPT;
138 class _LIBCPP_TYPE_VIS locale::id
143 static int32_t __next_id;
145 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR id() :__id_(0) {}
148 void operator=(const id&); // = delete;
149 id(const id&); // = delete;
150 public: // only needed for tests
154 friend class locale::__imp;
157 template <class _Facet>
158 inline _LIBCPP_INLINE_VISIBILITY
159 locale::locale(const locale& __other, _Facet* __f)
161 __install_ctor(__other, __f, __f ? __f->id.__get() : 0);
164 template <class _Facet>
166 locale::combine(const locale& __other) const
168 #ifndef _LIBCPP_NO_EXCEPTIONS
169 if (!_VSTD::has_facet<_Facet>(__other))
170 throw runtime_error("locale::combine: locale missing facet");
171 #endif // _LIBCPP_NO_EXCEPTIONS
172 return locale(*this, &const_cast<_Facet&>(_VSTD::use_facet<_Facet>(__other)));
175 template <class _Facet>
176 inline _LIBCPP_INLINE_VISIBILITY
178 has_facet(const locale& __l) _NOEXCEPT
180 return __l.has_facet(_Facet::id);
183 template <class _Facet>
184 inline _LIBCPP_INLINE_VISIBILITY
186 use_facet(const locale& __l)
188 return static_cast<const _Facet&>(*__l.use_facet(_Facet::id));
191 // template <class _CharT> class collate;
193 template <class _CharT>
194 class _LIBCPP_TYPE_VIS_ONLY collate
195 : public locale::facet
198 typedef _CharT char_type;
199 typedef basic_string<char_type> string_type;
201 _LIBCPP_INLINE_VISIBILITY
202 explicit collate(size_t __refs = 0)
203 : locale::facet(__refs) {}
205 _LIBCPP_INLINE_VISIBILITY
206 int compare(const char_type* __lo1, const char_type* __hi1,
207 const char_type* __lo2, const char_type* __hi2) const
209 return do_compare(__lo1, __hi1, __lo2, __hi2);
212 _LIBCPP_INLINE_VISIBILITY
213 string_type transform(const char_type* __lo, const char_type* __hi) const
215 return do_transform(__lo, __hi);
218 _LIBCPP_INLINE_VISIBILITY
219 long hash(const char_type* __lo, const char_type* __hi) const
221 return do_hash(__lo, __hi);
224 static locale::id id;
228 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
229 const char_type* __lo2, const char_type* __hi2) const;
230 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const
231 {return string_type(__lo, __hi);}
232 virtual long do_hash(const char_type* __lo, const char_type* __hi) const;
235 template <class _CharT> locale::id collate<_CharT>::id;
237 template <class _CharT>
238 collate<_CharT>::~collate()
242 template <class _CharT>
244 collate<_CharT>::do_compare(const char_type* __lo1, const char_type* __hi1,
245 const char_type* __lo2, const char_type* __hi2) const
247 for (; __lo2 != __hi2; ++__lo1, ++__lo2)
249 if (__lo1 == __hi1 || *__lo1 < *__lo2)
254 return __lo1 != __hi1;
257 template <class _CharT>
259 collate<_CharT>::do_hash(const char_type* __lo, const char_type* __hi) const
262 const size_t __sr = __CHAR_BIT__ * sizeof(size_t) - 8;
263 const size_t __mask = size_t(0xF) << (__sr + 4);
264 for(const char_type* __p = __lo; __p != __hi; ++__p)
266 __h = (__h << 4) + static_cast<size_t>(*__p);
267 size_t __g = __h & __mask;
268 __h ^= __g | (__g >> __sr);
270 return static_cast<long>(__h);
273 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS collate<char>)
274 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS collate<wchar_t>)
276 // template <class CharT> class collate_byname;
278 template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY collate_byname;
281 class _LIBCPP_TYPE_VIS collate_byname<char>
282 : public collate<char>
286 typedef char char_type;
287 typedef basic_string<char_type> string_type;
289 explicit collate_byname(const char* __n, size_t __refs = 0);
290 explicit collate_byname(const string& __n, size_t __refs = 0);
294 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
295 const char_type* __lo2, const char_type* __hi2) const;
296 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
300 class _LIBCPP_TYPE_VIS collate_byname<wchar_t>
301 : public collate<wchar_t>
305 typedef wchar_t char_type;
306 typedef basic_string<char_type> string_type;
308 explicit collate_byname(const char* __n, size_t __refs = 0);
309 explicit collate_byname(const string& __n, size_t __refs = 0);
314 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
315 const char_type* __lo2, const char_type* __hi2) const;
316 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
319 template <class _CharT, class _Traits, class _Allocator>
321 locale::operator()(const basic_string<_CharT, _Traits, _Allocator>& __x,
322 const basic_string<_CharT, _Traits, _Allocator>& __y) const
324 return _VSTD::use_facet<_VSTD::collate<_CharT> >(*this).compare(
325 __x.data(), __x.data() + __x.size(),
326 __y.data(), __y.data() + __y.size()) < 0;
329 // template <class charT> class ctype
331 class _LIBCPP_TYPE_VIS ctype_base
334 #if defined(__GLIBC__)
335 typedef unsigned short mask;
336 static const mask space = _ISspace;
337 static const mask print = _ISprint;
338 static const mask cntrl = _IScntrl;
339 static const mask upper = _ISupper;
340 static const mask lower = _ISlower;
341 static const mask alpha = _ISalpha;
342 static const mask digit = _ISdigit;
343 static const mask punct = _ISpunct;
344 static const mask xdigit = _ISxdigit;
345 static const mask blank = _ISblank;
346 #elif defined(_WIN32)
347 typedef unsigned short mask;
348 static const mask space = _SPACE;
349 static const mask print = _BLANK|_PUNCT|_ALPHA|_DIGIT;
350 static const mask cntrl = _CONTROL;
351 static const mask upper = _UPPER;
352 static const mask lower = _LOWER;
353 static const mask alpha = _ALPHA;
354 static const mask digit = _DIGIT;
355 static const mask punct = _PUNCT;
356 static const mask xdigit = _HEX;
357 static const mask blank = _BLANK;
358 # define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
359 #elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__)
361 typedef __uint32_t mask;
362 # elif defined(__FreeBSD__)
363 typedef unsigned long mask;
364 # elif defined(__EMSCRIPTEN__) || defined(__NetBSD__)
365 typedef unsigned short mask;
367 static const mask space = _CTYPE_S;
368 static const mask print = _CTYPE_R;
369 static const mask cntrl = _CTYPE_C;
370 static const mask upper = _CTYPE_U;
371 static const mask lower = _CTYPE_L;
372 static const mask alpha = _CTYPE_A;
373 static const mask digit = _CTYPE_D;
374 static const mask punct = _CTYPE_P;
375 static const mask xdigit = _CTYPE_X;
377 # if defined(__NetBSD__)
378 static const mask blank = _CTYPE_BL;
380 static const mask blank = _CTYPE_B;
382 #elif defined(__sun__) || defined(_AIX)
383 typedef unsigned int mask;
384 static const mask space = _ISSPACE;
385 static const mask print = _ISPRINT;
386 static const mask cntrl = _ISCNTRL;
387 static const mask upper = _ISUPPER;
388 static const mask lower = _ISLOWER;
389 static const mask alpha = _ISALPHA;
390 static const mask digit = _ISDIGIT;
391 static const mask punct = _ISPUNCT;
392 static const mask xdigit = _ISXDIGIT;
393 static const mask blank = _ISBLANK;
394 #elif defined(_NEWLIB_VERSION)
395 // Same type as Newlib's _ctype_ array in newlib/libc/include/ctype.h.
397 static const mask space = _S;
398 static const mask print = _P | _U | _L | _N | _B;
399 static const mask cntrl = _C;
400 static const mask upper = _U;
401 static const mask lower = _L;
402 static const mask alpha = _U | _L;
403 static const mask digit = _N;
404 static const mask punct = _P;
405 static const mask xdigit = _X | _N;
406 static const mask blank = _B;
407 # define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
408 # define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
409 # define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_XDIGIT
411 typedef unsigned long mask;
412 static const mask space = 1<<0;
413 static const mask print = 1<<1;
414 static const mask cntrl = 1<<2;
415 static const mask upper = 1<<3;
416 static const mask lower = 1<<4;
417 static const mask alpha = 1<<5;
418 static const mask digit = 1<<6;
419 static const mask punct = 1<<7;
420 static const mask xdigit = 1<<8;
421 static const mask blank = 1<<9;
423 static const mask alnum = alpha | digit;
424 static const mask graph = alnum | punct;
426 _LIBCPP_ALWAYS_INLINE ctype_base() {}
429 template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY ctype;
432 class _LIBCPP_TYPE_VIS ctype<wchar_t>
433 : public locale::facet,
437 typedef wchar_t char_type;
439 _LIBCPP_ALWAYS_INLINE
440 explicit ctype(size_t __refs = 0)
441 : locale::facet(__refs) {}
443 _LIBCPP_ALWAYS_INLINE
444 bool is(mask __m, char_type __c) const
446 return do_is(__m, __c);
449 _LIBCPP_ALWAYS_INLINE
450 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
452 return do_is(__low, __high, __vec);
455 _LIBCPP_ALWAYS_INLINE
456 const char_type* scan_is(mask __m, const char_type* __low, const char_type* __high) const
458 return do_scan_is(__m, __low, __high);
461 _LIBCPP_ALWAYS_INLINE
462 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
464 return do_scan_not(__m, __low, __high);
467 _LIBCPP_ALWAYS_INLINE
468 char_type toupper(char_type __c) const
470 return do_toupper(__c);
473 _LIBCPP_ALWAYS_INLINE
474 const char_type* toupper(char_type* __low, const char_type* __high) const
476 return do_toupper(__low, __high);
479 _LIBCPP_ALWAYS_INLINE
480 char_type tolower(char_type __c) const
482 return do_tolower(__c);
485 _LIBCPP_ALWAYS_INLINE
486 const char_type* tolower(char_type* __low, const char_type* __high) const
488 return do_tolower(__low, __high);
491 _LIBCPP_ALWAYS_INLINE
492 char_type widen(char __c) const
494 return do_widen(__c);
497 _LIBCPP_ALWAYS_INLINE
498 const char* widen(const char* __low, const char* __high, char_type* __to) const
500 return do_widen(__low, __high, __to);
503 _LIBCPP_ALWAYS_INLINE
504 char narrow(char_type __c, char __dfault) const
506 return do_narrow(__c, __dfault);
509 _LIBCPP_ALWAYS_INLINE
510 const char_type* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
512 return do_narrow(__low, __high, __dfault, __to);
515 static locale::id id;
519 virtual bool do_is(mask __m, char_type __c) const;
520 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
521 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
522 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
523 virtual char_type do_toupper(char_type) const;
524 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
525 virtual char_type do_tolower(char_type) const;
526 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
527 virtual char_type do_widen(char) const;
528 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
529 virtual char do_narrow(char_type, char __dfault) const;
530 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
534 class _LIBCPP_TYPE_VIS ctype<char>
535 : public locale::facet, public ctype_base
540 typedef char char_type;
542 explicit ctype(const mask* __tab = 0, bool __del = false, size_t __refs = 0);
544 _LIBCPP_ALWAYS_INLINE
545 bool is(mask __m, char_type __c) const
547 return isascii(__c) ? (__tab_[static_cast<int>(__c)] & __m) !=0 : false;
550 _LIBCPP_ALWAYS_INLINE
551 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
553 for (; __low != __high; ++__low, ++__vec)
554 *__vec = isascii(*__low) ? __tab_[static_cast<int>(*__low)] : 0;
558 _LIBCPP_ALWAYS_INLINE
559 const char_type* scan_is (mask __m, const char_type* __low, const char_type* __high) const
561 for (; __low != __high; ++__low)
562 if (isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m))
567 _LIBCPP_ALWAYS_INLINE
568 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
570 for (; __low != __high; ++__low)
571 if (!(isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m)))
576 _LIBCPP_ALWAYS_INLINE
577 char_type toupper(char_type __c) const
579 return do_toupper(__c);
582 _LIBCPP_ALWAYS_INLINE
583 const char_type* toupper(char_type* __low, const char_type* __high) const
585 return do_toupper(__low, __high);
588 _LIBCPP_ALWAYS_INLINE
589 char_type tolower(char_type __c) const
591 return do_tolower(__c);
594 _LIBCPP_ALWAYS_INLINE
595 const char_type* tolower(char_type* __low, const char_type* __high) const
597 return do_tolower(__low, __high);
600 _LIBCPP_ALWAYS_INLINE
601 char_type widen(char __c) const
603 return do_widen(__c);
606 _LIBCPP_ALWAYS_INLINE
607 const char* widen(const char* __low, const char* __high, char_type* __to) const
609 return do_widen(__low, __high, __to);
612 _LIBCPP_ALWAYS_INLINE
613 char narrow(char_type __c, char __dfault) const
615 return do_narrow(__c, __dfault);
618 _LIBCPP_ALWAYS_INLINE
619 const char* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
621 return do_narrow(__low, __high, __dfault, __to);
624 static locale::id id;
627 static const size_t table_size = _CACHED_RUNES;
629 static const size_t table_size = 256; // FIXME: Don't hardcode this.
631 _LIBCPP_ALWAYS_INLINE const mask* table() const _NOEXCEPT {return __tab_;}
632 static const mask* classic_table() _NOEXCEPT;
633 #if defined(__GLIBC__) || defined(__EMSCRIPTEN__)
634 static const int* __classic_upper_table() _NOEXCEPT;
635 static const int* __classic_lower_table() _NOEXCEPT;
637 #if defined(__NetBSD__)
638 static const short* __classic_upper_table() _NOEXCEPT;
639 static const short* __classic_lower_table() _NOEXCEPT;
644 virtual char_type do_toupper(char_type __c) const;
645 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
646 virtual char_type do_tolower(char_type __c) const;
647 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
648 virtual char_type do_widen(char __c) const;
649 virtual const char* do_widen(const char* __low, const char* __high, char_type* __to) const;
650 virtual char do_narrow(char_type __c, char __dfault) const;
651 virtual const char* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const;
654 // template <class CharT> class ctype_byname;
656 template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY ctype_byname;
659 class _LIBCPP_TYPE_VIS ctype_byname<char>
665 explicit ctype_byname(const char*, size_t = 0);
666 explicit ctype_byname(const string&, size_t = 0);
670 virtual char_type do_toupper(char_type) const;
671 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
672 virtual char_type do_tolower(char_type) const;
673 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
677 class _LIBCPP_TYPE_VIS ctype_byname<wchar_t>
678 : public ctype<wchar_t>
683 explicit ctype_byname(const char*, size_t = 0);
684 explicit ctype_byname(const string&, size_t = 0);
688 virtual bool do_is(mask __m, char_type __c) const;
689 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
690 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
691 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
692 virtual char_type do_toupper(char_type) const;
693 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
694 virtual char_type do_tolower(char_type) const;
695 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
696 virtual char_type do_widen(char) const;
697 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
698 virtual char do_narrow(char_type, char __dfault) const;
699 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
702 template <class _CharT>
703 inline _LIBCPP_INLINE_VISIBILITY
705 isspace(_CharT __c, const locale& __loc)
707 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
710 template <class _CharT>
711 inline _LIBCPP_INLINE_VISIBILITY
713 isprint(_CharT __c, const locale& __loc)
715 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c);
718 template <class _CharT>
719 inline _LIBCPP_INLINE_VISIBILITY
721 iscntrl(_CharT __c, const locale& __loc)
723 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c);
726 template <class _CharT>
727 inline _LIBCPP_INLINE_VISIBILITY
729 isupper(_CharT __c, const locale& __loc)
731 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c);
734 template <class _CharT>
735 inline _LIBCPP_INLINE_VISIBILITY
737 islower(_CharT __c, const locale& __loc)
739 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c);
742 template <class _CharT>
743 inline _LIBCPP_INLINE_VISIBILITY
745 isalpha(_CharT __c, const locale& __loc)
747 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c);
750 template <class _CharT>
751 inline _LIBCPP_INLINE_VISIBILITY
753 isdigit(_CharT __c, const locale& __loc)
755 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c);
758 template <class _CharT>
759 inline _LIBCPP_INLINE_VISIBILITY
761 ispunct(_CharT __c, const locale& __loc)
763 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c);
766 template <class _CharT>
767 inline _LIBCPP_INLINE_VISIBILITY
769 isxdigit(_CharT __c, const locale& __loc)
771 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c);
774 template <class _CharT>
775 inline _LIBCPP_INLINE_VISIBILITY
777 isalnum(_CharT __c, const locale& __loc)
779 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c);
782 template <class _CharT>
783 inline _LIBCPP_INLINE_VISIBILITY
785 isgraph(_CharT __c, const locale& __loc)
787 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c);
790 template <class _CharT>
791 inline _LIBCPP_INLINE_VISIBILITY
793 toupper(_CharT __c, const locale& __loc)
795 return use_facet<ctype<_CharT> >(__loc).toupper(__c);
798 template <class _CharT>
799 inline _LIBCPP_INLINE_VISIBILITY
801 tolower(_CharT __c, const locale& __loc)
803 return use_facet<ctype<_CharT> >(__loc).tolower(__c);
808 class _LIBCPP_TYPE_VIS codecvt_base
811 _LIBCPP_ALWAYS_INLINE codecvt_base() {}
812 enum result {ok, partial, error, noconv};
815 // template <class internT, class externT, class stateT> class codecvt;
817 template <class _InternT, class _ExternT, class _StateT> class _LIBCPP_TYPE_VIS_ONLY codecvt;
819 // template <> class codecvt<char, char, mbstate_t>
822 class _LIBCPP_TYPE_VIS codecvt<char, char, mbstate_t>
823 : public locale::facet,
827 typedef char intern_type;
828 typedef char extern_type;
829 typedef mbstate_t state_type;
831 _LIBCPP_ALWAYS_INLINE
832 explicit codecvt(size_t __refs = 0)
833 : locale::facet(__refs) {}
835 _LIBCPP_ALWAYS_INLINE
836 result out(state_type& __st,
837 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
838 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
840 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
843 _LIBCPP_ALWAYS_INLINE
844 result unshift(state_type& __st,
845 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
847 return do_unshift(__st, __to, __to_end, __to_nxt);
850 _LIBCPP_ALWAYS_INLINE
851 result in(state_type& __st,
852 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
853 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
855 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
858 _LIBCPP_ALWAYS_INLINE
859 int encoding() const _NOEXCEPT
861 return do_encoding();
864 _LIBCPP_ALWAYS_INLINE
865 bool always_noconv() const _NOEXCEPT
867 return do_always_noconv();
870 _LIBCPP_ALWAYS_INLINE
871 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
873 return do_length(__st, __frm, __end, __mx);
876 _LIBCPP_ALWAYS_INLINE
877 int max_length() const _NOEXCEPT
879 return do_max_length();
882 static locale::id id;
885 _LIBCPP_ALWAYS_INLINE
886 explicit codecvt(const char*, size_t __refs = 0)
887 : locale::facet(__refs) {}
891 virtual result do_out(state_type& __st,
892 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
893 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
894 virtual result do_in(state_type& __st,
895 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
896 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
897 virtual result do_unshift(state_type& __st,
898 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
899 virtual int do_encoding() const _NOEXCEPT;
900 virtual bool do_always_noconv() const _NOEXCEPT;
901 virtual int do_length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
902 virtual int do_max_length() const _NOEXCEPT;
905 // template <> class codecvt<wchar_t, char, mbstate_t>
908 class _LIBCPP_TYPE_VIS codecvt<wchar_t, char, mbstate_t>
909 : public locale::facet,
914 typedef wchar_t intern_type;
915 typedef char extern_type;
916 typedef mbstate_t state_type;
918 explicit codecvt(size_t __refs = 0);
920 _LIBCPP_ALWAYS_INLINE
921 result out(state_type& __st,
922 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
923 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
925 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
928 _LIBCPP_ALWAYS_INLINE
929 result unshift(state_type& __st,
930 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
932 return do_unshift(__st, __to, __to_end, __to_nxt);
935 _LIBCPP_ALWAYS_INLINE
936 result in(state_type& __st,
937 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
938 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
940 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
943 _LIBCPP_ALWAYS_INLINE
944 int encoding() const _NOEXCEPT
946 return do_encoding();
949 _LIBCPP_ALWAYS_INLINE
950 bool always_noconv() const _NOEXCEPT
952 return do_always_noconv();
955 _LIBCPP_ALWAYS_INLINE
956 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
958 return do_length(__st, __frm, __end, __mx);
961 _LIBCPP_ALWAYS_INLINE
962 int max_length() const _NOEXCEPT
964 return do_max_length();
967 static locale::id id;
970 explicit codecvt(const char*, size_t __refs = 0);
974 virtual result do_out(state_type& __st,
975 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
976 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
977 virtual result do_in(state_type& __st,
978 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
979 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
980 virtual result do_unshift(state_type& __st,
981 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
982 virtual int do_encoding() const _NOEXCEPT;
983 virtual bool do_always_noconv() const _NOEXCEPT;
984 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
985 virtual int do_max_length() const _NOEXCEPT;
988 // template <> class codecvt<char16_t, char, mbstate_t>
991 class _LIBCPP_TYPE_VIS codecvt<char16_t, char, mbstate_t>
992 : public locale::facet,
996 typedef char16_t intern_type;
997 typedef char extern_type;
998 typedef mbstate_t state_type;
1000 _LIBCPP_ALWAYS_INLINE
1001 explicit codecvt(size_t __refs = 0)
1002 : locale::facet(__refs) {}
1004 _LIBCPP_ALWAYS_INLINE
1005 result out(state_type& __st,
1006 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1007 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1009 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1012 _LIBCPP_ALWAYS_INLINE
1013 result unshift(state_type& __st,
1014 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1016 return do_unshift(__st, __to, __to_end, __to_nxt);
1019 _LIBCPP_ALWAYS_INLINE
1020 result in(state_type& __st,
1021 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1022 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1024 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1027 _LIBCPP_ALWAYS_INLINE
1028 int encoding() const _NOEXCEPT
1030 return do_encoding();
1033 _LIBCPP_ALWAYS_INLINE
1034 bool always_noconv() const _NOEXCEPT
1036 return do_always_noconv();
1039 _LIBCPP_ALWAYS_INLINE
1040 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1042 return do_length(__st, __frm, __end, __mx);
1045 _LIBCPP_ALWAYS_INLINE
1046 int max_length() const _NOEXCEPT
1048 return do_max_length();
1051 static locale::id id;
1054 _LIBCPP_ALWAYS_INLINE
1055 explicit codecvt(const char*, size_t __refs = 0)
1056 : locale::facet(__refs) {}
1060 virtual result do_out(state_type& __st,
1061 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1062 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1063 virtual result do_in(state_type& __st,
1064 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1065 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1066 virtual result do_unshift(state_type& __st,
1067 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1068 virtual int do_encoding() const _NOEXCEPT;
1069 virtual bool do_always_noconv() const _NOEXCEPT;
1070 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
1071 virtual int do_max_length() const _NOEXCEPT;
1074 // template <> class codecvt<char32_t, char, mbstate_t>
1077 class _LIBCPP_TYPE_VIS codecvt<char32_t, char, mbstate_t>
1078 : public locale::facet,
1082 typedef char32_t intern_type;
1083 typedef char extern_type;
1084 typedef mbstate_t state_type;
1086 _LIBCPP_ALWAYS_INLINE
1087 explicit codecvt(size_t __refs = 0)
1088 : locale::facet(__refs) {}
1090 _LIBCPP_ALWAYS_INLINE
1091 result out(state_type& __st,
1092 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1093 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1095 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1098 _LIBCPP_ALWAYS_INLINE
1099 result unshift(state_type& __st,
1100 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1102 return do_unshift(__st, __to, __to_end, __to_nxt);
1105 _LIBCPP_ALWAYS_INLINE
1106 result in(state_type& __st,
1107 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1108 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1110 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1113 _LIBCPP_ALWAYS_INLINE
1114 int encoding() const _NOEXCEPT
1116 return do_encoding();
1119 _LIBCPP_ALWAYS_INLINE
1120 bool always_noconv() const _NOEXCEPT
1122 return do_always_noconv();
1125 _LIBCPP_ALWAYS_INLINE
1126 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1128 return do_length(__st, __frm, __end, __mx);
1131 _LIBCPP_ALWAYS_INLINE
1132 int max_length() const _NOEXCEPT
1134 return do_max_length();
1137 static locale::id id;
1140 _LIBCPP_ALWAYS_INLINE
1141 explicit codecvt(const char*, size_t __refs = 0)
1142 : locale::facet(__refs) {}
1146 virtual result do_out(state_type& __st,
1147 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1148 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1149 virtual result do_in(state_type& __st,
1150 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1151 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1152 virtual result do_unshift(state_type& __st,
1153 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1154 virtual int do_encoding() const _NOEXCEPT;
1155 virtual bool do_always_noconv() const _NOEXCEPT;
1156 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
1157 virtual int do_max_length() const _NOEXCEPT;
1160 // template <class _InternT, class _ExternT, class _StateT> class codecvt_byname
1162 template <class _InternT, class _ExternT, class _StateT>
1163 class _LIBCPP_TYPE_VIS_ONLY codecvt_byname
1164 : public codecvt<_InternT, _ExternT, _StateT>
1167 _LIBCPP_ALWAYS_INLINE
1168 explicit codecvt_byname(const char* __nm, size_t __refs = 0)
1169 : codecvt<_InternT, _ExternT, _StateT>(__nm, __refs) {}
1170 _LIBCPP_ALWAYS_INLINE
1171 explicit codecvt_byname(const string& __nm, size_t __refs = 0)
1172 : codecvt<_InternT, _ExternT, _StateT>(__nm.c_str(), __refs) {}
1177 template <class _InternT, class _ExternT, class _StateT>
1178 codecvt_byname<_InternT, _ExternT, _StateT>::~codecvt_byname()
1182 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char, char, mbstate_t>)
1183 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<wchar_t, char, mbstate_t>)
1184 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char16_t, char, mbstate_t>)
1185 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char32_t, char, mbstate_t>)
1187 _LIBCPP_FUNC_VIS void __throw_runtime_error(const char*);
1189 template <size_t _Np>
1190 struct __narrow_to_utf8
1192 template <class _OutputIterator, class _CharT>
1194 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const;
1198 struct __narrow_to_utf8<8>
1200 template <class _OutputIterator, class _CharT>
1201 _LIBCPP_ALWAYS_INLINE
1203 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1205 for (; __wb < __we; ++__wb, ++__s)
1212 struct __narrow_to_utf8<16>
1213 : public codecvt<char16_t, char, mbstate_t>
1215 _LIBCPP_ALWAYS_INLINE
1216 __narrow_to_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1218 ~__narrow_to_utf8();
1220 template <class _OutputIterator, class _CharT>
1221 _LIBCPP_ALWAYS_INLINE
1223 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1227 while (__wb < __we && __r != error)
1229 const int __sz = 32;
1232 const char16_t* __wn = (const char16_t*)__wb;
1233 __r = do_out(__mb, (const char16_t*)__wb, (const char16_t*)__we, __wn,
1234 __buf, __buf+__sz, __bn);
1235 if (__r == codecvt_base::error || __wn == (const char16_t*)__wb)
1236 __throw_runtime_error("locale not supported");
1237 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1239 __wb = (const _CharT*)__wn;
1246 struct __narrow_to_utf8<32>
1247 : public codecvt<char32_t, char, mbstate_t>
1249 _LIBCPP_ALWAYS_INLINE
1250 __narrow_to_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1252 ~__narrow_to_utf8();
1254 template <class _OutputIterator, class _CharT>
1255 _LIBCPP_ALWAYS_INLINE
1257 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1261 while (__wb < __we && __r != error)
1263 const int __sz = 32;
1266 const char32_t* __wn = (const char32_t*)__wb;
1267 __r = do_out(__mb, (const char32_t*)__wb, (const char32_t*)__we, __wn,
1268 __buf, __buf+__sz, __bn);
1269 if (__r == codecvt_base::error || __wn == (const char32_t*)__wb)
1270 __throw_runtime_error("locale not supported");
1271 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1273 __wb = (const _CharT*)__wn;
1279 template <size_t _Np>
1280 struct __widen_from_utf8
1282 template <class _OutputIterator>
1284 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const;
1288 struct __widen_from_utf8<8>
1290 template <class _OutputIterator>
1291 _LIBCPP_ALWAYS_INLINE
1293 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1295 for (; __nb < __ne; ++__nb, ++__s)
1302 struct __widen_from_utf8<16>
1303 : public codecvt<char16_t, char, mbstate_t>
1305 _LIBCPP_ALWAYS_INLINE
1306 __widen_from_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1308 ~__widen_from_utf8();
1310 template <class _OutputIterator>
1311 _LIBCPP_ALWAYS_INLINE
1313 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1317 while (__nb < __ne && __r != error)
1319 const int __sz = 32;
1320 char16_t __buf[__sz];
1322 const char* __nn = __nb;
1323 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1324 __buf, __buf+__sz, __bn);
1325 if (__r == codecvt_base::error || __nn == __nb)
1326 __throw_runtime_error("locale not supported");
1327 for (const char16_t* __p = __buf; __p < __bn; ++__p, ++__s)
1328 *__s = (wchar_t)*__p;
1336 struct __widen_from_utf8<32>
1337 : public codecvt<char32_t, char, mbstate_t>
1339 _LIBCPP_ALWAYS_INLINE
1340 __widen_from_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1342 ~__widen_from_utf8();
1344 template <class _OutputIterator>
1345 _LIBCPP_ALWAYS_INLINE
1347 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1351 while (__nb < __ne && __r != error)
1353 const int __sz = 32;
1354 char32_t __buf[__sz];
1356 const char* __nn = __nb;
1357 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1358 __buf, __buf+__sz, __bn);
1359 if (__r == codecvt_base::error || __nn == __nb)
1360 __throw_runtime_error("locale not supported");
1361 for (const char32_t* __p = __buf; __p < __bn; ++__p, ++__s)
1362 *__s = (wchar_t)*__p;
1369 // template <class charT> class numpunct
1371 template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY numpunct;
1374 class _LIBCPP_TYPE_VIS numpunct<char>
1375 : public locale::facet
1378 typedef char char_type;
1379 typedef basic_string<char_type> string_type;
1381 explicit numpunct(size_t __refs = 0);
1383 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
1384 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
1385 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
1386 _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();}
1387 _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();}
1389 static locale::id id;
1393 virtual char_type do_decimal_point() const;
1394 virtual char_type do_thousands_sep() const;
1395 virtual string do_grouping() const;
1396 virtual string_type do_truename() const;
1397 virtual string_type do_falsename() const;
1399 char_type __decimal_point_;
1400 char_type __thousands_sep_;
1405 class _LIBCPP_TYPE_VIS numpunct<wchar_t>
1406 : public locale::facet
1409 typedef wchar_t char_type;
1410 typedef basic_string<char_type> string_type;
1412 explicit numpunct(size_t __refs = 0);
1414 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
1415 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
1416 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
1417 _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();}
1418 _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();}
1420 static locale::id id;
1424 virtual char_type do_decimal_point() const;
1425 virtual char_type do_thousands_sep() const;
1426 virtual string do_grouping() const;
1427 virtual string_type do_truename() const;
1428 virtual string_type do_falsename() const;
1430 char_type __decimal_point_;
1431 char_type __thousands_sep_;
1435 // template <class charT> class numpunct_byname
1437 template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY numpunct_byname;
1440 class _LIBCPP_TYPE_VIS numpunct_byname<char>
1441 : public numpunct<char>
1444 typedef char char_type;
1445 typedef basic_string<char_type> string_type;
1447 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1448 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1454 void __init(const char*);
1458 class _LIBCPP_TYPE_VIS numpunct_byname<wchar_t>
1459 : public numpunct<wchar_t>
1462 typedef wchar_t char_type;
1463 typedef basic_string<char_type> string_type;
1465 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1466 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1472 void __init(const char*);
1475 _LIBCPP_END_NAMESPACE_STD
1477 #endif // _LIBCPP___LOCALE