string_view 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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. // This will result in creating an invalid `string_view` object -- some calculations involving `size` would
  271. // overflow, making it effectively truncated.
  272. _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
  273. __len <= static_cast<size_type>(numeric_limits<difference_type>::max()),
  274. "string_view::string_view(_CharT *, size_t): length does not fit in difference_type");
  275. _LIBCPP_ASSERT_NON_NULL(
  276. __len == 0 || __s != nullptr, "string_view::string_view(_CharT *, size_t): received nullptr");
  277. #endif
  278. }
  279. #if _LIBCPP_STD_VER >= 20
  280. template <contiguous_iterator _It, sized_sentinel_for<_It> _End>
  281. requires(is_same_v<iter_value_t<_It>, _CharT> && !is_convertible_v<_End, size_type>)
  282. constexpr _LIBCPP_HIDE_FROM_ABI basic_string_view(_It __begin, _End __end)
  283. : __data_(std::to_address(__begin)), __size_(__end - __begin) {
  284. _LIBCPP_ASSERT_VALID_INPUT_RANGE(
  285. (__end - __begin) >= 0, "std::string_view::string_view(iterator, sentinel) received invalid range");
  286. }
  287. #endif // _LIBCPP_STD_VER >= 20
  288. #if _LIBCPP_STD_VER >= 23
  289. template <class _Range>
  290. requires(!is_same_v<remove_cvref_t<_Range>, basic_string_view> && ranges::contiguous_range<_Range> &&
  291. ranges::sized_range<_Range> && is_same_v<ranges::range_value_t<_Range>, _CharT> &&
  292. !is_convertible_v<_Range, const _CharT*> &&
  293. (!requires(remove_cvref_t<_Range>& __d) { __d.operator std::basic_string_view<_CharT, _Traits>(); }))
  294. constexpr explicit _LIBCPP_HIDE_FROM_ABI basic_string_view(_Range&& __r)
  295. : __data_(ranges::data(__r)), __size_(ranges::size(__r)) {}
  296. #endif // _LIBCPP_STD_VER >= 23
  297. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI basic_string_view(const _CharT* __s)
  298. : __data_(__s), __size_(std::__char_traits_length_checked<_Traits>(__s)) {}
  299. #if _LIBCPP_STD_VER >= 20
  300. basic_string_view(nullptr_t) = delete;
  301. #endif
  302. // [string.view.iterators], iterators
  303. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return cbegin(); }
  304. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return cend(); }
  305. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT {
  306. #ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
  307. return std::__make_bounded_iter(data(), data(), data() + size());
  308. #else
  309. return const_iterator(__data_);
  310. #endif
  311. }
  312. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT {
  313. #ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
  314. return std::__make_bounded_iter(data() + size(), data(), data() + size());
  315. #else
  316. return const_iterator(__data_ + __size_);
  317. #endif
  318. }
  319. _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT {
  320. return const_reverse_iterator(cend());
  321. }
  322. _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT {
  323. return const_reverse_iterator(cbegin());
  324. }
  325. _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT {
  326. return const_reverse_iterator(cend());
  327. }
  328. _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT {
  329. return const_reverse_iterator(cbegin());
  330. }
  331. // [string.view.capacity], capacity
  332. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __size_; }
  333. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI size_type length() const _NOEXCEPT { return __size_; }
  334. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
  335. return numeric_limits<size_type>::max() / sizeof(value_type);
  336. }
  337. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {
  338. return __size_ == 0;
  339. }
  340. // [string.view.access], element access
  341. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_reference operator[](size_type __pos) const _NOEXCEPT {
  342. return _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__pos < size(), "string_view[] index out of bounds"), __data_[__pos];
  343. }
  344. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_reference at(size_type __pos) const {
  345. return __pos >= size() ? (__throw_out_of_range("string_view::at"), __data_[0]) : __data_[__pos];
  346. }
  347. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_reference front() const _NOEXCEPT {
  348. return _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string_view::front(): string is empty"), __data_[0];
  349. }
  350. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_reference back() const _NOEXCEPT {
  351. return _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string_view::back(): string is empty"), __data_[__size_ - 1];
  352. }
  353. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_pointer data() const _NOEXCEPT { return __data_; }
  354. // [string.view.modifiers], modifiers:
  355. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI void remove_prefix(size_type __n) _NOEXCEPT {
  356. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n <= size(), "remove_prefix() can't remove more than size()");
  357. __data_ += __n;
  358. __size_ -= __n;
  359. }
  360. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI void remove_suffix(size_type __n) _NOEXCEPT {
  361. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n <= size(), "remove_suffix() can't remove more than size()");
  362. __size_ -= __n;
  363. }
  364. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI void swap(basic_string_view& __other) _NOEXCEPT {
  365. const value_type* __p = __data_;
  366. __data_ = __other.__data_;
  367. __other.__data_ = __p;
  368. size_type __sz = __size_;
  369. __size_ = __other.__size_;
  370. __other.__size_ = __sz;
  371. }
  372. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  373. copy(_CharT* __s, size_type __n, size_type __pos = 0) const {
  374. if (__pos > size())
  375. __throw_out_of_range("string_view::copy");
  376. size_type __rlen = std::min(__n, size() - __pos);
  377. _Traits::copy(__s, data() + __pos, __rlen);
  378. return __rlen;
  379. }
  380. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI basic_string_view substr(size_type __pos = 0, size_type __n = npos) const {
  381. return __pos > size() ? (__throw_out_of_range("string_view::substr"), basic_string_view())
  382. : basic_string_view(data() + __pos, std::min(__n, size() - __pos));
  383. }
  384. _LIBCPP_CONSTEXPR_SINCE_CXX14 int compare(basic_string_view __sv) const _NOEXCEPT {
  385. size_type __rlen = std::min(size(), __sv.size());
  386. int __retval = _Traits::compare(data(), __sv.data(), __rlen);
  387. if (__retval == 0) // first __rlen chars matched
  388. __retval = size() == __sv.size() ? 0 : (size() < __sv.size() ? -1 : 1);
  389. return __retval;
  390. }
  391. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI int
  392. compare(size_type __pos1, size_type __n1, basic_string_view __sv) const {
  393. return substr(__pos1, __n1).compare(__sv);
  394. }
  395. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI int
  396. compare(size_type __pos1, size_type __n1, basic_string_view __sv, size_type __pos2, size_type __n2) const {
  397. return substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
  398. }
  399. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI int compare(const _CharT* __s) const _NOEXCEPT {
  400. return compare(basic_string_view(__s));
  401. }
  402. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI int
  403. compare(size_type __pos1, size_type __n1, const _CharT* __s) const {
  404. return substr(__pos1, __n1).compare(basic_string_view(__s));
  405. }
  406. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI int
  407. compare(size_type __pos1, size_type __n1, const _CharT* __s, size_type __n2) const {
  408. return substr(__pos1, __n1).compare(basic_string_view(__s, __n2));
  409. }
  410. // find
  411. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  412. find(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT {
  413. _LIBCPP_ASSERT_NON_NULL(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
  414. return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __s.data(), __pos, __s.size());
  415. }
  416. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type find(_CharT __c, size_type __pos = 0) const _NOEXCEPT {
  417. return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
  418. }
  419. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  420. find(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT {
  421. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string_view::find(): received nullptr");
  422. return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
  423. }
  424. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  425. find(const _CharT* __s, size_type __pos = 0) const _NOEXCEPT {
  426. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string_view::find(): received nullptr");
  427. return std::__str_find<value_type, size_type, traits_type, npos>(
  428. data(), size(), __s, __pos, traits_type::length(__s));
  429. }
  430. // rfind
  431. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  432. rfind(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT {
  433. _LIBCPP_ASSERT_NON_NULL(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
  434. return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __s.data(), __pos, __s.size());
  435. }
  436. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  437. rfind(_CharT __c, size_type __pos = npos) const _NOEXCEPT {
  438. return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
  439. }
  440. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  441. rfind(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT {
  442. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string_view::rfind(): received nullptr");
  443. return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
  444. }
  445. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  446. rfind(const _CharT* __s, size_type __pos = npos) const _NOEXCEPT {
  447. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string_view::rfind(): received nullptr");
  448. return std::__str_rfind<value_type, size_type, traits_type, npos>(
  449. data(), size(), __s, __pos, traits_type::length(__s));
  450. }
  451. // find_first_of
  452. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  453. find_first_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT {
  454. _LIBCPP_ASSERT_NON_NULL(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_of(): received nullptr");
  455. return std::__str_find_first_of<value_type, size_type, traits_type, npos>(
  456. data(), size(), __s.data(), __pos, __s.size());
  457. }
  458. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  459. find_first_of(_CharT __c, size_type __pos = 0) const _NOEXCEPT {
  460. return find(__c, __pos);
  461. }
  462. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  463. find_first_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT {
  464. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string_view::find_first_of(): received nullptr");
  465. return std::__str_find_first_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
  466. }
  467. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  468. find_first_of(const _CharT* __s, size_type __pos = 0) const _NOEXCEPT {
  469. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string_view::find_first_of(): received nullptr");
  470. return std::__str_find_first_of<value_type, size_type, traits_type, npos>(
  471. data(), size(), __s, __pos, traits_type::length(__s));
  472. }
  473. // find_last_of
  474. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  475. find_last_of(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT {
  476. _LIBCPP_ASSERT_NON_NULL(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_of(): received nullptr");
  477. return std::__str_find_last_of<value_type, size_type, traits_type, npos>(
  478. data(), size(), __s.data(), __pos, __s.size());
  479. }
  480. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  481. find_last_of(_CharT __c, size_type __pos = npos) const _NOEXCEPT {
  482. return rfind(__c, __pos);
  483. }
  484. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  485. find_last_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT {
  486. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string_view::find_last_of(): received nullptr");
  487. return std::__str_find_last_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
  488. }
  489. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  490. find_last_of(const _CharT* __s, size_type __pos = npos) const _NOEXCEPT {
  491. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string_view::find_last_of(): received nullptr");
  492. return std::__str_find_last_of<value_type, size_type, traits_type, npos>(
  493. data(), size(), __s, __pos, traits_type::length(__s));
  494. }
  495. // find_first_not_of
  496. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  497. find_first_not_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT {
  498. _LIBCPP_ASSERT_NON_NULL(
  499. __s.size() == 0 || __s.data() != nullptr, "string_view::find_first_not_of(): received nullptr");
  500. return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(
  501. data(), size(), __s.data(), __pos, __s.size());
  502. }
  503. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  504. find_first_not_of(_CharT __c, size_type __pos = 0) const _NOEXCEPT {
  505. return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
  506. }
  507. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  508. find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT {
  509. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string_view::find_first_not_of(): received nullptr");
  510. return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
  511. }
  512. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  513. find_first_not_of(const _CharT* __s, size_type __pos = 0) const _NOEXCEPT {
  514. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string_view::find_first_not_of(): received nullptr");
  515. return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(
  516. data(), size(), __s, __pos, traits_type::length(__s));
  517. }
  518. // find_last_not_of
  519. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  520. find_last_not_of(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT {
  521. _LIBCPP_ASSERT_NON_NULL(
  522. __s.size() == 0 || __s.data() != nullptr, "string_view::find_last_not_of(): received nullptr");
  523. return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(
  524. data(), size(), __s.data(), __pos, __s.size());
  525. }
  526. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  527. find_last_not_of(_CharT __c, size_type __pos = npos) const _NOEXCEPT {
  528. return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
  529. }
  530. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  531. find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT {
  532. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string_view::find_last_not_of(): received nullptr");
  533. return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
  534. }
  535. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
  536. find_last_not_of(const _CharT* __s, size_type __pos = npos) const _NOEXCEPT {
  537. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string_view::find_last_not_of(): received nullptr");
  538. return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(
  539. data(), size(), __s, __pos, traits_type::length(__s));
  540. }
  541. #if _LIBCPP_STD_VER >= 20
  542. constexpr _LIBCPP_HIDE_FROM_ABI bool starts_with(basic_string_view __s) const noexcept {
  543. return size() >= __s.size() && compare(0, __s.size(), __s) == 0;
  544. }
  545. constexpr _LIBCPP_HIDE_FROM_ABI bool starts_with(value_type __c) const noexcept {
  546. return !empty() && _Traits::eq(front(), __c);
  547. }
  548. constexpr _LIBCPP_HIDE_FROM_ABI bool starts_with(const value_type* __s) const noexcept {
  549. return starts_with(basic_string_view(__s));
  550. }
  551. constexpr _LIBCPP_HIDE_FROM_ABI bool ends_with(basic_string_view __s) const noexcept {
  552. return size() >= __s.size() && compare(size() - __s.size(), npos, __s) == 0;
  553. }
  554. constexpr _LIBCPP_HIDE_FROM_ABI bool ends_with(value_type __c) const noexcept {
  555. return !empty() && _Traits::eq(back(), __c);
  556. }
  557. constexpr _LIBCPP_HIDE_FROM_ABI bool ends_with(const value_type* __s) const noexcept {
  558. return ends_with(basic_string_view(__s));
  559. }
  560. #endif
  561. #if _LIBCPP_STD_VER >= 20
  562. constexpr _LIBCPP_HIDE_FROM_ABI bool contains(basic_string_view __sv) const noexcept { return find(__sv) != npos; }
  563. constexpr _LIBCPP_HIDE_FROM_ABI bool contains(value_type __c) const noexcept { return find(__c) != npos; }
  564. constexpr _LIBCPP_HIDE_FROM_ABI bool contains(const value_type* __s) const { return find(__s) != npos; }
  565. #endif
  566. private:
  567. const value_type* __data_;
  568. size_type __size_;
  569. };
  570. _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(basic_string_view);
  571. #if _LIBCPP_STD_VER >= 20
  572. template <class _CharT, class _Traits>
  573. inline constexpr bool ranges::enable_view<basic_string_view<_CharT, _Traits>> = true;
  574. template <class _CharT, class _Traits>
  575. inline constexpr bool ranges::enable_borrowed_range<basic_string_view<_CharT, _Traits> > = true;
  576. #endif // _LIBCPP_STD_VER >= 20
  577. // [string.view.deduct]
  578. #if _LIBCPP_STD_VER >= 20
  579. template <contiguous_iterator _It, sized_sentinel_for<_It> _End>
  580. basic_string_view(_It, _End) -> basic_string_view<iter_value_t<_It>>;
  581. #endif // _LIBCPP_STD_VER >= 20
  582. #if _LIBCPP_STD_VER >= 23
  583. template <ranges::contiguous_range _Range>
  584. basic_string_view(_Range) -> basic_string_view<ranges::range_value_t<_Range>>;
  585. #endif
  586. // [string.view.comparison]
  587. #if _LIBCPP_STD_VER >= 20
  588. template <class _CharT, class _Traits>
  589. _LIBCPP_HIDE_FROM_ABI constexpr bool operator==(basic_string_view<_CharT, _Traits> __lhs,
  590. type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) noexcept {
  591. if (__lhs.size() != __rhs.size())
  592. return false;
  593. return __lhs.compare(__rhs) == 0;
  594. }
  595. template <class _CharT, class _Traits>
  596. _LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(basic_string_view<_CharT, _Traits> __lhs,
  597. type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) noexcept {
  598. if constexpr (requires { typename _Traits::comparison_category; }) {
  599. // [string.view]/4
  600. static_assert(
  601. __comparison_category<typename _Traits::comparison_category>, "return type is not a comparison category type");
  602. return static_cast<typename _Traits::comparison_category>(__lhs.compare(__rhs) <=> 0);
  603. } else {
  604. return static_cast<weak_ordering>(__lhs.compare(__rhs) <=> 0);
  605. }
  606. }
  607. #else
  608. // operator ==
  609. template <class _CharT, class _Traits>
  610. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  611. operator==(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  612. if (__lhs.size() != __rhs.size())
  613. return false;
  614. return __lhs.compare(__rhs) == 0;
  615. }
  616. // The dummy default template parameters are used to work around a MSVC issue with mangling, see VSO-409326 for details.
  617. // This applies to the other sufficient overloads below for the other comparison operators.
  618. template <class _CharT, class _Traits, int = 1>
  619. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  620. operator==(basic_string_view<_CharT, _Traits> __lhs,
  621. __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
  622. if (__lhs.size() != __rhs.size())
  623. return false;
  624. return __lhs.compare(__rhs) == 0;
  625. }
  626. template <class _CharT, class _Traits, int = 2>
  627. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  628. operator==(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
  629. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  630. if (__lhs.size() != __rhs.size())
  631. return false;
  632. return __lhs.compare(__rhs) == 0;
  633. }
  634. // operator !=
  635. template <class _CharT, class _Traits>
  636. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  637. operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  638. if (__lhs.size() != __rhs.size())
  639. return true;
  640. return __lhs.compare(__rhs) != 0;
  641. }
  642. template <class _CharT, class _Traits, int = 1>
  643. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  644. operator!=(basic_string_view<_CharT, _Traits> __lhs,
  645. __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
  646. if (__lhs.size() != __rhs.size())
  647. return true;
  648. return __lhs.compare(__rhs) != 0;
  649. }
  650. template <class _CharT, class _Traits, int = 2>
  651. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  652. operator!=(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
  653. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  654. if (__lhs.size() != __rhs.size())
  655. return true;
  656. return __lhs.compare(__rhs) != 0;
  657. }
  658. // operator <
  659. template <class _CharT, class _Traits>
  660. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  661. operator<(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  662. return __lhs.compare(__rhs) < 0;
  663. }
  664. template <class _CharT, class _Traits, int = 1>
  665. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  666. operator<(basic_string_view<_CharT, _Traits> __lhs,
  667. __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
  668. return __lhs.compare(__rhs) < 0;
  669. }
  670. template <class _CharT, class _Traits, int = 2>
  671. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  672. operator<(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
  673. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  674. return __lhs.compare(__rhs) < 0;
  675. }
  676. // operator >
  677. template <class _CharT, class _Traits>
  678. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  679. operator>(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  680. return __lhs.compare(__rhs) > 0;
  681. }
  682. template <class _CharT, class _Traits, int = 1>
  683. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  684. operator>(basic_string_view<_CharT, _Traits> __lhs,
  685. __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
  686. return __lhs.compare(__rhs) > 0;
  687. }
  688. template <class _CharT, class _Traits, int = 2>
  689. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  690. operator>(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
  691. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  692. return __lhs.compare(__rhs) > 0;
  693. }
  694. // operator <=
  695. template <class _CharT, class _Traits>
  696. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  697. operator<=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  698. return __lhs.compare(__rhs) <= 0;
  699. }
  700. template <class _CharT, class _Traits, int = 1>
  701. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  702. operator<=(basic_string_view<_CharT, _Traits> __lhs,
  703. __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
  704. return __lhs.compare(__rhs) <= 0;
  705. }
  706. template <class _CharT, class _Traits, int = 2>
  707. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  708. operator<=(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
  709. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  710. return __lhs.compare(__rhs) <= 0;
  711. }
  712. // operator >=
  713. template <class _CharT, class _Traits>
  714. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  715. operator>=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  716. return __lhs.compare(__rhs) >= 0;
  717. }
  718. template <class _CharT, class _Traits, int = 1>
  719. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  720. operator>=(basic_string_view<_CharT, _Traits> __lhs,
  721. __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
  722. return __lhs.compare(__rhs) >= 0;
  723. }
  724. template <class _CharT, class _Traits, int = 2>
  725. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
  726. operator>=(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
  727. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
  728. return __lhs.compare(__rhs) >= 0;
  729. }
  730. #endif // _LIBCPP_STD_VER >= 20
  731. template <class _CharT, class _Traits>
  732. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  733. operator<<(basic_ostream<_CharT, _Traits>& __os, basic_string_view<_CharT, _Traits> __str);
  734. // [string.view.hash]
  735. template <class _CharT>
  736. struct __string_view_hash : public __unary_function<basic_string_view<_CharT, char_traits<_CharT> >, size_t> {
  737. _LIBCPP_HIDE_FROM_ABI size_t operator()(const basic_string_view<_CharT, char_traits<_CharT> > __val) const _NOEXCEPT {
  738. return std::__do_string_hash(__val.data(), __val.data() + __val.size());
  739. }
  740. };
  741. template <>
  742. struct hash<basic_string_view<char, char_traits<char> > > : __string_view_hash<char> {};
  743. #ifndef _LIBCPP_HAS_NO_CHAR8_T
  744. template <>
  745. struct hash<basic_string_view<char8_t, char_traits<char8_t> > > : __string_view_hash<char8_t> {};
  746. #endif
  747. template <>
  748. struct hash<basic_string_view<char16_t, char_traits<char16_t> > > : __string_view_hash<char16_t> {};
  749. template <>
  750. struct hash<basic_string_view<char32_t, char_traits<char32_t> > > : __string_view_hash<char32_t> {};
  751. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  752. template <>
  753. struct hash<basic_string_view<wchar_t, char_traits<wchar_t> > > : __string_view_hash<wchar_t> {};
  754. #endif
  755. #if _LIBCPP_STD_VER >= 14
  756. inline namespace literals {
  757. inline namespace string_view_literals {
  758. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR basic_string_view<char>
  759. operator""sv(const char* __str, size_t __len) _NOEXCEPT {
  760. return basic_string_view<char>(__str, __len);
  761. }
  762. # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  763. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR basic_string_view<wchar_t>
  764. operator""sv(const wchar_t* __str, size_t __len) _NOEXCEPT {
  765. return basic_string_view<wchar_t>(__str, __len);
  766. }
  767. # endif
  768. # ifndef _LIBCPP_HAS_NO_CHAR8_T
  769. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR basic_string_view<char8_t>
  770. operator""sv(const char8_t* __str, size_t __len) _NOEXCEPT {
  771. return basic_string_view<char8_t>(__str, __len);
  772. }
  773. # endif
  774. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR basic_string_view<char16_t>
  775. operator""sv(const char16_t* __str, size_t __len) _NOEXCEPT {
  776. return basic_string_view<char16_t>(__str, __len);
  777. }
  778. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR basic_string_view<char32_t>
  779. operator""sv(const char32_t* __str, size_t __len) _NOEXCEPT {
  780. return basic_string_view<char32_t>(__str, __len);
  781. }
  782. } // namespace string_view_literals
  783. } // namespace literals
  784. #endif
  785. _LIBCPP_END_NAMESPACE_STD
  786. _LIBCPP_POP_MACROS
  787. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  788. # include <algorithm>
  789. # include <concepts>
  790. # include <cstdlib>
  791. # include <iterator>
  792. # include <type_traits>
  793. #endif
  794. #endif // _LIBCPP_STRING_VIEW