__locale 45 KB

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