char_traits.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___STRING_CHAR_TRAITS_H
  9. #define _LIBCPP___STRING_CHAR_TRAITS_H
  10. #include <__algorithm/copy_n.h>
  11. #include <__algorithm/fill_n.h>
  12. #include <__algorithm/find_end.h>
  13. #include <__algorithm/find_first_of.h>
  14. #include <__algorithm/min.h>
  15. #include <__compare/ordering.h>
  16. #include <__config>
  17. #include <__functional/hash.h>
  18. #include <__iterator/iterator_traits.h>
  19. #include <__type_traits/is_constant_evaluated.h>
  20. #include <cstddef>
  21. #include <cstdint>
  22. #include <cstdio>
  23. #include <cstring>
  24. #include <iosfwd>
  25. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  26. # include <cwchar> // for wmemcpy
  27. #endif
  28. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  29. # pragma GCC system_header
  30. #endif
  31. _LIBCPP_PUSH_MACROS
  32. #include <__undef_macros>
  33. _LIBCPP_BEGIN_NAMESPACE_STD
  34. template <class _CharT>
  35. struct char_traits;
  36. /*
  37. The Standard does not define the base template for char_traits because it is impossible to provide
  38. a correct definition for arbitrary character types. Instead, it requires implementations to provide
  39. specializations for predefined character types like `char`, `wchar_t` and others. We provide this as
  40. exposition-only to document what members a char_traits specialization should provide:
  41. {
  42. using char_type = _CharT;
  43. using int_type = ...;
  44. using off_type = ...;
  45. using pos_type = ...;
  46. using state_type = ...;
  47. static void assign(char_type&, const char_type&);
  48. static bool eq(char_type, char_type);
  49. static bool lt(char_type, char_type);
  50. static int compare(const char_type*, const char_type*, size_t);
  51. static size_t length(const char_type*);
  52. static const char_type* find(const char_type*, size_t, const char_type&);
  53. static char_type* move(char_type*, const char_type*, size_t);
  54. static char_type* copy(char_type*, const char_type*, size_t);
  55. static char_type* assign(char_type*, size_t, char_type);
  56. static int_type not_eof(int_type);
  57. static char_type to_char_type(int_type);
  58. static int_type to_int_type(char_type);
  59. static bool eq_int_type(int_type, int_type);
  60. static int_type eof();
  61. };
  62. */
  63. //
  64. // Temporary extension to provide a base template for std::char_traits.
  65. // TODO: Remove in LLVM 18.
  66. //
  67. template <class _CharT>
  68. struct _LIBCPP_DEPRECATED_("char_traits<T> for T not equal to char, wchar_t, char8_t, char16_t or char32_t is non-standard and is provided for a temporary period. It will be removed in LLVM 18, so please migrate off of it.")
  69. char_traits
  70. {
  71. using char_type = _CharT;
  72. using int_type = int;
  73. using off_type = streamoff;
  74. using pos_type = streampos;
  75. using state_type = mbstate_t;
  76. static inline void _LIBCPP_CONSTEXPR_SINCE_CXX17
  77. assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
  78. static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
  79. {return __c1 == __c2;}
  80. static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
  81. {return __c1 < __c2;}
  82. static _LIBCPP_CONSTEXPR_SINCE_CXX17
  83. int compare(const char_type* __s1, const char_type* __s2, size_t __n) {
  84. for (; __n; --__n, ++__s1, ++__s2)
  85. {
  86. if (lt(*__s1, *__s2))
  87. return -1;
  88. if (lt(*__s2, *__s1))
  89. return 1;
  90. }
  91. return 0;
  92. }
  93. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_SINCE_CXX17
  94. size_t length(const char_type* __s) {
  95. size_t __len = 0;
  96. for (; !eq(*__s, char_type(0)); ++__s)
  97. ++__len;
  98. return __len;
  99. }
  100. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_SINCE_CXX17
  101. const char_type* find(const char_type* __s, size_t __n, const char_type& __a) {
  102. for (; __n; --__n)
  103. {
  104. if (eq(*__s, __a))
  105. return __s;
  106. ++__s;
  107. }
  108. return nullptr;
  109. }
  110. static _LIBCPP_CONSTEXPR_SINCE_CXX20
  111. char_type* move(char_type* __s1, const char_type* __s2, size_t __n) {
  112. if (__n == 0) return __s1;
  113. char_type* __r = __s1;
  114. if (__s1 < __s2)
  115. {
  116. for (; __n; --__n, ++__s1, ++__s2)
  117. assign(*__s1, *__s2);
  118. }
  119. else if (__s2 < __s1)
  120. {
  121. __s1 += __n;
  122. __s2 += __n;
  123. for (; __n; --__n)
  124. assign(*--__s1, *--__s2);
  125. }
  126. return __r;
  127. }
  128. _LIBCPP_INLINE_VISIBILITY
  129. static _LIBCPP_CONSTEXPR_SINCE_CXX20
  130. char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) {
  131. if (!__libcpp_is_constant_evaluated()) {
  132. _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
  133. }
  134. char_type* __r = __s1;
  135. for (; __n; --__n, ++__s1, ++__s2)
  136. assign(*__s1, *__s2);
  137. return __r;
  138. }
  139. _LIBCPP_INLINE_VISIBILITY
  140. static _LIBCPP_CONSTEXPR_SINCE_CXX20
  141. char_type* assign(char_type* __s, size_t __n, char_type __a) {
  142. char_type* __r = __s;
  143. for (; __n; --__n, ++__s)
  144. assign(*__s, __a);
  145. return __r;
  146. }
  147. static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
  148. {return eq_int_type(__c, eof()) ? ~eof() : __c;}
  149. static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
  150. {return char_type(__c);}
  151. static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
  152. {return int_type(__c);}
  153. static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
  154. {return __c1 == __c2;}
  155. static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
  156. {return int_type(EOF);}
  157. };
  158. template <class _CharT>
  159. _LIBCPP_HIDE_FROM_ABI static inline _LIBCPP_CONSTEXPR_SINCE_CXX20
  160. _CharT* __char_traits_move(_CharT* __dest, const _CharT* __source, size_t __n) _NOEXCEPT
  161. {
  162. #ifdef _LIBCPP_COMPILER_GCC
  163. if (__libcpp_is_constant_evaluated()) {
  164. if (__n == 0)
  165. return __dest;
  166. _CharT* __allocation = new _CharT[__n];
  167. std::copy_n(__source, __n, __allocation);
  168. std::copy_n(static_cast<const _CharT*>(__allocation), __n, __dest);
  169. delete[] __allocation;
  170. return __dest;
  171. }
  172. #endif
  173. ::__builtin_memmove(__dest, __source, __n * sizeof(_CharT));
  174. return __dest;
  175. }
  176. // char_traits<char>
  177. template <>
  178. struct _LIBCPP_TEMPLATE_VIS char_traits<char>
  179. {
  180. using char_type = char;
  181. using int_type = int;
  182. using off_type = streamoff;
  183. using pos_type = streampos;
  184. using state_type = mbstate_t;
  185. #if _LIBCPP_STD_VER > 17
  186. using comparison_category = strong_ordering;
  187. #endif
  188. static inline _LIBCPP_CONSTEXPR_SINCE_CXX17
  189. void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
  190. static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
  191. {return __c1 == __c2;}
  192. static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
  193. {return (unsigned char)__c1 < (unsigned char)__c2;}
  194. static _LIBCPP_CONSTEXPR_SINCE_CXX17 int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
  195. if (__n == 0)
  196. return 0;
  197. return std::__constexpr_memcmp(__s1, __s2, __n);
  198. }
  199. static inline size_t _LIBCPP_CONSTEXPR_SINCE_CXX17 length(const char_type* __s) _NOEXCEPT {
  200. return std::__constexpr_strlen(__s);
  201. }
  202. static _LIBCPP_CONSTEXPR_SINCE_CXX17
  203. const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT {
  204. if (__n == 0)
  205. return nullptr;
  206. return std::__constexpr_char_memchr(__s, static_cast<int>(__a), __n);
  207. }
  208. static inline _LIBCPP_CONSTEXPR_SINCE_CXX20
  209. char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
  210. return std::__char_traits_move(__s1, __s2, __n);
  211. }
  212. static inline _LIBCPP_CONSTEXPR_SINCE_CXX20
  213. char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
  214. if (!__libcpp_is_constant_evaluated())
  215. _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
  216. std::copy_n(__s2, __n, __s1);
  217. return __s1;
  218. }
  219. static inline _LIBCPP_CONSTEXPR_SINCE_CXX20
  220. char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT {
  221. std::fill_n(__s, __n, __a);
  222. return __s;
  223. }
  224. static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
  225. {return eq_int_type(__c, eof()) ? ~eof() : __c;}
  226. static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
  227. {return char_type(__c);}
  228. static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
  229. {return int_type((unsigned char)__c);}
  230. static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
  231. {return __c1 == __c2;}
  232. static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
  233. {return int_type(EOF);}
  234. };
  235. // char_traits<wchar_t>
  236. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  237. template <>
  238. struct _LIBCPP_TEMPLATE_VIS char_traits<wchar_t>
  239. {
  240. using char_type = wchar_t;
  241. using int_type = wint_t;
  242. using off_type = streamoff;
  243. using pos_type = streampos;
  244. using state_type = mbstate_t;
  245. #if _LIBCPP_STD_VER > 17
  246. using comparison_category = strong_ordering;
  247. #endif
  248. static inline _LIBCPP_CONSTEXPR_SINCE_CXX17
  249. void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
  250. static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
  251. {return __c1 == __c2;}
  252. static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
  253. {return __c1 < __c2;}
  254. static _LIBCPP_CONSTEXPR_SINCE_CXX17 int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
  255. if (__n == 0)
  256. return 0;
  257. return std::__constexpr_wmemcmp(__s1, __s2, __n);
  258. }
  259. static _LIBCPP_CONSTEXPR_SINCE_CXX17 size_t length(const char_type* __s) _NOEXCEPT {
  260. return std::__constexpr_wcslen(__s);
  261. }
  262. static _LIBCPP_CONSTEXPR_SINCE_CXX17
  263. const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT {
  264. if (__n == 0)
  265. return nullptr;
  266. return std::__constexpr_wmemchr(__s, __a, __n);
  267. }
  268. static inline _LIBCPP_CONSTEXPR_SINCE_CXX20
  269. char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
  270. return std::__char_traits_move(__s1, __s2, __n);
  271. }
  272. static inline _LIBCPP_CONSTEXPR_SINCE_CXX20
  273. char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
  274. if (!__libcpp_is_constant_evaluated())
  275. _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
  276. std::copy_n(__s2, __n, __s1);
  277. return __s1;
  278. }
  279. static inline _LIBCPP_CONSTEXPR_SINCE_CXX20
  280. char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT {
  281. std::fill_n(__s, __n, __a);
  282. return __s;
  283. }
  284. static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
  285. {return eq_int_type(__c, eof()) ? ~eof() : __c;}
  286. static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
  287. {return char_type(__c);}
  288. static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
  289. {return int_type(__c);}
  290. static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
  291. {return __c1 == __c2;}
  292. static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
  293. {return int_type(WEOF);}
  294. };
  295. #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
  296. #ifndef _LIBCPP_HAS_NO_CHAR8_T
  297. template <>
  298. struct _LIBCPP_TEMPLATE_VIS char_traits<char8_t>
  299. {
  300. using char_type = char8_t;
  301. using int_type = unsigned int;
  302. using off_type = streamoff;
  303. using pos_type = u8streampos;
  304. using state_type = mbstate_t;
  305. #if _LIBCPP_STD_VER > 17
  306. using comparison_category = strong_ordering;
  307. #endif
  308. static inline constexpr void assign(char_type& __c1, const char_type& __c2) noexcept
  309. {__c1 = __c2;}
  310. static inline constexpr bool eq(char_type __c1, char_type __c2) noexcept
  311. {return __c1 == __c2;}
  312. static inline constexpr bool lt(char_type __c1, char_type __c2) noexcept
  313. {return __c1 < __c2;}
  314. static _LIBCPP_HIDE_FROM_ABI constexpr int
  315. compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
  316. return std::__constexpr_memcmp(__s1, __s2, __n);
  317. }
  318. static constexpr
  319. size_t length(const char_type* __s) _NOEXCEPT;
  320. _LIBCPP_INLINE_VISIBILITY static constexpr
  321. const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
  322. static _LIBCPP_CONSTEXPR_SINCE_CXX20
  323. char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
  324. return std::__char_traits_move(__s1, __s2, __n);
  325. }
  326. static _LIBCPP_CONSTEXPR_SINCE_CXX20
  327. char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
  328. if (!__libcpp_is_constant_evaluated())
  329. _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
  330. std::copy_n(__s2, __n, __s1);
  331. return __s1;
  332. }
  333. static _LIBCPP_CONSTEXPR_SINCE_CXX20
  334. char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT {
  335. std::fill_n(__s, __n, __a);
  336. return __s;
  337. }
  338. static inline constexpr int_type not_eof(int_type __c) noexcept
  339. {return eq_int_type(__c, eof()) ? ~eof() : __c;}
  340. static inline constexpr char_type to_char_type(int_type __c) noexcept
  341. {return char_type(__c);}
  342. static inline constexpr int_type to_int_type(char_type __c) noexcept
  343. {return int_type(__c);}
  344. static inline constexpr bool eq_int_type(int_type __c1, int_type __c2) noexcept
  345. {return __c1 == __c2;}
  346. static inline constexpr int_type eof() noexcept
  347. {return int_type(EOF);}
  348. };
  349. // TODO use '__builtin_strlen' if it ever supports char8_t ??
  350. inline constexpr
  351. size_t
  352. char_traits<char8_t>::length(const char_type* __s) _NOEXCEPT
  353. {
  354. size_t __len = 0;
  355. for (; !eq(*__s, char_type(0)); ++__s)
  356. ++__len;
  357. return __len;
  358. }
  359. // TODO use '__builtin_char_memchr' if it ever supports char8_t ??
  360. inline constexpr
  361. const char8_t*
  362. char_traits<char8_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
  363. {
  364. for (; __n; --__n)
  365. {
  366. if (eq(*__s, __a))
  367. return __s;
  368. ++__s;
  369. }
  370. return nullptr;
  371. }
  372. #endif // _LIBCPP_HAS_NO_CHAR8_T
  373. template <>
  374. struct _LIBCPP_TEMPLATE_VIS char_traits<char16_t>
  375. {
  376. using char_type = char16_t;
  377. using int_type = uint_least16_t;
  378. using off_type = streamoff;
  379. using pos_type = u16streampos;
  380. using state_type = mbstate_t;
  381. #if _LIBCPP_STD_VER > 17
  382. using comparison_category = strong_ordering;
  383. #endif
  384. static inline _LIBCPP_CONSTEXPR_SINCE_CXX17
  385. void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
  386. static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
  387. {return __c1 == __c2;}
  388. static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
  389. {return __c1 < __c2;}
  390. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_SINCE_CXX17
  391. int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
  392. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_SINCE_CXX17
  393. size_t length(const char_type* __s) _NOEXCEPT;
  394. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_SINCE_CXX17
  395. const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
  396. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  397. static char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
  398. return std::__char_traits_move(__s1, __s2, __n);
  399. }
  400. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  401. static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
  402. if (!__libcpp_is_constant_evaluated())
  403. _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
  404. std::copy_n(__s2, __n, __s1);
  405. return __s1;
  406. }
  407. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  408. static char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT {
  409. std::fill_n(__s, __n, __a);
  410. return __s;
  411. }
  412. static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
  413. {return eq_int_type(__c, eof()) ? ~eof() : __c;}
  414. static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
  415. {return char_type(__c);}
  416. static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
  417. {return int_type(__c);}
  418. static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
  419. {return __c1 == __c2;}
  420. static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
  421. {return int_type(0xFFFF);}
  422. };
  423. inline _LIBCPP_CONSTEXPR_SINCE_CXX17
  424. int
  425. char_traits<char16_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  426. {
  427. for (; __n; --__n, ++__s1, ++__s2)
  428. {
  429. if (lt(*__s1, *__s2))
  430. return -1;
  431. if (lt(*__s2, *__s1))
  432. return 1;
  433. }
  434. return 0;
  435. }
  436. inline _LIBCPP_CONSTEXPR_SINCE_CXX17
  437. size_t
  438. char_traits<char16_t>::length(const char_type* __s) _NOEXCEPT
  439. {
  440. size_t __len = 0;
  441. for (; !eq(*__s, char_type(0)); ++__s)
  442. ++__len;
  443. return __len;
  444. }
  445. inline _LIBCPP_CONSTEXPR_SINCE_CXX17
  446. const char16_t*
  447. char_traits<char16_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
  448. {
  449. for (; __n; --__n)
  450. {
  451. if (eq(*__s, __a))
  452. return __s;
  453. ++__s;
  454. }
  455. return nullptr;
  456. }
  457. template <>
  458. struct _LIBCPP_TEMPLATE_VIS char_traits<char32_t>
  459. {
  460. using char_type = char32_t;
  461. using int_type = uint_least32_t;
  462. using off_type = streamoff;
  463. using pos_type = u32streampos;
  464. using state_type = mbstate_t;
  465. #if _LIBCPP_STD_VER > 17
  466. using comparison_category = strong_ordering;
  467. #endif
  468. static inline _LIBCPP_CONSTEXPR_SINCE_CXX17
  469. void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
  470. static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
  471. {return __c1 == __c2;}
  472. static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
  473. {return __c1 < __c2;}
  474. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_SINCE_CXX17
  475. int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
  476. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_SINCE_CXX17
  477. size_t length(const char_type* __s) _NOEXCEPT;
  478. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_SINCE_CXX17
  479. const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
  480. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  481. static char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
  482. return std::__char_traits_move(__s1, __s2, __n);
  483. }
  484. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  485. static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
  486. std::copy_n(__s2, __n, __s1);
  487. return __s1;
  488. }
  489. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  490. static char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT {
  491. std::fill_n(__s, __n, __a);
  492. return __s;
  493. }
  494. static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
  495. {return eq_int_type(__c, eof()) ? ~eof() : __c;}
  496. static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
  497. {return char_type(__c);}
  498. static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
  499. {return int_type(__c);}
  500. static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
  501. {return __c1 == __c2;}
  502. static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
  503. {return int_type(0xFFFFFFFF);}
  504. };
  505. inline _LIBCPP_CONSTEXPR_SINCE_CXX17
  506. int
  507. char_traits<char32_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  508. {
  509. for (; __n; --__n, ++__s1, ++__s2)
  510. {
  511. if (lt(*__s1, *__s2))
  512. return -1;
  513. if (lt(*__s2, *__s1))
  514. return 1;
  515. }
  516. return 0;
  517. }
  518. inline _LIBCPP_CONSTEXPR_SINCE_CXX17
  519. size_t
  520. char_traits<char32_t>::length(const char_type* __s) _NOEXCEPT
  521. {
  522. size_t __len = 0;
  523. for (; !eq(*__s, char_type(0)); ++__s)
  524. ++__len;
  525. return __len;
  526. }
  527. inline _LIBCPP_CONSTEXPR_SINCE_CXX17
  528. const char32_t*
  529. char_traits<char32_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
  530. {
  531. for (; __n; --__n)
  532. {
  533. if (eq(*__s, __a))
  534. return __s;
  535. ++__s;
  536. }
  537. return nullptr;
  538. }
  539. // helper fns for basic_string and string_view
  540. // __str_find
  541. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  542. inline _SizeT _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  543. __str_find(const _CharT *__p, _SizeT __sz,
  544. _CharT __c, _SizeT __pos) _NOEXCEPT
  545. {
  546. if (__pos >= __sz)
  547. return __npos;
  548. const _CharT* __r = _Traits::find(__p + __pos, __sz - __pos, __c);
  549. if (__r == nullptr)
  550. return __npos;
  551. return static_cast<_SizeT>(__r - __p);
  552. }
  553. template <class _CharT, class _Traits>
  554. _LIBCPP_HIDE_FROM_ABI inline _LIBCPP_CONSTEXPR_SINCE_CXX14 const _CharT *
  555. __search_substring(const _CharT *__first1, const _CharT *__last1,
  556. const _CharT *__first2, const _CharT *__last2) _NOEXCEPT {
  557. // Take advantage of knowing source and pattern lengths.
  558. // Stop short when source is smaller than pattern.
  559. const ptrdiff_t __len2 = __last2 - __first2;
  560. if (__len2 == 0)
  561. return __first1;
  562. ptrdiff_t __len1 = __last1 - __first1;
  563. if (__len1 < __len2)
  564. return __last1;
  565. // First element of __first2 is loop invariant.
  566. _CharT __f2 = *__first2;
  567. while (true) {
  568. __len1 = __last1 - __first1;
  569. // Check whether __first1 still has at least __len2 bytes.
  570. if (__len1 < __len2)
  571. return __last1;
  572. // Find __f2 the first byte matching in __first1.
  573. __first1 = _Traits::find(__first1, __len1 - __len2 + 1, __f2);
  574. if (__first1 == nullptr)
  575. return __last1;
  576. // It is faster to compare from the first byte of __first1 even if we
  577. // already know that it matches the first byte of __first2: this is because
  578. // __first2 is most likely aligned, as it is user's "pattern" string, and
  579. // __first1 + 1 is most likely not aligned, as the match is in the middle of
  580. // the string.
  581. if (_Traits::compare(__first1, __first2, __len2) == 0)
  582. return __first1;
  583. ++__first1;
  584. }
  585. }
  586. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  587. inline _SizeT _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  588. __str_find(const _CharT *__p, _SizeT __sz,
  589. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  590. {
  591. if (__pos > __sz)
  592. return __npos;
  593. if (__n == 0) // There is nothing to search, just return __pos.
  594. return __pos;
  595. const _CharT *__r = std::__search_substring<_CharT, _Traits>(
  596. __p + __pos, __p + __sz, __s, __s + __n);
  597. if (__r == __p + __sz)
  598. return __npos;
  599. return static_cast<_SizeT>(__r - __p);
  600. }
  601. // __str_rfind
  602. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  603. inline _SizeT _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  604. __str_rfind(const _CharT *__p, _SizeT __sz,
  605. _CharT __c, _SizeT __pos) _NOEXCEPT
  606. {
  607. if (__sz < 1)
  608. return __npos;
  609. if (__pos < __sz)
  610. ++__pos;
  611. else
  612. __pos = __sz;
  613. for (const _CharT* __ps = __p + __pos; __ps != __p;)
  614. {
  615. if (_Traits::eq(*--__ps, __c))
  616. return static_cast<_SizeT>(__ps - __p);
  617. }
  618. return __npos;
  619. }
  620. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  621. inline _SizeT _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  622. __str_rfind(const _CharT *__p, _SizeT __sz,
  623. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  624. {
  625. __pos = _VSTD::min(__pos, __sz);
  626. if (__n < __sz - __pos)
  627. __pos += __n;
  628. else
  629. __pos = __sz;
  630. const _CharT* __r = std::__find_end_classic(__p, __p + __pos, __s, __s + __n, _Traits::eq);
  631. if (__n > 0 && __r == __p + __pos)
  632. return __npos;
  633. return static_cast<_SizeT>(__r - __p);
  634. }
  635. // __str_find_first_of
  636. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  637. inline _SizeT _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  638. __str_find_first_of(const _CharT *__p, _SizeT __sz,
  639. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  640. {
  641. if (__pos >= __sz || __n == 0)
  642. return __npos;
  643. const _CharT* __r = _VSTD::__find_first_of_ce
  644. (__p + __pos, __p + __sz, __s, __s + __n, _Traits::eq );
  645. if (__r == __p + __sz)
  646. return __npos;
  647. return static_cast<_SizeT>(__r - __p);
  648. }
  649. // __str_find_last_of
  650. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  651. inline _SizeT _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  652. __str_find_last_of(const _CharT *__p, _SizeT __sz,
  653. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  654. {
  655. if (__n != 0)
  656. {
  657. if (__pos < __sz)
  658. ++__pos;
  659. else
  660. __pos = __sz;
  661. for (const _CharT* __ps = __p + __pos; __ps != __p;)
  662. {
  663. const _CharT* __r = _Traits::find(__s, __n, *--__ps);
  664. if (__r)
  665. return static_cast<_SizeT>(__ps - __p);
  666. }
  667. }
  668. return __npos;
  669. }
  670. // __str_find_first_not_of
  671. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  672. inline _SizeT _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  673. __str_find_first_not_of(const _CharT *__p, _SizeT __sz,
  674. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  675. {
  676. if (__pos < __sz)
  677. {
  678. const _CharT* __pe = __p + __sz;
  679. for (const _CharT* __ps = __p + __pos; __ps != __pe; ++__ps)
  680. if (_Traits::find(__s, __n, *__ps) == nullptr)
  681. return static_cast<_SizeT>(__ps - __p);
  682. }
  683. return __npos;
  684. }
  685. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  686. inline _SizeT _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  687. __str_find_first_not_of(const _CharT *__p, _SizeT __sz,
  688. _CharT __c, _SizeT __pos) _NOEXCEPT
  689. {
  690. if (__pos < __sz)
  691. {
  692. const _CharT* __pe = __p + __sz;
  693. for (const _CharT* __ps = __p + __pos; __ps != __pe; ++__ps)
  694. if (!_Traits::eq(*__ps, __c))
  695. return static_cast<_SizeT>(__ps - __p);
  696. }
  697. return __npos;
  698. }
  699. // __str_find_last_not_of
  700. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  701. inline _SizeT _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  702. __str_find_last_not_of(const _CharT *__p, _SizeT __sz,
  703. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  704. {
  705. if (__pos < __sz)
  706. ++__pos;
  707. else
  708. __pos = __sz;
  709. for (const _CharT* __ps = __p + __pos; __ps != __p;)
  710. if (_Traits::find(__s, __n, *--__ps) == nullptr)
  711. return static_cast<_SizeT>(__ps - __p);
  712. return __npos;
  713. }
  714. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  715. inline _SizeT _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  716. __str_find_last_not_of(const _CharT *__p, _SizeT __sz,
  717. _CharT __c, _SizeT __pos) _NOEXCEPT
  718. {
  719. if (__pos < __sz)
  720. ++__pos;
  721. else
  722. __pos = __sz;
  723. for (const _CharT* __ps = __p + __pos; __ps != __p;)
  724. if (!_Traits::eq(*--__ps, __c))
  725. return static_cast<_SizeT>(__ps - __p);
  726. return __npos;
  727. }
  728. template<class _Ptr>
  729. inline _LIBCPP_INLINE_VISIBILITY
  730. size_t __do_string_hash(_Ptr __p, _Ptr __e)
  731. {
  732. typedef typename iterator_traits<_Ptr>::value_type value_type;
  733. return __murmur2_or_cityhash<size_t>()(__p, (__e-__p)*sizeof(value_type));
  734. }
  735. _LIBCPP_END_NAMESPACE_STD
  736. _LIBCPP_POP_MACROS
  737. #endif // _LIBCPP___STRING_CHAR_TRAITS_H