__locale 57 KB

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