__locale 57 KB

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