1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___LOCALE
11#define _LIBCPP___LOCALE
12
13#include <__config>
14#include <__configuration/availability.h>
15
16#if _LIBCPP_HAS_LOCALIZATION
17
18# include <__locale_dir/locale_base_api.h>
19# include <__memory/addressof.h>
20# include <__memory/shared_count.h>
21# include <__mutex/once_flag.h>
22# include <__type_traits/make_unsigned.h>
23# include <__utility/no_destroy.h>
24# include <__utility/private_constructor_tag.h>
25# include <cctype>
26# include <clocale>
27# include <cstdint>
28# include <cstdlib>
29# include <string>
30# include <text_encoding>
31
32// Some platforms require more includes than others. Keep the includes on all plaforms for now.
33# include <cstddef>
34# include <cstring>
35
36# if _LIBCPP_HAS_WIDE_CHARACTERS
37# include <cwchar>
38# else
39# include <__std_mbstate_t.h>
40# endif
41
42# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
43# pragma GCC system_header
44# endif
45
46_LIBCPP_BEGIN_NAMESPACE_STD
47_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
48
49class _LIBCPP_EXPORTED_FROM_ABI locale;
50
51template <class _CharT>
52class collate;
53
54template <class _Facet>
55_LIBCPP_HIDE_FROM_ABI bool has_facet(const locale&) _NOEXCEPT;
56
57template <class _Facet>
58_LIBCPP_HIDE_FROM_ABI const _Facet& use_facet(const locale&);
59
60class _LIBCPP_EXPORTED_FROM_ABI locale {
61public:
62 // locale is essentially a shared_ptr that doesn't support weak_ptrs and never got a move constructor,
63 // so it is trivially relocatable.
64 using __trivially_relocatable _LIBCPP_NODEBUG = locale;
65
66 // types:
67 class _LIBCPP_EXPORTED_FROM_ABI facet;
68 class _LIBCPP_EXPORTED_FROM_ABI id;
69
70 typedef int category;
71
72 static const category // values assigned here are for exposition only
73 none = 0,
74 collate = _LIBCPP_COLLATE_MASK, ctype = _LIBCPP_CTYPE_MASK, monetary = _LIBCPP_MONETARY_MASK,
75 numeric = _LIBCPP_NUMERIC_MASK, time = _LIBCPP_TIME_MASK, messages = _LIBCPP_MESSAGES_MASK,
76 all = collate | ctype | monetary | numeric | time | messages;
77
78 // construct/copy/destroy:
79 locale() _NOEXCEPT;
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_HIDE_FROM_ABI locale(const locale&, _Facet*);
87 locale(const locale&, const locale&, category);
88
89 ~locale();
90
91 const locale& operator=(const locale&) _NOEXCEPT;
92
93 template <class _Facet>
94 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI locale combine(const locale& __other) const {
95 if (!std::has_facet<_Facet>(__other))
96 __throw_runtime_error("locale::combine: locale missing facet");
97
98 return locale(*this, std::addressof(const_cast<_Facet&>(std::use_facet<_Facet>(__other))));
99 }
100
101 // locale operations:
102 [[__nodiscard__]] string name() const;
103 bool operator==(const locale&) const;
104# if _LIBCPP_STD_VER <= 17
105 _LIBCPP_HIDE_FROM_ABI bool operator!=(const locale& __y) const { return !(*this == __y); }
106# endif
107 template <class _CharT, class _Traits, class _Allocator>
108 [[__nodiscard__]]
109 _LIBCPP_HIDE_FROM_ABI bool operator()(const basic_string<_CharT, _Traits, _Allocator>& __x,
110 const basic_string<_CharT, _Traits, _Allocator>& __y) const {
111 return std::use_facet<std::collate<_CharT> >(*this).compare(
112 __x.data(), __x.data() + __x.size(), __y.data(), __y.data() + __y.size()) < 0;
113 }
114# if _LIBCPP_STD_VER >= 26
115 [[nodiscard]] _LIBCPP_AVAILABILITY_TEXT_ENCODING_ENVIRONMENT
116 _LIBCPP_HIDE_FROM_ABI std::text_encoding encoding() const {
117 std::string __name = name();
118 return std::__get_locale_encoding(name: __name.c_str());
119 }
120# endif
121
122 // global locale objects:
123 static locale global(const locale&);
124 [[__nodiscard__]] static const locale& classic();
125
126private:
127 class __imp;
128 __imp* __locale_;
129
130 template <class>
131 friend struct __no_destroy;
132
133 friend ios_base;
134
135 _LIBCPP_HIDE_FROM_ABI explicit locale(__private_constructor_tag, __imp* __loc) : __locale_(__loc) {}
136
137 void __install_ctor(const locale&, facet*, long);
138 static locale& __global();
139 bool has_facet(id&) const;
140 const facet* use_facet(id&) const;
141
142 template <class _Facet>
143 friend bool has_facet(const locale&) _NOEXCEPT;
144 template <class _Facet>
145 friend const _Facet& use_facet(const locale&);
146};
147
148class _LIBCPP_EXPORTED_FROM_ABI locale::facet : public __shared_count {
149protected:
150 _LIBCPP_HIDE_FROM_ABI explicit facet(size_t __refs = 0) : __shared_count(static_cast<long>(__refs) - 1) {}
151
152 ~facet() override;
153
154 // facet(const facet&) = delete; // effectively done in __shared_count
155 // void operator=(const facet&) = delete;
156
157private:
158 void __on_zero_shared() _NOEXCEPT override;
159};
160
161class _LIBCPP_EXPORTED_FROM_ABI locale::id {
162 once_flag __flag_;
163 int32_t __id_;
164
165 static int32_t __next_id;
166
167public:
168 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR id() : __id_(0) {}
169 void operator=(const id&) = delete;
170 id(const id&) = delete;
171
172public: // only needed for tests
173 long __get();
174
175 friend class locale;
176 friend class locale::__imp;
177};
178
179template <class _Facet>
180inline _LIBCPP_HIDE_FROM_ABI locale::locale(const locale& __other, _Facet* __f) {
181 __install_ctor(__other, __f, __f ? __f->id.__get() : 0);
182}
183
184template <class _Facet>
185[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool has_facet(const locale& __l) _NOEXCEPT {
186 return __l.has_facet(_Facet::id);
187}
188
189template <class _Facet>
190[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI const _Facet& use_facet(const locale& __l) {
191 return static_cast<const _Facet&>(*__l.use_facet(_Facet::id));
192}
193
194// template <class _CharT> class collate;
195
196template <class _CharT>
197class collate : public locale::facet {
198public:
199 typedef _CharT char_type;
200 typedef basic_string<char_type> string_type;
201
202 _LIBCPP_HIDE_FROM_ABI explicit collate(size_t __refs = 0) : locale::facet(__refs) {}
203
204 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI int
205 compare(const char_type* __lo1, const char_type* __hi1, const char_type* __lo2, const char_type* __hi2) const {
206 return do_compare(__lo1, __hi1, __lo2, __hi2);
207 }
208
209 // FIXME(EricWF): The _LIBCPP_ALWAYS_INLINE is needed on Windows to work
210 // around a dllimport bug that expects an external instantiation.
211 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE string_type
212 transform(const char_type* __lo, const char_type* __hi) const {
213 return do_transform(__lo, __hi);
214 }
215
216 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI long hash(const char_type* __lo, const char_type* __hi) const {
217 return do_hash(__lo, __hi);
218 }
219
220 static locale::id id;
221
222protected:
223 ~collate() override;
224 virtual int
225 do_compare(const char_type* __lo1, const char_type* __hi1, const char_type* __lo2, const char_type* __hi2) const;
226 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const {
227 return string_type(__lo, __hi);
228 }
229 virtual long do_hash(const char_type* __lo, const char_type* __hi) const;
230};
231
232template <class _CharT>
233locale::id collate<_CharT>::id;
234
235template <class _CharT>
236collate<_CharT>::~collate() {}
237
238template <class _CharT>
239int collate<_CharT>::do_compare(
240 const char_type* __lo1, const char_type* __hi1, const char_type* __lo2, const char_type* __hi2) const {
241 for (; __lo2 != __hi2; ++__lo1, ++__lo2) {
242 if (__lo1 == __hi1 || *__lo1 < *__lo2)
243 return -1;
244 if (*__lo2 < *__lo1)
245 return 1;
246 }
247 return __lo1 != __hi1;
248}
249
250template <class _CharT>
251long collate<_CharT>::do_hash(const char_type* __lo, const char_type* __hi) const {
252 size_t __h = 0;
253 const size_t __sr = __CHAR_BIT__ * sizeof(size_t) - 8;
254 const size_t __mask = size_t(0xF) << (__sr + 4);
255 for (const char_type* __p = __lo; __p != __hi; ++__p) {
256 __h = (__h << 4) + static_cast<size_t>(*__p);
257 size_t __g = __h & __mask;
258 __h ^= __g | (__g >> __sr);
259 }
260 return static_cast<long>(__h);
261}
262
263extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<char>;
264# if _LIBCPP_HAS_WIDE_CHARACTERS
265extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<wchar_t>;
266# endif
267
268// template <class CharT> class collate_byname;
269
270template <class _CharT>
271class collate_byname;
272
273template <>
274class _LIBCPP_EXPORTED_FROM_ABI collate_byname<char> : public collate<char> {
275 __locale::__locale_t __l_;
276
277public:
278 typedef char char_type;
279 typedef basic_string<char_type> string_type;
280
281 explicit collate_byname(const char* __n, size_t __refs = 0);
282 explicit collate_byname(const string& __n, size_t __refs = 0);
283
284protected:
285 ~collate_byname() override;
286 int do_compare(
287 const char_type* __lo1, const char_type* __hi1, const char_type* __lo2, const char_type* __hi2) const override;
288 string_type do_transform(const char_type* __lo, const char_type* __hi) const override;
289};
290
291# if _LIBCPP_HAS_WIDE_CHARACTERS
292template <>
293class _LIBCPP_EXPORTED_FROM_ABI collate_byname<wchar_t> : public collate<wchar_t> {
294 __locale::__locale_t __l_;
295
296public:
297 typedef wchar_t char_type;
298 typedef basic_string<char_type> string_type;
299
300 explicit collate_byname(const char* __n, size_t __refs = 0);
301 explicit collate_byname(const string& __n, size_t __refs = 0);
302
303protected:
304 ~collate_byname() override;
305
306 int do_compare(
307 const char_type* __lo1, const char_type* __hi1, const char_type* __lo2, const char_type* __hi2) const override;
308 string_type do_transform(const char_type* __lo, const char_type* __hi) const override;
309};
310# endif
311
312// template <class charT> class ctype
313
314class _LIBCPP_EXPORTED_FROM_ABI ctype_base {
315public:
316# if defined(_LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE)
317 typedef unsigned long mask;
318 static const mask space = 1 << 0;
319 static const mask print = 1 << 1;
320 static const mask cntrl = 1 << 2;
321 static const mask upper = 1 << 3;
322 static const mask lower = 1 << 4;
323 static const mask alpha = 1 << 5;
324 static const mask digit = 1 << 6;
325 static const mask punct = 1 << 7;
326 static const mask xdigit = 1 << 8;
327 static const mask blank = 1 << 9;
328# if defined(__BIONIC__)
329 // Historically this was a part of regex_traits rather than ctype_base. The
330 // historical value of the constant is preserved for ABI compatibility.
331 static const mask __regex_word = 0x8000;
332# else
333 static const mask __regex_word = 1 << 10;
334# endif // defined(__BIONIC__)
335# elif defined(__GLIBC__)
336 typedef unsigned short mask;
337 static const mask space = _ISspace;
338 static const mask print = _ISprint;
339 static const mask cntrl = _IScntrl;
340 static const mask upper = _ISupper;
341 static const mask lower = _ISlower;
342 static const mask alpha = _ISalpha;
343 static const mask digit = _ISdigit;
344 static const mask punct = _ISpunct;
345 static const mask xdigit = _ISxdigit;
346 static const mask blank = _ISblank;
347# if defined(__mips__) || (BYTE_ORDER == BIG_ENDIAN)
348 static const mask __regex_word = static_cast<mask>(_ISbit(15));
349# else
350 static const mask __regex_word = 0x80;
351# endif
352# elif defined(_LIBCPP_MSVCRT_LIKE)
353 typedef unsigned short mask;
354 static const mask space = _SPACE;
355 static const mask print = _BLANK | _PUNCT | _ALPHA | _DIGIT;
356 static const mask cntrl = _CONTROL;
357 static const mask upper = _UPPER;
358 static const mask lower = _LOWER;
359 static const mask alpha = _ALPHA;
360 static const mask digit = _DIGIT;
361 static const mask punct = _PUNCT;
362 static const mask xdigit = _HEX;
363 static const mask blank = _BLANK;
364 static const mask __regex_word = 0x4000; // 0x8000 and 0x0100 and 0x00ff are used
365# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
366# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
367# elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
368# ifdef __APPLE__
369 typedef uint32_t mask;
370# elif defined(__FreeBSD__)
371 typedef unsigned long mask;
372# elif defined(__NetBSD__)
373 typedef unsigned short mask;
374# endif
375 static const mask space = _CTYPE_S;
376 static const mask print = _CTYPE_R;
377 static const mask cntrl = _CTYPE_C;
378 static const mask upper = _CTYPE_U;
379 static const mask lower = _CTYPE_L;
380 static const mask alpha = _CTYPE_A;
381 static const mask digit = _CTYPE_D;
382 static const mask punct = _CTYPE_P;
383 static const mask xdigit = _CTYPE_X;
384
385# if defined(__NetBSD__)
386 static const mask blank = _CTYPE_BL;
387 // NetBSD defines classes up to 0x2000
388 // see sys/ctype_bits.h, _CTYPE_Q
389 static const mask __regex_word = 0x8000;
390# else
391 static const mask blank = _CTYPE_B;
392 static const mask __regex_word = 0x80;
393# endif
394# elif defined(_AIX)
395 typedef unsigned int mask;
396 static const mask space = _ISSPACE;
397 static const mask print = _ISPRINT;
398 static const mask cntrl = _ISCNTRL;
399 static const mask upper = _ISUPPER;
400 static const mask lower = _ISLOWER;
401 static const mask alpha = _ISALPHA;
402 static const mask digit = _ISDIGIT;
403 static const mask punct = _ISPUNCT;
404 static const mask xdigit = _ISXDIGIT;
405 static const mask blank = _ISBLANK;
406 static const mask __regex_word = 0x8000;
407# elif _LIBCPP_LIBC_NEWLIB
408 // Same type as Newlib's _ctype_ array in newlib/libc/include/ctype.h.
409 typedef char mask;
410 // In case char is signed, static_cast is needed to avoid warning on
411 // positive value becomming negative.
412 static const mask space = static_cast<mask>(_S);
413 static const mask print = static_cast<mask>(_P | _U | _L | _N | _B);
414 static const mask cntrl = static_cast<mask>(_C);
415 static const mask upper = static_cast<mask>(_U);
416 static const mask lower = static_cast<mask>(_L);
417 static const mask alpha = static_cast<mask>(_U | _L);
418 static const mask digit = static_cast<mask>(_N);
419 static const mask punct = static_cast<mask>(_P);
420 static const mask xdigit = static_cast<mask>(_X | _N);
421 static const mask blank = static_cast<mask>(_B);
422 // mask is already fully saturated, use a different type in regex_type_traits.
423 static const unsigned short __regex_word = 0x100;
424# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
425# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
426# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_XDIGIT
427# elif defined(__MVS__)
428# if defined(__NATIVE_ASCII_F)
429 typedef unsigned int mask;
430 static const mask space = _ISSPACE_A;
431 static const mask print = _ISPRINT_A;
432 static const mask cntrl = _ISCNTRL_A;
433 static const mask upper = _ISUPPER_A;
434 static const mask lower = _ISLOWER_A;
435 static const mask alpha = _ISALPHA_A;
436 static const mask digit = _ISDIGIT_A;
437 static const mask punct = _ISPUNCT_A;
438 static const mask xdigit = _ISXDIGIT_A;
439 static const mask blank = _ISBLANK_A;
440# else
441 typedef unsigned short mask;
442 static const mask space = __ISSPACE;
443 static const mask print = __ISPRINT;
444 static const mask cntrl = __ISCNTRL;
445 static const mask upper = __ISUPPER;
446 static const mask lower = __ISLOWER;
447 static const mask alpha = __ISALPHA;
448 static const mask digit = __ISDIGIT;
449 static const mask punct = __ISPUNCT;
450 static const mask xdigit = __ISXDIGIT;
451 static const mask blank = __ISBLANK;
452# endif
453 static const mask __regex_word = 0x8000;
454# else
455# error unknown rune table for this platform -- do you mean to define _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE?
456# endif
457 static const mask alnum = alpha | digit;
458 static const mask graph = alnum | punct;
459
460 _LIBCPP_HIDE_FROM_ABI ctype_base() {}
461
462 static_assert((__regex_word & ~(std::make_unsigned<mask>::type)(space | print | cntrl | upper | lower | alpha |
463 digit | punct | xdigit | blank)) == __regex_word,
464 "__regex_word can't overlap other bits");
465};
466
467template <class _CharT>
468class ctype;
469
470# if _LIBCPP_HAS_WIDE_CHARACTERS
471template <>
472class _LIBCPP_EXPORTED_FROM_ABI ctype<wchar_t> : public locale::facet, public ctype_base {
473public:
474 typedef wchar_t char_type;
475
476 _LIBCPP_HIDE_FROM_ABI explicit ctype(size_t __refs = 0) : locale::facet(__refs) {}
477
478 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool is(mask __m, char_type __c) const { return do_is(__m, __c); }
479
480 _LIBCPP_HIDE_FROM_ABI const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const {
481 return do_is(__low, __high, __vec);
482 }
483
484 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const char_type*
485 scan_is(mask __m, const char_type* __low, const char_type* __high) const {
486 return do_scan_is(__m, __low, __high);
487 }
488
489 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const char_type*
490 scan_not(mask __m, const char_type* __low, const char_type* __high) const {
491 return do_scan_not(__m, __low, __high);
492 }
493
494 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type toupper(char_type __c) const { return do_toupper(__c); }
495
496 _LIBCPP_HIDE_FROM_ABI const char_type* toupper(char_type* __low, const char_type* __high) const {
497 return do_toupper(__low, __high);
498 }
499
500 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type tolower(char_type __c) const { return do_tolower(__c); }
501
502 _LIBCPP_HIDE_FROM_ABI const char_type* tolower(char_type* __low, const char_type* __high) const {
503 return do_tolower(__low, __high);
504 }
505
506 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type widen(char __c) const { return do_widen(__c); }
507
508 _LIBCPP_HIDE_FROM_ABI const char* widen(const char* __low, const char* __high, char_type* __to) const {
509 return do_widen(__low, __high, dest: __to);
510 }
511
512 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char narrow(char_type __c, char __dfault) const {
513 return do_narrow(__c, __dfault);
514 }
515
516 _LIBCPP_HIDE_FROM_ABI const char_type*
517 narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const {
518 return do_narrow(__low, __high, __dfault, dest: __to);
519 }
520
521 static locale::id id;
522
523protected:
524 ~ctype() override;
525 virtual bool do_is(mask __m, char_type __c) const;
526 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
527 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
528 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
529 virtual char_type do_toupper(char_type) const;
530 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
531 virtual char_type do_tolower(char_type) const;
532 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
533 virtual char_type do_widen(char) const;
534 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
535 virtual char do_narrow(char_type, char __dfault) const;
536 virtual const char_type*
537 do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
538};
539# endif // _LIBCPP_HAS_WIDE_CHARACTERS
540
541inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_isascii(int __c) { return (__c & ~0x7F) == 0; }
542
543template <>
544class _LIBCPP_EXPORTED_FROM_ABI ctype<char> : public locale::facet, public ctype_base {
545 const mask* __tab_;
546 bool __del_;
547
548public:
549 typedef char char_type;
550
551 explicit ctype(const mask* __tab = nullptr, bool __del = false, size_t __refs = 0);
552
553 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool is(mask __m, char_type __c) const {
554 return std::__libcpp_isascii(__c) ? (__tab_[static_cast<int>(__c)] & __m) != 0 : false;
555 }
556
557 _LIBCPP_HIDE_FROM_ABI const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const {
558 for (; __low != __high; ++__low, ++__vec)
559 *__vec = std::__libcpp_isascii(c: *__low) ? __tab_[static_cast<int>(*__low)] : 0;
560 return __low;
561 }
562
563 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const char_type*
564 scan_is(mask __m, const char_type* __low, const char_type* __high) const {
565 for (; __low != __high; ++__low)
566 if (std::__libcpp_isascii(c: *__low) && (__tab_[static_cast<int>(*__low)] & __m))
567 break;
568 return __low;
569 }
570
571 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const char_type*
572 scan_not(mask __m, const char_type* __low, const char_type* __high) const {
573 for (; __low != __high; ++__low)
574 if (!std::__libcpp_isascii(c: *__low) || !(__tab_[static_cast<int>(*__low)] & __m))
575 break;
576 return __low;
577 }
578
579 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type toupper(char_type __c) const { return do_toupper(__c); }
580
581 _LIBCPP_HIDE_FROM_ABI const char_type* toupper(char_type* __low, const char_type* __high) const {
582 return do_toupper(__low, __high);
583 }
584
585 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type tolower(char_type __c) const { return do_tolower(__c); }
586
587 _LIBCPP_HIDE_FROM_ABI const char_type* tolower(char_type* __low, const char_type* __high) const {
588 return do_tolower(__low, __high);
589 }
590
591 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type widen(char __c) const { return do_widen(__c); }
592
593 _LIBCPP_HIDE_FROM_ABI const char* widen(const char* __low, const char* __high, char_type* __to) const {
594 return do_widen(__low, __high, __to);
595 }
596
597 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char narrow(char_type __c, char __dfault) const {
598 return do_narrow(__c, __dfault);
599 }
600
601 _LIBCPP_HIDE_FROM_ABI const char*
602 narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const {
603 return do_narrow(__low, __high, __dfault, __to);
604 }
605
606 static locale::id id;
607
608# ifdef _CACHED_RUNES
609 static const size_t table_size = _CACHED_RUNES;
610# else
611 static const size_t table_size = 256;
612# endif
613 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const mask* table() const _NOEXCEPT { return __tab_; }
614 [[__nodiscard__]] static const mask* classic_table() _NOEXCEPT;
615
616protected:
617 ~ctype() override;
618 virtual char_type do_toupper(char_type __c) const;
619 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
620 virtual char_type do_tolower(char_type __c) const;
621 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
622 virtual char_type do_widen(char __c) const;
623 virtual const char* do_widen(const char* __low, const char* __high, char_type* __to) const;
624 virtual char do_narrow(char_type __c, char __dfault) const;
625 virtual const char* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const;
626};
627
628// template <class CharT> class ctype_byname;
629
630template <class _CharT>
631class ctype_byname;
632
633template <>
634class _LIBCPP_EXPORTED_FROM_ABI ctype_byname<char> : public ctype<char> {
635 __locale::__locale_t __l_;
636
637public:
638 explicit ctype_byname(const char*, size_t = 0);
639 explicit ctype_byname(const string&, size_t = 0);
640
641protected:
642 ~ctype_byname() override;
643 char_type do_toupper(char_type) const override;
644 const char_type* do_toupper(char_type* __low, const char_type* __high) const override;
645 char_type do_tolower(char_type) const override;
646 const char_type* do_tolower(char_type* __low, const char_type* __high) const override;
647};
648
649# if _LIBCPP_HAS_WIDE_CHARACTERS
650template <>
651class _LIBCPP_EXPORTED_FROM_ABI ctype_byname<wchar_t> : public ctype<wchar_t> {
652 __locale::__locale_t __l_;
653
654public:
655 explicit ctype_byname(const char*, size_t = 0);
656 explicit ctype_byname(const string&, size_t = 0);
657
658protected:
659 ~ctype_byname() override;
660 bool do_is(mask __m, char_type __c) const override;
661 const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const override;
662 const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const override;
663 const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const override;
664 char_type do_toupper(char_type) const override;
665 const char_type* do_toupper(char_type* __low, const char_type* __high) const override;
666 char_type do_tolower(char_type) const override;
667 const char_type* do_tolower(char_type* __low, const char_type* __high) const override;
668 char_type do_widen(char) const override;
669 const char* do_widen(const char* __low, const char* __high, char_type* __dest) const override;
670 char do_narrow(char_type, char __dfault) const override;
671 const char_type*
672 do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const override;
673};
674# endif // _LIBCPP_HAS_WIDE_CHARACTERS
675
676template <class _CharT>
677[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool isspace(_CharT __c, const locale& __loc) {
678 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
679}
680
681template <class _CharT>
682[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool isprint(_CharT __c, const locale& __loc) {
683 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c);
684}
685
686template <class _CharT>
687[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool iscntrl(_CharT __c, const locale& __loc) {
688 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c);
689}
690
691template <class _CharT>
692[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool isupper(_CharT __c, const locale& __loc) {
693 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c);
694}
695
696template <class _CharT>
697[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool islower(_CharT __c, const locale& __loc) {
698 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c);
699}
700
701template <class _CharT>
702[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool isalpha(_CharT __c, const locale& __loc) {
703 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c);
704}
705
706template <class _CharT>
707[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool isdigit(_CharT __c, const locale& __loc) {
708 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c);
709}
710
711template <class _CharT>
712[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool ispunct(_CharT __c, const locale& __loc) {
713 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c);
714}
715
716template <class _CharT>
717[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool isxdigit(_CharT __c, const locale& __loc) {
718 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c);
719}
720
721template <class _CharT>
722[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool isalnum(_CharT __c, const locale& __loc) {
723 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c);
724}
725
726template <class _CharT>
727[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool isgraph(_CharT __c, const locale& __loc) {
728 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c);
729}
730
731template <class _CharT>
732[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool isblank(_CharT __c, const locale& __loc) {
733 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::blank, __c);
734}
735
736template <class _CharT>
737[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _CharT toupper(_CharT __c, const locale& __loc) {
738 return std::use_facet<ctype<_CharT> >(__loc).toupper(__c);
739}
740
741template <class _CharT>
742[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _CharT tolower(_CharT __c, const locale& __loc) {
743 return std::use_facet<ctype<_CharT> >(__loc).tolower(__c);
744}
745
746// codecvt_base
747
748class _LIBCPP_EXPORTED_FROM_ABI codecvt_base {
749public:
750 _LIBCPP_HIDE_FROM_ABI codecvt_base() {}
751 enum result { ok, partial, error, noconv };
752};
753
754// template <class internT, class externT, class stateT> class codecvt;
755
756template <class _InternT, class _ExternT, class _StateT>
757class codecvt;
758
759// template <> class codecvt<char, char, mbstate_t>
760
761template <>
762class _LIBCPP_EXPORTED_FROM_ABI codecvt<char, char, mbstate_t> : public locale::facet, public codecvt_base {
763public:
764 typedef char intern_type;
765 typedef char extern_type;
766 typedef mbstate_t state_type;
767
768 _LIBCPP_HIDE_FROM_ABI explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
769
770 _LIBCPP_HIDE_FROM_ABI result
771 out(state_type& __st,
772 const intern_type* __frm,
773 const intern_type* __frm_end,
774 const intern_type*& __frm_nxt,
775 extern_type* __to,
776 extern_type* __to_end,
777 extern_type*& __to_nxt) const {
778 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
779 }
780
781 _LIBCPP_HIDE_FROM_ABI result
782 unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
783 return do_unshift(__st, __to, __to_end, __to_nxt);
784 }
785
786 _LIBCPP_HIDE_FROM_ABI result
787 in(state_type& __st,
788 const extern_type* __frm,
789 const extern_type* __frm_end,
790 const extern_type*& __frm_nxt,
791 intern_type* __to,
792 intern_type* __to_end,
793 intern_type*& __to_nxt) const {
794 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
795 }
796
797 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
798
799 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
800
801 _LIBCPP_HIDE_FROM_ABI int
802 length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
803 return do_length(__st, __frm, __end, __mx);
804 }
805
806 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
807
808 static locale::id id;
809
810protected:
811 _LIBCPP_HIDE_FROM_ABI explicit codecvt(const char*, size_t __refs = 0) : locale::facet(__refs) {}
812
813 ~codecvt() override;
814
815 virtual result
816 do_out(state_type& __st,
817 const intern_type* __frm,
818 const intern_type* __frm_end,
819 const intern_type*& __frm_nxt,
820 extern_type* __to,
821 extern_type* __to_end,
822 extern_type*& __to_nxt) const;
823 virtual result
824 do_in(state_type& __st,
825 const extern_type* __frm,
826 const extern_type* __frm_end,
827 const extern_type*& __frm_nxt,
828 intern_type* __to,
829 intern_type* __to_end,
830 intern_type*& __to_nxt) const;
831 virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
832 virtual int do_encoding() const _NOEXCEPT;
833 virtual bool do_always_noconv() const _NOEXCEPT;
834 virtual int do_length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
835 virtual int do_max_length() const _NOEXCEPT;
836};
837
838// template <> class codecvt<wchar_t, char, mbstate_t>
839
840# if _LIBCPP_HAS_WIDE_CHARACTERS
841template <>
842class _LIBCPP_EXPORTED_FROM_ABI codecvt<wchar_t, char, mbstate_t> : public locale::facet, public codecvt_base {
843 __locale::__locale_t __l_;
844
845public:
846 typedef wchar_t intern_type;
847 typedef char extern_type;
848 typedef mbstate_t state_type;
849
850 explicit codecvt(size_t __refs = 0);
851
852 _LIBCPP_HIDE_FROM_ABI result
853 out(state_type& __st,
854 const intern_type* __frm,
855 const intern_type* __frm_end,
856 const intern_type*& __frm_nxt,
857 extern_type* __to,
858 extern_type* __to_end,
859 extern_type*& __to_nxt) const {
860 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
861 }
862
863 _LIBCPP_HIDE_FROM_ABI result
864 unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
865 return do_unshift(__st, __to, __to_end, __to_nxt);
866 }
867
868 _LIBCPP_HIDE_FROM_ABI result
869 in(state_type& __st,
870 const extern_type* __frm,
871 const extern_type* __frm_end,
872 const extern_type*& __frm_nxt,
873 intern_type* __to,
874 intern_type* __to_end,
875 intern_type*& __to_nxt) const {
876 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
877 }
878
879 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
880
881 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
882
883 _LIBCPP_HIDE_FROM_ABI int
884 length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
885 return do_length(__st, __frm, __end, __mx);
886 }
887
888 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
889
890 static locale::id id;
891
892protected:
893 explicit codecvt(const char*, size_t __refs = 0);
894
895 ~codecvt() override;
896
897 virtual result
898 do_out(state_type& __st,
899 const intern_type* __frm,
900 const intern_type* __frm_end,
901 const intern_type*& __frm_nxt,
902 extern_type* __to,
903 extern_type* __to_end,
904 extern_type*& __to_nxt) const;
905 virtual result
906 do_in(state_type& __st,
907 const extern_type* __frm,
908 const extern_type* __frm_end,
909 const extern_type*& __frm_nxt,
910 intern_type* __to,
911 intern_type* __to_end,
912 intern_type*& __to_nxt) const;
913 virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
914 virtual int do_encoding() const _NOEXCEPT;
915 virtual bool do_always_noconv() const _NOEXCEPT;
916 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
917 virtual int do_max_length() const _NOEXCEPT;
918};
919# endif // _LIBCPP_HAS_WIDE_CHARACTERS
920
921// template <> class codecvt<char16_t, char, mbstate_t> // deprecated in C++20
922
923template <>
924class _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_EXPORTED_FROM_ABI codecvt<char16_t, char, mbstate_t>
925 : public locale::facet, public codecvt_base {
926public:
927 typedef char16_t intern_type;
928 typedef char extern_type;
929 typedef mbstate_t state_type;
930
931 _LIBCPP_HIDE_FROM_ABI explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
932
933 _LIBCPP_HIDE_FROM_ABI result
934 out(state_type& __st,
935 const intern_type* __frm,
936 const intern_type* __frm_end,
937 const intern_type*& __frm_nxt,
938 extern_type* __to,
939 extern_type* __to_end,
940 extern_type*& __to_nxt) const {
941 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
942 }
943
944 _LIBCPP_HIDE_FROM_ABI result
945 unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
946 return do_unshift(__st, __to, __to_end, __to_nxt);
947 }
948
949 _LIBCPP_HIDE_FROM_ABI result
950 in(state_type& __st,
951 const extern_type* __frm,
952 const extern_type* __frm_end,
953 const extern_type*& __frm_nxt,
954 intern_type* __to,
955 intern_type* __to_end,
956 intern_type*& __to_nxt) const {
957 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
958 }
959
960 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
961
962 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
963
964 _LIBCPP_HIDE_FROM_ABI int
965 length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
966 return do_length(__st, __frm, __end, __mx);
967 }
968
969 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
970
971 static locale::id id;
972
973protected:
974 _LIBCPP_HIDE_FROM_ABI explicit codecvt(const char*, size_t __refs = 0) : locale::facet(__refs) {}
975
976 ~codecvt() override;
977
978 virtual result
979 do_out(state_type& __st,
980 const intern_type* __frm,
981 const intern_type* __frm_end,
982 const intern_type*& __frm_nxt,
983 extern_type* __to,
984 extern_type* __to_end,
985 extern_type*& __to_nxt) const;
986 virtual result
987 do_in(state_type& __st,
988 const extern_type* __frm,
989 const extern_type* __frm_end,
990 const extern_type*& __frm_nxt,
991 intern_type* __to,
992 intern_type* __to_end,
993 intern_type*& __to_nxt) const;
994 virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
995 virtual int do_encoding() const _NOEXCEPT;
996 virtual bool do_always_noconv() const _NOEXCEPT;
997 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
998 virtual int do_max_length() const _NOEXCEPT;
999};
1000
1001# if _LIBCPP_HAS_CHAR8_T
1002
1003// template <> class codecvt<char16_t, char8_t, mbstate_t> // C++20, deprecated in C++20
1004
1005template <>
1006class _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_EXPORTED_FROM_ABI codecvt<char16_t, char8_t, mbstate_t>
1007 : public locale::facet, public codecvt_base {
1008public:
1009 typedef char16_t intern_type;
1010 typedef char8_t extern_type;
1011 typedef mbstate_t state_type;
1012
1013 _LIBCPP_HIDE_FROM_ABI explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
1014
1015 _LIBCPP_HIDE_FROM_ABI result
1016 out(state_type& __st,
1017 const intern_type* __frm,
1018 const intern_type* __frm_end,
1019 const intern_type*& __frm_nxt,
1020 extern_type* __to,
1021 extern_type* __to_end,
1022 extern_type*& __to_nxt) const {
1023 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1024 }
1025
1026 _LIBCPP_HIDE_FROM_ABI result
1027 unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
1028 return do_unshift(__st, __to, __to_end, __to_nxt);
1029 }
1030
1031 _LIBCPP_HIDE_FROM_ABI result
1032 in(state_type& __st,
1033 const extern_type* __frm,
1034 const extern_type* __frm_end,
1035 const extern_type*& __frm_nxt,
1036 intern_type* __to,
1037 intern_type* __to_end,
1038 intern_type*& __to_nxt) const {
1039 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1040 }
1041
1042 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
1043
1044 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
1045
1046 _LIBCPP_HIDE_FROM_ABI int
1047 length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
1048 return do_length(__st, __frm, __end, __mx);
1049 }
1050
1051 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
1052
1053 static locale::id id;
1054
1055protected:
1056 _LIBCPP_HIDE_FROM_ABI explicit codecvt(const char*, size_t __refs = 0) : locale::facet(__refs) {}
1057
1058 ~codecvt() override;
1059
1060 virtual result
1061 do_out(state_type& __st,
1062 const intern_type* __frm,
1063 const intern_type* __frm_end,
1064 const intern_type*& __frm_nxt,
1065 extern_type* __to,
1066 extern_type* __to_end,
1067 extern_type*& __to_nxt) const;
1068 virtual result
1069 do_in(state_type& __st,
1070 const extern_type* __frm,
1071 const extern_type* __frm_end,
1072 const extern_type*& __frm_nxt,
1073 intern_type* __to,
1074 intern_type* __to_end,
1075 intern_type*& __to_nxt) const;
1076 virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1077 virtual int do_encoding() const _NOEXCEPT;
1078 virtual bool do_always_noconv() const _NOEXCEPT;
1079 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
1080 virtual int do_max_length() const _NOEXCEPT;
1081};
1082
1083# endif
1084
1085// template <> class codecvt<char32_t, char, mbstate_t> // deprecated in C++20
1086
1087template <>
1088class _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_EXPORTED_FROM_ABI codecvt<char32_t, char, mbstate_t>
1089 : public locale::facet, public codecvt_base {
1090public:
1091 typedef char32_t intern_type;
1092 typedef char extern_type;
1093 typedef mbstate_t state_type;
1094
1095 _LIBCPP_HIDE_FROM_ABI explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
1096
1097 _LIBCPP_HIDE_FROM_ABI result
1098 out(state_type& __st,
1099 const intern_type* __frm,
1100 const intern_type* __frm_end,
1101 const intern_type*& __frm_nxt,
1102 extern_type* __to,
1103 extern_type* __to_end,
1104 extern_type*& __to_nxt) const {
1105 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1106 }
1107
1108 _LIBCPP_HIDE_FROM_ABI result
1109 unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
1110 return do_unshift(__st, __to, __to_end, __to_nxt);
1111 }
1112
1113 _LIBCPP_HIDE_FROM_ABI result
1114 in(state_type& __st,
1115 const extern_type* __frm,
1116 const extern_type* __frm_end,
1117 const extern_type*& __frm_nxt,
1118 intern_type* __to,
1119 intern_type* __to_end,
1120 intern_type*& __to_nxt) const {
1121 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1122 }
1123
1124 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
1125
1126 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
1127
1128 _LIBCPP_HIDE_FROM_ABI int
1129 length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
1130 return do_length(__st, __frm, __end, __mx);
1131 }
1132
1133 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
1134
1135 static locale::id id;
1136
1137protected:
1138 _LIBCPP_HIDE_FROM_ABI explicit codecvt(const char*, size_t __refs = 0) : locale::facet(__refs) {}
1139
1140 ~codecvt() override;
1141
1142 virtual result
1143 do_out(state_type& __st,
1144 const intern_type* __frm,
1145 const intern_type* __frm_end,
1146 const intern_type*& __frm_nxt,
1147 extern_type* __to,
1148 extern_type* __to_end,
1149 extern_type*& __to_nxt) const;
1150 virtual result
1151 do_in(state_type& __st,
1152 const extern_type* __frm,
1153 const extern_type* __frm_end,
1154 const extern_type*& __frm_nxt,
1155 intern_type* __to,
1156 intern_type* __to_end,
1157 intern_type*& __to_nxt) const;
1158 virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1159 virtual int do_encoding() const _NOEXCEPT;
1160 virtual bool do_always_noconv() const _NOEXCEPT;
1161 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
1162 virtual int do_max_length() const _NOEXCEPT;
1163};
1164
1165# if _LIBCPP_HAS_CHAR8_T
1166
1167// template <> class codecvt<char32_t, char8_t, mbstate_t> // C++20, deprecated in C++20
1168
1169template <>
1170class _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_EXPORTED_FROM_ABI codecvt<char32_t, char8_t, mbstate_t>
1171 : public locale::facet, public codecvt_base {
1172public:
1173 typedef char32_t intern_type;
1174 typedef char8_t extern_type;
1175 typedef mbstate_t state_type;
1176
1177 _LIBCPP_HIDE_FROM_ABI explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
1178
1179 _LIBCPP_HIDE_FROM_ABI result
1180 out(state_type& __st,
1181 const intern_type* __frm,
1182 const intern_type* __frm_end,
1183 const intern_type*& __frm_nxt,
1184 extern_type* __to,
1185 extern_type* __to_end,
1186 extern_type*& __to_nxt) const {
1187 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1188 }
1189
1190 _LIBCPP_HIDE_FROM_ABI result
1191 unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
1192 return do_unshift(__st, __to, __to_end, __to_nxt);
1193 }
1194
1195 _LIBCPP_HIDE_FROM_ABI result
1196 in(state_type& __st,
1197 const extern_type* __frm,
1198 const extern_type* __frm_end,
1199 const extern_type*& __frm_nxt,
1200 intern_type* __to,
1201 intern_type* __to_end,
1202 intern_type*& __to_nxt) const {
1203 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1204 }
1205
1206 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
1207
1208 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
1209
1210 _LIBCPP_HIDE_FROM_ABI int
1211 length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
1212 return do_length(__st, __frm, __end, __mx);
1213 }
1214
1215 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
1216
1217 static locale::id id;
1218
1219protected:
1220 _LIBCPP_HIDE_FROM_ABI explicit codecvt(const char*, size_t __refs = 0) : locale::facet(__refs) {}
1221
1222 ~codecvt() override;
1223
1224 virtual result
1225 do_out(state_type& __st,
1226 const intern_type* __frm,
1227 const intern_type* __frm_end,
1228 const intern_type*& __frm_nxt,
1229 extern_type* __to,
1230 extern_type* __to_end,
1231 extern_type*& __to_nxt) const;
1232 virtual result
1233 do_in(state_type& __st,
1234 const extern_type* __frm,
1235 const extern_type* __frm_end,
1236 const extern_type*& __frm_nxt,
1237 intern_type* __to,
1238 intern_type* __to_end,
1239 intern_type*& __to_nxt) const;
1240 virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1241 virtual int do_encoding() const _NOEXCEPT;
1242 virtual bool do_always_noconv() const _NOEXCEPT;
1243 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
1244 virtual int do_max_length() const _NOEXCEPT;
1245};
1246
1247# endif
1248
1249// template <class _InternT, class _ExternT, class _StateT> class codecvt_byname
1250
1251template <class _InternT, class _ExternT, class _StateT>
1252class codecvt_byname : public codecvt<_InternT, _ExternT, _StateT> {
1253public:
1254 _LIBCPP_HIDE_FROM_ABI explicit codecvt_byname(const char* __nm, size_t __refs = 0)
1255 : codecvt<_InternT, _ExternT, _StateT>(__nm, __refs) {}
1256 _LIBCPP_HIDE_FROM_ABI explicit codecvt_byname(const string& __nm, size_t __refs = 0)
1257 : codecvt<_InternT, _ExternT, _StateT>(__nm.c_str(), __refs) {}
1258
1259protected:
1260 ~codecvt_byname() override;
1261};
1262
1263_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1264template <class _InternT, class _ExternT, class _StateT>
1265codecvt_byname<_InternT, _ExternT, _StateT>::~codecvt_byname() {}
1266_LIBCPP_SUPPRESS_DEPRECATED_POP
1267
1268extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char, char, mbstate_t>;
1269# if _LIBCPP_HAS_WIDE_CHARACTERS
1270extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<wchar_t, char, mbstate_t>;
1271# endif
1272extern template class _LIBCPP_DEPRECATED_IN_CXX20
1273_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char16_t, char, mbstate_t>; // deprecated in C++20
1274extern template class _LIBCPP_DEPRECATED_IN_CXX20
1275_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char32_t, char, mbstate_t>; // deprecated in C++20
1276# if _LIBCPP_HAS_CHAR8_T
1277extern template class _LIBCPP_DEPRECATED_IN_CXX20
1278_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char16_t, char8_t, mbstate_t>; // C++20, deprecated in C++20
1279extern template class _LIBCPP_DEPRECATED_IN_CXX20
1280_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char32_t, char8_t, mbstate_t>; // C++20, deprecated in C++20
1281# endif
1282
1283template <size_t _Np>
1284struct __narrow_to_utf8 {
1285 template <class _OutputIterator, class _CharT>
1286 _OutputIterator operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const;
1287};
1288
1289template <>
1290struct __narrow_to_utf8<8> {
1291 template <class _OutputIterator, class _CharT>
1292 _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const {
1293 for (; __wb < __we; ++__wb, ++__s)
1294 *__s = *__wb;
1295 return __s;
1296 }
1297};
1298
1299_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1300template <>
1301struct _LIBCPP_EXPORTED_FROM_ABI __narrow_to_utf8<16> : public codecvt<char16_t, char, mbstate_t> {
1302 _LIBCPP_HIDE_FROM_ABI __narrow_to_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1303 _LIBCPP_SUPPRESS_DEPRECATED_POP
1304
1305 ~__narrow_to_utf8() override;
1306
1307 template <class _OutputIterator, class _CharT>
1308 _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const {
1309 result __r = ok;
1310 mbstate_t __mb;
1311 while (__wb < __we && __r != error) {
1312 const int __sz = 32;
1313 char __buf[__sz];
1314 char* __bn;
1315 const char16_t* __wn = (const char16_t*)__wb;
1316 __r = do_out(st&: __mb, frm: (const char16_t*)__wb, frm_end: (const char16_t*)__we, frm_nxt&: __wn, to: __buf, to_end: __buf + __sz, to_nxt&: __bn);
1317 if (__r == codecvt_base::error || __wn == (const char16_t*)__wb)
1318 std::__throw_runtime_error("locale not supported");
1319 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1320 *__s = *__p;
1321 __wb = (const _CharT*)__wn;
1322 }
1323 return __s;
1324 }
1325};
1326
1327_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1328template <>
1329struct _LIBCPP_EXPORTED_FROM_ABI __narrow_to_utf8<32> : public codecvt<char32_t, char, mbstate_t> {
1330 _LIBCPP_HIDE_FROM_ABI __narrow_to_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1331 _LIBCPP_SUPPRESS_DEPRECATED_POP
1332
1333 ~__narrow_to_utf8() override;
1334
1335 template <class _OutputIterator, class _CharT>
1336 _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const {
1337 result __r = ok;
1338 mbstate_t __mb;
1339 while (__wb < __we && __r != error) {
1340 const int __sz = 32;
1341 char __buf[__sz];
1342 char* __bn;
1343 const char32_t* __wn = (const char32_t*)__wb;
1344 __r = do_out(st&: __mb, frm: (const char32_t*)__wb, frm_end: (const char32_t*)__we, frm_nxt&: __wn, to: __buf, to_end: __buf + __sz, to_nxt&: __bn);
1345 if (__r == codecvt_base::error || __wn == (const char32_t*)__wb)
1346 std::__throw_runtime_error("locale not supported");
1347 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1348 *__s = *__p;
1349 __wb = (const _CharT*)__wn;
1350 }
1351 return __s;
1352 }
1353};
1354
1355template <size_t _Np>
1356struct __widen_from_utf8 {
1357 template <class _OutputIterator>
1358 _OutputIterator operator()(_OutputIterator __s, const char* __nb, const char* __ne) const;
1359};
1360
1361template <>
1362struct __widen_from_utf8<8> {
1363 template <class _OutputIterator>
1364 _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const char* __nb, const char* __ne) const {
1365 for (; __nb < __ne; ++__nb, ++__s)
1366 *__s = *__nb;
1367 return __s;
1368 }
1369};
1370
1371_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1372template <>
1373struct _LIBCPP_EXPORTED_FROM_ABI __widen_from_utf8<16> : public codecvt<char16_t, char, mbstate_t> {
1374 _LIBCPP_HIDE_FROM_ABI __widen_from_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1375 _LIBCPP_SUPPRESS_DEPRECATED_POP
1376
1377 ~__widen_from_utf8() override;
1378
1379 template <class _OutputIterator>
1380 _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const char* __nb, const char* __ne) const {
1381 result __r = ok;
1382 mbstate_t __mb;
1383 while (__nb < __ne && __r != error) {
1384 const int __sz = 32;
1385 char16_t __buf[__sz];
1386 char16_t* __bn;
1387 const char* __nn = __nb;
1388 __r = do_in(st&: __mb, frm: __nb, frm_end: __ne - __nb > __sz ? __nb + __sz : __ne, frm_nxt&: __nn, to: __buf, to_end: __buf + __sz, to_nxt&: __bn);
1389 if (__r == codecvt_base::error || __nn == __nb)
1390 std::__throw_runtime_error("locale not supported");
1391 for (const char16_t* __p = __buf; __p < __bn; ++__p, ++__s)
1392 *__s = *__p;
1393 __nb = __nn;
1394 }
1395 return __s;
1396 }
1397};
1398
1399_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1400template <>
1401struct _LIBCPP_EXPORTED_FROM_ABI __widen_from_utf8<32> : public codecvt<char32_t, char, mbstate_t> {
1402 _LIBCPP_HIDE_FROM_ABI __widen_from_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1403 _LIBCPP_SUPPRESS_DEPRECATED_POP
1404
1405 ~__widen_from_utf8() override;
1406
1407 template <class _OutputIterator>
1408 _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const char* __nb, const char* __ne) const {
1409 result __r = ok;
1410 mbstate_t __mb;
1411 while (__nb < __ne && __r != error) {
1412 const int __sz = 32;
1413 char32_t __buf[__sz];
1414 char32_t* __bn;
1415 const char* __nn = __nb;
1416 __r = do_in(st&: __mb, frm: __nb, frm_end: __ne - __nb > __sz ? __nb + __sz : __ne, frm_nxt&: __nn, to: __buf, to_end: __buf + __sz, to_nxt&: __bn);
1417 if (__r == codecvt_base::error || __nn == __nb)
1418 std::__throw_runtime_error("locale not supported");
1419 for (const char32_t* __p = __buf; __p < __bn; ++__p, ++__s)
1420 *__s = *__p;
1421 __nb = __nn;
1422 }
1423 return __s;
1424 }
1425};
1426
1427// template <class charT> class numpunct
1428
1429template <class _CharT>
1430class numpunct;
1431
1432template <>
1433class _LIBCPP_EXPORTED_FROM_ABI numpunct<char> : public locale::facet {
1434public:
1435 typedef char char_type;
1436 typedef basic_string<char_type> string_type;
1437
1438 explicit numpunct(size_t __refs = 0);
1439
1440 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type decimal_point() const { return do_decimal_point(); }
1441 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type thousands_sep() const { return do_thousands_sep(); }
1442 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI string grouping() const { return do_grouping(); }
1443 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI string_type truename() const { return do_truename(); }
1444 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI string_type falsename() const { return do_falsename(); }
1445
1446 static locale::id id;
1447
1448protected:
1449 ~numpunct() override;
1450 virtual char_type do_decimal_point() const;
1451 virtual char_type do_thousands_sep() const;
1452 virtual string do_grouping() const;
1453 virtual string_type do_truename() const;
1454 virtual string_type do_falsename() const;
1455
1456 char_type __decimal_point_;
1457 char_type __thousands_sep_;
1458 string __grouping_;
1459};
1460
1461# if _LIBCPP_HAS_WIDE_CHARACTERS
1462template <>
1463class _LIBCPP_EXPORTED_FROM_ABI numpunct<wchar_t> : public locale::facet {
1464public:
1465 typedef wchar_t char_type;
1466 typedef basic_string<char_type> string_type;
1467
1468 explicit numpunct(size_t __refs = 0);
1469
1470 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type decimal_point() const { return do_decimal_point(); }
1471 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type thousands_sep() const { return do_thousands_sep(); }
1472 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI string grouping() const { return do_grouping(); }
1473 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI string_type truename() const { return do_truename(); }
1474 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI string_type falsename() const { return do_falsename(); }
1475
1476 static locale::id id;
1477
1478protected:
1479 ~numpunct() override;
1480 virtual char_type do_decimal_point() const;
1481 virtual char_type do_thousands_sep() const;
1482 virtual string do_grouping() const;
1483 virtual string_type do_truename() const;
1484 virtual string_type do_falsename() const;
1485
1486 char_type __decimal_point_;
1487 char_type __thousands_sep_;
1488 string __grouping_;
1489};
1490# endif // _LIBCPP_HAS_WIDE_CHARACTERS
1491
1492// template <class charT> class numpunct_byname
1493
1494template <class _CharT>
1495class numpunct_byname;
1496
1497template <>
1498class _LIBCPP_EXPORTED_FROM_ABI numpunct_byname<char> : public numpunct<char> {
1499public:
1500 typedef char char_type;
1501 typedef basic_string<char_type> string_type;
1502
1503 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1504 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1505
1506protected:
1507 ~numpunct_byname() override;
1508};
1509
1510# if _LIBCPP_HAS_WIDE_CHARACTERS
1511template <>
1512class _LIBCPP_EXPORTED_FROM_ABI numpunct_byname<wchar_t> : public numpunct<wchar_t> {
1513public:
1514 typedef wchar_t char_type;
1515 typedef basic_string<char_type> string_type;
1516
1517 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1518 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1519
1520protected:
1521 ~numpunct_byname() override;
1522};
1523# endif // _LIBCPP_HAS_WIDE_CHARACTERS
1524
1525_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
1526_LIBCPP_END_NAMESPACE_STD
1527
1528#endif // _LIBCPP_HAS_LOCALIZATION
1529
1530#endif // _LIBCPP___LOCALE
1531