char_traits.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. // Character Traits for use by standard string and iostream -*- C++ -*-
  2. // Copyright (C) 1997-2021 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. #if __cplusplus > 201703L
  34. # include <compare>
  35. #endif
  36. #ifndef _GLIBCXX_ALWAYS_INLINE
  37. # define _GLIBCXX_ALWAYS_INLINE inline __attribute__((__always_inline__))
  38. #endif
  39. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  40. {
  41. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  42. /**
  43. * @brief Mapping from character type to associated types.
  44. *
  45. * @note This is an implementation class for the generic version
  46. * of char_traits. It defines int_type, off_type, pos_type, and
  47. * state_type. By default these are unsigned long, streamoff,
  48. * streampos, and mbstate_t. Users who need a different set of
  49. * types, but who don't need to change the definitions of any function
  50. * defined in char_traits, can specialize __gnu_cxx::_Char_types
  51. * while leaving __gnu_cxx::char_traits alone. */
  52. template<typename _CharT>
  53. struct _Char_types
  54. {
  55. typedef unsigned long int_type;
  56. typedef std::streampos pos_type;
  57. typedef std::streamoff off_type;
  58. typedef std::mbstate_t state_type;
  59. };
  60. /**
  61. * @brief Base class used to implement std::char_traits.
  62. *
  63. * @note For any given actual character type, this definition is
  64. * probably wrong. (Most of the member functions are likely to be
  65. * right, but the int_type and state_type typedefs, and the eof()
  66. * member function, are likely to be wrong.) The reason this class
  67. * exists is so users can specialize it. Classes in namespace std
  68. * may not be specialized for fundamental types, but classes in
  69. * namespace __gnu_cxx may be.
  70. *
  71. * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/strings.html#strings.string.character_types
  72. * for advice on how to make use of this class for @a unusual character
  73. * types. Also, check out include/ext/pod_char_traits.h.
  74. */
  75. template<typename _CharT>
  76. struct char_traits
  77. {
  78. typedef _CharT char_type;
  79. typedef typename _Char_types<_CharT>::int_type int_type;
  80. typedef typename _Char_types<_CharT>::pos_type pos_type;
  81. typedef typename _Char_types<_CharT>::off_type off_type;
  82. typedef typename _Char_types<_CharT>::state_type state_type;
  83. #if __cpp_lib_three_way_comparison
  84. using comparison_category = std::strong_ordering;
  85. #endif
  86. static _GLIBCXX14_CONSTEXPR void
  87. assign(char_type& __c1, const char_type& __c2)
  88. { __c1 = __c2; }
  89. static _GLIBCXX_CONSTEXPR bool
  90. eq(const char_type& __c1, const char_type& __c2)
  91. { return __c1 == __c2; }
  92. static _GLIBCXX_CONSTEXPR bool
  93. lt(const char_type& __c1, const char_type& __c2)
  94. { return __c1 < __c2; }
  95. static _GLIBCXX14_CONSTEXPR int
  96. compare(const char_type* __s1, const char_type* __s2, std::size_t __n);
  97. static _GLIBCXX14_CONSTEXPR std::size_t
  98. length(const char_type* __s);
  99. static _GLIBCXX14_CONSTEXPR const char_type*
  100. find(const char_type* __s, std::size_t __n, const char_type& __a);
  101. static _GLIBCXX20_CONSTEXPR char_type*
  102. move(char_type* __s1, const char_type* __s2, std::size_t __n);
  103. static _GLIBCXX20_CONSTEXPR char_type*
  104. copy(char_type* __s1, const char_type* __s2, std::size_t __n);
  105. static _GLIBCXX20_CONSTEXPR char_type*
  106. assign(char_type* __s, std::size_t __n, char_type __a);
  107. static _GLIBCXX_CONSTEXPR char_type
  108. to_char_type(const int_type& __c)
  109. { return static_cast<char_type>(__c); }
  110. static _GLIBCXX_CONSTEXPR int_type
  111. to_int_type(const char_type& __c)
  112. { return static_cast<int_type>(__c); }
  113. static _GLIBCXX_CONSTEXPR bool
  114. eq_int_type(const int_type& __c1, const int_type& __c2)
  115. { return __c1 == __c2; }
  116. static _GLIBCXX_CONSTEXPR int_type
  117. eof()
  118. { return static_cast<int_type>(_GLIBCXX_STDIO_EOF); }
  119. static _GLIBCXX_CONSTEXPR int_type
  120. not_eof(const int_type& __c)
  121. { return !eq_int_type(__c, eof()) ? __c : to_int_type(char_type()); }
  122. };
  123. template<typename _CharT>
  124. _GLIBCXX14_CONSTEXPR int
  125. char_traits<_CharT>::
  126. compare(const char_type* __s1, const char_type* __s2, std::size_t __n)
  127. {
  128. for (std::size_t __i = 0; __i < __n; ++__i)
  129. if (lt(__s1[__i], __s2[__i]))
  130. return -1;
  131. else if (lt(__s2[__i], __s1[__i]))
  132. return 1;
  133. return 0;
  134. }
  135. template<typename _CharT>
  136. _GLIBCXX14_CONSTEXPR std::size_t
  137. char_traits<_CharT>::
  138. length(const char_type* __p)
  139. {
  140. std::size_t __i = 0;
  141. while (!eq(__p[__i], char_type()))
  142. ++__i;
  143. return __i;
  144. }
  145. template<typename _CharT>
  146. _GLIBCXX14_CONSTEXPR const typename char_traits<_CharT>::char_type*
  147. char_traits<_CharT>::
  148. find(const char_type* __s, std::size_t __n, const char_type& __a)
  149. {
  150. for (std::size_t __i = 0; __i < __n; ++__i)
  151. if (eq(__s[__i], __a))
  152. return __s + __i;
  153. return 0;
  154. }
  155. template<typename _CharT>
  156. _GLIBCXX20_CONSTEXPR
  157. typename char_traits<_CharT>::char_type*
  158. char_traits<_CharT>::
  159. move(char_type* __s1, const char_type* __s2, std::size_t __n)
  160. {
  161. if (__n == 0)
  162. return __s1;
  163. #ifdef __cpp_lib_is_constant_evaluated
  164. if (std::is_constant_evaluated())
  165. {
  166. if (__s1 > __s2 && __s1 < __s2 + __n)
  167. std::copy_backward(__s2, __s2 + __n, __s1 + __n);
  168. else
  169. std::copy(__s2, __s2 + __n, __s1);
  170. return __s1;
  171. }
  172. #endif
  173. return static_cast<_CharT*>(__builtin_memmove(__s1, __s2,
  174. __n * sizeof(char_type)));
  175. }
  176. template<typename _CharT>
  177. _GLIBCXX20_CONSTEXPR
  178. typename char_traits<_CharT>::char_type*
  179. char_traits<_CharT>::
  180. copy(char_type* __s1, const char_type* __s2, std::size_t __n)
  181. {
  182. // NB: Inline std::copy so no recursive dependencies.
  183. std::copy(__s2, __s2 + __n, __s1);
  184. return __s1;
  185. }
  186. template<typename _CharT>
  187. _GLIBCXX20_CONSTEXPR
  188. typename char_traits<_CharT>::char_type*
  189. char_traits<_CharT>::
  190. assign(char_type* __s, std::size_t __n, char_type __a)
  191. {
  192. // NB: Inline std::fill_n so no recursive dependencies.
  193. std::fill_n(__s, __n, __a);
  194. return __s;
  195. }
  196. _GLIBCXX_END_NAMESPACE_VERSION
  197. } // namespace
  198. namespace std _GLIBCXX_VISIBILITY(default)
  199. {
  200. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  201. #if __cplusplus >= 201703L
  202. #if __cplusplus == 201703L
  203. // Unofficial macro indicating P0426R1 support
  204. # define __cpp_lib_constexpr_char_traits 201611L
  205. #else
  206. // Also support P1032R1 in C++20
  207. # define __cpp_lib_constexpr_char_traits 201811L
  208. #endif
  209. /**
  210. * @brief Determine whether the characters of a NULL-terminated
  211. * string are known at compile time.
  212. * @param __s The string.
  213. *
  214. * Assumes that _CharT is a built-in character type.
  215. */
  216. template<typename _CharT>
  217. static _GLIBCXX_ALWAYS_INLINE constexpr bool
  218. __constant_string_p(const _CharT* __s)
  219. {
  220. #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
  221. (void) __s;
  222. // In constexpr contexts all strings should be constant.
  223. return __builtin_is_constant_evaluated();
  224. #else
  225. while (__builtin_constant_p(*__s) && *__s)
  226. __s++;
  227. return __builtin_constant_p(*__s);
  228. #endif
  229. }
  230. /**
  231. * @brief Determine whether the characters of a character array are
  232. * known at compile time.
  233. * @param __a The character array.
  234. * @param __n Number of characters.
  235. *
  236. * Assumes that _CharT is a built-in character type.
  237. */
  238. template<typename _CharT>
  239. static _GLIBCXX_ALWAYS_INLINE constexpr bool
  240. __constant_char_array_p(const _CharT* __a, size_t __n)
  241. {
  242. #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
  243. (void) __a;
  244. (void) __n;
  245. // In constexpr contexts all character arrays should be constant.
  246. return __builtin_is_constant_evaluated();
  247. #else
  248. size_t __i = 0;
  249. while (__i < __n && __builtin_constant_p(__a[__i]))
  250. __i++;
  251. return __i == __n;
  252. #endif
  253. }
  254. #endif
  255. // 21.1
  256. /**
  257. * @brief Basis for explicit traits specializations.
  258. *
  259. * @note For any given actual character type, this definition is
  260. * probably wrong. Since this is just a thin wrapper around
  261. * __gnu_cxx::char_traits, it is possible to achieve a more
  262. * appropriate definition by specializing __gnu_cxx::char_traits.
  263. *
  264. * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/strings.html#strings.string.character_types
  265. * for advice on how to make use of this class for @a unusual character
  266. * types. Also, check out include/ext/pod_char_traits.h.
  267. */
  268. template<class _CharT>
  269. struct char_traits : public __gnu_cxx::char_traits<_CharT>
  270. { };
  271. /// 21.1.3.1 char_traits specializations
  272. template<>
  273. struct char_traits<char>
  274. {
  275. typedef char char_type;
  276. typedef int int_type;
  277. typedef streampos pos_type;
  278. typedef streamoff off_type;
  279. typedef mbstate_t state_type;
  280. #if __cpp_lib_three_way_comparison
  281. using comparison_category = strong_ordering;
  282. #endif
  283. static _GLIBCXX17_CONSTEXPR void
  284. assign(char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
  285. { __c1 = __c2; }
  286. static _GLIBCXX_CONSTEXPR bool
  287. eq(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
  288. { return __c1 == __c2; }
  289. static _GLIBCXX_CONSTEXPR bool
  290. lt(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
  291. {
  292. // LWG 467.
  293. return (static_cast<unsigned char>(__c1)
  294. < static_cast<unsigned char>(__c2));
  295. }
  296. static _GLIBCXX17_CONSTEXPR int
  297. compare(const char_type* __s1, const char_type* __s2, size_t __n)
  298. {
  299. if (__n == 0)
  300. return 0;
  301. #if __cplusplus >= 201703L
  302. if (__builtin_constant_p(__n)
  303. && __constant_char_array_p(__s1, __n)
  304. && __constant_char_array_p(__s2, __n))
  305. {
  306. for (size_t __i = 0; __i < __n; ++__i)
  307. if (lt(__s1[__i], __s2[__i]))
  308. return -1;
  309. else if (lt(__s2[__i], __s1[__i]))
  310. return 1;
  311. return 0;
  312. }
  313. #endif
  314. return __builtin_memcmp(__s1, __s2, __n);
  315. }
  316. static _GLIBCXX17_CONSTEXPR size_t
  317. length(const char_type* __s)
  318. {
  319. #if __cplusplus >= 201703L
  320. if (__constant_string_p(__s))
  321. return __gnu_cxx::char_traits<char_type>::length(__s);
  322. #endif
  323. return __builtin_strlen(__s);
  324. }
  325. static _GLIBCXX17_CONSTEXPR const char_type*
  326. find(const char_type* __s, size_t __n, const char_type& __a)
  327. {
  328. if (__n == 0)
  329. return 0;
  330. #if __cplusplus >= 201703L
  331. if (__builtin_constant_p(__n)
  332. && __builtin_constant_p(__a)
  333. && __constant_char_array_p(__s, __n))
  334. return __gnu_cxx::char_traits<char_type>::find(__s, __n, __a);
  335. #endif
  336. return static_cast<const char_type*>(__builtin_memchr(__s, __a, __n));
  337. }
  338. static _GLIBCXX20_CONSTEXPR char_type*
  339. move(char_type* __s1, const char_type* __s2, size_t __n)
  340. {
  341. if (__n == 0)
  342. return __s1;
  343. #ifdef __cpp_lib_is_constant_evaluated
  344. if (std::is_constant_evaluated())
  345. return __gnu_cxx::char_traits<char_type>::move(__s1, __s2, __n);
  346. #endif
  347. return static_cast<char_type*>(__builtin_memmove(__s1, __s2, __n));
  348. }
  349. static _GLIBCXX20_CONSTEXPR char_type*
  350. copy(char_type* __s1, const char_type* __s2, size_t __n)
  351. {
  352. if (__n == 0)
  353. return __s1;
  354. #ifdef __cpp_lib_is_constant_evaluated
  355. if (std::is_constant_evaluated())
  356. return __gnu_cxx::char_traits<char_type>::copy(__s1, __s2, __n);
  357. #endif
  358. return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n));
  359. }
  360. static _GLIBCXX20_CONSTEXPR char_type*
  361. assign(char_type* __s, size_t __n, char_type __a)
  362. {
  363. if (__n == 0)
  364. return __s;
  365. #ifdef __cpp_lib_is_constant_evaluated
  366. if (std::is_constant_evaluated())
  367. return __gnu_cxx::char_traits<char_type>::assign(__s, __n, __a);
  368. #endif
  369. return static_cast<char_type*>(__builtin_memset(__s, __a, __n));
  370. }
  371. static _GLIBCXX_CONSTEXPR char_type
  372. to_char_type(const int_type& __c) _GLIBCXX_NOEXCEPT
  373. { return static_cast<char_type>(__c); }
  374. // To keep both the byte 0xff and the eof symbol 0xffffffff
  375. // from ending up as 0xffffffff.
  376. static _GLIBCXX_CONSTEXPR int_type
  377. to_int_type(const char_type& __c) _GLIBCXX_NOEXCEPT
  378. { return static_cast<int_type>(static_cast<unsigned char>(__c)); }
  379. static _GLIBCXX_CONSTEXPR bool
  380. eq_int_type(const int_type& __c1, const int_type& __c2) _GLIBCXX_NOEXCEPT
  381. { return __c1 == __c2; }
  382. static _GLIBCXX_CONSTEXPR int_type
  383. eof() _GLIBCXX_NOEXCEPT
  384. { return static_cast<int_type>(_GLIBCXX_STDIO_EOF); }
  385. static _GLIBCXX_CONSTEXPR int_type
  386. not_eof(const int_type& __c) _GLIBCXX_NOEXCEPT
  387. { return (__c == eof()) ? 0 : __c; }
  388. };
  389. #ifdef _GLIBCXX_USE_WCHAR_T
  390. /// 21.1.3.2 char_traits specializations
  391. template<>
  392. struct char_traits<wchar_t>
  393. {
  394. typedef wchar_t char_type;
  395. typedef wint_t int_type;
  396. typedef streamoff off_type;
  397. typedef wstreampos pos_type;
  398. typedef mbstate_t state_type;
  399. #if __cpp_lib_three_way_comparison
  400. using comparison_category = strong_ordering;
  401. #endif
  402. static _GLIBCXX17_CONSTEXPR void
  403. assign(char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
  404. { __c1 = __c2; }
  405. static _GLIBCXX_CONSTEXPR bool
  406. eq(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
  407. { return __c1 == __c2; }
  408. static _GLIBCXX_CONSTEXPR bool
  409. lt(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
  410. { return __c1 < __c2; }
  411. static _GLIBCXX17_CONSTEXPR int
  412. compare(const char_type* __s1, const char_type* __s2, size_t __n)
  413. {
  414. if (__n == 0)
  415. return 0;
  416. #if __cplusplus >= 201703L
  417. if (__builtin_constant_p(__n)
  418. && __constant_char_array_p(__s1, __n)
  419. && __constant_char_array_p(__s2, __n))
  420. return __gnu_cxx::char_traits<char_type>::compare(__s1, __s2, __n);
  421. #endif
  422. return wmemcmp(__s1, __s2, __n);
  423. }
  424. static _GLIBCXX17_CONSTEXPR size_t
  425. length(const char_type* __s)
  426. {
  427. #if __cplusplus >= 201703L
  428. if (__constant_string_p(__s))
  429. return __gnu_cxx::char_traits<char_type>::length(__s);
  430. #endif
  431. return wcslen(__s);
  432. }
  433. static _GLIBCXX17_CONSTEXPR const char_type*
  434. find(const char_type* __s, size_t __n, const char_type& __a)
  435. {
  436. if (__n == 0)
  437. return 0;
  438. #if __cplusplus >= 201703L
  439. if (__builtin_constant_p(__n)
  440. && __builtin_constant_p(__a)
  441. && __constant_char_array_p(__s, __n))
  442. return __gnu_cxx::char_traits<char_type>::find(__s, __n, __a);
  443. #endif
  444. return wmemchr(__s, __a, __n);
  445. }
  446. static _GLIBCXX20_CONSTEXPR char_type*
  447. move(char_type* __s1, const char_type* __s2, size_t __n)
  448. {
  449. if (__n == 0)
  450. return __s1;
  451. #ifdef __cpp_lib_is_constant_evaluated
  452. if (std::is_constant_evaluated())
  453. return __gnu_cxx::char_traits<char_type>::move(__s1, __s2, __n);
  454. #endif
  455. return wmemmove(__s1, __s2, __n);
  456. }
  457. static _GLIBCXX20_CONSTEXPR char_type*
  458. copy(char_type* __s1, const char_type* __s2, size_t __n)
  459. {
  460. if (__n == 0)
  461. return __s1;
  462. #ifdef __cpp_lib_is_constant_evaluated
  463. if (std::is_constant_evaluated())
  464. return __gnu_cxx::char_traits<char_type>::copy(__s1, __s2, __n);
  465. #endif
  466. return wmemcpy(__s1, __s2, __n);
  467. }
  468. static _GLIBCXX20_CONSTEXPR char_type*
  469. assign(char_type* __s, size_t __n, char_type __a)
  470. {
  471. if (__n == 0)
  472. return __s;
  473. #ifdef __cpp_lib_is_constant_evaluated
  474. if (std::is_constant_evaluated())
  475. return __gnu_cxx::char_traits<char_type>::assign(__s, __n, __a);
  476. #endif
  477. return wmemset(__s, __a, __n);
  478. }
  479. static _GLIBCXX_CONSTEXPR char_type
  480. to_char_type(const int_type& __c) _GLIBCXX_NOEXCEPT
  481. { return char_type(__c); }
  482. static _GLIBCXX_CONSTEXPR int_type
  483. to_int_type(const char_type& __c) _GLIBCXX_NOEXCEPT
  484. { return int_type(__c); }
  485. static _GLIBCXX_CONSTEXPR bool
  486. eq_int_type(const int_type& __c1, const int_type& __c2) _GLIBCXX_NOEXCEPT
  487. { return __c1 == __c2; }
  488. static _GLIBCXX_CONSTEXPR int_type
  489. eof() _GLIBCXX_NOEXCEPT
  490. { return static_cast<int_type>(WEOF); }
  491. static _GLIBCXX_CONSTEXPR int_type
  492. not_eof(const int_type& __c) _GLIBCXX_NOEXCEPT
  493. { return eq_int_type(__c, eof()) ? 0 : __c; }
  494. };
  495. #endif //_GLIBCXX_USE_WCHAR_T
  496. #ifdef _GLIBCXX_USE_CHAR8_T
  497. template<>
  498. struct char_traits<char8_t>
  499. {
  500. typedef char8_t char_type;
  501. typedef unsigned int int_type;
  502. typedef u8streampos pos_type;
  503. typedef streamoff off_type;
  504. typedef mbstate_t state_type;
  505. #if __cpp_lib_three_way_comparison
  506. using comparison_category = strong_ordering;
  507. #endif
  508. static _GLIBCXX17_CONSTEXPR void
  509. assign(char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
  510. { __c1 = __c2; }
  511. static _GLIBCXX_CONSTEXPR bool
  512. eq(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
  513. { return __c1 == __c2; }
  514. static _GLIBCXX_CONSTEXPR bool
  515. lt(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
  516. { return __c1 < __c2; }
  517. static _GLIBCXX17_CONSTEXPR int
  518. compare(const char_type* __s1, const char_type* __s2, size_t __n)
  519. {
  520. if (__n == 0)
  521. return 0;
  522. #if __cplusplus > 201402
  523. if (__builtin_constant_p(__n)
  524. && __constant_char_array_p(__s1, __n)
  525. && __constant_char_array_p(__s2, __n))
  526. return __gnu_cxx::char_traits<char_type>::compare(__s1, __s2, __n);
  527. #endif
  528. return __builtin_memcmp(__s1, __s2, __n);
  529. }
  530. static _GLIBCXX17_CONSTEXPR size_t
  531. length(const char_type* __s)
  532. {
  533. #if __cplusplus > 201402
  534. if (__constant_string_p(__s))
  535. return __gnu_cxx::char_traits<char_type>::length(__s);
  536. #endif
  537. size_t __i = 0;
  538. while (!eq(__s[__i], char_type()))
  539. ++__i;
  540. return __i;
  541. }
  542. static _GLIBCXX17_CONSTEXPR const char_type*
  543. find(const char_type* __s, size_t __n, const char_type& __a)
  544. {
  545. if (__n == 0)
  546. return 0;
  547. #if __cplusplus > 201402
  548. if (__builtin_constant_p(__n)
  549. && __builtin_constant_p(__a)
  550. && __constant_char_array_p(__s, __n))
  551. return __gnu_cxx::char_traits<char_type>::find(__s, __n, __a);
  552. #endif
  553. return static_cast<const char_type*>(__builtin_memchr(__s, __a, __n));
  554. }
  555. static _GLIBCXX20_CONSTEXPR char_type*
  556. move(char_type* __s1, const char_type* __s2, size_t __n)
  557. {
  558. if (__n == 0)
  559. return __s1;
  560. #ifdef __cpp_lib_is_constant_evaluated
  561. if (std::is_constant_evaluated())
  562. return __gnu_cxx::char_traits<char_type>::move(__s1, __s2, __n);
  563. #endif
  564. return static_cast<char_type*>(__builtin_memmove(__s1, __s2, __n));
  565. }
  566. static _GLIBCXX20_CONSTEXPR char_type*
  567. copy(char_type* __s1, const char_type* __s2, size_t __n)
  568. {
  569. if (__n == 0)
  570. return __s1;
  571. #ifdef __cpp_lib_is_constant_evaluated
  572. if (std::is_constant_evaluated())
  573. return __gnu_cxx::char_traits<char_type>::copy(__s1, __s2, __n);
  574. #endif
  575. return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n));
  576. }
  577. static _GLIBCXX20_CONSTEXPR char_type*
  578. assign(char_type* __s, size_t __n, char_type __a)
  579. {
  580. if (__n == 0)
  581. return __s;
  582. #ifdef __cpp_lib_is_constant_evaluated
  583. if (std::is_constant_evaluated())
  584. return __gnu_cxx::char_traits<char_type>::assign(__s, __n, __a);
  585. #endif
  586. return static_cast<char_type*>(__builtin_memset(__s, __a, __n));
  587. }
  588. static _GLIBCXX_CONSTEXPR char_type
  589. to_char_type(const int_type& __c) _GLIBCXX_NOEXCEPT
  590. { return char_type(__c); }
  591. static _GLIBCXX_CONSTEXPR int_type
  592. to_int_type(const char_type& __c) _GLIBCXX_NOEXCEPT
  593. { return int_type(__c); }
  594. static _GLIBCXX_CONSTEXPR bool
  595. eq_int_type(const int_type& __c1, const int_type& __c2) _GLIBCXX_NOEXCEPT
  596. { return __c1 == __c2; }
  597. static _GLIBCXX_CONSTEXPR int_type
  598. eof() _GLIBCXX_NOEXCEPT
  599. { return static_cast<int_type>(-1); }
  600. static _GLIBCXX_CONSTEXPR int_type
  601. not_eof(const int_type& __c) _GLIBCXX_NOEXCEPT
  602. { return eq_int_type(__c, eof()) ? 0 : __c; }
  603. };
  604. #endif //_GLIBCXX_USE_CHAR8_T
  605. _GLIBCXX_END_NAMESPACE_VERSION
  606. } // namespace
  607. #if __cplusplus >= 201103L
  608. #include <cstdint>
  609. namespace std _GLIBCXX_VISIBILITY(default)
  610. {
  611. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  612. template<>
  613. struct char_traits<char16_t>
  614. {
  615. typedef char16_t char_type;
  616. #ifdef _GLIBCXX_USE_C99_STDINT_TR1
  617. typedef uint_least16_t int_type;
  618. #elif defined __UINT_LEAST16_TYPE__
  619. typedef __UINT_LEAST16_TYPE__ int_type;
  620. #else
  621. typedef make_unsigned<char16_t>::type int_type;
  622. #endif
  623. typedef streamoff off_type;
  624. typedef u16streampos pos_type;
  625. typedef mbstate_t state_type;
  626. #if __cpp_lib_three_way_comparison
  627. using comparison_category = strong_ordering;
  628. #endif
  629. static _GLIBCXX17_CONSTEXPR void
  630. assign(char_type& __c1, const char_type& __c2) noexcept
  631. { __c1 = __c2; }
  632. static constexpr bool
  633. eq(const char_type& __c1, const char_type& __c2) noexcept
  634. { return __c1 == __c2; }
  635. static constexpr bool
  636. lt(const char_type& __c1, const char_type& __c2) noexcept
  637. { return __c1 < __c2; }
  638. static _GLIBCXX17_CONSTEXPR int
  639. compare(const char_type* __s1, const char_type* __s2, size_t __n)
  640. {
  641. for (size_t __i = 0; __i < __n; ++__i)
  642. if (lt(__s1[__i], __s2[__i]))
  643. return -1;
  644. else if (lt(__s2[__i], __s1[__i]))
  645. return 1;
  646. return 0;
  647. }
  648. static _GLIBCXX17_CONSTEXPR size_t
  649. length(const char_type* __s)
  650. {
  651. size_t __i = 0;
  652. while (!eq(__s[__i], char_type()))
  653. ++__i;
  654. return __i;
  655. }
  656. static _GLIBCXX17_CONSTEXPR const char_type*
  657. find(const char_type* __s, size_t __n, const char_type& __a)
  658. {
  659. for (size_t __i = 0; __i < __n; ++__i)
  660. if (eq(__s[__i], __a))
  661. return __s + __i;
  662. return 0;
  663. }
  664. static _GLIBCXX20_CONSTEXPR char_type*
  665. move(char_type* __s1, const char_type* __s2, size_t __n)
  666. {
  667. if (__n == 0)
  668. return __s1;
  669. #ifdef __cpp_lib_is_constant_evaluated
  670. if (std::is_constant_evaluated())
  671. return __gnu_cxx::char_traits<char_type>::move(__s1, __s2, __n);
  672. #endif
  673. return (static_cast<char_type*>
  674. (__builtin_memmove(__s1, __s2, __n * sizeof(char_type))));
  675. }
  676. static _GLIBCXX20_CONSTEXPR char_type*
  677. copy(char_type* __s1, const char_type* __s2, size_t __n)
  678. {
  679. if (__n == 0)
  680. return __s1;
  681. #ifdef __cpp_lib_is_constant_evaluated
  682. if (std::is_constant_evaluated())
  683. return __gnu_cxx::char_traits<char_type>::copy(__s1, __s2, __n);
  684. #endif
  685. return (static_cast<char_type*>
  686. (__builtin_memcpy(__s1, __s2, __n * sizeof(char_type))));
  687. }
  688. static _GLIBCXX20_CONSTEXPR 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 __c == eof() ? int_type(0xfffd) : 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. template<>
  712. struct char_traits<char32_t>
  713. {
  714. typedef char32_t char_type;
  715. #ifdef _GLIBCXX_USE_C99_STDINT_TR1
  716. typedef uint_least32_t int_type;
  717. #elif defined __UINT_LEAST32_TYPE__
  718. typedef __UINT_LEAST32_TYPE__ int_type;
  719. #else
  720. typedef make_unsigned<char32_t>::type int_type;
  721. #endif
  722. typedef streamoff off_type;
  723. typedef u32streampos pos_type;
  724. typedef mbstate_t state_type;
  725. #if __cpp_lib_three_way_comparison
  726. using comparison_category = strong_ordering;
  727. #endif
  728. static _GLIBCXX17_CONSTEXPR void
  729. assign(char_type& __c1, const char_type& __c2) noexcept
  730. { __c1 = __c2; }
  731. static constexpr bool
  732. eq(const char_type& __c1, const char_type& __c2) noexcept
  733. { return __c1 == __c2; }
  734. static constexpr bool
  735. lt(const char_type& __c1, const char_type& __c2) noexcept
  736. { return __c1 < __c2; }
  737. static _GLIBCXX17_CONSTEXPR int
  738. compare(const char_type* __s1, const char_type* __s2, size_t __n)
  739. {
  740. for (size_t __i = 0; __i < __n; ++__i)
  741. if (lt(__s1[__i], __s2[__i]))
  742. return -1;
  743. else if (lt(__s2[__i], __s1[__i]))
  744. return 1;
  745. return 0;
  746. }
  747. static _GLIBCXX17_CONSTEXPR size_t
  748. length(const char_type* __s)
  749. {
  750. size_t __i = 0;
  751. while (!eq(__s[__i], char_type()))
  752. ++__i;
  753. return __i;
  754. }
  755. static _GLIBCXX17_CONSTEXPR const char_type*
  756. find(const char_type* __s, size_t __n, const char_type& __a)
  757. {
  758. for (size_t __i = 0; __i < __n; ++__i)
  759. if (eq(__s[__i], __a))
  760. return __s + __i;
  761. return 0;
  762. }
  763. static _GLIBCXX20_CONSTEXPR char_type*
  764. move(char_type* __s1, const char_type* __s2, size_t __n)
  765. {
  766. if (__n == 0)
  767. return __s1;
  768. #ifdef __cpp_lib_is_constant_evaluated
  769. if (std::is_constant_evaluated())
  770. return __gnu_cxx::char_traits<char_type>::move(__s1, __s2, __n);
  771. #endif
  772. return (static_cast<char_type*>
  773. (__builtin_memmove(__s1, __s2, __n * sizeof(char_type))));
  774. }
  775. static _GLIBCXX20_CONSTEXPR char_type*
  776. copy(char_type* __s1, const char_type* __s2, size_t __n)
  777. {
  778. if (__n == 0)
  779. return __s1;
  780. #ifdef __cpp_lib_is_constant_evaluated
  781. if (std::is_constant_evaluated())
  782. return __gnu_cxx::char_traits<char_type>::copy(__s1, __s2, __n);
  783. #endif
  784. return (static_cast<char_type*>
  785. (__builtin_memcpy(__s1, __s2, __n * sizeof(char_type))));
  786. }
  787. static _GLIBCXX20_CONSTEXPR char_type*
  788. assign(char_type* __s, size_t __n, char_type __a)
  789. {
  790. for (size_t __i = 0; __i < __n; ++__i)
  791. assign(__s[__i], __a);
  792. return __s;
  793. }
  794. static constexpr char_type
  795. to_char_type(const int_type& __c) noexcept
  796. { return char_type(__c); }
  797. static constexpr int_type
  798. to_int_type(const char_type& __c) noexcept
  799. { return int_type(__c); }
  800. static constexpr bool
  801. eq_int_type(const int_type& __c1, const int_type& __c2) noexcept
  802. { return __c1 == __c2; }
  803. static constexpr int_type
  804. eof() noexcept
  805. { return static_cast<int_type>(-1); }
  806. static constexpr int_type
  807. not_eof(const int_type& __c) noexcept
  808. { return eq_int_type(__c, eof()) ? 0 : __c; }
  809. };
  810. #if __cpp_lib_three_way_comparison
  811. namespace __detail
  812. {
  813. template<typename _ChTraits>
  814. constexpr auto
  815. __char_traits_cmp_cat(int __cmp) noexcept
  816. {
  817. if constexpr (requires { typename _ChTraits::comparison_category; })
  818. {
  819. using _Cat = typename _ChTraits::comparison_category;
  820. static_assert( !is_void_v<common_comparison_category_t<_Cat>> );
  821. return static_cast<_Cat>(__cmp <=> 0);
  822. }
  823. else
  824. return static_cast<weak_ordering>(__cmp <=> 0);
  825. }
  826. } // namespace __detail
  827. #endif // C++20
  828. _GLIBCXX_END_NAMESPACE_VERSION
  829. } // namespace
  830. #endif // C++11
  831. #endif // _CHAR_TRAITS_H