string_view 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  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_VIEW
  10. #define _LIBCPP_STRING_VIEW
  11. // clang-format off
  12. /*
  13. string_view synopsis
  14. #include <compare>
  15. namespace std {
  16. // 7.2, Class template basic_string_view
  17. template<class charT, class traits = char_traits<charT>>
  18. class basic_string_view;
  19. template<class charT, class traits>
  20. inline constexpr bool ranges::enable_view<basic_string_view<charT, traits>> = true;
  21. template<class charT, class traits>
  22. inline constexpr bool ranges::enable_borrowed_range<basic_string_view<charT, traits>> = true; // C++20
  23. // 7.9, basic_string_view non-member comparison functions
  24. template<class charT, class traits>
  25. constexpr bool operator==(basic_string_view<charT, traits> x,
  26. basic_string_view<charT, traits> y) noexcept;
  27. template<class charT, class traits> // Removed in C++20
  28. constexpr bool operator!=(basic_string_view<charT, traits> x,
  29. basic_string_view<charT, traits> y) noexcept;
  30. template<class charT, class traits> // Removed in C++20
  31. constexpr bool operator< (basic_string_view<charT, traits> x,
  32. basic_string_view<charT, traits> y) noexcept;
  33. template<class charT, class traits> // Removed in C++20
  34. constexpr bool operator> (basic_string_view<charT, traits> x,
  35. basic_string_view<charT, traits> y) noexcept;
  36. template<class charT, class traits> // Removed in C++20
  37. constexpr bool operator<=(basic_string_view<charT, traits> x,
  38. basic_string_view<charT, traits> y) noexcept;
  39. template<class charT, class traits> // Removed in C++20
  40. constexpr bool operator>=(basic_string_view<charT, traits> x,
  41. basic_string_view<charT, traits> y) noexcept;
  42. template<class charT, class traits> // Since C++20
  43. constexpr see below operator<=>(basic_string_view<charT, traits> x,
  44. basic_string_view<charT, traits> y) noexcept;
  45. // see below, sufficient additional overloads of comparison functions
  46. // 7.10, Inserters and extractors
  47. template<class charT, class traits>
  48. basic_ostream<charT, traits>&
  49. operator<<(basic_ostream<charT, traits>& os,
  50. basic_string_view<charT, traits> str);
  51. // basic_string_view typedef names
  52. typedef basic_string_view<char> string_view;
  53. typedef basic_string_view<char8_t> u8string_view; // C++20
  54. typedef basic_string_view<char16_t> u16string_view;
  55. typedef basic_string_view<char32_t> u32string_view;
  56. typedef basic_string_view<wchar_t> wstring_view;
  57. template<class charT, class traits = char_traits<charT>>
  58. class basic_string_view {
  59. public:
  60. // types
  61. typedef traits traits_type;
  62. typedef charT value_type;
  63. typedef charT* pointer;
  64. typedef const charT* const_pointer;
  65. typedef charT& reference;
  66. typedef const charT& const_reference;
  67. typedef implementation-defined const_iterator;
  68. typedef const_iterator iterator;
  69. typedef reverse_iterator<const_iterator> const_reverse_iterator;
  70. typedef const_reverse_iterator reverse_iterator;
  71. typedef size_t size_type;
  72. typedef ptrdiff_t difference_type;
  73. static constexpr size_type npos = size_type(-1);
  74. // 7.3, basic_string_view constructors and assignment operators
  75. constexpr basic_string_view() noexcept;
  76. constexpr basic_string_view(const basic_string_view&) noexcept = default;
  77. basic_string_view& operator=(const basic_string_view&) noexcept = default;
  78. template<class Allocator>
  79. constexpr basic_string_view(const charT* str);
  80. basic_string_view(nullptr_t) = delete; // C++23
  81. constexpr basic_string_view(const charT* str, size_type len);
  82. template <class It, class End>
  83. constexpr basic_string_view(It begin, End end); // C++20
  84. template <class Range>
  85. constexpr basic_string_view(Range&& r); // C++23
  86. // 7.4, basic_string_view iterator support
  87. constexpr const_iterator begin() const noexcept;
  88. constexpr const_iterator end() const noexcept;
  89. constexpr const_iterator cbegin() const noexcept;
  90. constexpr const_iterator cend() const noexcept;
  91. const_reverse_iterator rbegin() const noexcept;
  92. const_reverse_iterator rend() const noexcept;
  93. const_reverse_iterator crbegin() const noexcept;
  94. const_reverse_iterator crend() const noexcept;
  95. // 7.5, basic_string_view capacity
  96. constexpr size_type size() const noexcept;
  97. constexpr size_type length() const noexcept;
  98. constexpr size_type max_size() const noexcept;
  99. constexpr bool empty() const noexcept;
  100. // 7.6, basic_string_view element access
  101. constexpr const_reference operator[](size_type pos) const;
  102. constexpr const_reference at(size_type pos) const;
  103. constexpr const_reference front() const;
  104. constexpr const_reference back() const;
  105. constexpr const_pointer data() const noexcept;
  106. // 7.7, basic_string_view modifiers
  107. constexpr void remove_prefix(size_type n);
  108. constexpr void remove_suffix(size_type n);
  109. constexpr void swap(basic_string_view& s) noexcept;
  110. size_type copy(charT* s, size_type n, size_type pos = 0) const; // constexpr in C++20
  111. constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const;
  112. constexpr int compare(basic_string_view s) const noexcept;
  113. constexpr int compare(size_type pos1, size_type n1, basic_string_view s) const;
  114. constexpr int compare(size_type pos1, size_type n1,
  115. basic_string_view s, size_type pos2, size_type n2) const;
  116. constexpr int compare(const charT* s) const;
  117. constexpr int compare(size_type pos1, size_type n1, const charT* s) const;
  118. constexpr int compare(size_type pos1, size_type n1,
  119. const charT* s, size_type n2) const;
  120. constexpr size_type find(basic_string_view s, size_type pos = 0) const noexcept;
  121. constexpr size_type find(charT c, size_type pos = 0) const noexcept;
  122. constexpr size_type find(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
  123. constexpr size_type find(const charT* s, size_type pos = 0) const noexcept; // noexcept as an extension
  124. constexpr size_type rfind(basic_string_view s, size_type pos = npos) const noexcept;
  125. constexpr size_type rfind(charT c, size_type pos = npos) const noexcept;
  126. constexpr size_type rfind(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
  127. constexpr size_type rfind(const charT* s, size_type pos = npos) const noexcept; // noexcept as an extension
  128. constexpr size_type find_first_of(basic_string_view s, size_type pos = 0) const noexcept;
  129. constexpr size_type find_first_of(charT c, size_type pos = 0) const noexcept;
  130. constexpr size_type find_first_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
  131. constexpr size_type find_first_of(const charT* s, size_type pos = 0) const noexcept; // noexcept as an extension
  132. constexpr size_type find_last_of(basic_string_view s, size_type pos = npos) const noexcept;
  133. constexpr size_type find_last_of(charT c, size_type pos = npos) const noexcept;
  134. constexpr size_type find_last_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
  135. constexpr size_type find_last_of(const charT* s, size_type pos = npos) const noexcept; // noexcept as an extension
  136. constexpr size_type find_first_not_of(basic_string_view s, size_type pos = 0) const noexcept;
  137. constexpr size_type find_first_not_of(charT c, size_type pos = 0) const noexcept;
  138. constexpr size_type find_first_not_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
  139. constexpr size_type find_first_not_of(const charT* s, size_type pos = 0) const noexcept; // noexcept as an extension
  140. constexpr size_type find_last_not_of(basic_string_view s, size_type pos = npos) const noexcept;
  141. constexpr size_type find_last_not_of(charT c, size_type pos = npos) const noexcept;
  142. constexpr size_type find_last_not_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
  143. constexpr size_type find_last_not_of(const charT* s, size_type pos = npos) const noexcept; // noexcept as an extension
  144. constexpr bool starts_with(basic_string_view s) const noexcept; // C++20
  145. constexpr bool starts_with(charT c) const noexcept; // C++20
  146. constexpr bool starts_with(const charT* s) const; // C++20
  147. constexpr bool ends_with(basic_string_view s) const noexcept; // C++20
  148. constexpr bool ends_with(charT c) const noexcept; // C++20
  149. constexpr bool ends_with(const charT* s) const; // C++20
  150. constexpr bool contains(basic_string_view s) const noexcept; // C++23
  151. constexpr bool contains(charT c) const noexcept; // C++23
  152. constexpr bool contains(const charT* s) const; // C++23
  153. private:
  154. const_pointer data_; // exposition only
  155. size_type size_; // exposition only
  156. };
  157. // basic_string_view deduction guides
  158. template<class It, class End>
  159. basic_string_view(It, End) -> basic_string_view<iter_value_t<It>>; // C++20
  160. template<class Range>
  161. basic_string_view(Range&&) -> basic_string_view<ranges::range_value_t<Range>>; // C++23
  162. // 7.11, Hash support
  163. template <class T> struct hash;
  164. template <> struct hash<string_view>;
  165. template <> struct hash<u8string_view>; // C++20
  166. template <> struct hash<u16string_view>;
  167. template <> struct hash<u32string_view>;
  168. template <> struct hash<wstring_view>;
  169. constexpr basic_string_view<char> operator""sv(const char *str, size_t len) noexcept;
  170. constexpr basic_string_view<wchar_t> operator""sv(const wchar_t *str, size_t len) noexcept;
  171. constexpr basic_string_view<char8_t> operator""sv(const char8_t *str, size_t len) noexcept; // C++20
  172. constexpr basic_string_view<char16_t> operator""sv(const char16_t *str, size_t len) noexcept;
  173. constexpr basic_string_view<char32_t> operator""sv(const char32_t *str, size_t len) noexcept;
  174. } // namespace std
  175. */
  176. // clang-format on
  177. #include <__algorithm/min.h>
  178. #include <__assert>
  179. #include <__config>
  180. #include <__functional/hash.h>
  181. #include <__functional/unary_function.h>
  182. #include <__fwd/string_view.h>
  183. #include <__iterator/bounded_iter.h>
  184. #include <__iterator/concepts.h>
  185. #include <__iterator/iterator_traits.h>
  186. #include <__iterator/reverse_iterator.h>
  187. #include <__iterator/wrap_iter.h>
  188. #include <__memory/pointer_traits.h>
  189. #include <__ranges/concepts.h>
  190. #include <__ranges/data.h>
  191. #include <__ranges/enable_borrowed_range.h>
  192. #include <__ranges/enable_view.h>
  193. #include <__ranges/size.h>
  194. #include <__string/char_traits.h>
  195. #include <__type_traits/is_array.h>
  196. #include <__type_traits/is_convertible.h>
  197. #include <__type_traits/is_same.h>
  198. #include <__type_traits/is_standard_layout.h>
  199. #include <__type_traits/is_trivial.h>
  200. #include <__type_traits/remove_cvref.h>
  201. #include <__type_traits/remove_reference.h>
  202. #include <__type_traits/type_identity.h>
  203. #include <cstddef>
  204. #include <iosfwd>
  205. #include <limits>
  206. #include <stdexcept>
  207. #include <version>
  208. // standard-mandated includes
  209. // [iterator.range]
  210. #include <__iterator/access.h>
  211. #include <__iterator/data.h>
  212. #include <__iterator/empty.h>
  213. #include <__iterator/reverse_access.h>
  214. #include <__iterator/size.h>
  215. // [string.view.synop]
  216. #include <compare>
  217. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  218. # pragma GCC system_header
  219. #endif
  220. _LIBCPP_PUSH_MACROS
  221. #include <__undef_macros>
  222. _LIBCPP_BEGIN_NAMESPACE_STD
  223. // TODO: This is a workaround for some vendors to carry a downstream diff to accept `nullptr` in
  224. // string_view constructors. This can be refactored when this exact form isn't needed anymore.
  225. template <class _Traits>
  226. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR inline size_t
  227. __char_traits_length_checked(const typename _Traits::char_type* __s) _NOEXCEPT {
  228. // This needs to be a single statement for C++11 constexpr
  229. return _LIBCPP_ASSERT_NON_NULL(
  230. __s != nullptr, "null pointer passed to non-null argument of char_traits<...>::length"),
  231. _Traits::length(__s);
  232. }
  233. template <class _CharT, class _Traits>
  234. class basic_string_view {
  235. public:
  236. // types
  237. using traits_type = _Traits;
  238. using value_type = _CharT;
  239. using pointer = _CharT*;
  240. using const_pointer = const _CharT*;
  241. using reference = _CharT&;
  242. using const_reference = const _CharT&;
  243. #if defined(_LIBCPP_ABI_BOUNDED_ITERATORS)
  244. using const_iterator = __bounded_iter<const_pointer>;
  245. #elif defined(_LIBCPP_ABI_USE_WRAP_ITER_IN_STD_STRING_VIEW)
  246. using const_iterator = __wrap_iter<const_pointer>;
  247. #else
  248. using const_iterator = const_pointer;
  249. #endif
  250. using iterator = const_iterator;
  251. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  252. using reverse_iterator = const_reverse_iterator;
  253. using size_type = size_t;
  254. using difference_type = ptrdiff_t;
  255. static _LIBCPP_CONSTEXPR const size_type npos = -1; // size_type(-1);
  256. static_assert((!is_array<value_type>::value), "Character type of basic_string_view must not be an array");
  257. static_assert((is_standard_layout<value_type>::value), "Character type of basic_string_view must be standard-layout");
  258. static_assert((is_trivial<value_type>::value), "Character type of basic_string_view must be trivial");
  259. static_assert((is_same<_CharT, typename traits_type::char_type>::value),
  260. "traits_type::char_type must be the same type as CharT");
  261. // [string.view.cons], construct/copy
  262. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI basic_string_view() _NOEXCEPT : __data_(nullptr), __size_(0) {}
  263. _LIBCPP_HIDE_FROM_ABI basic_string_view(const basic_string_view&) _NOEXCEPT = default;
  264. _LIBCPP_HIDE_FROM_ABI basic_string_view& operator=(const basic_string_view&) _NOEXCEPT = default;
  265. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI basic_string_view(nullptr_t, size_t) = delete;
  266. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI basic_string_view(const _CharT* __s, size_type __len) _NOEXCEPT
  267. : __data_(__s),
  268. __size_(__len) {
  269. #if _LIBCPP_STD_VER >= 14
  270. // Allocations must fit in `ptrdiff_t` for pointer arithmetic to work. If `__len` exceeds it, the input
  271. // range could not have been valid. Most likely the caller underflowed some arithmetic and inadvertently
  272. // passed in a negative length.
  273. _LIBCPP_ASSERT_VALID_INPUT_RANGE(
  274. __len <= static_cast<size_type>(numeric_limits<difference_type>::max()),
  275. "string_view::string_view(_CharT *, size_t): length does not fit in difference_type");
  276. _LIBCPP_ASSERT_NON_NULL(
  277. __len == 0 || __s != nullptr, "string_view::string_view(_CharT *, size_t): received nullptr");
  278. #endif
  279. }
  280. #if _LIBCPP_STD_VER >= 20
  281. template <contiguous_iterator _It, sized_sentinel_for<_It> _End>
  282. requires(is_same_v<iter_value_t<_It>, _CharT> && !is_convertible_v<_End, size_type>)
  283. constexpr _LIBCPP_HIDE_FROM_ABI basic_string_view(_It __begin, _End __end)
  284. : __data_(std::to_address(__begin)), __size_(__end - __begin) {
  285. _LIBCPP_ASSERT_VALID_INPUT_RANGE(
  286. (__end - __begin) >= 0, "std::string_view::string_view(iterator, sentinel) received invalid range");
  287. }
  288. #endif // _LIBCPP_STD_VER >= 20
  289. #if _LIBCPP_STD_VER >= 23
  290. template <class _Range>
  291. requires(!is_same_v<remove_cvref_t<_Range>, basic_string_view> && ranges::contiguous_range<_Range> &&
  292. ranges::sized_range<_Range> && is_same_v<ranges::range_value_t<_Range>, _CharT> &&
  293. !is_convertible_v<_Range, const _CharT*> &&
  294. (!requires(remove_cvref_t<_Range>& __d) { __d.operator std::basic_string_view<_CharT, _Traits>(); }))
  295. constexpr explicit _LIBCPP_HIDE_FROM_ABI basic_string_view(_Range&& __r)
  296. : __data_(ranges::data(__r)), __size_(ranges::size(__r)) {}
  297. #endif // _LIBCPP_STD_VER >= 23
  298. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI basic_string_view(const _CharT* __s)
  299. : __data_(__s), __size_(std::__char_traits_length_checked<_Traits>(__s)) {}
  300. #if _LIBCPP_STD_VER >= 20
  301. basic_string_view(nullptr_t) = delete;
  302. #endif
  303. // [string.view.iterators], iterators
  304. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return cbegin(); }
  305. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return cend(); }
  306. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT {
  307. #ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
  308. return std::__make_bounded_iter(data(), data(), data() + size());
  309. #else
  310. return const_iterator(__data_);
  311. #endif
  312. }
  313. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT {
  314. #ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
  315. return std::__make_bounded_iter(data() + size(), data(), data() + size());
  316. #else
  317. return const_iterator(__data_ + __size_);
  318. #endif
  319. }
  320. _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT {
  321. return const_reverse_iterator(cend());
  322. }
  323. _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT {
  324. return const_reverse_iterator(cbegin());
  325. }
  326. _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT {
  327. return const_reverse_iterator(cend());
  328. }
  329. _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT {
  330. return const_reverse_iterator(cbegin());
  331. }
  332. // [string.view.capacity], capacity
  333. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __size_; }
  334. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI size_type length() const _NOEXCEPT { return __size_; }
  335. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
  336. return numeric_limits<size_type>::max() / sizeof(value_type);
  337. }
  338. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {
  339. return __size_ == 0;
  340. }
  341. // [string.view.access], element access
  342. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_reference operator[](size_type __pos) const _NOEXCEPT {
  343. return _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__pos < size(), "string_view[] index out of bounds"), __data_[__pos];
  344. }
  345. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_reference at(size_type __pos) const {
  346. return __pos >= size() ? (__throw_out_of_range("string_view::at"), __data_[0]) : __data_[__pos];
  347. }
  348. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_reference front() const _NOEXCEPT {
  349. return _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string_view::front(): string is empty"), __data_[0];
  350. }
  351. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_reference back() const _NOEXCEPT {
  352. return _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string_view::back(): string is empty"), __data_[__size_ - 1];
  353. }
  354. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_pointer data() const _NOEXCEPT { return __data_; }
  355. // [string.view.modifiers], modifiers:
  356. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI void remove_prefix(size_type __n) _NOEXCEPT {
  357. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n <= size(), "remove_prefix() can't remove more than size()");
  358. __data_ += __n;
  359. __size_ -= __n;
  360. }
  361. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI void remove_suffix(size_type __n) _NOEXCEPT {
  362. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n <= size(), "remove_suffix() can't remove more than size()");
  363. __size_ -= __n;
  364. }
  365. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI void swap(basic_string_view& __other) _NOEXCEPT {
  366. const value_type* __p = __data_;
  367. __data_ = __other.__data_;
  368. __other.__data_ = __p;
  369. size_type __sz = __size_;
  370. __size_ = __other.__size_;
  371. __other.__size_ = __sz;
  372. }
  373. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  374. copy(_CharT* __s, size_type __n, size_type __pos = 0) const {
  375. if (__pos > size())
  376. __throw_out_of_range("string_view::copy");
  377. size_type __rlen = std::min(__n, size() - __pos);
  378. _Traits::copy(__s, data() + __pos, __rlen);
  379. return __rlen;
  380. }
  381. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI basic_string_view substr(size_type __pos = 0, size_type __n = npos) const {
  382. return __pos > size() ? (__throw_out_of_range("string_view::substr"), basic_string_view())
  383. : basic_string_view(data() + __pos, std::min(__n, size() - __pos));
  384. }
  385. _LIBCPP_CONSTEXPR_SINCE_CXX14 int compare(basic_string_view __sv) const _NOEXCEPT {
  386. size_type __rlen = std::min(size(), __sv.size());
  387. int __retval = _Traits::compare(data(), __sv.data(), __rlen);
  388. if (__retval == 0) // first __rlen chars matched
  389. __retval = size() == __sv.size() ? 0 : (size() < __sv.size() ? -1 : 1);
  390. return __retval;
  391. }
  392. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI int
  393. compare(size_type __pos1, size_type __n1, basic_string_view __sv) const {
  394. return substr(__pos1, __n1).compare(__sv);
  395. }
  396. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI int
  397. compare(size_type __pos1, size_type __n1, basic_string_view __sv, size_type __pos2, size_type __n2) const {
  398. return substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
  399. }
  400. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI int compare(const _CharT* __s) const _NOEXCEPT {
  401. return compare(basic_string_view(__s));
  402. }
  403. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI int
  404. compare(size_type __pos1, size_type __n1, const _CharT* __s) const {
  405. return substr(__pos1, __n1).compare(basic_string_view(__s));
  406. }
  407. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI int
  408. compare(size_type __pos1, size_type __n1, const _CharT* __s, size_type __n2) const {
  409. return substr(__pos1, __n1).compare(basic_string_view(__s, __n2));
  410. }
  411. // find
  412. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  413. find(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT {
  414. _LIBCPP_ASSERT_NON_NULL(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
  415. return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __s.data(), __pos, __s.size());
  416. }
  417. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type find(_CharT __c, size_type __pos = 0) const _NOEXCEPT {
  418. return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
  419. }
  420. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  421. find(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT {
  422. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string_view::find(): received nullptr");
  423. return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
  424. }
  425. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  426. find(const _CharT* __s, size_type __pos = 0) const _NOEXCEPT {
  427. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string_view::find(): received nullptr");
  428. return std::__str_find<value_type, size_type, traits_type, npos>(
  429. data(), size(), __s, __pos, traits_type::length(__s));
  430. }
  431. // rfind
  432. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  433. rfind(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT {
  434. _LIBCPP_ASSERT_NON_NULL(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
  435. return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __s.data(), __pos, __s.size());
  436. }
  437. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  438. rfind(_CharT __c, size_type __pos = npos) const _NOEXCEPT {
  439. return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
  440. }
  441. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  442. rfind(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT {
  443. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string_view::rfind(): received nullptr");
  444. return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
  445. }
  446. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  447. rfind(const _CharT* __s, size_type __pos = npos) const _NOEXCEPT {
  448. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string_view::rfind(): received nullptr");
  449. return std::__str_rfind<value_type, size_type, traits_type, npos>(
  450. data(), size(), __s, __pos, traits_type::length(__s));
  451. }
  452. // find_first_of
  453. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  454. find_first_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT {
  455. _LIBCPP_ASSERT_NON_NULL(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_of(): received nullptr");
  456. return std::__str_find_first_of<value_type, size_type, traits_type, npos>(
  457. data(), size(), __s.data(), __pos, __s.size());
  458. }
  459. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  460. find_first_of(_CharT __c, size_type __pos = 0) const _NOEXCEPT {
  461. return find(__c, __pos);
  462. }
  463. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  464. find_first_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT {
  465. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string_view::find_first_of(): received nullptr");
  466. return std::__str_find_first_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
  467. }
  468. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  469. find_first_of(const _CharT* __s, size_type __pos = 0) const _NOEXCEPT {
  470. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string_view::find_first_of(): received nullptr");
  471. return std::__str_find_first_of<value_type, size_type, traits_type, npos>(
  472. data(), size(), __s, __pos, traits_type::length(__s));
  473. }
  474. // find_last_of
  475. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  476. find_last_of(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT {
  477. _LIBCPP_ASSERT_NON_NULL(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_of(): received nullptr");
  478. return std::__str_find_last_of<value_type, size_type, traits_type, npos>(
  479. data(), size(), __s.data(), __pos, __s.size());
  480. }
  481. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  482. find_last_of(_CharT __c, size_type __pos = npos) const _NOEXCEPT {
  483. return rfind(__c, __pos);
  484. }
  485. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  486. find_last_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT {
  487. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string_view::find_last_of(): received nullptr");
  488. return std::__str_find_last_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
  489. }
  490. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  491. find_last_of(const _CharT* __s, size_type __pos = npos) const _NOEXCEPT {
  492. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string_view::find_last_of(): received nullptr");
  493. return std::__str_find_last_of<value_type, size_type, traits_type, npos>(
  494. data(), size(), __s, __pos, traits_type::length(__s));
  495. }
  496. // find_first_not_of
  497. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  498. find_first_not_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT {
  499. _LIBCPP_ASSERT_NON_NULL(
  500. __s.size() == 0 || __s.data() != nullptr, "string_view::find_first_not_of(): received nullptr");
  501. return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(
  502. data(), size(), __s.data(), __pos, __s.size());
  503. }
  504. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  505. find_first_not_of(_CharT __c, size_type __pos = 0) const _NOEXCEPT {
  506. return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
  507. }
  508. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  509. find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT {
  510. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string_view::find_first_not_of(): received nullptr");
  511. return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
  512. }
  513. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  514. find_first_not_of(const _CharT* __s, size_type __pos = 0) const _NOEXCEPT {
  515. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string_view::find_first_not_of(): received nullptr");
  516. return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(
  517. data(), size(), __s, __pos, traits_type::length(__s));
  518. }
  519. // find_last_not_of
  520. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  521. find_last_not_of(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT {
  522. _LIBCPP_ASSERT_NON_NULL(
  523. __s.size() == 0 || __s.data() != nullptr, "string_view::find_last_not_of(): received nullptr");
  524. return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(
  525. data(), size(), __s.data(), __pos, __s.size());
  526. }
  527. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  528. find_last_not_of(_CharT __c, size_type __pos = npos) const _NOEXCEPT {
  529. return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
  530. }
  531. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  532. find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT {
  533. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string_view::find_last_not_of(): received nullptr");
  534. return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
  535. }
  536. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  537. find_last_not_of(const _CharT* __s, size_type __pos = npos) const _NOEXCEPT {
  538. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string_view::find_last_not_of(): received nullptr");
  539. return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(
  540. data(), size(), __s, __pos, traits_type::length(__s));
  541. }
  542. #if _LIBCPP_STD_VER >= 20
  543. constexpr _LIBCPP_HIDE_FROM_ABI bool starts_with(basic_string_view __s) const noexcept {
  544. return size() >= __s.size() && compare(0, __s.size(), __s) == 0;
  545. }
  546. constexpr _LIBCPP_HIDE_FROM_ABI bool starts_with(value_type __c) const noexcept {
  547. return !empty() && _Traits::eq(front(), __c);
  548. }
  549. constexpr _LIBCPP_HIDE_FROM_ABI bool starts_with(const value_type* __s) const noexcept {
  550. return starts_with(basic_string_view(__s));
  551. }
  552. constexpr _LIBCPP_HIDE_FROM_ABI bool ends_with(basic_string_view __s) const noexcept {
  553. return size() >= __s.size() && compare(size() - __s.size(), npos, __s) == 0;
  554. }
  555. constexpr _LIBCPP_HIDE_FROM_ABI bool ends_with(value_type __c) const noexcept {
  556. return !empty() && _Traits::eq(back(), __c);
  557. }
  558. constexpr _LIBCPP_HIDE_FROM_ABI bool ends_with(const value_type* __s) const noexcept {
  559. return ends_with(basic_string_view(__s));
  560. }
  561. #endif
  562. #if _LIBCPP_STD_VER >= 20
  563. constexpr _LIBCPP_HIDE_FROM_ABI bool contains(basic_string_view __sv) const noexcept { return find(__sv) != npos; }
  564. constexpr _LIBCPP_HIDE_FROM_ABI bool contains(value_type __c) const noexcept { return find(__c) != npos; }
  565. constexpr _LIBCPP_HIDE_FROM_ABI bool contains(const value_type* __s) const { return find(__s) != npos; }
  566. #endif
  567. private:
  568. const value_type* __data_;
  569. size_type __size_;
  570. };
  571. _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(basic_string_view);
  572. #if _LIBCPP_STD_VER >= 20
  573. template <class _CharT, class _Traits>
  574. inline constexpr bool ranges::enable_view<basic_string_view<_CharT, _Traits>> = true;
  575. template <class _CharT, class _Traits>
  576. inline constexpr bool ranges::enable_borrowed_range<basic_string_view<_CharT, _Traits> > = true;
  577. #endif // _LIBCPP_STD_VER >= 20
  578. // [string.view.deduct]
  579. #if _LIBCPP_STD_VER >= 20
  580. template <contiguous_iterator _It, sized_sentinel_for<_It> _End>
  581. basic_string_view(_It, _End) -> basic_string_view<iter_value_t<_It>>;
  582. #endif // _LIBCPP_STD_VER >= 20
  583. #if _LIBCPP_STD_VER >= 23
  584. template <ranges::contiguous_range _Range>
  585. basic_string_view(_Range) -> basic_string_view<ranges::range_value_t<_Range>>;
  586. #endif
  587. // [string.view.comparison]
  588. #if _LIBCPP_STD_VER >= 20
  589. template <class _CharT, class _Traits>
  590. _LIBCPP_HIDE_FROM_ABI constexpr bool operator==(basic_string_view<_CharT, _Traits> __lhs,
  591. type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) noexcept {
  592. if (__lhs.size() != __rhs.size())
  593. return false;
  594. return __lhs.compare(__rhs) == 0;
  595. }
  596. template <class _CharT, class _Traits>
  597. _LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(basic_string_view<_CharT, _Traits> __lhs,
  598. type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) noexcept {
  599. if constexpr (requires { typename _Traits::comparison_category; }) {
  600. // [string.view]/4
  601. static_assert(
  602. __comparison_category<typename _Traits::comparison_category>, "return type is not a comparison category type");
  603. return static_cast<typename _Traits::comparison_category>(__lhs.compare(__rhs) <=> 0);
  604. } else {
  605. return static_cast<weak_ordering>(__lhs.compare(__rhs) <=> 0);
  606. }
  607. }
  608. #else
  609. // operator ==
  610. template <class _CharT, class _Traits>
  611. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  612. operator==(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  613. if (__lhs.size() != __rhs.size())
  614. return false;
  615. return __lhs.compare(__rhs) == 0;
  616. }
  617. // The dummy default template parameters are used to work around a MSVC issue with mangling, see VSO-409326 for details.
  618. // This applies to the other sufficient overloads below for the other comparison operators.
  619. template <class _CharT, class _Traits, int = 1>
  620. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  621. operator==(basic_string_view<_CharT, _Traits> __lhs,
  622. __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
  623. if (__lhs.size() != __rhs.size())
  624. return false;
  625. return __lhs.compare(__rhs) == 0;
  626. }
  627. template <class _CharT, class _Traits, int = 2>
  628. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  629. operator==(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
  630. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  631. if (__lhs.size() != __rhs.size())
  632. return false;
  633. return __lhs.compare(__rhs) == 0;
  634. }
  635. // operator !=
  636. template <class _CharT, class _Traits>
  637. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  638. operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  639. if (__lhs.size() != __rhs.size())
  640. return true;
  641. return __lhs.compare(__rhs) != 0;
  642. }
  643. template <class _CharT, class _Traits, int = 1>
  644. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  645. operator!=(basic_string_view<_CharT, _Traits> __lhs,
  646. __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
  647. if (__lhs.size() != __rhs.size())
  648. return true;
  649. return __lhs.compare(__rhs) != 0;
  650. }
  651. template <class _CharT, class _Traits, int = 2>
  652. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  653. operator!=(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
  654. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  655. if (__lhs.size() != __rhs.size())
  656. return true;
  657. return __lhs.compare(__rhs) != 0;
  658. }
  659. // operator <
  660. template <class _CharT, class _Traits>
  661. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  662. operator<(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  663. return __lhs.compare(__rhs) < 0;
  664. }
  665. template <class _CharT, class _Traits, int = 1>
  666. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  667. operator<(basic_string_view<_CharT, _Traits> __lhs,
  668. __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
  669. return __lhs.compare(__rhs) < 0;
  670. }
  671. template <class _CharT, class _Traits, int = 2>
  672. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  673. operator<(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
  674. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  675. return __lhs.compare(__rhs) < 0;
  676. }
  677. // operator >
  678. template <class _CharT, class _Traits>
  679. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  680. operator>(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  681. return __lhs.compare(__rhs) > 0;
  682. }
  683. template <class _CharT, class _Traits, int = 1>
  684. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  685. operator>(basic_string_view<_CharT, _Traits> __lhs,
  686. __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
  687. return __lhs.compare(__rhs) > 0;
  688. }
  689. template <class _CharT, class _Traits, int = 2>
  690. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  691. operator>(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
  692. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  693. return __lhs.compare(__rhs) > 0;
  694. }
  695. // operator <=
  696. template <class _CharT, class _Traits>
  697. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  698. operator<=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  699. return __lhs.compare(__rhs) <= 0;
  700. }
  701. template <class _CharT, class _Traits, int = 1>
  702. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  703. operator<=(basic_string_view<_CharT, _Traits> __lhs,
  704. __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
  705. return __lhs.compare(__rhs) <= 0;
  706. }
  707. template <class _CharT, class _Traits, int = 2>
  708. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  709. operator<=(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
  710. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  711. return __lhs.compare(__rhs) <= 0;
  712. }
  713. // operator >=
  714. template <class _CharT, class _Traits>
  715. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  716. operator>=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  717. return __lhs.compare(__rhs) >= 0;
  718. }
  719. template <class _CharT, class _Traits, int = 1>
  720. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  721. operator>=(basic_string_view<_CharT, _Traits> __lhs,
  722. __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
  723. return __lhs.compare(__rhs) >= 0;
  724. }
  725. template <class _CharT, class _Traits, int = 2>
  726. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  727. operator>=(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
  728. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  729. return __lhs.compare(__rhs) >= 0;
  730. }
  731. #endif // _LIBCPP_STD_VER >= 20
  732. template <class _CharT, class _Traits>
  733. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  734. operator<<(basic_ostream<_CharT, _Traits>& __os, basic_string_view<_CharT, _Traits> __str);
  735. // [string.view.hash]
  736. template <class _CharT>
  737. struct __string_view_hash : public __unary_function<basic_string_view<_CharT, char_traits<_CharT> >, size_t> {
  738. _LIBCPP_HIDE_FROM_ABI size_t operator()(const basic_string_view<_CharT, char_traits<_CharT> > __val) const _NOEXCEPT {
  739. return std::__do_string_hash(__val.data(), __val.data() + __val.size());
  740. }
  741. };
  742. template <>
  743. struct hash<basic_string_view<char, char_traits<char> > > : __string_view_hash<char> {};
  744. #ifndef _LIBCPP_HAS_NO_CHAR8_T
  745. template <>
  746. struct hash<basic_string_view<char8_t, char_traits<char8_t> > > : __string_view_hash<char8_t> {};
  747. #endif
  748. template <>
  749. struct hash<basic_string_view<char16_t, char_traits<char16_t> > > : __string_view_hash<char16_t> {};
  750. template <>
  751. struct hash<basic_string_view<char32_t, char_traits<char32_t> > > : __string_view_hash<char32_t> {};
  752. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  753. template <>
  754. struct hash<basic_string_view<wchar_t, char_traits<wchar_t> > > : __string_view_hash<wchar_t> {};
  755. #endif
  756. #if _LIBCPP_STD_VER >= 14
  757. inline namespace literals {
  758. inline namespace string_view_literals {
  759. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR basic_string_view<char>
  760. operator""sv(const char* __str, size_t __len) _NOEXCEPT {
  761. return basic_string_view<char>(__str, __len);
  762. }
  763. # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  764. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR basic_string_view<wchar_t>
  765. operator""sv(const wchar_t* __str, size_t __len) _NOEXCEPT {
  766. return basic_string_view<wchar_t>(__str, __len);
  767. }
  768. # endif
  769. # ifndef _LIBCPP_HAS_NO_CHAR8_T
  770. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR basic_string_view<char8_t>
  771. operator""sv(const char8_t* __str, size_t __len) _NOEXCEPT {
  772. return basic_string_view<char8_t>(__str, __len);
  773. }
  774. # endif
  775. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR basic_string_view<char16_t>
  776. operator""sv(const char16_t* __str, size_t __len) _NOEXCEPT {
  777. return basic_string_view<char16_t>(__str, __len);
  778. }
  779. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR basic_string_view<char32_t>
  780. operator""sv(const char32_t* __str, size_t __len) _NOEXCEPT {
  781. return basic_string_view<char32_t>(__str, __len);
  782. }
  783. } // namespace string_view_literals
  784. } // namespace literals
  785. #endif
  786. _LIBCPP_END_NAMESPACE_STD
  787. _LIBCPP_POP_MACROS
  788. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  789. # include <algorithm>
  790. # include <concepts>
  791. # include <cstdlib>
  792. # include <iterator>
  793. # include <type_traits>
  794. #endif
  795. #endif // _LIBCPP_STRING_VIEW