string_view 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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++2b
  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++2b
  150. constexpr bool contains(charT c) const noexcept; // C++2b
  151. constexpr bool contains(const charT* s) const; // C++2b
  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/concepts.h>
  182. #include <__iterator/readable_traits.h>
  183. #include <__iterator/reverse_iterator.h>
  184. #include <__memory/pointer_traits.h>
  185. #include <__ranges/concepts.h>
  186. #include <__ranges/data.h>
  187. #include <__ranges/enable_borrowed_range.h>
  188. #include <__ranges/enable_view.h>
  189. #include <__ranges/size.h>
  190. #include <__string/char_traits.h>
  191. #include <iosfwd>
  192. #include <limits>
  193. #include <stdexcept>
  194. #include <type_traits>
  195. #include <version>
  196. // standard-mandated includes
  197. // [iterator.range]
  198. #include <__iterator/access.h>
  199. #include <__iterator/data.h>
  200. #include <__iterator/empty.h>
  201. #include <__iterator/reverse_access.h>
  202. #include <__iterator/size.h>
  203. // [string.view.synop]
  204. #include <compare>
  205. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  206. # pragma GCC system_header
  207. #endif
  208. _LIBCPP_PUSH_MACROS
  209. #include <__undef_macros>
  210. _LIBCPP_BEGIN_NAMESPACE_STD
  211. // TODO: This is a workaround for some vendors to carry a downstream diff to accept `nullptr` in
  212. // string_view constructors. This can be refactored when this exact form isn't needed anymore.
  213. template <class _Traits>
  214. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
  215. inline size_t __char_traits_length_checked(const typename _Traits::char_type* __s) _NOEXCEPT {
  216. // This needs to be a single statement for C++11 constexpr
  217. return _LIBCPP_ASSERT(__s != nullptr, "null pointer passed to non-null argument of char_traits<...>::length"), _Traits::length(__s);
  218. }
  219. template<class _CharT, class _Traits>
  220. class basic_string_view {
  221. public:
  222. // types
  223. using traits_type = _Traits;
  224. using value_type = _CharT;
  225. using pointer = _CharT*;
  226. using const_pointer = const _CharT*;
  227. using reference = _CharT&;
  228. using const_reference = const _CharT&;
  229. using const_iterator = const_pointer; // See [string.view.iterators]
  230. using iterator = const_iterator;
  231. using const_reverse_iterator = _VSTD::reverse_iterator<const_iterator>;
  232. using reverse_iterator = const_reverse_iterator;
  233. using size_type = size_t;
  234. using difference_type = ptrdiff_t;
  235. static _LIBCPP_CONSTEXPR const size_type npos = -1; // size_type(-1);
  236. static_assert((!is_array<value_type>::value), "Character type of basic_string_view must not be an array");
  237. static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string_view must be standard-layout");
  238. static_assert(( is_trivial<value_type>::value), "Character type of basic_string_view must be trivial");
  239. static_assert((is_same<_CharT, typename traits_type::char_type>::value),
  240. "traits_type::char_type must be the same type as CharT");
  241. // [string.view.cons], construct/copy
  242. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  243. basic_string_view() _NOEXCEPT : __data_(nullptr), __size_(0) {}
  244. _LIBCPP_INLINE_VISIBILITY
  245. basic_string_view(const basic_string_view&) _NOEXCEPT = default;
  246. _LIBCPP_INLINE_VISIBILITY
  247. basic_string_view& operator=(const basic_string_view&) _NOEXCEPT = default;
  248. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  249. basic_string_view(nullptr_t, size_t) = delete;
  250. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  251. basic_string_view(const _CharT* __s, size_type __len) _NOEXCEPT
  252. : __data_(__s), __size_(__len)
  253. {
  254. #if _LIBCPP_STD_VER > 11
  255. _LIBCPP_ASSERT(__len == 0 || __s != nullptr, "string_view::string_view(_CharT *, size_t): received nullptr");
  256. #endif
  257. }
  258. #if _LIBCPP_STD_VER > 17
  259. template <contiguous_iterator _It, sized_sentinel_for<_It> _End>
  260. requires (is_same_v<iter_value_t<_It>, _CharT> && !is_convertible_v<_End, size_type>)
  261. constexpr _LIBCPP_HIDE_FROM_ABI basic_string_view(_It __begin, _End __end)
  262. : __data_(_VSTD::to_address(__begin)), __size_(__end - __begin)
  263. {
  264. _LIBCPP_ASSERT((__end - __begin) >= 0, "std::string_view::string_view(iterator, sentinel) received invalid range");
  265. }
  266. #endif // _LIBCPP_STD_VER > 17
  267. #if _LIBCPP_STD_VER > 20
  268. template <class _Range>
  269. requires (
  270. !is_same_v<remove_cvref_t<_Range>, basic_string_view> &&
  271. ranges::contiguous_range<_Range> &&
  272. ranges::sized_range<_Range> &&
  273. is_same_v<ranges::range_value_t<_Range>, _CharT> &&
  274. !is_convertible_v<_Range, const _CharT*> &&
  275. (!requires(remove_cvref_t<_Range>& __d) {
  276. __d.operator _VSTD::basic_string_view<_CharT, _Traits>();
  277. }) &&
  278. (!requires {
  279. typename remove_reference_t<_Range>::traits_type;
  280. } || is_same_v<typename remove_reference_t<_Range>::traits_type, _Traits>)
  281. )
  282. constexpr explicit _LIBCPP_HIDE_FROM_ABI
  283. basic_string_view(_Range&& __r) : __data_(ranges::data(__r)), __size_(ranges::size(__r)) {}
  284. #endif // _LIBCPP_STD_VER > 20
  285. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  286. basic_string_view(const _CharT* __s)
  287. : __data_(__s), __size_(_VSTD::__char_traits_length_checked<_Traits>(__s)) {}
  288. #if _LIBCPP_STD_VER > 17
  289. basic_string_view(nullptr_t) = delete;
  290. #endif
  291. // [string.view.iterators], iterators
  292. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  293. const_iterator begin() const _NOEXCEPT { return cbegin(); }
  294. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  295. const_iterator end() const _NOEXCEPT { return cend(); }
  296. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  297. const_iterator cbegin() const _NOEXCEPT { return __data_; }
  298. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  299. const_iterator cend() const _NOEXCEPT { return __data_ + __size_; }
  300. _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_INLINE_VISIBILITY
  301. const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
  302. _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_INLINE_VISIBILITY
  303. const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
  304. _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_INLINE_VISIBILITY
  305. const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
  306. _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_INLINE_VISIBILITY
  307. const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
  308. // [string.view.capacity], capacity
  309. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  310. size_type size() const _NOEXCEPT { return __size_; }
  311. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  312. size_type length() const _NOEXCEPT { return __size_; }
  313. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  314. size_type max_size() const _NOEXCEPT { return numeric_limits<size_type>::max() / sizeof(value_type); }
  315. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  316. bool empty() const _NOEXCEPT { return __size_ == 0; }
  317. // [string.view.access], element access
  318. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  319. const_reference operator[](size_type __pos) const _NOEXCEPT {
  320. return _LIBCPP_ASSERT(__pos < size(), "string_view[] index out of bounds"), __data_[__pos];
  321. }
  322. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  323. const_reference at(size_type __pos) const
  324. {
  325. return __pos >= size()
  326. ? (__throw_out_of_range("string_view::at"), __data_[0])
  327. : __data_[__pos];
  328. }
  329. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  330. const_reference front() const _NOEXCEPT
  331. {
  332. return _LIBCPP_ASSERT(!empty(), "string_view::front(): string is empty"), __data_[0];
  333. }
  334. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  335. const_reference back() const _NOEXCEPT
  336. {
  337. return _LIBCPP_ASSERT(!empty(), "string_view::back(): string is empty"), __data_[__size_-1];
  338. }
  339. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  340. const_pointer data() const _NOEXCEPT { return __data_; }
  341. // [string.view.modifiers], modifiers:
  342. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  343. void remove_prefix(size_type __n) _NOEXCEPT
  344. {
  345. _LIBCPP_ASSERT(__n <= size(), "remove_prefix() can't remove more than size()");
  346. __data_ += __n;
  347. __size_ -= __n;
  348. }
  349. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  350. void remove_suffix(size_type __n) _NOEXCEPT
  351. {
  352. _LIBCPP_ASSERT(__n <= size(), "remove_suffix() can't remove more than size()");
  353. __size_ -= __n;
  354. }
  355. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  356. void swap(basic_string_view& __other) _NOEXCEPT
  357. {
  358. const value_type *__p = __data_;
  359. __data_ = __other.__data_;
  360. __other.__data_ = __p;
  361. size_type __sz = __size_;
  362. __size_ = __other.__size_;
  363. __other.__size_ = __sz;
  364. }
  365. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  366. size_type copy(_CharT* __s, size_type __n, size_type __pos = 0) const
  367. {
  368. if (__pos > size())
  369. __throw_out_of_range("string_view::copy");
  370. size_type __rlen = _VSTD::min(__n, size() - __pos);
  371. _Traits::copy(__s, data() + __pos, __rlen);
  372. return __rlen;
  373. }
  374. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  375. basic_string_view substr(size_type __pos = 0, size_type __n = npos) const
  376. {
  377. return __pos > size()
  378. ? (__throw_out_of_range("string_view::substr"), basic_string_view())
  379. : basic_string_view(data() + __pos, _VSTD::min(__n, size() - __pos));
  380. }
  381. _LIBCPP_CONSTEXPR_SINCE_CXX14 int compare(basic_string_view __sv) const _NOEXCEPT
  382. {
  383. size_type __rlen = _VSTD::min(size(), __sv.size());
  384. int __retval = _Traits::compare(data(), __sv.data(), __rlen);
  385. if (__retval == 0) // first __rlen chars matched
  386. __retval = size() == __sv.size() ? 0 : (size() < __sv.size() ? -1 : 1);
  387. return __retval;
  388. }
  389. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  390. int compare(size_type __pos1, size_type __n1, basic_string_view __sv) const
  391. {
  392. return substr(__pos1, __n1).compare(__sv);
  393. }
  394. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  395. int compare( size_type __pos1, size_type __n1,
  396. basic_string_view __sv, size_type __pos2, size_type __n2) const
  397. {
  398. return substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
  399. }
  400. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  401. int compare(const _CharT* __s) const _NOEXCEPT
  402. {
  403. return compare(basic_string_view(__s));
  404. }
  405. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  406. int compare(size_type __pos1, size_type __n1, const _CharT* __s) const
  407. {
  408. return substr(__pos1, __n1).compare(basic_string_view(__s));
  409. }
  410. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  411. int compare(size_type __pos1, size_type __n1, const _CharT* __s, size_type __n2) const
  412. {
  413. return substr(__pos1, __n1).compare(basic_string_view(__s, __n2));
  414. }
  415. // find
  416. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  417. size_type find(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
  418. {
  419. _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
  420. return std::__str_find<value_type, size_type, traits_type, npos>
  421. (data(), size(), __s.data(), __pos, __s.size());
  422. }
  423. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  424. size_type find(_CharT __c, size_type __pos = 0) const _NOEXCEPT
  425. {
  426. return std::__str_find<value_type, size_type, traits_type, npos>
  427. (data(), size(), __c, __pos);
  428. }
  429. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  430. size_type find(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
  431. {
  432. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find(): received nullptr");
  433. return std::__str_find<value_type, size_type, traits_type, npos>
  434. (data(), size(), __s, __pos, __n);
  435. }
  436. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  437. size_type find(const _CharT* __s, size_type __pos = 0) const _NOEXCEPT
  438. {
  439. _LIBCPP_ASSERT(__s != nullptr, "string_view::find(): received nullptr");
  440. return std::__str_find<value_type, size_type, traits_type, npos>
  441. (data(), size(), __s, __pos, traits_type::length(__s));
  442. }
  443. // rfind
  444. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  445. size_type rfind(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT
  446. {
  447. _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
  448. return std::__str_rfind<value_type, size_type, traits_type, npos>
  449. (data(), size(), __s.data(), __pos, __s.size());
  450. }
  451. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  452. size_type rfind(_CharT __c, size_type __pos = npos) const _NOEXCEPT
  453. {
  454. return std::__str_rfind<value_type, size_type, traits_type, npos>
  455. (data(), size(), __c, __pos);
  456. }
  457. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  458. size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
  459. {
  460. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::rfind(): received nullptr");
  461. return std::__str_rfind<value_type, size_type, traits_type, npos>
  462. (data(), size(), __s, __pos, __n);
  463. }
  464. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  465. size_type rfind(const _CharT* __s, size_type __pos=npos) const _NOEXCEPT
  466. {
  467. _LIBCPP_ASSERT(__s != nullptr, "string_view::rfind(): received nullptr");
  468. return std::__str_rfind<value_type, size_type, traits_type, npos>
  469. (data(), size(), __s, __pos, traits_type::length(__s));
  470. }
  471. // find_first_of
  472. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  473. size_type find_first_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
  474. {
  475. _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_of(): received nullptr");
  476. return std::__str_find_first_of<value_type, size_type, traits_type, npos>
  477. (data(), size(), __s.data(), __pos, __s.size());
  478. }
  479. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  480. size_type find_first_of(_CharT __c, size_type __pos = 0) const _NOEXCEPT
  481. { return find(__c, __pos); }
  482. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  483. size_type find_first_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
  484. {
  485. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_of(): received nullptr");
  486. return std::__str_find_first_of<value_type, size_type, traits_type, npos>
  487. (data(), size(), __s, __pos, __n);
  488. }
  489. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  490. size_type find_first_of(const _CharT* __s, size_type __pos=0) const _NOEXCEPT
  491. {
  492. _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_of(): received nullptr");
  493. return std::__str_find_first_of<value_type, size_type, traits_type, npos>
  494. (data(), size(), __s, __pos, traits_type::length(__s));
  495. }
  496. // find_last_of
  497. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  498. size_type find_last_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
  499. {
  500. _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_of(): received nullptr");
  501. return std::__str_find_last_of<value_type, size_type, traits_type, npos>
  502. (data(), size(), __s.data(), __pos, __s.size());
  503. }
  504. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  505. size_type find_last_of(_CharT __c, size_type __pos = npos) const _NOEXCEPT
  506. { return rfind(__c, __pos); }
  507. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  508. size_type find_last_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
  509. {
  510. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_of(): received nullptr");
  511. return std::__str_find_last_of<value_type, size_type, traits_type, npos>
  512. (data(), size(), __s, __pos, __n);
  513. }
  514. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  515. size_type find_last_of(const _CharT* __s, size_type __pos=npos) const _NOEXCEPT
  516. {
  517. _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_of(): received nullptr");
  518. return std::__str_find_last_of<value_type, size_type, traits_type, npos>
  519. (data(), size(), __s, __pos, traits_type::length(__s));
  520. }
  521. // find_first_not_of
  522. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  523. size_type find_first_not_of(basic_string_view __s, size_type __pos=0) const _NOEXCEPT
  524. {
  525. _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_not_of(): received nullptr");
  526. return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>
  527. (data(), size(), __s.data(), __pos, __s.size());
  528. }
  529. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  530. size_type find_first_not_of(_CharT __c, size_type __pos=0) const _NOEXCEPT
  531. {
  532. return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>
  533. (data(), size(), __c, __pos);
  534. }
  535. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  536. size_type find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
  537. {
  538. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_not_of(): received nullptr");
  539. return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>
  540. (data(), size(), __s, __pos, __n);
  541. }
  542. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  543. size_type find_first_not_of(const _CharT* __s, size_type __pos=0) const _NOEXCEPT
  544. {
  545. _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_not_of(): received nullptr");
  546. return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>
  547. (data(), size(), __s, __pos, traits_type::length(__s));
  548. }
  549. // find_last_not_of
  550. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  551. size_type find_last_not_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
  552. {
  553. _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_not_of(): received nullptr");
  554. return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>
  555. (data(), size(), __s.data(), __pos, __s.size());
  556. }
  557. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  558. size_type find_last_not_of(_CharT __c, size_type __pos=npos) const _NOEXCEPT
  559. {
  560. return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>
  561. (data(), size(), __c, __pos);
  562. }
  563. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  564. size_type find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
  565. {
  566. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_not_of(): received nullptr");
  567. return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>
  568. (data(), size(), __s, __pos, __n);
  569. }
  570. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  571. size_type find_last_not_of(const _CharT* __s, size_type __pos=npos) const _NOEXCEPT
  572. {
  573. _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_not_of(): received nullptr");
  574. return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>
  575. (data(), size(), __s, __pos, traits_type::length(__s));
  576. }
  577. //WARN: disabled std guards in order to allow using these options without switching to new std
  578. //#if _LIBCPP_STD_VER > 17
  579. constexpr _LIBCPP_INLINE_VISIBILITY
  580. bool starts_with(basic_string_view __s) const noexcept
  581. { return size() >= __s.size() && compare(0, __s.size(), __s) == 0; }
  582. constexpr _LIBCPP_INLINE_VISIBILITY
  583. bool starts_with(value_type __c) const noexcept
  584. { return !empty() && _Traits::eq(front(), __c); }
  585. constexpr _LIBCPP_INLINE_VISIBILITY
  586. bool starts_with(const value_type* __s) const noexcept
  587. { return starts_with(basic_string_view(__s)); }
  588. constexpr _LIBCPP_INLINE_VISIBILITY
  589. bool ends_with(basic_string_view __s) const noexcept
  590. { return size() >= __s.size() && compare(size() - __s.size(), npos, __s) == 0; }
  591. constexpr _LIBCPP_INLINE_VISIBILITY
  592. bool ends_with(value_type __c) const noexcept
  593. { return !empty() && _Traits::eq(back(), __c); }
  594. constexpr _LIBCPP_INLINE_VISIBILITY
  595. bool ends_with(const value_type* __s) const noexcept
  596. { return ends_with(basic_string_view(__s)); }
  597. //#endif
  598. #if _LIBCPP_STD_VER >= 20
  599. constexpr _LIBCPP_INLINE_VISIBILITY
  600. bool contains(basic_string_view __sv) const noexcept
  601. { return find(__sv) != npos; }
  602. constexpr _LIBCPP_INLINE_VISIBILITY
  603. bool contains(value_type __c) const noexcept
  604. { return find(__c) != npos; }
  605. constexpr _LIBCPP_INLINE_VISIBILITY
  606. bool contains(const value_type* __s) const
  607. { return find(__s) != npos; }
  608. #endif
  609. private:
  610. const value_type* __data_;
  611. size_type __size_;
  612. };
  613. _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(basic_string_view);
  614. #if _LIBCPP_STD_VER > 17
  615. template <class _CharT, class _Traits>
  616. inline constexpr bool ranges::enable_view<basic_string_view<_CharT, _Traits>> = true;
  617. template <class _CharT, class _Traits>
  618. inline constexpr bool ranges::enable_borrowed_range<basic_string_view<_CharT, _Traits> > = true;
  619. #endif // _LIBCPP_STD_VER > 17
  620. // [string.view.deduct]
  621. #if _LIBCPP_STD_VER > 17
  622. template <contiguous_iterator _It, sized_sentinel_for<_It> _End>
  623. basic_string_view(_It, _End) -> basic_string_view<iter_value_t<_It>>;
  624. #endif // _LIBCPP_STD_VER > 17
  625. #if _LIBCPP_STD_VER > 20
  626. template <ranges::contiguous_range _Range>
  627. basic_string_view(_Range) -> basic_string_view<ranges::range_value_t<_Range>>;
  628. #endif
  629. // [string.view.comparison]
  630. // operator ==
  631. template<class _CharT, class _Traits>
  632. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  633. bool operator==(basic_string_view<_CharT, _Traits> __lhs,
  634. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  635. {
  636. if (__lhs.size() != __rhs.size()) return false;
  637. return __lhs.compare(__rhs) == 0;
  638. }
  639. // The dummy default template parameters are used to work around a MSVC issue with mangling, see VSO-409326 for details.
  640. // This applies to the other sufficient overloads below for the other comparison operators.
  641. template<class _CharT, class _Traits, int = 1>
  642. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  643. bool operator==(basic_string_view<_CharT, _Traits> __lhs,
  644. __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT
  645. {
  646. if (__lhs.size() != __rhs.size()) return false;
  647. return __lhs.compare(__rhs) == 0;
  648. }
  649. #if _LIBCPP_STD_VER < 20
  650. // This overload is automatically generated in C++20.
  651. template<class _CharT, class _Traits, int = 2>
  652. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  653. bool operator==(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
  654. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  655. {
  656. if (__lhs.size() != __rhs.size()) return false;
  657. return __lhs.compare(__rhs) == 0;
  658. }
  659. #endif // _LIBCPP_STD_VER > 17
  660. // operator <=>
  661. #if _LIBCPP_STD_VER > 17
  662. template <class _CharT, class _Traits>
  663. _LIBCPP_HIDE_FROM_ABI constexpr auto
  664. operator<=>(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) noexcept {
  665. if constexpr (requires { typename _Traits::comparison_category; }) {
  666. // [string.view]/4
  667. static_assert(
  668. __comparison_category<typename _Traits::comparison_category>,
  669. "return type is not a comparison category type");
  670. return static_cast<typename _Traits::comparison_category>(__lhs.compare(__rhs) <=> 0);
  671. } else {
  672. return static_cast<weak_ordering>(__lhs.compare(__rhs) <=> 0);
  673. }
  674. }
  675. template <class _CharT, class _Traits, int = 1>
  676. _LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(
  677. basic_string_view<_CharT, _Traits> __lhs, type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) noexcept {
  678. if constexpr (requires { typename _Traits::comparison_category; }) {
  679. // [string.view]/4
  680. static_assert(
  681. __comparison_category<typename _Traits::comparison_category>,
  682. "return type is not a comparison category type");
  683. return static_cast<typename _Traits::comparison_category>(__lhs.compare(__rhs) <=> 0);
  684. } else {
  685. return static_cast<weak_ordering>(__lhs.compare(__rhs) <=> 0);
  686. }
  687. }
  688. #else // _LIBCPP_STD_VER > 17
  689. // operator !=
  690. template<class _CharT, class _Traits>
  691. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  692. bool operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  693. {
  694. if (__lhs.size() != __rhs.size())
  695. return true;
  696. return __lhs.compare(__rhs) != 0;
  697. }
  698. template<class _CharT, class _Traits, int = 1>
  699. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  700. bool operator!=(basic_string_view<_CharT, _Traits> __lhs,
  701. __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT
  702. {
  703. if (__lhs.size() != __rhs.size())
  704. return true;
  705. return __lhs.compare(__rhs) != 0;
  706. }
  707. template<class _CharT, class _Traits, int = 2>
  708. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  709. bool operator!=(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
  710. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  711. {
  712. if (__lhs.size() != __rhs.size())
  713. return true;
  714. return __lhs.compare(__rhs) != 0;
  715. }
  716. // operator <
  717. template<class _CharT, class _Traits>
  718. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  719. bool operator<(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  720. {
  721. return __lhs.compare(__rhs) < 0;
  722. }
  723. template<class _CharT, class _Traits, int = 1>
  724. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  725. bool operator<(basic_string_view<_CharT, _Traits> __lhs,
  726. __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT
  727. {
  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. return __lhs.compare(__rhs) < 0;
  736. }
  737. // operator >
  738. template<class _CharT, class _Traits>
  739. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  740. bool operator> (basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  741. {
  742. return __lhs.compare(__rhs) > 0;
  743. }
  744. template<class _CharT, class _Traits, int = 1>
  745. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  746. bool operator>(basic_string_view<_CharT, _Traits> __lhs,
  747. __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT
  748. {
  749. return __lhs.compare(__rhs) > 0;
  750. }
  751. template<class _CharT, class _Traits, int = 2>
  752. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  753. bool operator>(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
  754. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  755. {
  756. return __lhs.compare(__rhs) > 0;
  757. }
  758. // operator <=
  759. template<class _CharT, class _Traits>
  760. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  761. bool operator<=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  762. {
  763. return __lhs.compare(__rhs) <= 0;
  764. }
  765. template<class _CharT, class _Traits, int = 1>
  766. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  767. bool operator<=(basic_string_view<_CharT, _Traits> __lhs,
  768. __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT
  769. {
  770. return __lhs.compare(__rhs) <= 0;
  771. }
  772. template<class _CharT, class _Traits, int = 2>
  773. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  774. bool operator<=(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
  775. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  776. {
  777. return __lhs.compare(__rhs) <= 0;
  778. }
  779. // operator >=
  780. template<class _CharT, class _Traits>
  781. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  782. bool operator>=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  783. {
  784. return __lhs.compare(__rhs) >= 0;
  785. }
  786. template<class _CharT, class _Traits, int = 1>
  787. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  788. bool operator>=(basic_string_view<_CharT, _Traits> __lhs,
  789. __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT
  790. {
  791. return __lhs.compare(__rhs) >= 0;
  792. }
  793. template<class _CharT, class _Traits, int = 2>
  794. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  795. bool operator>=(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
  796. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  797. {
  798. return __lhs.compare(__rhs) >= 0;
  799. }
  800. #endif // _LIBCPP_STD_VER > 17
  801. template<class _CharT, class _Traits>
  802. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  803. operator<<(basic_ostream<_CharT, _Traits>& __os,
  804. basic_string_view<_CharT, _Traits> __str);
  805. // [string.view.hash]
  806. template<class _CharT>
  807. struct __string_view_hash : public __unary_function<basic_string_view<_CharT, char_traits<_CharT> >, size_t>
  808. {
  809. _LIBCPP_INLINE_VISIBILITY
  810. size_t operator()(const basic_string_view<_CharT, char_traits<_CharT> > __val) const _NOEXCEPT {
  811. return std::__do_string_hash(__val.data(), __val.data() + __val.size());
  812. }
  813. };
  814. template <>
  815. struct hash<basic_string_view<char, char_traits<char> > > : __string_view_hash<char> {};
  816. #ifndef _LIBCPP_HAS_NO_CHAR8_T
  817. template <>
  818. struct hash<basic_string_view<char8_t, char_traits<char8_t> > > : __string_view_hash<char8_t> {};
  819. #endif
  820. template <>
  821. struct hash<basic_string_view<char16_t, char_traits<char16_t> > > : __string_view_hash<char16_t> {};
  822. template <>
  823. struct hash<basic_string_view<char32_t, char_traits<char32_t> > > : __string_view_hash<char32_t> {};
  824. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  825. template <>
  826. struct hash<basic_string_view<wchar_t, char_traits<wchar_t> > > : __string_view_hash<wchar_t> {};
  827. #endif
  828. #if _LIBCPP_STD_VER > 11
  829. inline namespace literals
  830. {
  831. inline namespace string_view_literals
  832. {
  833. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  834. basic_string_view<char> operator "" sv(const char *__str, size_t __len) _NOEXCEPT
  835. {
  836. return basic_string_view<char> (__str, __len);
  837. }
  838. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  839. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  840. basic_string_view<wchar_t> operator "" sv(const wchar_t *__str, size_t __len) _NOEXCEPT
  841. {
  842. return basic_string_view<wchar_t> (__str, __len);
  843. }
  844. #endif
  845. #ifndef _LIBCPP_HAS_NO_CHAR8_T
  846. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  847. basic_string_view<char8_t> operator "" sv(const char8_t *__str, size_t __len) _NOEXCEPT
  848. {
  849. return basic_string_view<char8_t> (__str, __len);
  850. }
  851. #endif
  852. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  853. basic_string_view<char16_t> operator "" sv(const char16_t *__str, size_t __len) _NOEXCEPT
  854. {
  855. return basic_string_view<char16_t> (__str, __len);
  856. }
  857. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  858. basic_string_view<char32_t> operator "" sv(const char32_t *__str, size_t __len) _NOEXCEPT
  859. {
  860. return basic_string_view<char32_t> (__str, __len);
  861. }
  862. } // namespace string_view_literals
  863. } // namespace literals
  864. #endif
  865. _LIBCPP_END_NAMESPACE_STD
  866. _LIBCPP_POP_MACROS
  867. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  868. # include <algorithm>
  869. # include <concepts>
  870. # include <functional>
  871. # include <iterator>
  872. #endif
  873. #endif // _LIBCPP_STRING_VIEW