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 #endif // __GLIBC__ || __APPLE__ || __FreeBSD__ || __sun__ || __EMSCRIPTEN__ || __IBMCPP__
42 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
43 #pragma GCC system_header
46 _LIBCPP_BEGIN_NAMESPACE_STD
48 class _LIBCPP_TYPE_VIS locale;
50 template <class _Facet>
51 _LIBCPP_INLINE_VISIBILITY
53 has_facet(const locale&) _NOEXCEPT;
55 template <class _Facet>
56 _LIBCPP_INLINE_VISIBILITY
58 use_facet(const locale&);
60 class _LIBCPP_TYPE_VIS locale
64 class _LIBCPP_TYPE_VIS facet;
65 class _LIBCPP_TYPE_VIS id;
68 static const category // values assigned here are for exposition only
70 collate = LC_COLLATE_MASK,
71 ctype = LC_CTYPE_MASK,
72 monetary = LC_MONETARY_MASK,
73 numeric = LC_NUMERIC_MASK,
75 messages = LC_MESSAGES_MASK,
76 all = collate | ctype | monetary | numeric | time | messages;
78 // construct/copy/destroy:
80 locale(const locale&) _NOEXCEPT;
81 explicit locale(const char*);
82 explicit locale(const string&);
83 locale(const locale&, const char*, category);
84 locale(const locale&, const string&, category);
85 template <class _Facet>
86 _LIBCPP_INLINE_VISIBILITY locale(const locale&, _Facet*);
87 locale(const locale&, const locale&, category);
91 const locale& operator=(const locale&) _NOEXCEPT;
93 template <class _Facet> locale combine(const locale&) const;
97 bool operator==(const locale&) const;
98 bool operator!=(const locale& __y) const {return !(*this == __y);}
99 template <class _CharT, class _Traits, class _Allocator>
100 bool operator()(const basic_string<_CharT, _Traits, _Allocator>&,
101 const basic_string<_CharT, _Traits, _Allocator>&) const;
103 // global locale objects:
104 static locale global(const locale&);
105 static const locale& classic();
111 void __install_ctor(const locale&, facet*, long);
112 static locale& __global();
113 bool has_facet(id&) const;
114 const facet* use_facet(id&) const;
116 template <class _Facet> friend bool has_facet(const locale&) _NOEXCEPT;
117 template <class _Facet> friend const _Facet& use_facet(const locale&);
120 class _LIBCPP_TYPE_VIS locale::facet
121 : public __shared_count
124 _LIBCPP_INLINE_VISIBILITY
125 explicit facet(size_t __refs = 0)
126 : __shared_count(static_cast<long>(__refs)-1) {}
130 // facet(const facet&) = delete; // effectively done in __shared_count
131 // void operator=(const facet&) = delete;
133 virtual void __on_zero_shared() _NOEXCEPT;
136 class _LIBCPP_TYPE_VIS locale::id
141 static int32_t __next_id;
143 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR id() :__id_(0) {}
146 void operator=(const id&); // = delete;
147 id(const id&); // = delete;
148 public: // only needed for tests
152 friend class locale::__imp;
155 template <class _Facet>
156 inline _LIBCPP_INLINE_VISIBILITY
157 locale::locale(const locale& __other, _Facet* __f)
159 __install_ctor(__other, __f, __f ? __f->id.__get() : 0);
162 template <class _Facet>
164 locale::combine(const locale& __other) const
166 #ifndef _LIBCPP_NO_EXCEPTIONS
167 if (!_VSTD::has_facet<_Facet>(__other))
168 throw runtime_error("locale::combine: locale missing facet");
169 #endif // _LIBCPP_NO_EXCEPTIONS
170 return locale(*this, &const_cast<_Facet&>(_VSTD::use_facet<_Facet>(__other)));
173 template <class _Facet>
174 inline _LIBCPP_INLINE_VISIBILITY
176 has_facet(const locale& __l) _NOEXCEPT
178 return __l.has_facet(_Facet::id);
181 template <class _Facet>
182 inline _LIBCPP_INLINE_VISIBILITY
184 use_facet(const locale& __l)
186 return static_cast<const _Facet&>(*__l.use_facet(_Facet::id));
189 // template <class _CharT> class collate;
191 template <class _CharT>
192 class _LIBCPP_TYPE_VIS_ONLY collate
193 : public locale::facet
196 typedef _CharT char_type;
197 typedef basic_string<char_type> string_type;
199 _LIBCPP_INLINE_VISIBILITY
200 explicit collate(size_t __refs = 0)
201 : locale::facet(__refs) {}
203 _LIBCPP_INLINE_VISIBILITY
204 int compare(const char_type* __lo1, const char_type* __hi1,
205 const char_type* __lo2, const char_type* __hi2) const
207 return do_compare(__lo1, __hi1, __lo2, __hi2);
210 _LIBCPP_INLINE_VISIBILITY
211 string_type transform(const char_type* __lo, const char_type* __hi) const
213 return do_transform(__lo, __hi);
216 _LIBCPP_INLINE_VISIBILITY
217 long hash(const char_type* __lo, const char_type* __hi) const
219 return do_hash(__lo, __hi);
222 static locale::id id;
226 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
227 const char_type* __lo2, const char_type* __hi2) const;
228 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const
229 {return string_type(__lo, __hi);}
230 virtual long do_hash(const char_type* __lo, const char_type* __hi) const;
233 template <class _CharT> locale::id collate<_CharT>::id;
235 template <class _CharT>
236 collate<_CharT>::~collate()
240 template <class _CharT>
242 collate<_CharT>::do_compare(const char_type* __lo1, const char_type* __hi1,
243 const char_type* __lo2, const char_type* __hi2) const
245 for (; __lo2 != __hi2; ++__lo1, ++__lo2)
247 if (__lo1 == __hi1 || *__lo1 < *__lo2)
252 return __lo1 != __hi1;
255 template <class _CharT>
257 collate<_CharT>::do_hash(const char_type* __lo, const char_type* __hi) const
260 const size_t __sr = __CHAR_BIT__ * sizeof(size_t) - 8;
261 const size_t __mask = size_t(0xF) << (__sr + 4);
262 for(const char_type* __p = __lo; __p != __hi; ++__p)
264 __h = (__h << 4) + static_cast<size_t>(*__p);
265 size_t __g = __h & __mask;
266 __h ^= __g | (__g >> __sr);
268 return static_cast<long>(__h);
271 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS collate<char>)
272 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS collate<wchar_t>)
274 // template <class CharT> class collate_byname;
276 template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY collate_byname;
279 class _LIBCPP_TYPE_VIS collate_byname<char>
280 : public collate<char>
284 typedef char char_type;
285 typedef basic_string<char_type> string_type;
287 explicit collate_byname(const char* __n, size_t __refs = 0);
288 explicit collate_byname(const string& __n, size_t __refs = 0);
292 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
293 const char_type* __lo2, const char_type* __hi2) const;
294 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
298 class _LIBCPP_TYPE_VIS collate_byname<wchar_t>
299 : public collate<wchar_t>
303 typedef wchar_t char_type;
304 typedef basic_string<char_type> string_type;
306 explicit collate_byname(const char* __n, size_t __refs = 0);
307 explicit collate_byname(const string& __n, size_t __refs = 0);
312 virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
313 const char_type* __lo2, const char_type* __hi2) const;
314 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
317 template <class _CharT, class _Traits, class _Allocator>
319 locale::operator()(const basic_string<_CharT, _Traits, _Allocator>& __x,
320 const basic_string<_CharT, _Traits, _Allocator>& __y) const
322 return _VSTD::use_facet<_VSTD::collate<_CharT> >(*this).compare(
323 __x.data(), __x.data() + __x.size(),
324 __y.data(), __y.data() + __y.size()) < 0;
327 // template <class charT> class ctype
329 class _LIBCPP_TYPE_VIS ctype_base
333 typedef unsigned short mask;
334 static const mask space = _ISspace;
335 static const mask print = _ISprint;
336 static const mask cntrl = _IScntrl;
337 static const mask upper = _ISupper;
338 static const mask lower = _ISlower;
339 static const mask alpha = _ISalpha;
340 static const mask digit = _ISdigit;
341 static const mask punct = _ISpunct;
342 static const mask xdigit = _ISxdigit;
343 static const mask blank = _ISblank;
344 #elif defined(_WIN32)
345 typedef unsigned short mask;
346 static const mask space = _SPACE;
347 static const mask print = _BLANK|_PUNCT|_ALPHA|_DIGIT;
348 static const mask cntrl = _CONTROL;
349 static const mask upper = _UPPER;
350 static const mask lower = _LOWER;
351 static const mask alpha = _ALPHA;
352 static const mask digit = _DIGIT;
353 static const mask punct = _PUNCT;
354 static const mask xdigit = _HEX;
355 static const mask blank = _BLANK;
356 # define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
357 #elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__) || defined(__minix)
359 typedef __uint32_t mask;
360 # elif defined(__FreeBSD__)
361 typedef unsigned long mask;
362 # elif defined(__EMSCRIPTEN__) || defined(__NetBSD__) || defined(__minix)
363 typedef unsigned short mask;
365 static const mask space = _CTYPE_S;
366 static const mask print = _CTYPE_R;
367 static const mask cntrl = _CTYPE_C;
368 static const mask upper = _CTYPE_U;
369 static const mask lower = _CTYPE_L;
370 static const mask alpha = _CTYPE_A;
371 static const mask digit = _CTYPE_D;
372 static const mask punct = _CTYPE_P;
373 static const mask xdigit = _CTYPE_X;
375 # if defined(__NetBSD__) || defined(__minix)
376 static const mask blank = _CTYPE_BL;
378 static const mask blank = _CTYPE_B;
380 #elif defined(__sun__) || defined(_AIX)
381 typedef unsigned int mask;
382 static const mask space = _ISSPACE;
383 static const mask print = _ISPRINT;
384 static const mask cntrl = _ISCNTRL;
385 static const mask upper = _ISUPPER;
386 static const mask lower = _ISLOWER;
387 static const mask alpha = _ISALPHA;
388 static const mask digit = _ISDIGIT;
389 static const mask punct = _ISPUNCT;
390 static const mask xdigit = _ISXDIGIT;
391 static const mask blank = _ISBLANK;
392 #elif defined(_NEWLIB_VERSION)
393 // Same type as Newlib's _ctype_ array in newlib/libc/include/ctype.h.
395 static const mask space = _S;
396 static const mask print = _P | _U | _L | _N | _B;
397 static const mask cntrl = _C;
398 static const mask upper = _U;
399 static const mask lower = _L;
400 static const mask alpha = _U | _L;
401 static const mask digit = _N;
402 static const mask punct = _P;
403 static const mask xdigit = _X | _N;
404 static const mask blank = _B;
405 # define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
406 # define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
407 # define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_XDIGIT
409 typedef unsigned long mask;
410 static const mask space = 1<<0;
411 static const mask print = 1<<1;
412 static const mask cntrl = 1<<2;
413 static const mask upper = 1<<3;
414 static const mask lower = 1<<4;
415 static const mask alpha = 1<<5;
416 static const mask digit = 1<<6;
417 static const mask punct = 1<<7;
418 static const mask xdigit = 1<<8;
419 static const mask blank = 1<<9;
421 static const mask alnum = alpha | digit;
422 static const mask graph = alnum | punct;
424 _LIBCPP_ALWAYS_INLINE ctype_base() {}
427 template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY ctype;
430 class _LIBCPP_TYPE_VIS ctype<wchar_t>
431 : public locale::facet,
435 typedef wchar_t char_type;
437 _LIBCPP_ALWAYS_INLINE
438 explicit ctype(size_t __refs = 0)
439 : locale::facet(__refs) {}
441 _LIBCPP_ALWAYS_INLINE
442 bool is(mask __m, char_type __c) const
444 return do_is(__m, __c);
447 _LIBCPP_ALWAYS_INLINE
448 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
450 return do_is(__low, __high, __vec);
453 _LIBCPP_ALWAYS_INLINE
454 const char_type* scan_is(mask __m, const char_type* __low, const char_type* __high) const
456 return do_scan_is(__m, __low, __high);
459 _LIBCPP_ALWAYS_INLINE
460 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
462 return do_scan_not(__m, __low, __high);
465 _LIBCPP_ALWAYS_INLINE
466 char_type toupper(char_type __c) const
468 return do_toupper(__c);
471 _LIBCPP_ALWAYS_INLINE
472 const char_type* toupper(char_type* __low, const char_type* __high) const
474 return do_toupper(__low, __high);
477 _LIBCPP_ALWAYS_INLINE
478 char_type tolower(char_type __c) const
480 return do_tolower(__c);
483 _LIBCPP_ALWAYS_INLINE
484 const char_type* tolower(char_type* __low, const char_type* __high) const
486 return do_tolower(__low, __high);
489 _LIBCPP_ALWAYS_INLINE
490 char_type widen(char __c) const
492 return do_widen(__c);
495 _LIBCPP_ALWAYS_INLINE
496 const char* widen(const char* __low, const char* __high, char_type* __to) const
498 return do_widen(__low, __high, __to);
501 _LIBCPP_ALWAYS_INLINE
502 char narrow(char_type __c, char __dfault) const
504 return do_narrow(__c, __dfault);
507 _LIBCPP_ALWAYS_INLINE
508 const char_type* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
510 return do_narrow(__low, __high, __dfault, __to);
513 static locale::id id;
517 virtual bool do_is(mask __m, char_type __c) const;
518 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
519 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
520 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
521 virtual char_type do_toupper(char_type) const;
522 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
523 virtual char_type do_tolower(char_type) const;
524 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
525 virtual char_type do_widen(char) const;
526 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
527 virtual char do_narrow(char_type, char __dfault) const;
528 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
532 class _LIBCPP_TYPE_VIS ctype<char>
533 : public locale::facet, public ctype_base
538 typedef char char_type;
540 explicit ctype(const mask* __tab = 0, bool __del = false, size_t __refs = 0);
542 _LIBCPP_ALWAYS_INLINE
543 bool is(mask __m, char_type __c) const
545 return isascii(__c) ? (__tab_[static_cast<int>(__c)] & __m) !=0 : false;
548 _LIBCPP_ALWAYS_INLINE
549 const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
551 for (; __low != __high; ++__low, ++__vec)
552 *__vec = isascii(*__low) ? __tab_[static_cast<int>(*__low)] : 0;
556 _LIBCPP_ALWAYS_INLINE
557 const char_type* scan_is (mask __m, const char_type* __low, const char_type* __high) const
559 for (; __low != __high; ++__low)
560 if (isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m))
565 _LIBCPP_ALWAYS_INLINE
566 const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
568 for (; __low != __high; ++__low)
569 if (!(isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m)))
574 _LIBCPP_ALWAYS_INLINE
575 char_type toupper(char_type __c) const
577 return do_toupper(__c);
580 _LIBCPP_ALWAYS_INLINE
581 const char_type* toupper(char_type* __low, const char_type* __high) const
583 return do_toupper(__low, __high);
586 _LIBCPP_ALWAYS_INLINE
587 char_type tolower(char_type __c) const
589 return do_tolower(__c);
592 _LIBCPP_ALWAYS_INLINE
593 const char_type* tolower(char_type* __low, const char_type* __high) const
595 return do_tolower(__low, __high);
598 _LIBCPP_ALWAYS_INLINE
599 char_type widen(char __c) const
601 return do_widen(__c);
604 _LIBCPP_ALWAYS_INLINE
605 const char* widen(const char* __low, const char* __high, char_type* __to) const
607 return do_widen(__low, __high, __to);
610 _LIBCPP_ALWAYS_INLINE
611 char narrow(char_type __c, char __dfault) const
613 return do_narrow(__c, __dfault);
616 _LIBCPP_ALWAYS_INLINE
617 const char* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
619 return do_narrow(__low, __high, __dfault, __to);
622 static locale::id id;
625 static const size_t table_size = _CACHED_RUNES;
627 static const size_t table_size = 256; // FIXME: Don't hardcode this.
629 _LIBCPP_ALWAYS_INLINE const mask* table() const _NOEXCEPT {return __tab_;}
630 static const mask* classic_table() _NOEXCEPT;
631 #if defined(__GLIBC__) || defined(__EMSCRIPTEN__)
632 static const int* __classic_upper_table() _NOEXCEPT;
633 static const int* __classic_lower_table() _NOEXCEPT;
635 #if defined(__NetBSD__) || defined(__minix)
636 static const short* __classic_upper_table() _NOEXCEPT;
637 static const short* __classic_lower_table() _NOEXCEPT;
642 virtual char_type do_toupper(char_type __c) const;
643 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
644 virtual char_type do_tolower(char_type __c) const;
645 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
646 virtual char_type do_widen(char __c) const;
647 virtual const char* do_widen(const char* __low, const char* __high, char_type* __to) const;
648 virtual char do_narrow(char_type __c, char __dfault) const;
649 virtual const char* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const;
652 // template <class CharT> class ctype_byname;
654 template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY ctype_byname;
657 class _LIBCPP_TYPE_VIS ctype_byname<char>
663 explicit ctype_byname(const char*, size_t = 0);
664 explicit ctype_byname(const string&, size_t = 0);
668 virtual char_type do_toupper(char_type) const;
669 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
670 virtual char_type do_tolower(char_type) const;
671 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
675 class _LIBCPP_TYPE_VIS ctype_byname<wchar_t>
676 : public ctype<wchar_t>
681 explicit ctype_byname(const char*, size_t = 0);
682 explicit ctype_byname(const string&, size_t = 0);
686 virtual bool do_is(mask __m, char_type __c) const;
687 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
688 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
689 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
690 virtual char_type do_toupper(char_type) const;
691 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
692 virtual char_type do_tolower(char_type) const;
693 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
694 virtual char_type do_widen(char) const;
695 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
696 virtual char do_narrow(char_type, char __dfault) const;
697 virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
700 template <class _CharT>
701 inline _LIBCPP_INLINE_VISIBILITY
703 isspace(_CharT __c, const locale& __loc)
705 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
708 template <class _CharT>
709 inline _LIBCPP_INLINE_VISIBILITY
711 isprint(_CharT __c, const locale& __loc)
713 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c);
716 template <class _CharT>
717 inline _LIBCPP_INLINE_VISIBILITY
719 iscntrl(_CharT __c, const locale& __loc)
721 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c);
724 template <class _CharT>
725 inline _LIBCPP_INLINE_VISIBILITY
727 isupper(_CharT __c, const locale& __loc)
729 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c);
732 template <class _CharT>
733 inline _LIBCPP_INLINE_VISIBILITY
735 islower(_CharT __c, const locale& __loc)
737 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c);
740 template <class _CharT>
741 inline _LIBCPP_INLINE_VISIBILITY
743 isalpha(_CharT __c, const locale& __loc)
745 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c);
748 template <class _CharT>
749 inline _LIBCPP_INLINE_VISIBILITY
751 isdigit(_CharT __c, const locale& __loc)
753 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c);
756 template <class _CharT>
757 inline _LIBCPP_INLINE_VISIBILITY
759 ispunct(_CharT __c, const locale& __loc)
761 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c);
764 template <class _CharT>
765 inline _LIBCPP_INLINE_VISIBILITY
767 isxdigit(_CharT __c, const locale& __loc)
769 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c);
772 template <class _CharT>
773 inline _LIBCPP_INLINE_VISIBILITY
775 isalnum(_CharT __c, const locale& __loc)
777 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c);
780 template <class _CharT>
781 inline _LIBCPP_INLINE_VISIBILITY
783 isgraph(_CharT __c, const locale& __loc)
785 return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c);
788 template <class _CharT>
789 inline _LIBCPP_INLINE_VISIBILITY
791 toupper(_CharT __c, const locale& __loc)
793 return use_facet<ctype<_CharT> >(__loc).toupper(__c);
796 template <class _CharT>
797 inline _LIBCPP_INLINE_VISIBILITY
799 tolower(_CharT __c, const locale& __loc)
801 return use_facet<ctype<_CharT> >(__loc).tolower(__c);
806 class _LIBCPP_TYPE_VIS codecvt_base
809 _LIBCPP_ALWAYS_INLINE codecvt_base() {}
810 enum result {ok, partial, error, noconv};
813 // template <class internT, class externT, class stateT> class codecvt;
815 template <class _InternT, class _ExternT, class _StateT> class _LIBCPP_TYPE_VIS_ONLY codecvt;
817 // template <> class codecvt<char, char, mbstate_t>
820 class _LIBCPP_TYPE_VIS codecvt<char, char, mbstate_t>
821 : public locale::facet,
825 typedef char intern_type;
826 typedef char extern_type;
827 typedef mbstate_t state_type;
829 _LIBCPP_ALWAYS_INLINE
830 explicit codecvt(size_t __refs = 0)
831 : locale::facet(__refs) {}
833 _LIBCPP_ALWAYS_INLINE
834 result out(state_type& __st,
835 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
836 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
838 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
841 _LIBCPP_ALWAYS_INLINE
842 result unshift(state_type& __st,
843 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
845 return do_unshift(__st, __to, __to_end, __to_nxt);
848 _LIBCPP_ALWAYS_INLINE
849 result in(state_type& __st,
850 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
851 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
853 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
856 _LIBCPP_ALWAYS_INLINE
857 int encoding() const _NOEXCEPT
859 return do_encoding();
862 _LIBCPP_ALWAYS_INLINE
863 bool always_noconv() const _NOEXCEPT
865 return do_always_noconv();
868 _LIBCPP_ALWAYS_INLINE
869 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
871 return do_length(__st, __frm, __end, __mx);
874 _LIBCPP_ALWAYS_INLINE
875 int max_length() const _NOEXCEPT
877 return do_max_length();
880 static locale::id id;
883 _LIBCPP_ALWAYS_INLINE
884 explicit codecvt(const char*, size_t __refs = 0)
885 : locale::facet(__refs) {}
889 virtual result do_out(state_type& __st,
890 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
891 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
892 virtual result do_in(state_type& __st,
893 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
894 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
895 virtual result do_unshift(state_type& __st,
896 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
897 virtual int do_encoding() const _NOEXCEPT;
898 virtual bool do_always_noconv() const _NOEXCEPT;
899 virtual int do_length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
900 virtual int do_max_length() const _NOEXCEPT;
903 // template <> class codecvt<wchar_t, char, mbstate_t>
906 class _LIBCPP_TYPE_VIS codecvt<wchar_t, char, mbstate_t>
907 : public locale::facet,
912 typedef wchar_t intern_type;
913 typedef char extern_type;
914 typedef mbstate_t state_type;
916 explicit codecvt(size_t __refs = 0);
918 _LIBCPP_ALWAYS_INLINE
919 result out(state_type& __st,
920 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
921 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
923 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
926 _LIBCPP_ALWAYS_INLINE
927 result unshift(state_type& __st,
928 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
930 return do_unshift(__st, __to, __to_end, __to_nxt);
933 _LIBCPP_ALWAYS_INLINE
934 result in(state_type& __st,
935 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
936 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
938 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
941 _LIBCPP_ALWAYS_INLINE
942 int encoding() const _NOEXCEPT
944 return do_encoding();
947 _LIBCPP_ALWAYS_INLINE
948 bool always_noconv() const _NOEXCEPT
950 return do_always_noconv();
953 _LIBCPP_ALWAYS_INLINE
954 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
956 return do_length(__st, __frm, __end, __mx);
959 _LIBCPP_ALWAYS_INLINE
960 int max_length() const _NOEXCEPT
962 return do_max_length();
965 static locale::id id;
968 explicit codecvt(const char*, size_t __refs = 0);
972 virtual result do_out(state_type& __st,
973 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
974 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
975 virtual result do_in(state_type& __st,
976 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
977 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
978 virtual result do_unshift(state_type& __st,
979 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
980 virtual int do_encoding() const _NOEXCEPT;
981 virtual bool do_always_noconv() const _NOEXCEPT;
982 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
983 virtual int do_max_length() const _NOEXCEPT;
986 // template <> class codecvt<char16_t, char, mbstate_t>
989 class _LIBCPP_TYPE_VIS codecvt<char16_t, char, mbstate_t>
990 : public locale::facet,
994 typedef char16_t intern_type;
995 typedef char extern_type;
996 typedef mbstate_t state_type;
998 _LIBCPP_ALWAYS_INLINE
999 explicit codecvt(size_t __refs = 0)
1000 : locale::facet(__refs) {}
1002 _LIBCPP_ALWAYS_INLINE
1003 result out(state_type& __st,
1004 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1005 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1007 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1010 _LIBCPP_ALWAYS_INLINE
1011 result unshift(state_type& __st,
1012 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1014 return do_unshift(__st, __to, __to_end, __to_nxt);
1017 _LIBCPP_ALWAYS_INLINE
1018 result in(state_type& __st,
1019 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1020 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1022 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1025 _LIBCPP_ALWAYS_INLINE
1026 int encoding() const _NOEXCEPT
1028 return do_encoding();
1031 _LIBCPP_ALWAYS_INLINE
1032 bool always_noconv() const _NOEXCEPT
1034 return do_always_noconv();
1037 _LIBCPP_ALWAYS_INLINE
1038 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1040 return do_length(__st, __frm, __end, __mx);
1043 _LIBCPP_ALWAYS_INLINE
1044 int max_length() const _NOEXCEPT
1046 return do_max_length();
1049 static locale::id id;
1052 _LIBCPP_ALWAYS_INLINE
1053 explicit codecvt(const char*, size_t __refs = 0)
1054 : locale::facet(__refs) {}
1058 virtual result do_out(state_type& __st,
1059 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1060 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1061 virtual result do_in(state_type& __st,
1062 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1063 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1064 virtual result do_unshift(state_type& __st,
1065 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1066 virtual int do_encoding() const _NOEXCEPT;
1067 virtual bool do_always_noconv() const _NOEXCEPT;
1068 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
1069 virtual int do_max_length() const _NOEXCEPT;
1072 // template <> class codecvt<char32_t, char, mbstate_t>
1075 class _LIBCPP_TYPE_VIS codecvt<char32_t, char, mbstate_t>
1076 : public locale::facet,
1080 typedef char32_t intern_type;
1081 typedef char extern_type;
1082 typedef mbstate_t state_type;
1084 _LIBCPP_ALWAYS_INLINE
1085 explicit codecvt(size_t __refs = 0)
1086 : locale::facet(__refs) {}
1088 _LIBCPP_ALWAYS_INLINE
1089 result out(state_type& __st,
1090 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1091 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1093 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1096 _LIBCPP_ALWAYS_INLINE
1097 result unshift(state_type& __st,
1098 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1100 return do_unshift(__st, __to, __to_end, __to_nxt);
1103 _LIBCPP_ALWAYS_INLINE
1104 result in(state_type& __st,
1105 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1106 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1108 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1111 _LIBCPP_ALWAYS_INLINE
1112 int encoding() const _NOEXCEPT
1114 return do_encoding();
1117 _LIBCPP_ALWAYS_INLINE
1118 bool always_noconv() const _NOEXCEPT
1120 return do_always_noconv();
1123 _LIBCPP_ALWAYS_INLINE
1124 int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1126 return do_length(__st, __frm, __end, __mx);
1129 _LIBCPP_ALWAYS_INLINE
1130 int max_length() const _NOEXCEPT
1132 return do_max_length();
1135 static locale::id id;
1138 _LIBCPP_ALWAYS_INLINE
1139 explicit codecvt(const char*, size_t __refs = 0)
1140 : locale::facet(__refs) {}
1144 virtual result do_out(state_type& __st,
1145 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1146 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1147 virtual result do_in(state_type& __st,
1148 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1149 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1150 virtual result do_unshift(state_type& __st,
1151 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1152 virtual int do_encoding() const _NOEXCEPT;
1153 virtual bool do_always_noconv() const _NOEXCEPT;
1154 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
1155 virtual int do_max_length() const _NOEXCEPT;
1158 // template <class _InternT, class _ExternT, class _StateT> class codecvt_byname
1160 template <class _InternT, class _ExternT, class _StateT>
1161 class _LIBCPP_TYPE_VIS_ONLY codecvt_byname
1162 : public codecvt<_InternT, _ExternT, _StateT>
1165 _LIBCPP_ALWAYS_INLINE
1166 explicit codecvt_byname(const char* __nm, size_t __refs = 0)
1167 : codecvt<_InternT, _ExternT, _StateT>(__nm, __refs) {}
1168 _LIBCPP_ALWAYS_INLINE
1169 explicit codecvt_byname(const string& __nm, size_t __refs = 0)
1170 : codecvt<_InternT, _ExternT, _StateT>(__nm.c_str(), __refs) {}
1175 template <class _InternT, class _ExternT, class _StateT>
1176 codecvt_byname<_InternT, _ExternT, _StateT>::~codecvt_byname()
1180 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char, char, mbstate_t>)
1181 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<wchar_t, char, mbstate_t>)
1182 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char16_t, char, mbstate_t>)
1183 _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char32_t, char, mbstate_t>)
1185 _LIBCPP_FUNC_VIS void __throw_runtime_error(const char*);
1187 template <size_t _Np>
1188 struct __narrow_to_utf8
1190 template <class _OutputIterator, class _CharT>
1192 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const;
1196 struct __narrow_to_utf8<8>
1198 template <class _OutputIterator, class _CharT>
1199 _LIBCPP_ALWAYS_INLINE
1201 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1203 for (; __wb < __we; ++__wb, ++__s)
1210 struct __narrow_to_utf8<16>
1211 : public codecvt<char16_t, char, mbstate_t>
1213 _LIBCPP_ALWAYS_INLINE
1214 __narrow_to_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1216 ~__narrow_to_utf8();
1218 template <class _OutputIterator, class _CharT>
1219 _LIBCPP_ALWAYS_INLINE
1221 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1225 while (__wb < __we && __r != error)
1227 const int __sz = 32;
1230 const char16_t* __wn = (const char16_t*)__wb;
1231 __r = do_out(__mb, (const char16_t*)__wb, (const char16_t*)__we, __wn,
1232 __buf, __buf+__sz, __bn);
1233 if (__r == codecvt_base::error || __wn == (const char16_t*)__wb)
1234 __throw_runtime_error("locale not supported");
1235 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1237 __wb = (const _CharT*)__wn;
1244 struct __narrow_to_utf8<32>
1245 : public codecvt<char32_t, char, mbstate_t>
1247 _LIBCPP_ALWAYS_INLINE
1248 __narrow_to_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1250 ~__narrow_to_utf8();
1252 template <class _OutputIterator, class _CharT>
1253 _LIBCPP_ALWAYS_INLINE
1255 operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1259 while (__wb < __we && __r != error)
1261 const int __sz = 32;
1264 const char32_t* __wn = (const char32_t*)__wb;
1265 __r = do_out(__mb, (const char32_t*)__wb, (const char32_t*)__we, __wn,
1266 __buf, __buf+__sz, __bn);
1267 if (__r == codecvt_base::error || __wn == (const char32_t*)__wb)
1268 __throw_runtime_error("locale not supported");
1269 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1271 __wb = (const _CharT*)__wn;
1277 template <size_t _Np>
1278 struct __widen_from_utf8
1280 template <class _OutputIterator>
1282 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const;
1286 struct __widen_from_utf8<8>
1288 template <class _OutputIterator>
1289 _LIBCPP_ALWAYS_INLINE
1291 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1293 for (; __nb < __ne; ++__nb, ++__s)
1300 struct __widen_from_utf8<16>
1301 : public codecvt<char16_t, char, mbstate_t>
1303 _LIBCPP_ALWAYS_INLINE
1304 __widen_from_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1306 ~__widen_from_utf8();
1308 template <class _OutputIterator>
1309 _LIBCPP_ALWAYS_INLINE
1311 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1315 while (__nb < __ne && __r != error)
1317 const int __sz = 32;
1318 char16_t __buf[__sz];
1320 const char* __nn = __nb;
1321 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1322 __buf, __buf+__sz, __bn);
1323 if (__r == codecvt_base::error || __nn == __nb)
1324 __throw_runtime_error("locale not supported");
1325 for (const char16_t* __p = __buf; __p < __bn; ++__p, ++__s)
1326 *__s = (wchar_t)*__p;
1334 struct __widen_from_utf8<32>
1335 : public codecvt<char32_t, char, mbstate_t>
1337 _LIBCPP_ALWAYS_INLINE
1338 __widen_from_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1340 ~__widen_from_utf8();
1342 template <class _OutputIterator>
1343 _LIBCPP_ALWAYS_INLINE
1345 operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1349 while (__nb < __ne && __r != error)
1351 const int __sz = 32;
1352 char32_t __buf[__sz];
1354 const char* __nn = __nb;
1355 __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1356 __buf, __buf+__sz, __bn);
1357 if (__r == codecvt_base::error || __nn == __nb)
1358 __throw_runtime_error("locale not supported");
1359 for (const char32_t* __p = __buf; __p < __bn; ++__p, ++__s)
1360 *__s = (wchar_t)*__p;
1367 // template <class charT> class numpunct
1369 template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY numpunct;
1372 class _LIBCPP_TYPE_VIS numpunct<char>
1373 : public locale::facet
1376 typedef char char_type;
1377 typedef basic_string<char_type> string_type;
1379 explicit numpunct(size_t __refs = 0);
1381 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
1382 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
1383 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
1384 _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();}
1385 _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();}
1387 static locale::id id;
1391 virtual char_type do_decimal_point() const;
1392 virtual char_type do_thousands_sep() const;
1393 virtual string do_grouping() const;
1394 virtual string_type do_truename() const;
1395 virtual string_type do_falsename() const;
1397 char_type __decimal_point_;
1398 char_type __thousands_sep_;
1403 class _LIBCPP_TYPE_VIS numpunct<wchar_t>
1404 : public locale::facet
1407 typedef wchar_t char_type;
1408 typedef basic_string<char_type> string_type;
1410 explicit numpunct(size_t __refs = 0);
1412 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
1413 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
1414 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
1415 _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();}
1416 _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();}
1418 static locale::id id;
1422 virtual char_type do_decimal_point() const;
1423 virtual char_type do_thousands_sep() const;
1424 virtual string do_grouping() const;
1425 virtual string_type do_truename() const;
1426 virtual string_type do_falsename() const;
1428 char_type __decimal_point_;
1429 char_type __thousands_sep_;
1433 // template <class charT> class numpunct_byname
1435 template <class _CharT> class _LIBCPP_TYPE_VIS_ONLY numpunct_byname;
1438 class _LIBCPP_TYPE_VIS numpunct_byname<char>
1439 : public numpunct<char>
1442 typedef char char_type;
1443 typedef basic_string<char_type> string_type;
1445 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1446 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1452 void __init(const char*);
1456 class _LIBCPP_TYPE_VIS numpunct_byname<wchar_t>
1457 : public numpunct<wchar_t>
1460 typedef wchar_t char_type;
1461 typedef basic_string<char_type> string_type;
1463 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1464 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1470 void __init(const char*);
1473 _LIBCPP_END_NAMESPACE_STD
1475 #endif // _LIBCPP___LOCALE