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 _LIBCPP_DIAGNOSTIC_PUSH
338 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wmodules-ambiguous-internal-linkage")
339 static const mask space = _ISspace;
340 static const mask print = _ISprint;
341 static const mask cntrl = _IScntrl;
342 static const mask upper = _ISupper;
343 static const mask lower = _ISlower;
344 static const mask alpha = _ISalpha;
345 static const mask digit = _ISdigit;
346 static const mask punct = _ISpunct;
347 static const mask xdigit = _ISxdigit;
348 static const mask blank = _ISblank;
349 _LIBCPP_DIAGNOSTIC_POP
350# if defined(__mips__) || (BYTE_ORDER == BIG_ENDIAN)
351 static const mask __regex_word = static_cast<mask>(_ISbit(15));
352# else
353 static const mask __regex_word = 0x80;
354# endif
355# elif defined(_LIBCPP_MSVCRT_LIKE)
356 typedef unsigned short mask;
357 static const mask space = _SPACE;
358 static const mask print = _BLANK | _PUNCT | _ALPHA | _DIGIT;
359 static const mask cntrl = _CONTROL;
360 static const mask upper = _UPPER;
361 static const mask lower = _LOWER;
362 static const mask alpha = _ALPHA;
363 static const mask digit = _DIGIT;
364 static const mask punct = _PUNCT;
365 static const mask xdigit = _HEX;
366 static const mask blank = _BLANK;
367 static const mask __regex_word = 0x4000; // 0x8000 and 0x0100 and 0x00ff are used
368# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
369# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
370# elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
371# ifdef __APPLE__
372 typedef uint32_t mask;
373# elif defined(__FreeBSD__)
374 typedef unsigned long mask;
375# elif defined(__NetBSD__)
376 typedef unsigned short mask;
377# endif
378 static const mask space = _CTYPE_S;
379 static const mask print = _CTYPE_R;
380 static const mask cntrl = _CTYPE_C;
381 static const mask upper = _CTYPE_U;
382 static const mask lower = _CTYPE_L;
383 static const mask alpha = _CTYPE_A;
384 static const mask digit = _CTYPE_D;
385 static const mask punct = _CTYPE_P;
386 static const mask xdigit = _CTYPE_X;
387
388# if defined(__NetBSD__)
389 static const mask blank = _CTYPE_BL;
390 // NetBSD defines classes up to 0x2000
391 // see sys/ctype_bits.h, _CTYPE_Q
392 static const mask __regex_word = 0x8000;
393# else
394 static const mask blank = _CTYPE_B;
395 static const mask __regex_word = 0x80;
396# endif
397# elif defined(_AIX)
398 typedef unsigned int mask;
399 static const mask space = _ISSPACE;
400 static const mask print = _ISPRINT;
401 static const mask cntrl = _ISCNTRL;
402 static const mask upper = _ISUPPER;
403 static const mask lower = _ISLOWER;
404 static const mask alpha = _ISALPHA;
405 static const mask digit = _ISDIGIT;
406 static const mask punct = _ISPUNCT;
407 static const mask xdigit = _ISXDIGIT;
408 static const mask blank = _ISBLANK;
409 static const mask __regex_word = 0x8000;
410# elif _LIBCPP_LIBC_NEWLIB
411 // Same type as Newlib's _ctype_ array in newlib/libc/include/ctype.h.
412 typedef char mask;
413 // In case char is signed, static_cast is needed to avoid warning on
414 // positive value becomming negative.
415 static const mask space = static_cast<mask>(_S);
416 static const mask print = static_cast<mask>(_P | _U | _L | _N | _B);
417 static const mask cntrl = static_cast<mask>(_C);
418 static const mask upper = static_cast<mask>(_U);
419 static const mask lower = static_cast<mask>(_L);
420 static const mask alpha = static_cast<mask>(_U | _L);
421 static const mask digit = static_cast<mask>(_N);
422 static const mask punct = static_cast<mask>(_P);
423 static const mask xdigit = static_cast<mask>(_X | _N);
424 static const mask blank = static_cast<mask>(_B);
425 // mask is already fully saturated, use a different type in regex_type_traits.
426 static const unsigned short __regex_word = 0x100;
427# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
428# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
429# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_XDIGIT
430# elif defined(__MVS__)
431# if defined(__NATIVE_ASCII_F)
432 typedef unsigned int mask;
433 static const mask space = _ISSPACE_A;
434 static const mask print = _ISPRINT_A;
435 static const mask cntrl = _ISCNTRL_A;
436 static const mask upper = _ISUPPER_A;
437 static const mask lower = _ISLOWER_A;
438 static const mask alpha = _ISALPHA_A;
439 static const mask digit = _ISDIGIT_A;
440 static const mask punct = _ISPUNCT_A;
441 static const mask xdigit = _ISXDIGIT_A;
442 static const mask blank = _ISBLANK_A;
443# else
444 typedef unsigned short mask;
445 static const mask space = __ISSPACE;
446 static const mask print = __ISPRINT;
447 static const mask cntrl = __ISCNTRL;
448 static const mask upper = __ISUPPER;
449 static const mask lower = __ISLOWER;
450 static const mask alpha = __ISALPHA;
451 static const mask digit = __ISDIGIT;
452 static const mask punct = __ISPUNCT;
453 static const mask xdigit = __ISXDIGIT;
454 static const mask blank = __ISBLANK;
455# endif
456 static const mask __regex_word = 0x8000;
457# else
458# error unknown rune table for this platform -- do you mean to define _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE?
459# endif
460 static const mask alnum = alpha | digit;
461 static const mask graph = alnum | punct;
462
463 _LIBCPP_HIDE_FROM_ABI ctype_base() {}
464
465 static_assert((__regex_word & ~(std::make_unsigned<mask>::type)(space | print | cntrl | upper | lower | alpha |
466 digit | punct | xdigit | blank)) == __regex_word,
467 "__regex_word can't overlap other bits");
468};
469
470template <class _CharT>
471class ctype;
472
473# if _LIBCPP_HAS_WIDE_CHARACTERS
474template <>
475class _LIBCPP_EXPORTED_FROM_ABI ctype<wchar_t> : public locale::facet, public ctype_base {
476public:
477 typedef wchar_t char_type;
478
479 _LIBCPP_HIDE_FROM_ABI explicit ctype(size_t __refs = 0) : locale::facet(__refs) {}
480
481 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool is(mask __m, char_type __c) const { return do_is(__m, __c); }
482
483 _LIBCPP_HIDE_FROM_ABI const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const {
484 return do_is(__low, __high, __vec);
485 }
486
487 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const char_type*
488 scan_is(mask __m, const char_type* __low, const char_type* __high) const {
489 return do_scan_is(__m, __low, __high);
490 }
491
492 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const char_type*
493 scan_not(mask __m, const char_type* __low, const char_type* __high) const {
494 return do_scan_not(__m, __low, __high);
495 }
496
497 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type toupper(char_type __c) const { return do_toupper(__c); }
498
499 _LIBCPP_HIDE_FROM_ABI const char_type* toupper(char_type* __low, const char_type* __high) const {
500 return do_toupper(__low, __high);
501 }
502
503 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type tolower(char_type __c) const { return do_tolower(__c); }
504
505 _LIBCPP_HIDE_FROM_ABI const char_type* tolower(char_type* __low, const char_type* __high) const {
506 return do_tolower(__low, __high);
507 }
508
509 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type widen(char __c) const { return do_widen(__c); }
510
511 _LIBCPP_HIDE_FROM_ABI const char* widen(const char* __low, const char* __high, char_type* __to) const {
512 return do_widen(__low, __high, dest: __to);
513 }
514
515 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char narrow(char_type __c, char __dfault) const {
516 return do_narrow(__c, __dfault);
517 }
518
519 _LIBCPP_HIDE_FROM_ABI const char_type*
520 narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const {
521 return do_narrow(__low, __high, __dfault, dest: __to);
522 }
523
524 static locale::id id;
525
526protected:
527 ~ctype() override;
528 virtual bool do_is(mask __m, char_type __c) const;
529 virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
530 virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
531 virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
532 virtual char_type do_toupper(char_type) const;
533 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
534 virtual char_type do_tolower(char_type) const;
535 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
536 virtual char_type do_widen(char) const;
537 virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
538 virtual char do_narrow(char_type, char __dfault) const;
539 virtual const char_type*
540 do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
541};
542# endif // _LIBCPP_HAS_WIDE_CHARACTERS
543
544inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_isascii(int __c) { return (__c & ~0x7F) == 0; }
545
546template <>
547class _LIBCPP_EXPORTED_FROM_ABI ctype<char> : public locale::facet, public ctype_base {
548 const mask* __tab_;
549 bool __del_;
550
551public:
552 typedef char char_type;
553
554 explicit ctype(const mask* __tab = nullptr, bool __del = false, size_t __refs = 0);
555
556 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool is(mask __m, char_type __c) const {
557 return std::__libcpp_isascii(__c) ? (__tab_[static_cast<int>(__c)] & __m) != 0 : false;
558 }
559
560 _LIBCPP_HIDE_FROM_ABI const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const {
561 for (; __low != __high; ++__low, ++__vec)
562 *__vec = std::__libcpp_isascii(c: *__low) ? __tab_[static_cast<int>(*__low)] : 0;
563 return __low;
564 }
565
566 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const char_type*
567 scan_is(mask __m, const char_type* __low, const char_type* __high) const {
568 for (; __low != __high; ++__low)
569 if (std::__libcpp_isascii(c: *__low) && (__tab_[static_cast<int>(*__low)] & __m))
570 break;
571 return __low;
572 }
573
574 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const char_type*
575 scan_not(mask __m, const char_type* __low, const char_type* __high) const {
576 for (; __low != __high; ++__low)
577 if (!std::__libcpp_isascii(c: *__low) || !(__tab_[static_cast<int>(*__low)] & __m))
578 break;
579 return __low;
580 }
581
582 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type toupper(char_type __c) const { return do_toupper(__c); }
583
584 _LIBCPP_HIDE_FROM_ABI const char_type* toupper(char_type* __low, const char_type* __high) const {
585 return do_toupper(__low, __high);
586 }
587
588 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type tolower(char_type __c) const { return do_tolower(__c); }
589
590 _LIBCPP_HIDE_FROM_ABI const char_type* tolower(char_type* __low, const char_type* __high) const {
591 return do_tolower(__low, __high);
592 }
593
594 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type widen(char __c) const { return do_widen(__c); }
595
596 _LIBCPP_HIDE_FROM_ABI const char* widen(const char* __low, const char* __high, char_type* __to) const {
597 return do_widen(__low, __high, __to);
598 }
599
600 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char narrow(char_type __c, char __dfault) const {
601 return do_narrow(__c, __dfault);
602 }
603
604 _LIBCPP_HIDE_FROM_ABI const char*
605 narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const {
606 return do_narrow(__low, __high, __dfault, __to);
607 }
608
609 static locale::id id;
610
611# ifdef _CACHED_RUNES
612 static const size_t table_size = _CACHED_RUNES;
613# else
614 static const size_t table_size = 256;
615# endif
616 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const mask* table() const _NOEXCEPT { return __tab_; }
617 [[__nodiscard__]] static const mask* classic_table() _NOEXCEPT;
618
619protected:
620 ~ctype() override;
621 virtual char_type do_toupper(char_type __c) const;
622 virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
623 virtual char_type do_tolower(char_type __c) const;
624 virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
625 virtual char_type do_widen(char __c) const;
626 virtual const char* do_widen(const char* __low, const char* __high, char_type* __to) const;
627 virtual char do_narrow(char_type __c, char __dfault) const;
628 virtual const char* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const;
629};
630
631// template <class CharT> class ctype_byname;
632
633template <class _CharT>
634class ctype_byname;
635
636template <>
637class _LIBCPP_EXPORTED_FROM_ABI ctype_byname<char> : public ctype<char> {
638 __locale::__locale_t __l_;
639
640public:
641 explicit ctype_byname(const char*, size_t = 0);
642 explicit ctype_byname(const string&, size_t = 0);
643
644protected:
645 ~ctype_byname() override;
646 char_type do_toupper(char_type) const override;
647 const char_type* do_toupper(char_type* __low, const char_type* __high) const override;
648 char_type do_tolower(char_type) const override;
649 const char_type* do_tolower(char_type* __low, const char_type* __high) const override;
650};
651
652# if _LIBCPP_HAS_WIDE_CHARACTERS
653template <>
654class _LIBCPP_EXPORTED_FROM_ABI ctype_byname<wchar_t> : public ctype<wchar_t> {
655 __locale::__locale_t __l_;
656
657public:
658 explicit ctype_byname(const char*, size_t = 0);
659 explicit ctype_byname(const string&, size_t = 0);
660
661protected:
662 ~ctype_byname() override;
663 bool do_is(mask __m, char_type __c) const override;
664 const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const override;
665 const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const override;
666 const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const override;
667 char_type do_toupper(char_type) const override;
668 const char_type* do_toupper(char_type* __low, const char_type* __high) const override;
669 char_type do_tolower(char_type) const override;
670 const char_type* do_tolower(char_type* __low, const char_type* __high) const override;
671 char_type do_widen(char) const override;
672 const char* do_widen(const char* __low, const char* __high, char_type* __dest) const override;
673 char do_narrow(char_type, char __dfault) const override;
674 const char_type*
675 do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const override;
676};
677# endif // _LIBCPP_HAS_WIDE_CHARACTERS
678
679template <class _CharT>
680[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool isspace(_CharT __c, const locale& __loc) {
681 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
682}
683
684template <class _CharT>
685[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool isprint(_CharT __c, const locale& __loc) {
686 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c);
687}
688
689template <class _CharT>
690[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool iscntrl(_CharT __c, const locale& __loc) {
691 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c);
692}
693
694template <class _CharT>
695[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool isupper(_CharT __c, const locale& __loc) {
696 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c);
697}
698
699template <class _CharT>
700[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool islower(_CharT __c, const locale& __loc) {
701 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c);
702}
703
704template <class _CharT>
705[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool isalpha(_CharT __c, const locale& __loc) {
706 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c);
707}
708
709template <class _CharT>
710[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool isdigit(_CharT __c, const locale& __loc) {
711 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c);
712}
713
714template <class _CharT>
715[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool ispunct(_CharT __c, const locale& __loc) {
716 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c);
717}
718
719template <class _CharT>
720[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool isxdigit(_CharT __c, const locale& __loc) {
721 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c);
722}
723
724template <class _CharT>
725[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool isalnum(_CharT __c, const locale& __loc) {
726 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c);
727}
728
729template <class _CharT>
730[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool isgraph(_CharT __c, const locale& __loc) {
731 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c);
732}
733
734template <class _CharT>
735[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool isblank(_CharT __c, const locale& __loc) {
736 return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::blank, __c);
737}
738
739template <class _CharT>
740[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _CharT toupper(_CharT __c, const locale& __loc) {
741 return std::use_facet<ctype<_CharT> >(__loc).toupper(__c);
742}
743
744template <class _CharT>
745[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _CharT tolower(_CharT __c, const locale& __loc) {
746 return std::use_facet<ctype<_CharT> >(__loc).tolower(__c);
747}
748
749// codecvt_base
750
751class _LIBCPP_EXPORTED_FROM_ABI codecvt_base {
752public:
753 _LIBCPP_HIDE_FROM_ABI codecvt_base() {}
754 enum result { ok, partial, error, noconv };
755};
756
757// template <class internT, class externT, class stateT> class codecvt;
758
759template <class _InternT, class _ExternT, class _StateT>
760class codecvt;
761
762// template <> class codecvt<char, char, mbstate_t>
763
764template <>
765class _LIBCPP_EXPORTED_FROM_ABI codecvt<char, char, mbstate_t> : public locale::facet, public codecvt_base {
766public:
767 typedef char intern_type;
768 typedef char extern_type;
769 typedef mbstate_t state_type;
770
771 _LIBCPP_HIDE_FROM_ABI explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
772
773 _LIBCPP_HIDE_FROM_ABI result
774 out(state_type& __st,
775 const intern_type* __frm,
776 const intern_type* __frm_end,
777 const intern_type*& __frm_nxt,
778 extern_type* __to,
779 extern_type* __to_end,
780 extern_type*& __to_nxt) const {
781 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
782 }
783
784 _LIBCPP_HIDE_FROM_ABI result
785 unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
786 return do_unshift(__st, __to, __to_end, __to_nxt);
787 }
788
789 _LIBCPP_HIDE_FROM_ABI result
790 in(state_type& __st,
791 const extern_type* __frm,
792 const extern_type* __frm_end,
793 const extern_type*& __frm_nxt,
794 intern_type* __to,
795 intern_type* __to_end,
796 intern_type*& __to_nxt) const {
797 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
798 }
799
800 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
801
802 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
803
804 _LIBCPP_HIDE_FROM_ABI int
805 length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
806 return do_length(__st, __frm, __end, __mx);
807 }
808
809 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
810
811 static locale::id id;
812
813protected:
814 _LIBCPP_HIDE_FROM_ABI explicit codecvt(const char*, size_t __refs = 0) : locale::facet(__refs) {}
815
816 ~codecvt() override;
817
818 virtual result
819 do_out(state_type& __st,
820 const intern_type* __frm,
821 const intern_type* __frm_end,
822 const intern_type*& __frm_nxt,
823 extern_type* __to,
824 extern_type* __to_end,
825 extern_type*& __to_nxt) const;
826 virtual result
827 do_in(state_type& __st,
828 const extern_type* __frm,
829 const extern_type* __frm_end,
830 const extern_type*& __frm_nxt,
831 intern_type* __to,
832 intern_type* __to_end,
833 intern_type*& __to_nxt) const;
834 virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
835 virtual int do_encoding() const _NOEXCEPT;
836 virtual bool do_always_noconv() const _NOEXCEPT;
837 virtual int do_length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
838 virtual int do_max_length() const _NOEXCEPT;
839};
840
841// template <> class codecvt<wchar_t, char, mbstate_t>
842
843# if _LIBCPP_HAS_WIDE_CHARACTERS
844template <>
845class _LIBCPP_EXPORTED_FROM_ABI codecvt<wchar_t, char, mbstate_t> : public locale::facet, public codecvt_base {
846 __locale::__locale_t __l_;
847
848public:
849 typedef wchar_t intern_type;
850 typedef char extern_type;
851 typedef mbstate_t state_type;
852
853 explicit codecvt(size_t __refs = 0);
854
855 _LIBCPP_HIDE_FROM_ABI result
856 out(state_type& __st,
857 const intern_type* __frm,
858 const intern_type* __frm_end,
859 const intern_type*& __frm_nxt,
860 extern_type* __to,
861 extern_type* __to_end,
862 extern_type*& __to_nxt) const {
863 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
864 }
865
866 _LIBCPP_HIDE_FROM_ABI result
867 unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
868 return do_unshift(__st, __to, __to_end, __to_nxt);
869 }
870
871 _LIBCPP_HIDE_FROM_ABI result
872 in(state_type& __st,
873 const extern_type* __frm,
874 const extern_type* __frm_end,
875 const extern_type*& __frm_nxt,
876 intern_type* __to,
877 intern_type* __to_end,
878 intern_type*& __to_nxt) const {
879 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
880 }
881
882 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
883
884 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
885
886 _LIBCPP_HIDE_FROM_ABI int
887 length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
888 return do_length(__st, __frm, __end, __mx);
889 }
890
891 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
892
893 static locale::id id;
894
895protected:
896 explicit codecvt(const char*, size_t __refs = 0);
897
898 ~codecvt() override;
899
900 virtual result
901 do_out(state_type& __st,
902 const intern_type* __frm,
903 const intern_type* __frm_end,
904 const intern_type*& __frm_nxt,
905 extern_type* __to,
906 extern_type* __to_end,
907 extern_type*& __to_nxt) const;
908 virtual result
909 do_in(state_type& __st,
910 const extern_type* __frm,
911 const extern_type* __frm_end,
912 const extern_type*& __frm_nxt,
913 intern_type* __to,
914 intern_type* __to_end,
915 intern_type*& __to_nxt) const;
916 virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
917 virtual int do_encoding() const _NOEXCEPT;
918 virtual bool do_always_noconv() const _NOEXCEPT;
919 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
920 virtual int do_max_length() const _NOEXCEPT;
921};
922# endif // _LIBCPP_HAS_WIDE_CHARACTERS
923
924// template <> class codecvt<char16_t, char, mbstate_t> // deprecated in C++20
925
926template <>
927class _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_EXPORTED_FROM_ABI codecvt<char16_t, char, mbstate_t>
928 : public locale::facet, public codecvt_base {
929public:
930 typedef char16_t intern_type;
931 typedef char extern_type;
932 typedef mbstate_t state_type;
933
934 _LIBCPP_HIDE_FROM_ABI explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
935
936 _LIBCPP_HIDE_FROM_ABI result
937 out(state_type& __st,
938 const intern_type* __frm,
939 const intern_type* __frm_end,
940 const intern_type*& __frm_nxt,
941 extern_type* __to,
942 extern_type* __to_end,
943 extern_type*& __to_nxt) const {
944 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
945 }
946
947 _LIBCPP_HIDE_FROM_ABI result
948 unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
949 return do_unshift(__st, __to, __to_end, __to_nxt);
950 }
951
952 _LIBCPP_HIDE_FROM_ABI result
953 in(state_type& __st,
954 const extern_type* __frm,
955 const extern_type* __frm_end,
956 const extern_type*& __frm_nxt,
957 intern_type* __to,
958 intern_type* __to_end,
959 intern_type*& __to_nxt) const {
960 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
961 }
962
963 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
964
965 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
966
967 _LIBCPP_HIDE_FROM_ABI int
968 length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
969 return do_length(__st, __frm, __end, __mx);
970 }
971
972 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
973
974 static locale::id id;
975
976protected:
977 _LIBCPP_HIDE_FROM_ABI explicit codecvt(const char*, size_t __refs = 0) : locale::facet(__refs) {}
978
979 ~codecvt() override;
980
981 virtual result
982 do_out(state_type& __st,
983 const intern_type* __frm,
984 const intern_type* __frm_end,
985 const intern_type*& __frm_nxt,
986 extern_type* __to,
987 extern_type* __to_end,
988 extern_type*& __to_nxt) const;
989 virtual result
990 do_in(state_type& __st,
991 const extern_type* __frm,
992 const extern_type* __frm_end,
993 const extern_type*& __frm_nxt,
994 intern_type* __to,
995 intern_type* __to_end,
996 intern_type*& __to_nxt) const;
997 virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
998 virtual int do_encoding() const _NOEXCEPT;
999 virtual bool do_always_noconv() const _NOEXCEPT;
1000 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
1001 virtual int do_max_length() const _NOEXCEPT;
1002};
1003
1004# if _LIBCPP_HAS_CHAR8_T
1005
1006// template <> class codecvt<char16_t, char8_t, mbstate_t> // C++20, deprecated in C++20
1007
1008template <>
1009class _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_EXPORTED_FROM_ABI codecvt<char16_t, char8_t, mbstate_t>
1010 : public locale::facet, public codecvt_base {
1011public:
1012 typedef char16_t intern_type;
1013 typedef char8_t extern_type;
1014 typedef mbstate_t state_type;
1015
1016 _LIBCPP_HIDE_FROM_ABI explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
1017
1018 _LIBCPP_HIDE_FROM_ABI result
1019 out(state_type& __st,
1020 const intern_type* __frm,
1021 const intern_type* __frm_end,
1022 const intern_type*& __frm_nxt,
1023 extern_type* __to,
1024 extern_type* __to_end,
1025 extern_type*& __to_nxt) const {
1026 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1027 }
1028
1029 _LIBCPP_HIDE_FROM_ABI result
1030 unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
1031 return do_unshift(__st, __to, __to_end, __to_nxt);
1032 }
1033
1034 _LIBCPP_HIDE_FROM_ABI result
1035 in(state_type& __st,
1036 const extern_type* __frm,
1037 const extern_type* __frm_end,
1038 const extern_type*& __frm_nxt,
1039 intern_type* __to,
1040 intern_type* __to_end,
1041 intern_type*& __to_nxt) const {
1042 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1043 }
1044
1045 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
1046
1047 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
1048
1049 _LIBCPP_HIDE_FROM_ABI int
1050 length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
1051 return do_length(__st, __frm, __end, __mx);
1052 }
1053
1054 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
1055
1056 static locale::id id;
1057
1058protected:
1059 _LIBCPP_HIDE_FROM_ABI explicit codecvt(const char*, size_t __refs = 0) : locale::facet(__refs) {}
1060
1061 ~codecvt() override;
1062
1063 virtual result
1064 do_out(state_type& __st,
1065 const intern_type* __frm,
1066 const intern_type* __frm_end,
1067 const intern_type*& __frm_nxt,
1068 extern_type* __to,
1069 extern_type* __to_end,
1070 extern_type*& __to_nxt) const;
1071 virtual result
1072 do_in(state_type& __st,
1073 const extern_type* __frm,
1074 const extern_type* __frm_end,
1075 const extern_type*& __frm_nxt,
1076 intern_type* __to,
1077 intern_type* __to_end,
1078 intern_type*& __to_nxt) const;
1079 virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1080 virtual int do_encoding() const _NOEXCEPT;
1081 virtual bool do_always_noconv() const _NOEXCEPT;
1082 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
1083 virtual int do_max_length() const _NOEXCEPT;
1084};
1085
1086# endif
1087
1088// template <> class codecvt<char32_t, char, mbstate_t> // deprecated in C++20
1089
1090template <>
1091class _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_EXPORTED_FROM_ABI codecvt<char32_t, char, mbstate_t>
1092 : public locale::facet, public codecvt_base {
1093public:
1094 typedef char32_t intern_type;
1095 typedef char extern_type;
1096 typedef mbstate_t state_type;
1097
1098 _LIBCPP_HIDE_FROM_ABI explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
1099
1100 _LIBCPP_HIDE_FROM_ABI result
1101 out(state_type& __st,
1102 const intern_type* __frm,
1103 const intern_type* __frm_end,
1104 const intern_type*& __frm_nxt,
1105 extern_type* __to,
1106 extern_type* __to_end,
1107 extern_type*& __to_nxt) const {
1108 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1109 }
1110
1111 _LIBCPP_HIDE_FROM_ABI result
1112 unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
1113 return do_unshift(__st, __to, __to_end, __to_nxt);
1114 }
1115
1116 _LIBCPP_HIDE_FROM_ABI result
1117 in(state_type& __st,
1118 const extern_type* __frm,
1119 const extern_type* __frm_end,
1120 const extern_type*& __frm_nxt,
1121 intern_type* __to,
1122 intern_type* __to_end,
1123 intern_type*& __to_nxt) const {
1124 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1125 }
1126
1127 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
1128
1129 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
1130
1131 _LIBCPP_HIDE_FROM_ABI int
1132 length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
1133 return do_length(__st, __frm, __end, __mx);
1134 }
1135
1136 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
1137
1138 static locale::id id;
1139
1140protected:
1141 _LIBCPP_HIDE_FROM_ABI explicit codecvt(const char*, size_t __refs = 0) : locale::facet(__refs) {}
1142
1143 ~codecvt() override;
1144
1145 virtual result
1146 do_out(state_type& __st,
1147 const intern_type* __frm,
1148 const intern_type* __frm_end,
1149 const intern_type*& __frm_nxt,
1150 extern_type* __to,
1151 extern_type* __to_end,
1152 extern_type*& __to_nxt) const;
1153 virtual result
1154 do_in(state_type& __st,
1155 const extern_type* __frm,
1156 const extern_type* __frm_end,
1157 const extern_type*& __frm_nxt,
1158 intern_type* __to,
1159 intern_type* __to_end,
1160 intern_type*& __to_nxt) const;
1161 virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1162 virtual int do_encoding() const _NOEXCEPT;
1163 virtual bool do_always_noconv() const _NOEXCEPT;
1164 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
1165 virtual int do_max_length() const _NOEXCEPT;
1166};
1167
1168# if _LIBCPP_HAS_CHAR8_T
1169
1170// template <> class codecvt<char32_t, char8_t, mbstate_t> // C++20, deprecated in C++20
1171
1172template <>
1173class _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_EXPORTED_FROM_ABI codecvt<char32_t, char8_t, mbstate_t>
1174 : public locale::facet, public codecvt_base {
1175public:
1176 typedef char32_t intern_type;
1177 typedef char8_t extern_type;
1178 typedef mbstate_t state_type;
1179
1180 _LIBCPP_HIDE_FROM_ABI explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
1181
1182 _LIBCPP_HIDE_FROM_ABI result
1183 out(state_type& __st,
1184 const intern_type* __frm,
1185 const intern_type* __frm_end,
1186 const intern_type*& __frm_nxt,
1187 extern_type* __to,
1188 extern_type* __to_end,
1189 extern_type*& __to_nxt) const {
1190 return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1191 }
1192
1193 _LIBCPP_HIDE_FROM_ABI result
1194 unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
1195 return do_unshift(__st, __to, __to_end, __to_nxt);
1196 }
1197
1198 _LIBCPP_HIDE_FROM_ABI result
1199 in(state_type& __st,
1200 const extern_type* __frm,
1201 const extern_type* __frm_end,
1202 const extern_type*& __frm_nxt,
1203 intern_type* __to,
1204 intern_type* __to_end,
1205 intern_type*& __to_nxt) const {
1206 return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1207 }
1208
1209 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
1210
1211 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
1212
1213 _LIBCPP_HIDE_FROM_ABI int
1214 length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
1215 return do_length(__st, __frm, __end, __mx);
1216 }
1217
1218 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
1219
1220 static locale::id id;
1221
1222protected:
1223 _LIBCPP_HIDE_FROM_ABI explicit codecvt(const char*, size_t __refs = 0) : locale::facet(__refs) {}
1224
1225 ~codecvt() override;
1226
1227 virtual result
1228 do_out(state_type& __st,
1229 const intern_type* __frm,
1230 const intern_type* __frm_end,
1231 const intern_type*& __frm_nxt,
1232 extern_type* __to,
1233 extern_type* __to_end,
1234 extern_type*& __to_nxt) const;
1235 virtual result
1236 do_in(state_type& __st,
1237 const extern_type* __frm,
1238 const extern_type* __frm_end,
1239 const extern_type*& __frm_nxt,
1240 intern_type* __to,
1241 intern_type* __to_end,
1242 intern_type*& __to_nxt) const;
1243 virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1244 virtual int do_encoding() const _NOEXCEPT;
1245 virtual bool do_always_noconv() const _NOEXCEPT;
1246 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
1247 virtual int do_max_length() const _NOEXCEPT;
1248};
1249
1250# endif
1251
1252// template <class _InternT, class _ExternT, class _StateT> class codecvt_byname
1253
1254template <class _InternT, class _ExternT, class _StateT>
1255class codecvt_byname : public codecvt<_InternT, _ExternT, _StateT> {
1256public:
1257 _LIBCPP_HIDE_FROM_ABI explicit codecvt_byname(const char* __nm, size_t __refs = 0)
1258 : codecvt<_InternT, _ExternT, _StateT>(__nm, __refs) {}
1259 _LIBCPP_HIDE_FROM_ABI explicit codecvt_byname(const string& __nm, size_t __refs = 0)
1260 : codecvt<_InternT, _ExternT, _StateT>(__nm.c_str(), __refs) {}
1261
1262protected:
1263 ~codecvt_byname() override;
1264};
1265
1266_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1267template <class _InternT, class _ExternT, class _StateT>
1268codecvt_byname<_InternT, _ExternT, _StateT>::~codecvt_byname() {}
1269_LIBCPP_SUPPRESS_DEPRECATED_POP
1270
1271extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char, char, mbstate_t>;
1272# if _LIBCPP_HAS_WIDE_CHARACTERS
1273extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<wchar_t, char, mbstate_t>;
1274# endif
1275extern template class _LIBCPP_DEPRECATED_IN_CXX20
1276_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char16_t, char, mbstate_t>; // deprecated in C++20
1277extern template class _LIBCPP_DEPRECATED_IN_CXX20
1278_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char32_t, char, mbstate_t>; // deprecated in C++20
1279# if _LIBCPP_HAS_CHAR8_T
1280extern template class _LIBCPP_DEPRECATED_IN_CXX20
1281_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char16_t, char8_t, mbstate_t>; // C++20, deprecated in C++20
1282extern template class _LIBCPP_DEPRECATED_IN_CXX20
1283_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char32_t, char8_t, mbstate_t>; // C++20, deprecated in C++20
1284# endif
1285
1286template <size_t _Np>
1287struct __narrow_to_utf8 {
1288 template <class _OutputIterator, class _CharT>
1289 _OutputIterator operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const;
1290};
1291
1292template <>
1293struct __narrow_to_utf8<8> {
1294 template <class _OutputIterator, class _CharT>
1295 _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const {
1296 for (; __wb < __we; ++__wb, ++__s)
1297 *__s = *__wb;
1298 return __s;
1299 }
1300};
1301
1302_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1303template <>
1304struct _LIBCPP_EXPORTED_FROM_ABI __narrow_to_utf8<16> : public codecvt<char16_t, char, mbstate_t> {
1305 _LIBCPP_HIDE_FROM_ABI __narrow_to_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1306 _LIBCPP_SUPPRESS_DEPRECATED_POP
1307
1308 ~__narrow_to_utf8() override;
1309
1310 template <class _OutputIterator, class _CharT>
1311 _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const {
1312 result __r = ok;
1313 mbstate_t __mb;
1314 while (__wb < __we && __r != error) {
1315 const int __sz = 32;
1316 char __buf[__sz];
1317 char* __bn;
1318 const char16_t* __wn = (const char16_t*)__wb;
1319 __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);
1320 if (__r == codecvt_base::error || __wn == (const char16_t*)__wb)
1321 std::__throw_runtime_error("locale not supported");
1322 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1323 *__s = *__p;
1324 __wb = (const _CharT*)__wn;
1325 }
1326 return __s;
1327 }
1328};
1329
1330_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1331template <>
1332struct _LIBCPP_EXPORTED_FROM_ABI __narrow_to_utf8<32> : public codecvt<char32_t, char, mbstate_t> {
1333 _LIBCPP_HIDE_FROM_ABI __narrow_to_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1334 _LIBCPP_SUPPRESS_DEPRECATED_POP
1335
1336 ~__narrow_to_utf8() override;
1337
1338 template <class _OutputIterator, class _CharT>
1339 _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const {
1340 result __r = ok;
1341 mbstate_t __mb;
1342 while (__wb < __we && __r != error) {
1343 const int __sz = 32;
1344 char __buf[__sz];
1345 char* __bn;
1346 const char32_t* __wn = (const char32_t*)__wb;
1347 __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);
1348 if (__r == codecvt_base::error || __wn == (const char32_t*)__wb)
1349 std::__throw_runtime_error("locale not supported");
1350 for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1351 *__s = *__p;
1352 __wb = (const _CharT*)__wn;
1353 }
1354 return __s;
1355 }
1356};
1357
1358template <size_t _Np>
1359struct __widen_from_utf8 {
1360 template <class _OutputIterator>
1361 _OutputIterator operator()(_OutputIterator __s, const char* __nb, const char* __ne) const;
1362};
1363
1364template <>
1365struct __widen_from_utf8<8> {
1366 template <class _OutputIterator>
1367 _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const char* __nb, const char* __ne) const {
1368 for (; __nb < __ne; ++__nb, ++__s)
1369 *__s = *__nb;
1370 return __s;
1371 }
1372};
1373
1374_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1375template <>
1376struct _LIBCPP_EXPORTED_FROM_ABI __widen_from_utf8<16> : public codecvt<char16_t, char, mbstate_t> {
1377 _LIBCPP_HIDE_FROM_ABI __widen_from_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1378 _LIBCPP_SUPPRESS_DEPRECATED_POP
1379
1380 ~__widen_from_utf8() override;
1381
1382 template <class _OutputIterator>
1383 _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const char* __nb, const char* __ne) const {
1384 result __r = ok;
1385 mbstate_t __mb;
1386 while (__nb < __ne && __r != error) {
1387 const int __sz = 32;
1388 char16_t __buf[__sz];
1389 char16_t* __bn;
1390 const char* __nn = __nb;
1391 __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);
1392 if (__r == codecvt_base::error || __nn == __nb)
1393 std::__throw_runtime_error("locale not supported");
1394 for (const char16_t* __p = __buf; __p < __bn; ++__p, ++__s)
1395 *__s = *__p;
1396 __nb = __nn;
1397 }
1398 return __s;
1399 }
1400};
1401
1402_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1403template <>
1404struct _LIBCPP_EXPORTED_FROM_ABI __widen_from_utf8<32> : public codecvt<char32_t, char, mbstate_t> {
1405 _LIBCPP_HIDE_FROM_ABI __widen_from_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1406 _LIBCPP_SUPPRESS_DEPRECATED_POP
1407
1408 ~__widen_from_utf8() override;
1409
1410 template <class _OutputIterator>
1411 _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const char* __nb, const char* __ne) const {
1412 result __r = ok;
1413 mbstate_t __mb;
1414 while (__nb < __ne && __r != error) {
1415 const int __sz = 32;
1416 char32_t __buf[__sz];
1417 char32_t* __bn;
1418 const char* __nn = __nb;
1419 __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);
1420 if (__r == codecvt_base::error || __nn == __nb)
1421 std::__throw_runtime_error("locale not supported");
1422 for (const char32_t* __p = __buf; __p < __bn; ++__p, ++__s)
1423 *__s = *__p;
1424 __nb = __nn;
1425 }
1426 return __s;
1427 }
1428};
1429
1430// template <class charT> class numpunct
1431
1432template <class _CharT>
1433class numpunct;
1434
1435template <>
1436class _LIBCPP_EXPORTED_FROM_ABI numpunct<char> : public locale::facet {
1437public:
1438 typedef char char_type;
1439 typedef basic_string<char_type> string_type;
1440
1441 explicit numpunct(size_t __refs = 0);
1442
1443 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type decimal_point() const { return do_decimal_point(); }
1444 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type thousands_sep() const { return do_thousands_sep(); }
1445 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI string grouping() const { return do_grouping(); }
1446 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI string_type truename() const { return do_truename(); }
1447 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI string_type falsename() const { return do_falsename(); }
1448
1449 static locale::id id;
1450
1451protected:
1452 ~numpunct() override;
1453 virtual char_type do_decimal_point() const;
1454 virtual char_type do_thousands_sep() const;
1455 virtual string do_grouping() const;
1456 virtual string_type do_truename() const;
1457 virtual string_type do_falsename() const;
1458
1459 char_type __decimal_point_;
1460 char_type __thousands_sep_;
1461 string __grouping_;
1462};
1463
1464# if _LIBCPP_HAS_WIDE_CHARACTERS
1465template <>
1466class _LIBCPP_EXPORTED_FROM_ABI numpunct<wchar_t> : public locale::facet {
1467public:
1468 typedef wchar_t char_type;
1469 typedef basic_string<char_type> string_type;
1470
1471 explicit numpunct(size_t __refs = 0);
1472
1473 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type decimal_point() const { return do_decimal_point(); }
1474 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type thousands_sep() const { return do_thousands_sep(); }
1475 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI string grouping() const { return do_grouping(); }
1476 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI string_type truename() const { return do_truename(); }
1477 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI string_type falsename() const { return do_falsename(); }
1478
1479 static locale::id id;
1480
1481protected:
1482 ~numpunct() override;
1483 virtual char_type do_decimal_point() const;
1484 virtual char_type do_thousands_sep() const;
1485 virtual string do_grouping() const;
1486 virtual string_type do_truename() const;
1487 virtual string_type do_falsename() const;
1488
1489 char_type __decimal_point_;
1490 char_type __thousands_sep_;
1491 string __grouping_;
1492};
1493# endif // _LIBCPP_HAS_WIDE_CHARACTERS
1494
1495// template <class charT> class numpunct_byname
1496
1497template <class _CharT>
1498class numpunct_byname;
1499
1500template <>
1501class _LIBCPP_EXPORTED_FROM_ABI numpunct_byname<char> : public numpunct<char> {
1502public:
1503 typedef char char_type;
1504 typedef basic_string<char_type> string_type;
1505
1506 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1507 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1508
1509protected:
1510 ~numpunct_byname() override;
1511};
1512
1513# if _LIBCPP_HAS_WIDE_CHARACTERS
1514template <>
1515class _LIBCPP_EXPORTED_FROM_ABI numpunct_byname<wchar_t> : public numpunct<wchar_t> {
1516public:
1517 typedef wchar_t char_type;
1518 typedef basic_string<char_type> string_type;
1519
1520 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1521 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1522
1523protected:
1524 ~numpunct_byname() override;
1525};
1526# endif // _LIBCPP_HAS_WIDE_CHARACTERS
1527
1528_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
1529_LIBCPP_END_NAMESPACE_STD
1530
1531#endif // _LIBCPP_HAS_LOCALIZATION
1532
1533#endif // _LIBCPP___LOCALE
1534