__locale 57 KB

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