string_view 43 KB

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