__locale 57 KB

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