__string 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. // -*- C++ -*-
  2. //===-------------------------- __string ----------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___STRING
  10. #define _LIBCPP___STRING
  11. /*
  12. string synopsis
  13. namespace std
  14. {
  15. template <class charT>
  16. struct char_traits
  17. {
  18. typedef charT char_type;
  19. typedef ... int_type;
  20. typedef streamoff off_type;
  21. typedef streampos pos_type;
  22. typedef mbstate_t state_type;
  23. static constexpr void assign(char_type& c1, const char_type& c2) noexcept;
  24. static constexpr bool eq(char_type c1, char_type c2) noexcept;
  25. static constexpr bool lt(char_type c1, char_type c2) noexcept;
  26. static constexpr int compare(const char_type* s1, const char_type* s2, size_t n);
  27. static constexpr size_t length(const char_type* s);
  28. static constexpr const char_type*
  29. find(const char_type* s, size_t n, const char_type& a);
  30. static char_type* move(char_type* s1, const char_type* s2, size_t n);
  31. static char_type* copy(char_type* s1, const char_type* s2, size_t n);
  32. static char_type* assign(char_type* s, size_t n, char_type a);
  33. static constexpr int_type not_eof(int_type c) noexcept;
  34. static constexpr char_type to_char_type(int_type c) noexcept;
  35. static constexpr int_type to_int_type(char_type c) noexcept;
  36. static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
  37. static constexpr int_type eof() noexcept;
  38. };
  39. template <> struct char_traits<char>;
  40. template <> struct char_traits<wchar_t>;
  41. template <> struct char_traits<char8_t>; // c++20
  42. } // std
  43. */
  44. #include <__config>
  45. #include <algorithm> // for search and min
  46. #include <cstdio> // For EOF.
  47. #include <memory> // for __murmur2_or_cityhash
  48. #include <__debug>
  49. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  50. #pragma GCC system_header
  51. #endif
  52. _LIBCPP_PUSH_MACROS
  53. #include <__undef_macros>
  54. _LIBCPP_BEGIN_NAMESPACE_STD
  55. // char_traits
  56. template <class _CharT>
  57. struct _LIBCPP_TEMPLATE_VIS char_traits
  58. {
  59. typedef _CharT char_type;
  60. typedef int int_type;
  61. typedef streamoff off_type;
  62. typedef streampos pos_type;
  63. typedef mbstate_t state_type;
  64. static inline void _LIBCPP_CONSTEXPR_AFTER_CXX14
  65. assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
  66. static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
  67. {return __c1 == __c2;}
  68. static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
  69. {return __c1 < __c2;}
  70. static _LIBCPP_CONSTEXPR_AFTER_CXX14
  71. int compare(const char_type* __s1, const char_type* __s2, size_t __n);
  72. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
  73. size_t length(const char_type* __s);
  74. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
  75. const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
  76. static char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
  77. _LIBCPP_INLINE_VISIBILITY
  78. static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
  79. _LIBCPP_INLINE_VISIBILITY
  80. static char_type* assign(char_type* __s, size_t __n, char_type __a);
  81. static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
  82. {return eq_int_type(__c, eof()) ? ~eof() : __c;}
  83. static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
  84. {return char_type(__c);}
  85. static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
  86. {return int_type(__c);}
  87. static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
  88. {return __c1 == __c2;}
  89. static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
  90. {return int_type(EOF);}
  91. };
  92. template <class _CharT>
  93. _LIBCPP_CONSTEXPR_AFTER_CXX14 int
  94. char_traits<_CharT>::compare(const char_type* __s1, const char_type* __s2, size_t __n)
  95. {
  96. for (; __n; --__n, ++__s1, ++__s2)
  97. {
  98. if (lt(*__s1, *__s2))
  99. return -1;
  100. if (lt(*__s2, *__s1))
  101. return 1;
  102. }
  103. return 0;
  104. }
  105. template <class _CharT>
  106. inline
  107. _LIBCPP_CONSTEXPR_AFTER_CXX14 size_t
  108. char_traits<_CharT>::length(const char_type* __s)
  109. {
  110. size_t __len = 0;
  111. for (; !eq(*__s, char_type(0)); ++__s)
  112. ++__len;
  113. return __len;
  114. }
  115. template <class _CharT>
  116. inline
  117. _LIBCPP_CONSTEXPR_AFTER_CXX14 const _CharT*
  118. char_traits<_CharT>::find(const char_type* __s, size_t __n, const char_type& __a)
  119. {
  120. for (; __n; --__n)
  121. {
  122. if (eq(*__s, __a))
  123. return __s;
  124. ++__s;
  125. }
  126. return 0;
  127. }
  128. template <class _CharT>
  129. _CharT*
  130. char_traits<_CharT>::move(char_type* __s1, const char_type* __s2, size_t __n)
  131. {
  132. char_type* __r = __s1;
  133. if (__s1 < __s2)
  134. {
  135. for (; __n; --__n, ++__s1, ++__s2)
  136. assign(*__s1, *__s2);
  137. }
  138. else if (__s2 < __s1)
  139. {
  140. __s1 += __n;
  141. __s2 += __n;
  142. for (; __n; --__n)
  143. assign(*--__s1, *--__s2);
  144. }
  145. return __r;
  146. }
  147. template <class _CharT>
  148. inline
  149. _CharT*
  150. char_traits<_CharT>::copy(char_type* __s1, const char_type* __s2, size_t __n)
  151. {
  152. _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
  153. char_type* __r = __s1;
  154. for (; __n; --__n, ++__s1, ++__s2)
  155. assign(*__s1, *__s2);
  156. return __r;
  157. }
  158. template <class _CharT>
  159. inline
  160. _CharT*
  161. char_traits<_CharT>::assign(char_type* __s, size_t __n, char_type __a)
  162. {
  163. char_type* __r = __s;
  164. for (; __n; --__n, ++__s)
  165. assign(*__s, __a);
  166. return __r;
  167. }
  168. // char_traits<char>
  169. template <>
  170. struct _LIBCPP_TEMPLATE_VIS char_traits<char>
  171. {
  172. typedef char char_type;
  173. typedef int int_type;
  174. typedef streamoff off_type;
  175. typedef streampos pos_type;
  176. typedef mbstate_t state_type;
  177. static inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  178. void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
  179. static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
  180. {return __c1 == __c2;}
  181. static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
  182. {return (unsigned char)__c1 < (unsigned char)__c2;}
  183. static _LIBCPP_CONSTEXPR_AFTER_CXX14
  184. int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
  185. static inline size_t _LIBCPP_CONSTEXPR_AFTER_CXX14
  186. length(const char_type* __s) _NOEXCEPT {return __builtin_strlen(__s);}
  187. static _LIBCPP_CONSTEXPR_AFTER_CXX14
  188. const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
  189. static inline char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  190. {return __n == 0 ? __s1 : (char_type*) memmove(__s1, __s2, __n);}
  191. static inline char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  192. {
  193. _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
  194. return __n == 0 ? __s1 : (char_type*)memcpy(__s1, __s2, __n);
  195. }
  196. static inline char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT
  197. {return __n == 0 ? __s : (char_type*)memset(__s, to_int_type(__a), __n);}
  198. static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
  199. {return eq_int_type(__c, eof()) ? ~eof() : __c;}
  200. static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
  201. {return char_type(__c);}
  202. static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
  203. {return int_type((unsigned char)__c);}
  204. static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
  205. {return __c1 == __c2;}
  206. static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
  207. {return int_type(EOF);}
  208. };
  209. inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  210. int
  211. char_traits<char>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  212. {
  213. if (__n == 0)
  214. return 0;
  215. #if __has_feature(cxx_constexpr_string_builtins)
  216. return __builtin_memcmp(__s1, __s2, __n);
  217. #elif _LIBCPP_STD_VER <= 14
  218. return memcmp(__s1, __s2, __n);
  219. #else
  220. for (; __n; --__n, ++__s1, ++__s2)
  221. {
  222. if (lt(*__s1, *__s2))
  223. return -1;
  224. if (lt(*__s2, *__s1))
  225. return 1;
  226. }
  227. return 0;
  228. #endif
  229. }
  230. inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  231. const char*
  232. char_traits<char>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
  233. {
  234. if (__n == 0)
  235. return nullptr;
  236. #if __has_feature(cxx_constexpr_string_builtins)
  237. return __builtin_char_memchr(__s, to_int_type(__a), __n);
  238. #elif _LIBCPP_STD_VER <= 14
  239. return (const char_type*) memchr(__s, to_int_type(__a), __n);
  240. #else
  241. for (; __n; --__n)
  242. {
  243. if (eq(*__s, __a))
  244. return __s;
  245. ++__s;
  246. }
  247. return nullptr;
  248. #endif
  249. }
  250. // char_traits<wchar_t>
  251. template <>
  252. struct _LIBCPP_TEMPLATE_VIS char_traits<wchar_t>
  253. {
  254. typedef wchar_t char_type;
  255. typedef wint_t int_type;
  256. typedef streamoff off_type;
  257. typedef streampos pos_type;
  258. typedef mbstate_t state_type;
  259. static inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  260. void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
  261. static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
  262. {return __c1 == __c2;}
  263. static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
  264. {return __c1 < __c2;}
  265. static _LIBCPP_CONSTEXPR_AFTER_CXX14
  266. int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
  267. static _LIBCPP_CONSTEXPR_AFTER_CXX14
  268. size_t length(const char_type* __s) _NOEXCEPT;
  269. static _LIBCPP_CONSTEXPR_AFTER_CXX14
  270. const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
  271. static inline char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  272. {return __n == 0 ? __s1 : (char_type*)wmemmove(__s1, __s2, __n);}
  273. static inline char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  274. {
  275. _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
  276. return __n == 0 ? __s1 : (char_type*)wmemcpy(__s1, __s2, __n);
  277. }
  278. static inline char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT
  279. {return __n == 0 ? __s : (char_type*)wmemset(__s, __a, __n);}
  280. static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
  281. {return eq_int_type(__c, eof()) ? ~eof() : __c;}
  282. static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
  283. {return char_type(__c);}
  284. static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
  285. {return int_type(__c);}
  286. static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
  287. {return __c1 == __c2;}
  288. static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
  289. {return int_type(WEOF);}
  290. };
  291. inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  292. int
  293. char_traits<wchar_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  294. {
  295. if (__n == 0)
  296. return 0;
  297. #if __has_feature(cxx_constexpr_string_builtins)
  298. return __builtin_wmemcmp(__s1, __s2, __n);
  299. #elif _LIBCPP_STD_VER <= 14
  300. return wmemcmp(__s1, __s2, __n);
  301. #else
  302. for (; __n; --__n, ++__s1, ++__s2)
  303. {
  304. if (lt(*__s1, *__s2))
  305. return -1;
  306. if (lt(*__s2, *__s1))
  307. return 1;
  308. }
  309. return 0;
  310. #endif
  311. }
  312. template <class _Traits>
  313. _LIBCPP_INLINE_VISIBILITY
  314. _LIBCPP_CONSTEXPR
  315. inline size_t __char_traits_length_checked(const typename _Traits::char_type* __s) _NOEXCEPT {
  316. #if _LIBCPP_DEBUG_LEVEL >= 1
  317. return __s ? _Traits::length(__s) : (_VSTD::__libcpp_debug_function(_VSTD::__libcpp_debug_info(__FILE__, __LINE__, "p == nullptr", "null pointer pass to non-null argument of char_traits<...>::length")), 0);
  318. #else
  319. return _Traits::length(__s);
  320. #endif
  321. }
  322. inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  323. size_t
  324. char_traits<wchar_t>::length(const char_type* __s) _NOEXCEPT
  325. {
  326. #if __has_feature(cxx_constexpr_string_builtins)
  327. return __builtin_wcslen(__s);
  328. #elif _LIBCPP_STD_VER <= 14
  329. return wcslen(__s);
  330. #else
  331. size_t __len = 0;
  332. for (; !eq(*__s, char_type(0)); ++__s)
  333. ++__len;
  334. return __len;
  335. #endif
  336. }
  337. inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  338. const wchar_t*
  339. char_traits<wchar_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
  340. {
  341. if (__n == 0)
  342. return nullptr;
  343. #if __has_feature(cxx_constexpr_string_builtins)
  344. return __builtin_wmemchr(__s, __a, __n);
  345. #elif _LIBCPP_STD_VER <= 14
  346. return wmemchr(__s, __a, __n);
  347. #else
  348. for (; __n; --__n)
  349. {
  350. if (eq(*__s, __a))
  351. return __s;
  352. ++__s;
  353. }
  354. return nullptr;
  355. #endif
  356. }
  357. #ifndef _LIBCPP_NO_HAS_CHAR8_T
  358. template <>
  359. struct _LIBCPP_TEMPLATE_VIS char_traits<char8_t>
  360. {
  361. typedef char8_t char_type;
  362. typedef unsigned int int_type;
  363. typedef streamoff off_type;
  364. typedef u8streampos pos_type;
  365. typedef mbstate_t state_type;
  366. static inline constexpr void assign(char_type& __c1, const char_type& __c2) noexcept
  367. {__c1 = __c2;}
  368. static inline constexpr bool eq(char_type __c1, char_type __c2) noexcept
  369. {return __c1 == __c2;}
  370. static inline constexpr bool lt(char_type __c1, char_type __c2) noexcept
  371. {return __c1 < __c2;}
  372. static constexpr
  373. int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
  374. static constexpr
  375. size_t length(const char_type* __s) _NOEXCEPT;
  376. _LIBCPP_INLINE_VISIBILITY static constexpr
  377. const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
  378. static char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  379. {return __n == 0 ? __s1 : (char_type*) memmove(__s1, __s2, __n);}
  380. static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  381. {
  382. _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
  383. return __n == 0 ? __s1 : (char_type*)memcpy(__s1, __s2, __n);
  384. }
  385. static char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT
  386. {return __n == 0 ? __s : (char_type*)memset(__s, to_int_type(__a), __n);}
  387. static inline constexpr int_type not_eof(int_type __c) noexcept
  388. {return eq_int_type(__c, eof()) ? ~eof() : __c;}
  389. static inline constexpr char_type to_char_type(int_type __c) noexcept
  390. {return char_type(__c);}
  391. static inline constexpr int_type to_int_type(char_type __c) noexcept
  392. {return int_type(__c);}
  393. static inline constexpr bool eq_int_type(int_type __c1, int_type __c2) noexcept
  394. {return __c1 == __c2;}
  395. static inline constexpr int_type eof() noexcept
  396. {return int_type(EOF);}
  397. };
  398. // TODO use '__builtin_strlen' if it ever supports char8_t ??
  399. inline constexpr
  400. size_t
  401. char_traits<char8_t>::length(const char_type* __s) _NOEXCEPT
  402. {
  403. size_t __len = 0;
  404. for (; !eq(*__s, char_type(0)); ++__s)
  405. ++__len;
  406. return __len;
  407. }
  408. inline constexpr
  409. int
  410. char_traits<char8_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  411. {
  412. #if __has_feature(cxx_constexpr_string_builtins)
  413. return __builtin_memcmp(__s1, __s2, __n);
  414. #else
  415. for (; __n; --__n, ++__s1, ++__s2)
  416. {
  417. if (lt(*__s1, *__s2))
  418. return -1;
  419. if (lt(*__s2, *__s1))
  420. return 1;
  421. }
  422. return 0;
  423. #endif
  424. }
  425. // TODO use '__builtin_char_memchr' if it ever supports char8_t ??
  426. inline constexpr
  427. const char8_t*
  428. char_traits<char8_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
  429. {
  430. for (; __n; --__n)
  431. {
  432. if (eq(*__s, __a))
  433. return __s;
  434. ++__s;
  435. }
  436. return 0;
  437. }
  438. #endif // #_LIBCPP_NO_HAS_CHAR8_T
  439. #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
  440. template <>
  441. struct _LIBCPP_TEMPLATE_VIS char_traits<char16_t>
  442. {
  443. typedef char16_t char_type;
  444. typedef uint_least16_t int_type;
  445. typedef streamoff off_type;
  446. typedef u16streampos pos_type;
  447. typedef mbstate_t state_type;
  448. static inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  449. void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
  450. static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
  451. {return __c1 == __c2;}
  452. static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
  453. {return __c1 < __c2;}
  454. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
  455. int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
  456. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
  457. size_t length(const char_type* __s) _NOEXCEPT;
  458. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
  459. const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
  460. _LIBCPP_INLINE_VISIBILITY
  461. static char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
  462. _LIBCPP_INLINE_VISIBILITY
  463. static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
  464. _LIBCPP_INLINE_VISIBILITY
  465. static char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT;
  466. static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
  467. {return eq_int_type(__c, eof()) ? ~eof() : __c;}
  468. static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
  469. {return char_type(__c);}
  470. static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
  471. {return int_type(__c);}
  472. static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
  473. {return __c1 == __c2;}
  474. static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
  475. {return int_type(0xFFFF);}
  476. };
  477. inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  478. int
  479. char_traits<char16_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  480. {
  481. for (; __n; --__n, ++__s1, ++__s2)
  482. {
  483. if (lt(*__s1, *__s2))
  484. return -1;
  485. if (lt(*__s2, *__s1))
  486. return 1;
  487. }
  488. return 0;
  489. }
  490. inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  491. size_t
  492. char_traits<char16_t>::length(const char_type* __s) _NOEXCEPT
  493. {
  494. size_t __len = 0;
  495. for (; !eq(*__s, char_type(0)); ++__s)
  496. ++__len;
  497. return __len;
  498. }
  499. inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  500. const char16_t*
  501. char_traits<char16_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
  502. {
  503. for (; __n; --__n)
  504. {
  505. if (eq(*__s, __a))
  506. return __s;
  507. ++__s;
  508. }
  509. return 0;
  510. }
  511. inline
  512. char16_t*
  513. char_traits<char16_t>::move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  514. {
  515. char_type* __r = __s1;
  516. if (__s1 < __s2)
  517. {
  518. for (; __n; --__n, ++__s1, ++__s2)
  519. assign(*__s1, *__s2);
  520. }
  521. else if (__s2 < __s1)
  522. {
  523. __s1 += __n;
  524. __s2 += __n;
  525. for (; __n; --__n)
  526. assign(*--__s1, *--__s2);
  527. }
  528. return __r;
  529. }
  530. inline
  531. char16_t*
  532. char_traits<char16_t>::copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  533. {
  534. _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
  535. char_type* __r = __s1;
  536. for (; __n; --__n, ++__s1, ++__s2)
  537. assign(*__s1, *__s2);
  538. return __r;
  539. }
  540. inline
  541. char16_t*
  542. char_traits<char16_t>::assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT
  543. {
  544. char_type* __r = __s;
  545. for (; __n; --__n, ++__s)
  546. assign(*__s, __a);
  547. return __r;
  548. }
  549. template <>
  550. struct _LIBCPP_TEMPLATE_VIS char_traits<char32_t>
  551. {
  552. typedef char32_t char_type;
  553. typedef uint_least32_t int_type;
  554. typedef streamoff off_type;
  555. typedef u32streampos pos_type;
  556. typedef mbstate_t state_type;
  557. static inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  558. void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
  559. static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
  560. {return __c1 == __c2;}
  561. static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
  562. {return __c1 < __c2;}
  563. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
  564. int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
  565. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
  566. size_t length(const char_type* __s) _NOEXCEPT;
  567. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
  568. const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
  569. _LIBCPP_INLINE_VISIBILITY
  570. static char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
  571. _LIBCPP_INLINE_VISIBILITY
  572. static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
  573. _LIBCPP_INLINE_VISIBILITY
  574. static char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT;
  575. static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
  576. {return eq_int_type(__c, eof()) ? ~eof() : __c;}
  577. static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
  578. {return char_type(__c);}
  579. static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
  580. {return int_type(__c);}
  581. static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
  582. {return __c1 == __c2;}
  583. static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
  584. {return int_type(0xFFFFFFFF);}
  585. };
  586. inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  587. int
  588. char_traits<char32_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  589. {
  590. for (; __n; --__n, ++__s1, ++__s2)
  591. {
  592. if (lt(*__s1, *__s2))
  593. return -1;
  594. if (lt(*__s2, *__s1))
  595. return 1;
  596. }
  597. return 0;
  598. }
  599. inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  600. size_t
  601. char_traits<char32_t>::length(const char_type* __s) _NOEXCEPT
  602. {
  603. size_t __len = 0;
  604. for (; !eq(*__s, char_type(0)); ++__s)
  605. ++__len;
  606. return __len;
  607. }
  608. inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  609. const char32_t*
  610. char_traits<char32_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
  611. {
  612. for (; __n; --__n)
  613. {
  614. if (eq(*__s, __a))
  615. return __s;
  616. ++__s;
  617. }
  618. return 0;
  619. }
  620. inline
  621. char32_t*
  622. char_traits<char32_t>::move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  623. {
  624. char_type* __r = __s1;
  625. if (__s1 < __s2)
  626. {
  627. for (; __n; --__n, ++__s1, ++__s2)
  628. assign(*__s1, *__s2);
  629. }
  630. else if (__s2 < __s1)
  631. {
  632. __s1 += __n;
  633. __s2 += __n;
  634. for (; __n; --__n)
  635. assign(*--__s1, *--__s2);
  636. }
  637. return __r;
  638. }
  639. inline
  640. char32_t*
  641. char_traits<char32_t>::copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  642. {
  643. _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
  644. char_type* __r = __s1;
  645. for (; __n; --__n, ++__s1, ++__s2)
  646. assign(*__s1, *__s2);
  647. return __r;
  648. }
  649. inline
  650. char32_t*
  651. char_traits<char32_t>::assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT
  652. {
  653. char_type* __r = __s;
  654. for (; __n; --__n, ++__s)
  655. assign(*__s, __a);
  656. return __r;
  657. }
  658. #endif // _LIBCPP_HAS_NO_UNICODE_CHARS
  659. // helper fns for basic_string and string_view
  660. // __str_find
  661. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  662. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  663. __str_find(const _CharT *__p, _SizeT __sz,
  664. _CharT __c, _SizeT __pos) _NOEXCEPT
  665. {
  666. if (__pos >= __sz)
  667. return __npos;
  668. const _CharT* __r = _Traits::find(__p + __pos, __sz - __pos, __c);
  669. if (__r == 0)
  670. return __npos;
  671. return static_cast<_SizeT>(__r - __p);
  672. }
  673. template <class _CharT, class _Traits>
  674. inline _LIBCPP_CONSTEXPR_AFTER_CXX11 const _CharT *
  675. __search_substring(const _CharT *__first1, const _CharT *__last1,
  676. const _CharT *__first2, const _CharT *__last2) {
  677. // Take advantage of knowing source and pattern lengths.
  678. // Stop short when source is smaller than pattern.
  679. const ptrdiff_t __len2 = __last2 - __first2;
  680. if (__len2 == 0)
  681. return __first1;
  682. ptrdiff_t __len1 = __last1 - __first1;
  683. if (__len1 < __len2)
  684. return __last1;
  685. // First element of __first2 is loop invariant.
  686. _CharT __f2 = *__first2;
  687. while (true) {
  688. __len1 = __last1 - __first1;
  689. // Check whether __first1 still has at least __len2 bytes.
  690. if (__len1 < __len2)
  691. return __last1;
  692. // Find __f2 the first byte matching in __first1.
  693. __first1 = _Traits::find(__first1, __len1 - __len2 + 1, __f2);
  694. if (__first1 == 0)
  695. return __last1;
  696. // It is faster to compare from the first byte of __first1 even if we
  697. // already know that it matches the first byte of __first2: this is because
  698. // __first2 is most likely aligned, as it is user's "pattern" string, and
  699. // __first1 + 1 is most likely not aligned, as the match is in the middle of
  700. // the string.
  701. if (_Traits::compare(__first1, __first2, __len2) == 0)
  702. return __first1;
  703. ++__first1;
  704. }
  705. }
  706. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  707. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  708. __str_find(const _CharT *__p, _SizeT __sz,
  709. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  710. {
  711. if (__pos > __sz)
  712. return __npos;
  713. if (__n == 0) // There is nothing to search, just return __pos.
  714. return __pos;
  715. const _CharT *__r = __search_substring<_CharT, _Traits>(
  716. __p + __pos, __p + __sz, __s, __s + __n);
  717. if (__r == __p + __sz)
  718. return __npos;
  719. return static_cast<_SizeT>(__r - __p);
  720. }
  721. // __str_rfind
  722. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  723. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  724. __str_rfind(const _CharT *__p, _SizeT __sz,
  725. _CharT __c, _SizeT __pos) _NOEXCEPT
  726. {
  727. if (__sz < 1)
  728. return __npos;
  729. if (__pos < __sz)
  730. ++__pos;
  731. else
  732. __pos = __sz;
  733. for (const _CharT* __ps = __p + __pos; __ps != __p;)
  734. {
  735. if (_Traits::eq(*--__ps, __c))
  736. return static_cast<_SizeT>(__ps - __p);
  737. }
  738. return __npos;
  739. }
  740. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  741. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  742. __str_rfind(const _CharT *__p, _SizeT __sz,
  743. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  744. {
  745. __pos = _VSTD::min(__pos, __sz);
  746. if (__n < __sz - __pos)
  747. __pos += __n;
  748. else
  749. __pos = __sz;
  750. const _CharT* __r = _VSTD::__find_end(
  751. __p, __p + __pos, __s, __s + __n, _Traits::eq,
  752. random_access_iterator_tag(), random_access_iterator_tag());
  753. if (__n > 0 && __r == __p + __pos)
  754. return __npos;
  755. return static_cast<_SizeT>(__r - __p);
  756. }
  757. // __str_find_first_of
  758. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  759. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  760. __str_find_first_of(const _CharT *__p, _SizeT __sz,
  761. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  762. {
  763. if (__pos >= __sz || __n == 0)
  764. return __npos;
  765. const _CharT* __r = _VSTD::__find_first_of_ce
  766. (__p + __pos, __p + __sz, __s, __s + __n, _Traits::eq );
  767. if (__r == __p + __sz)
  768. return __npos;
  769. return static_cast<_SizeT>(__r - __p);
  770. }
  771. // __str_find_last_of
  772. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  773. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  774. __str_find_last_of(const _CharT *__p, _SizeT __sz,
  775. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  776. {
  777. if (__n != 0)
  778. {
  779. if (__pos < __sz)
  780. ++__pos;
  781. else
  782. __pos = __sz;
  783. for (const _CharT* __ps = __p + __pos; __ps != __p;)
  784. {
  785. const _CharT* __r = _Traits::find(__s, __n, *--__ps);
  786. if (__r)
  787. return static_cast<_SizeT>(__ps - __p);
  788. }
  789. }
  790. return __npos;
  791. }
  792. // __str_find_first_not_of
  793. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  794. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  795. __str_find_first_not_of(const _CharT *__p, _SizeT __sz,
  796. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  797. {
  798. if (__pos < __sz)
  799. {
  800. const _CharT* __pe = __p + __sz;
  801. for (const _CharT* __ps = __p + __pos; __ps != __pe; ++__ps)
  802. if (_Traits::find(__s, __n, *__ps) == 0)
  803. return static_cast<_SizeT>(__ps - __p);
  804. }
  805. return __npos;
  806. }
  807. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  808. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  809. __str_find_first_not_of(const _CharT *__p, _SizeT __sz,
  810. _CharT __c, _SizeT __pos) _NOEXCEPT
  811. {
  812. if (__pos < __sz)
  813. {
  814. const _CharT* __pe = __p + __sz;
  815. for (const _CharT* __ps = __p + __pos; __ps != __pe; ++__ps)
  816. if (!_Traits::eq(*__ps, __c))
  817. return static_cast<_SizeT>(__ps - __p);
  818. }
  819. return __npos;
  820. }
  821. // __str_find_last_not_of
  822. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  823. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  824. __str_find_last_not_of(const _CharT *__p, _SizeT __sz,
  825. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  826. {
  827. if (__pos < __sz)
  828. ++__pos;
  829. else
  830. __pos = __sz;
  831. for (const _CharT* __ps = __p + __pos; __ps != __p;)
  832. if (_Traits::find(__s, __n, *--__ps) == 0)
  833. return static_cast<_SizeT>(__ps - __p);
  834. return __npos;
  835. }
  836. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  837. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  838. __str_find_last_not_of(const _CharT *__p, _SizeT __sz,
  839. _CharT __c, _SizeT __pos) _NOEXCEPT
  840. {
  841. if (__pos < __sz)
  842. ++__pos;
  843. else
  844. __pos = __sz;
  845. for (const _CharT* __ps = __p + __pos; __ps != __p;)
  846. if (!_Traits::eq(*--__ps, __c))
  847. return static_cast<_SizeT>(__ps - __p);
  848. return __npos;
  849. }
  850. template<class _Ptr>
  851. inline _LIBCPP_INLINE_VISIBILITY
  852. size_t __do_string_hash(_Ptr __p, _Ptr __e)
  853. {
  854. typedef typename iterator_traits<_Ptr>::value_type value_type;
  855. return __murmur2_or_cityhash<size_t>()(__p, (__e-__p)*sizeof(value_type));
  856. }
  857. template <class _CharT, class _Iter, class _Traits=char_traits<_CharT> >
  858. struct __quoted_output_proxy
  859. {
  860. _Iter __first;
  861. _Iter __last;
  862. _CharT __delim;
  863. _CharT __escape;
  864. __quoted_output_proxy(_Iter __f, _Iter __l, _CharT __d, _CharT __e)
  865. : __first(__f), __last(__l), __delim(__d), __escape(__e) {}
  866. // This would be a nice place for a string_ref
  867. };
  868. _LIBCPP_END_NAMESPACE_STD
  869. _LIBCPP_POP_MACROS
  870. #endif // _LIBCPP___STRING