char_traits.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. // Character Traits for use by standard string and iostream -*- C++ -*-
  2. // Copyright (C) 1997-2019 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/char_traits.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{string}
  23. */
  24. //
  25. // ISO C++ 14882: 21 Strings library
  26. //
  27. #ifndef _CHAR_TRAITS_H
  28. #define _CHAR_TRAITS_H 1
  29. #pragma GCC system_header
  30. #include <bits/stl_algobase.h> // std::copy, std::fill_n
  31. #include <bits/postypes.h> // For streampos
  32. #include <cwchar> // For WEOF, wmemmove, wmemset, etc.
  33. #ifndef _GLIBCXX_ALWAYS_INLINE
  34. # define _GLIBCXX_ALWAYS_INLINE inline __attribute__((__always_inline__))
  35. #endif
  36. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  37. {
  38. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  39. /**
  40. * @brief Mapping from character type to associated types.
  41. *
  42. * @note This is an implementation class for the generic version
  43. * of char_traits. It defines int_type, off_type, pos_type, and
  44. * state_type. By default these are unsigned long, streamoff,
  45. * streampos, and mbstate_t. Users who need a different set of
  46. * types, but who don't need to change the definitions of any function
  47. * defined in char_traits, can specialize __gnu_cxx::_Char_types
  48. * while leaving __gnu_cxx::char_traits alone. */
  49. template<typename _CharT>
  50. struct _Char_types
  51. {
  52. typedef unsigned long int_type;
  53. typedef std::streampos pos_type;
  54. typedef std::streamoff off_type;
  55. typedef std::mbstate_t state_type;
  56. };
  57. /**
  58. * @brief Base class used to implement std::char_traits.
  59. *
  60. * @note For any given actual character type, this definition is
  61. * probably wrong. (Most of the member functions are likely to be
  62. * right, but the int_type and state_type typedefs, and the eof()
  63. * member function, are likely to be wrong.) The reason this class
  64. * exists is so users can specialize it. Classes in namespace std
  65. * may not be specialized for fundamental types, but classes in
  66. * namespace __gnu_cxx may be.
  67. *
  68. * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/strings.html#strings.string.character_types
  69. * for advice on how to make use of this class for @a unusual character
  70. * types. Also, check out include/ext/pod_char_traits.h.
  71. */
  72. template<typename _CharT>
  73. struct char_traits
  74. {
  75. typedef _CharT char_type;
  76. typedef typename _Char_types<_CharT>::int_type int_type;
  77. typedef typename _Char_types<_CharT>::pos_type pos_type;
  78. typedef typename _Char_types<_CharT>::off_type off_type;
  79. typedef typename _Char_types<_CharT>::state_type state_type;
  80. static _GLIBCXX14_CONSTEXPR void
  81. assign(char_type& __c1, const char_type& __c2)
  82. { __c1 = __c2; }
  83. static _GLIBCXX_CONSTEXPR bool
  84. eq(const char_type& __c1, const char_type& __c2)
  85. { return __c1 == __c2; }
  86. static _GLIBCXX_CONSTEXPR bool
  87. lt(const char_type& __c1, const char_type& __c2)
  88. { return __c1 < __c2; }
  89. static _GLIBCXX14_CONSTEXPR int
  90. compare(const char_type* __s1, const char_type* __s2, std::size_t __n);
  91. static _GLIBCXX14_CONSTEXPR std::size_t
  92. length(const char_type* __s);
  93. static _GLIBCXX14_CONSTEXPR const char_type*
  94. find(const char_type* __s, std::size_t __n, const char_type& __a);
  95. static char_type*
  96. move(char_type* __s1, const char_type* __s2, std::size_t __n);
  97. static char_type*
  98. copy(char_type* __s1, const char_type* __s2, std::size_t __n);
  99. static char_type*
  100. assign(char_type* __s, std::size_t __n, char_type __a);
  101. static _GLIBCXX_CONSTEXPR char_type
  102. to_char_type(const int_type& __c)
  103. { return static_cast<char_type>(__c); }
  104. static _GLIBCXX_CONSTEXPR int_type
  105. to_int_type(const char_type& __c)
  106. { return static_cast<int_type>(__c); }
  107. static _GLIBCXX_CONSTEXPR bool
  108. eq_int_type(const int_type& __c1, const int_type& __c2)
  109. { return __c1 == __c2; }
  110. static _GLIBCXX_CONSTEXPR int_type
  111. eof()
  112. { return static_cast<int_type>(_GLIBCXX_STDIO_EOF); }
  113. static _GLIBCXX_CONSTEXPR int_type
  114. not_eof(const int_type& __c)
  115. { return !eq_int_type(__c, eof()) ? __c : to_int_type(char_type()); }
  116. };
  117. template<typename _CharT>
  118. _GLIBCXX14_CONSTEXPR int
  119. char_traits<_CharT>::
  120. compare(const char_type* __s1, const char_type* __s2, std::size_t __n)
  121. {
  122. for (std::size_t __i = 0; __i < __n; ++__i)
  123. if (lt(__s1[__i], __s2[__i]))
  124. return -1;
  125. else if (lt(__s2[__i], __s1[__i]))
  126. return 1;
  127. return 0;
  128. }
  129. template<typename _CharT>
  130. _GLIBCXX14_CONSTEXPR std::size_t
  131. char_traits<_CharT>::
  132. length(const char_type* __p)
  133. {
  134. std::size_t __i = 0;
  135. while (!eq(__p[__i], char_type()))
  136. ++__i;
  137. return __i;
  138. }
  139. template<typename _CharT>
  140. _GLIBCXX14_CONSTEXPR const typename char_traits<_CharT>::char_type*
  141. char_traits<_CharT>::
  142. find(const char_type* __s, std::size_t __n, const char_type& __a)
  143. {
  144. for (std::size_t __i = 0; __i < __n; ++__i)
  145. if (eq(__s[__i], __a))
  146. return __s + __i;
  147. return 0;
  148. }
  149. template<typename _CharT>
  150. typename char_traits<_CharT>::char_type*
  151. char_traits<_CharT>::
  152. move(char_type* __s1, const char_type* __s2, std::size_t __n)
  153. {
  154. if (__n == 0)
  155. return __s1;
  156. return static_cast<_CharT*>(__builtin_memmove(__s1, __s2,
  157. __n * sizeof(char_type)));
  158. }
  159. template<typename _CharT>
  160. typename char_traits<_CharT>::char_type*
  161. char_traits<_CharT>::
  162. copy(char_type* __s1, const char_type* __s2, std::size_t __n)
  163. {
  164. // NB: Inline std::copy so no recursive dependencies.
  165. std::copy(__s2, __s2 + __n, __s1);
  166. return __s1;
  167. }
  168. template<typename _CharT>
  169. typename char_traits<_CharT>::char_type*
  170. char_traits<_CharT>::
  171. assign(char_type* __s, std::size_t __n, char_type __a)
  172. {
  173. // NB: Inline std::fill_n so no recursive dependencies.
  174. std::fill_n(__s, __n, __a);
  175. return __s;
  176. }
  177. _GLIBCXX_END_NAMESPACE_VERSION
  178. } // namespace
  179. namespace std _GLIBCXX_VISIBILITY(default)
  180. {
  181. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  182. #if __cplusplus >= 201703L
  183. #define __cpp_lib_constexpr_char_traits 201611
  184. /**
  185. * @brief Determine whether the characters of a NULL-terminated
  186. * string are known at compile time.
  187. * @param __s The string.
  188. *
  189. * Assumes that _CharT is a built-in character type.
  190. */
  191. template<typename _CharT>
  192. static _GLIBCXX_ALWAYS_INLINE constexpr bool
  193. __constant_string_p(const _CharT* __s)
  194. {
  195. #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
  196. (void) __s;
  197. // In constexpr contexts all strings should be constant.
  198. return __builtin_is_constant_evaluated();
  199. #else
  200. while (__builtin_constant_p(*__s) && *__s)
  201. __s++;
  202. return __builtin_constant_p(*__s);
  203. #endif
  204. }
  205. /**
  206. * @brief Determine whether the characters of a character array are
  207. * known at compile time.
  208. * @param __a The character array.
  209. * @param __n Number of characters.
  210. *
  211. * Assumes that _CharT is a built-in character type.
  212. */
  213. template<typename _CharT>
  214. static _GLIBCXX_ALWAYS_INLINE constexpr bool
  215. __constant_char_array_p(const _CharT* __a, size_t __n)
  216. {
  217. #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
  218. (void) __a;
  219. (void) __n;
  220. // In constexpr contexts all character arrays should be constant.
  221. return __builtin_is_constant_evaluated();
  222. #else
  223. size_t __i = 0;
  224. while (__i < __n && __builtin_constant_p(__a[__i]))
  225. __i++;
  226. return __i == __n;
  227. #endif
  228. }
  229. #endif
  230. // 21.1
  231. /**
  232. * @brief Basis for explicit traits specializations.
  233. *
  234. * @note For any given actual character type, this definition is
  235. * probably wrong. Since this is just a thin wrapper around
  236. * __gnu_cxx::char_traits, it is possible to achieve a more
  237. * appropriate definition by specializing __gnu_cxx::char_traits.
  238. *
  239. * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/strings.html#strings.string.character_types
  240. * for advice on how to make use of this class for @a unusual character
  241. * types. Also, check out include/ext/pod_char_traits.h.
  242. */
  243. template<class _CharT>
  244. struct char_traits : public __gnu_cxx::char_traits<_CharT>
  245. { };
  246. /// 21.1.3.1 char_traits specializations
  247. template<>
  248. struct char_traits<char>
  249. {
  250. typedef char char_type;
  251. typedef int int_type;
  252. typedef streampos pos_type;
  253. typedef streamoff off_type;
  254. typedef mbstate_t state_type;
  255. static _GLIBCXX17_CONSTEXPR void
  256. assign(char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
  257. { __c1 = __c2; }
  258. static _GLIBCXX_CONSTEXPR bool
  259. eq(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
  260. { return __c1 == __c2; }
  261. static _GLIBCXX_CONSTEXPR bool
  262. lt(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
  263. {
  264. // LWG 467.
  265. return (static_cast<unsigned char>(__c1)
  266. < static_cast<unsigned char>(__c2));
  267. }
  268. static _GLIBCXX17_CONSTEXPR int
  269. compare(const char_type* __s1, const char_type* __s2, size_t __n)
  270. {
  271. if (__n == 0)
  272. return 0;
  273. #if __cplusplus >= 201703L
  274. if (__builtin_constant_p(__n)
  275. && __constant_char_array_p(__s1, __n)
  276. && __constant_char_array_p(__s2, __n))
  277. return __gnu_cxx::char_traits<char_type>::compare(__s1, __s2, __n);
  278. #endif
  279. return __builtin_memcmp(__s1, __s2, __n);
  280. }
  281. static _GLIBCXX17_CONSTEXPR size_t
  282. length(const char_type* __s)
  283. {
  284. #if __cplusplus >= 201703L
  285. if (__constant_string_p(__s))
  286. return __gnu_cxx::char_traits<char_type>::length(__s);
  287. #endif
  288. return __builtin_strlen(__s);
  289. }
  290. static _GLIBCXX17_CONSTEXPR const char_type*
  291. find(const char_type* __s, size_t __n, const char_type& __a)
  292. {
  293. if (__n == 0)
  294. return 0;
  295. #if __cplusplus >= 201703L
  296. if (__builtin_constant_p(__n)
  297. && __builtin_constant_p(__a)
  298. && __constant_char_array_p(__s, __n))
  299. return __gnu_cxx::char_traits<char_type>::find(__s, __n, __a);
  300. #endif
  301. return static_cast<const char_type*>(__builtin_memchr(__s, __a, __n));
  302. }
  303. static char_type*
  304. move(char_type* __s1, const char_type* __s2, size_t __n)
  305. {
  306. if (__n == 0)
  307. return __s1;
  308. return static_cast<char_type*>(__builtin_memmove(__s1, __s2, __n));
  309. }
  310. static char_type*
  311. copy(char_type* __s1, const char_type* __s2, size_t __n)
  312. {
  313. if (__n == 0)
  314. return __s1;
  315. return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n));
  316. }
  317. static char_type*
  318. assign(char_type* __s, size_t __n, char_type __a)
  319. {
  320. if (__n == 0)
  321. return __s;
  322. return static_cast<char_type*>(__builtin_memset(__s, __a, __n));
  323. }
  324. static _GLIBCXX_CONSTEXPR char_type
  325. to_char_type(const int_type& __c) _GLIBCXX_NOEXCEPT
  326. { return static_cast<char_type>(__c); }
  327. // To keep both the byte 0xff and the eof symbol 0xffffffff
  328. // from ending up as 0xffffffff.
  329. static _GLIBCXX_CONSTEXPR int_type
  330. to_int_type(const char_type& __c) _GLIBCXX_NOEXCEPT
  331. { return static_cast<int_type>(static_cast<unsigned char>(__c)); }
  332. static _GLIBCXX_CONSTEXPR bool
  333. eq_int_type(const int_type& __c1, const int_type& __c2) _GLIBCXX_NOEXCEPT
  334. { return __c1 == __c2; }
  335. static _GLIBCXX_CONSTEXPR int_type
  336. eof() _GLIBCXX_NOEXCEPT
  337. { return static_cast<int_type>(_GLIBCXX_STDIO_EOF); }
  338. static _GLIBCXX_CONSTEXPR int_type
  339. not_eof(const int_type& __c) _GLIBCXX_NOEXCEPT
  340. { return (__c == eof()) ? 0 : __c; }
  341. };
  342. #ifdef _GLIBCXX_USE_WCHAR_T
  343. /// 21.1.3.2 char_traits specializations
  344. template<>
  345. struct char_traits<wchar_t>
  346. {
  347. typedef wchar_t char_type;
  348. typedef wint_t int_type;
  349. typedef streamoff off_type;
  350. typedef wstreampos pos_type;
  351. typedef mbstate_t state_type;
  352. static _GLIBCXX17_CONSTEXPR void
  353. assign(char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
  354. { __c1 = __c2; }
  355. static _GLIBCXX_CONSTEXPR bool
  356. eq(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
  357. { return __c1 == __c2; }
  358. static _GLIBCXX_CONSTEXPR bool
  359. lt(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
  360. { return __c1 < __c2; }
  361. static _GLIBCXX17_CONSTEXPR int
  362. compare(const char_type* __s1, const char_type* __s2, size_t __n)
  363. {
  364. if (__n == 0)
  365. return 0;
  366. #if __cplusplus >= 201703L
  367. if (__builtin_constant_p(__n)
  368. && __constant_char_array_p(__s1, __n)
  369. && __constant_char_array_p(__s2, __n))
  370. return __gnu_cxx::char_traits<char_type>::compare(__s1, __s2, __n);
  371. #endif
  372. return wmemcmp(__s1, __s2, __n);
  373. }
  374. static _GLIBCXX17_CONSTEXPR size_t
  375. length(const char_type* __s)
  376. {
  377. #if __cplusplus >= 201703L
  378. if (__constant_string_p(__s))
  379. return __gnu_cxx::char_traits<char_type>::length(__s);
  380. #endif
  381. return wcslen(__s);
  382. }
  383. static _GLIBCXX17_CONSTEXPR const char_type*
  384. find(const char_type* __s, size_t __n, const char_type& __a)
  385. {
  386. if (__n == 0)
  387. return 0;
  388. #if __cplusplus >= 201703L
  389. if (__builtin_constant_p(__n)
  390. && __builtin_constant_p(__a)
  391. && __constant_char_array_p(__s, __n))
  392. return __gnu_cxx::char_traits<char_type>::find(__s, __n, __a);
  393. #endif
  394. return wmemchr(__s, __a, __n);
  395. }
  396. static char_type*
  397. move(char_type* __s1, const char_type* __s2, size_t __n)
  398. {
  399. if (__n == 0)
  400. return __s1;
  401. return wmemmove(__s1, __s2, __n);
  402. }
  403. static char_type*
  404. copy(char_type* __s1, const char_type* __s2, size_t __n)
  405. {
  406. if (__n == 0)
  407. return __s1;
  408. return wmemcpy(__s1, __s2, __n);
  409. }
  410. static char_type*
  411. assign(char_type* __s, size_t __n, char_type __a)
  412. {
  413. if (__n == 0)
  414. return __s;
  415. return wmemset(__s, __a, __n);
  416. }
  417. static _GLIBCXX_CONSTEXPR char_type
  418. to_char_type(const int_type& __c) _GLIBCXX_NOEXCEPT
  419. { return char_type(__c); }
  420. static _GLIBCXX_CONSTEXPR int_type
  421. to_int_type(const char_type& __c) _GLIBCXX_NOEXCEPT
  422. { return int_type(__c); }
  423. static _GLIBCXX_CONSTEXPR bool
  424. eq_int_type(const int_type& __c1, const int_type& __c2) _GLIBCXX_NOEXCEPT
  425. { return __c1 == __c2; }
  426. static _GLIBCXX_CONSTEXPR int_type
  427. eof() _GLIBCXX_NOEXCEPT
  428. { return static_cast<int_type>(WEOF); }
  429. static _GLIBCXX_CONSTEXPR int_type
  430. not_eof(const int_type& __c) _GLIBCXX_NOEXCEPT
  431. { return eq_int_type(__c, eof()) ? 0 : __c; }
  432. };
  433. #endif //_GLIBCXX_USE_WCHAR_T
  434. #ifdef _GLIBCXX_USE_CHAR8_T
  435. template<>
  436. struct char_traits<char8_t>
  437. {
  438. typedef char8_t char_type;
  439. typedef unsigned int int_type;
  440. typedef u8streampos pos_type;
  441. typedef streamoff off_type;
  442. typedef mbstate_t state_type;
  443. static _GLIBCXX17_CONSTEXPR void
  444. assign(char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
  445. { __c1 = __c2; }
  446. static _GLIBCXX_CONSTEXPR bool
  447. eq(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
  448. { return __c1 == __c2; }
  449. static _GLIBCXX_CONSTEXPR bool
  450. lt(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
  451. { return __c1 < __c2; }
  452. static _GLIBCXX17_CONSTEXPR int
  453. compare(const char_type* __s1, const char_type* __s2, size_t __n)
  454. {
  455. if (__n == 0)
  456. return 0;
  457. #if __cplusplus > 201402
  458. if (__builtin_constant_p(__n)
  459. && __constant_char_array_p(__s1, __n)
  460. && __constant_char_array_p(__s2, __n))
  461. return __gnu_cxx::char_traits<char_type>::compare(__s1, __s2, __n);
  462. #endif
  463. return __builtin_memcmp(__s1, __s2, __n);
  464. }
  465. static _GLIBCXX17_CONSTEXPR size_t
  466. length(const char_type* __s)
  467. {
  468. #if __cplusplus > 201402
  469. if (__constant_string_p(__s))
  470. return __gnu_cxx::char_traits<char_type>::length(__s);
  471. #endif
  472. size_t __i = 0;
  473. while (!eq(__s[__i], char_type()))
  474. ++__i;
  475. return __i;
  476. }
  477. static _GLIBCXX17_CONSTEXPR const char_type*
  478. find(const char_type* __s, size_t __n, const char_type& __a)
  479. {
  480. if (__n == 0)
  481. return 0;
  482. #if __cplusplus > 201402
  483. if (__builtin_constant_p(__n)
  484. && __builtin_constant_p(__a)
  485. && __constant_char_array_p(__s, __n))
  486. return __gnu_cxx::char_traits<char_type>::find(__s, __n, __a);
  487. #endif
  488. return static_cast<const char_type*>(__builtin_memchr(__s, __a, __n));
  489. }
  490. static char_type*
  491. move(char_type* __s1, const char_type* __s2, size_t __n)
  492. {
  493. if (__n == 0)
  494. return __s1;
  495. return static_cast<char_type*>(__builtin_memmove(__s1, __s2, __n));
  496. }
  497. static char_type*
  498. copy(char_type* __s1, const char_type* __s2, size_t __n)
  499. {
  500. if (__n == 0)
  501. return __s1;
  502. return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n));
  503. }
  504. static char_type*
  505. assign(char_type* __s, size_t __n, char_type __a)
  506. {
  507. if (__n == 0)
  508. return __s;
  509. return static_cast<char_type*>(__builtin_memset(__s, __a, __n));
  510. }
  511. static _GLIBCXX_CONSTEXPR char_type
  512. to_char_type(const int_type& __c) _GLIBCXX_NOEXCEPT
  513. { return char_type(__c); }
  514. static _GLIBCXX_CONSTEXPR int_type
  515. to_int_type(const char_type& __c) _GLIBCXX_NOEXCEPT
  516. { return int_type(__c); }
  517. static _GLIBCXX_CONSTEXPR bool
  518. eq_int_type(const int_type& __c1, const int_type& __c2) _GLIBCXX_NOEXCEPT
  519. { return __c1 == __c2; }
  520. static _GLIBCXX_CONSTEXPR int_type
  521. eof() _GLIBCXX_NOEXCEPT
  522. { return static_cast<int_type>(-1); }
  523. static _GLIBCXX_CONSTEXPR int_type
  524. not_eof(const int_type& __c) _GLIBCXX_NOEXCEPT
  525. { return eq_int_type(__c, eof()) ? 0 : __c; }
  526. };
  527. #endif //_GLIBCXX_USE_CHAR8_T
  528. _GLIBCXX_END_NAMESPACE_VERSION
  529. } // namespace
  530. #if __cplusplus >= 201103L
  531. #include <cstdint>
  532. namespace std _GLIBCXX_VISIBILITY(default)
  533. {
  534. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  535. template<>
  536. struct char_traits<char16_t>
  537. {
  538. typedef char16_t char_type;
  539. #ifdef _GLIBCXX_USE_C99_STDINT_TR1
  540. typedef uint_least16_t int_type;
  541. #elif defined __UINT_LEAST16_TYPE__
  542. typedef __UINT_LEAST16_TYPE__ int_type;
  543. #else
  544. typedef make_unsigned<char16_t>::type int_type;
  545. #endif
  546. typedef streamoff off_type;
  547. typedef u16streampos pos_type;
  548. typedef mbstate_t state_type;
  549. static _GLIBCXX17_CONSTEXPR void
  550. assign(char_type& __c1, const char_type& __c2) noexcept
  551. { __c1 = __c2; }
  552. static constexpr bool
  553. eq(const char_type& __c1, const char_type& __c2) noexcept
  554. { return __c1 == __c2; }
  555. static constexpr bool
  556. lt(const char_type& __c1, const char_type& __c2) noexcept
  557. { return __c1 < __c2; }
  558. static _GLIBCXX17_CONSTEXPR int
  559. compare(const char_type* __s1, const char_type* __s2, size_t __n)
  560. {
  561. for (size_t __i = 0; __i < __n; ++__i)
  562. if (lt(__s1[__i], __s2[__i]))
  563. return -1;
  564. else if (lt(__s2[__i], __s1[__i]))
  565. return 1;
  566. return 0;
  567. }
  568. static _GLIBCXX17_CONSTEXPR size_t
  569. length(const char_type* __s)
  570. {
  571. size_t __i = 0;
  572. while (!eq(__s[__i], char_type()))
  573. ++__i;
  574. return __i;
  575. }
  576. static _GLIBCXX17_CONSTEXPR const char_type*
  577. find(const char_type* __s, size_t __n, const char_type& __a)
  578. {
  579. for (size_t __i = 0; __i < __n; ++__i)
  580. if (eq(__s[__i], __a))
  581. return __s + __i;
  582. return 0;
  583. }
  584. static char_type*
  585. move(char_type* __s1, const char_type* __s2, size_t __n)
  586. {
  587. if (__n == 0)
  588. return __s1;
  589. return (static_cast<char_type*>
  590. (__builtin_memmove(__s1, __s2, __n * sizeof(char_type))));
  591. }
  592. static char_type*
  593. copy(char_type* __s1, const char_type* __s2, size_t __n)
  594. {
  595. if (__n == 0)
  596. return __s1;
  597. return (static_cast<char_type*>
  598. (__builtin_memcpy(__s1, __s2, __n * sizeof(char_type))));
  599. }
  600. static char_type*
  601. assign(char_type* __s, size_t __n, char_type __a)
  602. {
  603. for (size_t __i = 0; __i < __n; ++__i)
  604. assign(__s[__i], __a);
  605. return __s;
  606. }
  607. static constexpr char_type
  608. to_char_type(const int_type& __c) noexcept
  609. { return char_type(__c); }
  610. static constexpr int_type
  611. to_int_type(const char_type& __c) noexcept
  612. { return __c == eof() ? int_type(0xfffd) : int_type(__c); }
  613. static constexpr bool
  614. eq_int_type(const int_type& __c1, const int_type& __c2) noexcept
  615. { return __c1 == __c2; }
  616. static constexpr int_type
  617. eof() noexcept
  618. { return static_cast<int_type>(-1); }
  619. static constexpr int_type
  620. not_eof(const int_type& __c) noexcept
  621. { return eq_int_type(__c, eof()) ? 0 : __c; }
  622. };
  623. template<>
  624. struct char_traits<char32_t>
  625. {
  626. typedef char32_t char_type;
  627. #ifdef _GLIBCXX_USE_C99_STDINT_TR1
  628. typedef uint_least32_t int_type;
  629. #elif defined __UINT_LEAST32_TYPE__
  630. typedef __UINT_LEAST32_TYPE__ int_type;
  631. #else
  632. typedef make_unsigned<char32_t>::type int_type;
  633. #endif
  634. typedef streamoff off_type;
  635. typedef u32streampos pos_type;
  636. typedef mbstate_t state_type;
  637. static _GLIBCXX17_CONSTEXPR void
  638. assign(char_type& __c1, const char_type& __c2) noexcept
  639. { __c1 = __c2; }
  640. static constexpr bool
  641. eq(const char_type& __c1, const char_type& __c2) noexcept
  642. { return __c1 == __c2; }
  643. static constexpr bool
  644. lt(const char_type& __c1, const char_type& __c2) noexcept
  645. { return __c1 < __c2; }
  646. static _GLIBCXX17_CONSTEXPR int
  647. compare(const char_type* __s1, const char_type* __s2, size_t __n)
  648. {
  649. for (size_t __i = 0; __i < __n; ++__i)
  650. if (lt(__s1[__i], __s2[__i]))
  651. return -1;
  652. else if (lt(__s2[__i], __s1[__i]))
  653. return 1;
  654. return 0;
  655. }
  656. static _GLIBCXX17_CONSTEXPR size_t
  657. length(const char_type* __s)
  658. {
  659. size_t __i = 0;
  660. while (!eq(__s[__i], char_type()))
  661. ++__i;
  662. return __i;
  663. }
  664. static _GLIBCXX17_CONSTEXPR const char_type*
  665. find(const char_type* __s, size_t __n, const char_type& __a)
  666. {
  667. for (size_t __i = 0; __i < __n; ++__i)
  668. if (eq(__s[__i], __a))
  669. return __s + __i;
  670. return 0;
  671. }
  672. static char_type*
  673. move(char_type* __s1, const char_type* __s2, size_t __n)
  674. {
  675. if (__n == 0)
  676. return __s1;
  677. return (static_cast<char_type*>
  678. (__builtin_memmove(__s1, __s2, __n * sizeof(char_type))));
  679. }
  680. static char_type*
  681. copy(char_type* __s1, const char_type* __s2, size_t __n)
  682. {
  683. if (__n == 0)
  684. return __s1;
  685. return (static_cast<char_type*>
  686. (__builtin_memcpy(__s1, __s2, __n * sizeof(char_type))));
  687. }
  688. static char_type*
  689. assign(char_type* __s, size_t __n, char_type __a)
  690. {
  691. for (size_t __i = 0; __i < __n; ++__i)
  692. assign(__s[__i], __a);
  693. return __s;
  694. }
  695. static constexpr char_type
  696. to_char_type(const int_type& __c) noexcept
  697. { return char_type(__c); }
  698. static constexpr int_type
  699. to_int_type(const char_type& __c) noexcept
  700. { return int_type(__c); }
  701. static constexpr bool
  702. eq_int_type(const int_type& __c1, const int_type& __c2) noexcept
  703. { return __c1 == __c2; }
  704. static constexpr int_type
  705. eof() noexcept
  706. { return static_cast<int_type>(-1); }
  707. static constexpr int_type
  708. not_eof(const int_type& __c) noexcept
  709. { return eq_int_type(__c, eof()) ? 0 : __c; }
  710. };
  711. _GLIBCXX_END_NAMESPACE_VERSION
  712. } // namespace
  713. #endif // C++11
  714. #endif // _CHAR_TRAITS_H