char_traits.h 29 KB

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