__locale 52 KB

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