string_view 40 KB

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