__locale 52 KB

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