blob: bc92dd5b1cb2e187b4ae28e797f0eea69c06c68a [file] [log] [blame]
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001// -*- C++ -*-
2//===------------------------ string_view ---------------------------------===//
3//
Chandler Carruth7642bb12019-01-19 08:50:56 +00004// 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
Marshall Clowdf63a6d2016-07-21 05:31:24 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STRING_VIEW
11#define _LIBCPP_STRING_VIEW
12
13/*
14string_view synopsis
15
16namespace std {
17
18 // 7.2, Class template basic_string_view
19 template<class charT, class traits = char_traits<charT>>
20 class basic_string_view;
21
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>
27 constexpr bool operator!=(basic_string_view<charT, traits> x,
28 basic_string_view<charT, traits> y) noexcept;
29 template<class charT, class traits>
30 constexpr bool operator< (basic_string_view<charT, traits> x,
31 basic_string_view<charT, traits> y) noexcept;
32 template<class charT, class traits>
33 constexpr bool operator> (basic_string_view<charT, traits> x,
34 basic_string_view<charT, traits> y) noexcept;
35 template<class charT, class traits>
36 constexpr bool operator<=(basic_string_view<charT, traits> x,
37 basic_string_view<charT, traits> y) noexcept;
38 template<class charT, class traits>
39 constexpr bool operator>=(basic_string_view<charT, traits> x,
40 basic_string_view<charT, traits> y) noexcept;
41 // see below, sufficient additional overloads of comparison functions
42
43 // 7.10, Inserters and extractors
44 template<class charT, class traits>
45 basic_ostream<charT, traits>&
46 operator<<(basic_ostream<charT, traits>& os,
47 basic_string_view<charT, traits> str);
48
49 // basic_string_view typedef names
50 typedef basic_string_view<char> string_view;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +010051 typedef basic_string_view<char8_t> u8string_view; // C++20
Marshall Clowdf63a6d2016-07-21 05:31:24 +000052 typedef basic_string_view<char16_t> u16string_view;
53 typedef basic_string_view<char32_t> u32string_view;
54 typedef basic_string_view<wchar_t> wstring_view;
55
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
74 // 7.3, basic_string_view constructors and assignment operators
75 constexpr basic_string_view() noexcept;
76 constexpr basic_string_view(const basic_string_view&) noexcept = default;
77 basic_string_view& operator=(const basic_string_view&) noexcept = default;
78 template<class Allocator>
79 constexpr basic_string_view(const charT* str);
80 constexpr basic_string_view(const charT* str, size_type len);
81
82 // 7.4, basic_string_view iterator support
83 constexpr const_iterator begin() const noexcept;
84 constexpr const_iterator end() const noexcept;
85 constexpr const_iterator cbegin() const noexcept;
86 constexpr const_iterator cend() const noexcept;
87 const_reverse_iterator rbegin() const noexcept;
88 const_reverse_iterator rend() const noexcept;
89 const_reverse_iterator crbegin() const noexcept;
90 const_reverse_iterator crend() const noexcept;
91
92 // 7.5, basic_string_view capacity
93 constexpr size_type size() const noexcept;
94 constexpr size_type length() const noexcept;
95 constexpr size_type max_size() const noexcept;
96 constexpr bool empty() const noexcept;
97
98 // 7.6, basic_string_view element access
99 constexpr const_reference operator[](size_type pos) const;
100 constexpr const_reference at(size_type pos) const;
101 constexpr const_reference front() const;
102 constexpr const_reference back() const;
103 constexpr const_pointer data() const noexcept;
104
105 // 7.7, basic_string_view modifiers
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000106 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
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500110 size_type copy(charT* s, size_type n, size_type pos = 0) const; // constexpr in C++20
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000111
112 constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const;
113 constexpr int compare(basic_string_view s) const noexcept;
114 constexpr int compare(size_type pos1, size_type n1, basic_string_view s) const;
115 constexpr int compare(size_type pos1, size_type n1,
116 basic_string_view s, size_type pos2, size_type n2) const;
117 constexpr int compare(const charT* s) const;
118 constexpr int compare(size_type pos1, size_type n1, const charT* s) const;
119 constexpr int compare(size_type pos1, size_type n1,
120 const charT* s, size_type n2) const;
121 constexpr size_type find(basic_string_view s, size_type pos = 0) const noexcept;
122 constexpr size_type find(charT c, size_type pos = 0) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800123 constexpr size_type find(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
124 constexpr size_type find(const charT* s, size_type pos = 0) const noexcept; // noexcept as an extension
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000125 constexpr size_type rfind(basic_string_view s, size_type pos = npos) const noexcept;
126 constexpr size_type rfind(charT c, size_type pos = npos) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800127 constexpr size_type rfind(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
128 constexpr size_type rfind(const charT* s, size_type pos = npos) const noexcept; // noexcept as an extension
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000129 constexpr size_type find_first_of(basic_string_view s, size_type pos = 0) const noexcept;
130 constexpr size_type find_first_of(charT c, size_type pos = 0) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800131 constexpr size_type find_first_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
132 constexpr size_type find_first_of(const charT* s, size_type pos = 0) const noexcept; // noexcept as an extension
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000133 constexpr size_type find_last_of(basic_string_view s, size_type pos = npos) const noexcept;
134 constexpr size_type find_last_of(charT c, size_type pos = npos) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800135 constexpr size_type find_last_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
136 constexpr size_type find_last_of(const charT* s, size_type pos = npos) const noexcept; // noexcept as an extension
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000137 constexpr size_type find_first_not_of(basic_string_view s, size_type pos = 0) const noexcept;
138 constexpr size_type find_first_not_of(charT c, size_type pos = 0) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800139 constexpr size_type find_first_not_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
140 constexpr size_type find_first_not_of(const charT* s, size_type pos = 0) const noexcept; // noexcept as an extension
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000141 constexpr size_type find_last_not_of(basic_string_view s, size_type pos = npos) const noexcept;
142 constexpr size_type find_last_not_of(charT c, size_type pos = npos) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800143 constexpr size_type find_last_not_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
144 constexpr size_type find_last_not_of(const charT* s, size_type pos = npos) const noexcept; // noexcept as an extension
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000145
Marek Kurdej24b4c512021-01-07 12:29:04 +0100146 constexpr bool starts_with(basic_string_view s) const noexcept; // C++20
147 constexpr bool starts_with(charT c) const noexcept; // C++20
148 constexpr bool starts_with(const charT* s) const; // C++20
149 constexpr bool ends_with(basic_string_view s) const noexcept; // C++20
150 constexpr bool ends_with(charT c) const noexcept; // C++20
151 constexpr bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000152
Wim Leflere023c3542021-01-19 14:33:30 -0500153 constexpr bool contains(basic_string_view s) const noexcept; // C++2b
154 constexpr bool contains(charT c) const noexcept; // C++2b
155 constexpr bool contains(const charT* s) const; // C++2b
156
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000157 private:
158 const_pointer data_; // exposition only
159 size_type size_; // exposition only
160 };
161
162 // 7.11, Hash support
163 template <class T> struct hash;
164 template <> struct hash<string_view>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100165 template <> struct hash<u8string_view>; // C++20
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000166 template <> struct hash<u16string_view>;
167 template <> struct hash<u32string_view>;
168 template <> struct hash<wstring_view>;
169
Marshall Clowabe6daf2017-10-24 14:06:00 +0000170 constexpr basic_string_view<char> operator "" sv( const char *str, size_t len ) noexcept;
171 constexpr basic_string_view<wchar_t> operator "" sv( const wchar_t *str, size_t len ) noexcept;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100172 constexpr basic_string_view<char8_t> operator "" sv( const char8_t *str, size_t len ) noexcept; // C++20
Marshall Clowabe6daf2017-10-24 14:06:00 +0000173 constexpr basic_string_view<char16_t> operator "" sv( const char16_t *str, size_t len ) noexcept;
174 constexpr basic_string_view<char32_t> operator "" sv( const char32_t *str, size_t len ) noexcept;
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000175
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000176} // namespace std
177
178
179*/
180
181#include <__config>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000182#include <__string>
Eric Fiselier14116922019-09-25 18:56:54 +0000183#include <iosfwd>
Eric Fiselier2d357f72016-12-05 23:53:23 +0000184#include <algorithm>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000185#include <iterator>
Eric Fiselier2d357f72016-12-05 23:53:23 +0000186#include <limits>
187#include <stdexcept>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000188#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000189#include <__debug>
190
191#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
192#pragma GCC system_header
193#endif
194
Eric Fiselierf4433a32017-05-31 22:07:49 +0000195_LIBCPP_PUSH_MACROS
196#include <__undef_macros>
197
198
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000199_LIBCPP_BEGIN_NAMESPACE_STD
200
201template<class _CharT, class _Traits = char_traits<_CharT> >
Richard Smith256954d2020-11-11 17:12:18 -0800202 class _LIBCPP_TEMPLATE_VIS basic_string_view;
203
204typedef basic_string_view<char> string_view;
205#ifndef _LIBCPP_NO_HAS_CHAR8_T
206typedef basic_string_view<char8_t> u8string_view;
207#endif
208typedef basic_string_view<char16_t> u16string_view;
209typedef basic_string_view<char32_t> u32string_view;
210typedef basic_string_view<wchar_t> wstring_view;
211
212template<class _CharT, class _Traits>
213class
214 _LIBCPP_PREFERRED_NAME(string_view)
215#ifndef _LIBCPP_NO_HAS_CHAR8_T
216 _LIBCPP_PREFERRED_NAME(u8string_view)
217#endif
218 _LIBCPP_PREFERRED_NAME(u16string_view)
219 _LIBCPP_PREFERRED_NAME(u32string_view)
220 _LIBCPP_PREFERRED_NAME(wstring_view)
221 basic_string_view {
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000222public:
Marshall Clowb7db4972017-11-15 20:02:27 +0000223 // types
224 typedef _Traits traits_type;
225 typedef _CharT value_type;
Marshall Clow7d2c5182017-12-20 16:31:40 +0000226 typedef _CharT* pointer;
Marshall Clowb7db4972017-11-15 20:02:27 +0000227 typedef const _CharT* const_pointer;
Marshall Clow7d2c5182017-12-20 16:31:40 +0000228 typedef _CharT& reference;
Marshall Clowb7db4972017-11-15 20:02:27 +0000229 typedef const _CharT& const_reference;
230 typedef const_pointer const_iterator; // See [string.view.iterators]
231 typedef const_iterator iterator;
232 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
233 typedef const_reverse_iterator reverse_iterator;
234 typedef size_t size_type;
235 typedef ptrdiff_t difference_type;
236 static _LIBCPP_CONSTEXPR const size_type npos = -1; // size_type(-1);
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000237
Marshall Clow79f33542018-03-21 00:36:05 +0000238 static_assert((!is_array<value_type>::value), "Character type of basic_string_view must not be an array");
239 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string_view must be standard-layout");
240 static_assert(( is_trivial<value_type>::value), "Character type of basic_string_view must be trivial");
Marshall Clowa3a74e02017-03-15 18:41:11 +0000241 static_assert((is_same<_CharT, typename traits_type::char_type>::value),
242 "traits_type::char_type must be the same type as CharT");
243
Marshall Clowb7db4972017-11-15 20:02:27 +0000244 // [string.view.cons], construct/copy
245 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
246 basic_string_view() _NOEXCEPT : __data (nullptr), __size(0) {}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000247
Marshall Clowb7db4972017-11-15 20:02:27 +0000248 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
249 basic_string_view(const basic_string_view&) _NOEXCEPT = default;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000250
Marshall Clowb7db4972017-11-15 20:02:27 +0000251 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
252 basic_string_view& operator=(const basic_string_view&) _NOEXCEPT = default;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000253
Marshall Clowb7db4972017-11-15 20:02:27 +0000254 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
255 basic_string_view(const _CharT* __s, size_type __len) _NOEXCEPT
256 : __data(__s), __size(__len)
257 {
Marshall Clowca1bcdb2019-06-04 02:07:11 +0000258#if _LIBCPP_STD_VER > 11
259 _LIBCPP_ASSERT(__len == 0 || __s != nullptr, "string_view::string_view(_CharT *, size_t): received nullptr");
260#endif
Marshall Clowb7db4972017-11-15 20:02:27 +0000261 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000262
Marshall Clowb7db4972017-11-15 20:02:27 +0000263 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
264 basic_string_view(const _CharT* __s)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500265 : __data(__s), __size(_VSTD::__char_traits_length_checked<_Traits>(__s)) {}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000266
Marshall Clowb7db4972017-11-15 20:02:27 +0000267 // [string.view.iterators], iterators
268 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
269 const_iterator begin() const _NOEXCEPT { return cbegin(); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000270
Marshall Clowb7db4972017-11-15 20:02:27 +0000271 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
272 const_iterator end() const _NOEXCEPT { return cend(); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000273
Marshall Clowb7db4972017-11-15 20:02:27 +0000274 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
275 const_iterator cbegin() const _NOEXCEPT { return __data; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000276
Marshall Clowb7db4972017-11-15 20:02:27 +0000277 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
278 const_iterator cend() const _NOEXCEPT { return __data + __size; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000279
Marshall Clowb7db4972017-11-15 20:02:27 +0000280 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
281 const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000282
Marshall Clowb7db4972017-11-15 20:02:27 +0000283 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
284 const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000285
Marshall Clowb7db4972017-11-15 20:02:27 +0000286 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
287 const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000288
Marshall Clowb7db4972017-11-15 20:02:27 +0000289 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
290 const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000291
Marshall Clowb7db4972017-11-15 20:02:27 +0000292 // [string.view.capacity], capacity
293 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
294 size_type size() const _NOEXCEPT { return __size; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000295
Marshall Clowb7db4972017-11-15 20:02:27 +0000296 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
297 size_type length() const _NOEXCEPT { return __size; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000298
Marshall Clowb7db4972017-11-15 20:02:27 +0000299 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
300 size_type max_size() const _NOEXCEPT { return numeric_limits<size_type>::max(); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000301
Marshall Clowb7db4972017-11-15 20:02:27 +0000302 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
303 bool empty() const _NOEXCEPT { return __size == 0; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000304
Marshall Clowb7db4972017-11-15 20:02:27 +0000305 // [string.view.access], element access
306 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Chris Palmer1b114a52020-10-06 13:01:50 -0400307 const_reference operator[](size_type __pos) const _NOEXCEPT {
308 return _LIBCPP_ASSERT(__pos < size(), "string_view[] index out of bounds"), __data[__pos];
309 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000310
Marshall Clowb7db4972017-11-15 20:02:27 +0000311 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
312 const_reference at(size_type __pos) const
313 {
314 return __pos >= size()
315 ? (__throw_out_of_range("string_view::at"), __data[0])
316 : __data[__pos];
317 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000318
Marshall Clowb7db4972017-11-15 20:02:27 +0000319 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +0000320 const_reference front() const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000321 {
322 return _LIBCPP_ASSERT(!empty(), "string_view::front(): string is empty"), __data[0];
323 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000324
Marshall Clowb7db4972017-11-15 20:02:27 +0000325 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +0000326 const_reference back() const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000327 {
328 return _LIBCPP_ASSERT(!empty(), "string_view::back(): string is empty"), __data[__size-1];
329 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000330
Marshall Clowb7db4972017-11-15 20:02:27 +0000331 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
332 const_pointer data() const _NOEXCEPT { return __data; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000333
Marshall Clowb7db4972017-11-15 20:02:27 +0000334 // [string.view.modifiers], modifiers:
335 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
336 void remove_prefix(size_type __n) _NOEXCEPT
337 {
338 _LIBCPP_ASSERT(__n <= size(), "remove_prefix() can't remove more than size()");
339 __data += __n;
340 __size -= __n;
341 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000342
Marshall Clowb7db4972017-11-15 20:02:27 +0000343 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
344 void remove_suffix(size_type __n) _NOEXCEPT
345 {
346 _LIBCPP_ASSERT(__n <= size(), "remove_suffix() can't remove more than size()");
347 __size -= __n;
348 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000349
Marshall Clowb7db4972017-11-15 20:02:27 +0000350 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
351 void swap(basic_string_view& __other) _NOEXCEPT
352 {
353 const value_type *__p = __data;
354 __data = __other.__data;
355 __other.__data = __p;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000356
Marshall Clowb7db4972017-11-15 20:02:27 +0000357 size_type __sz = __size;
358 __size = __other.__size;
359 __other.__size = __sz;
360 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000361
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500362 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowb7db4972017-11-15 20:02:27 +0000363 size_type copy(_CharT* __s, size_type __n, size_type __pos = 0) const
364 {
365 if (__pos > size())
366 __throw_out_of_range("string_view::copy");
367 size_type __rlen = _VSTD::min(__n, size() - __pos);
368 _Traits::copy(__s, data() + __pos, __rlen);
369 return __rlen;
370 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000371
Marshall Clowb7db4972017-11-15 20:02:27 +0000372 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
373 basic_string_view substr(size_type __pos = 0, size_type __n = npos) const
374 {
375 return __pos > size()
376 ? (__throw_out_of_range("string_view::substr"), basic_string_view())
377 : basic_string_view(data() + __pos, _VSTD::min(__n, size() - __pos));
378 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000379
Marshall Clowb7db4972017-11-15 20:02:27 +0000380 _LIBCPP_CONSTEXPR_AFTER_CXX11 int compare(basic_string_view __sv) const _NOEXCEPT
381 {
382 size_type __rlen = _VSTD::min( size(), __sv.size());
383 int __retval = _Traits::compare(data(), __sv.data(), __rlen);
384 if ( __retval == 0 ) // first __rlen chars matched
385 __retval = size() == __sv.size() ? 0 : ( size() < __sv.size() ? -1 : 1 );
386 return __retval;
387 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000388
Marshall Clowb7db4972017-11-15 20:02:27 +0000389 _LIBCPP_CONSTEXPR_AFTER_CXX11 _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 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000394
Marshall Clowb7db4972017-11-15 20:02:27 +0000395 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000396 int compare( size_type __pos1, size_type __n1,
Marshall Clowb7db4972017-11-15 20:02:27 +0000397 basic_string_view __sv, size_type __pos2, size_type __n2) const
398 {
399 return substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
400 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000401
Marshall Clowb7db4972017-11-15 20:02:27 +0000402 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
403 int compare(const _CharT* __s) const _NOEXCEPT
404 {
405 return compare(basic_string_view(__s));
406 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000407
Marshall Clowb7db4972017-11-15 20:02:27 +0000408 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
409 int compare(size_type __pos1, size_type __n1, const _CharT* __s) const
410 {
411 return substr(__pos1, __n1).compare(basic_string_view(__s));
412 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000413
Marshall Clowb7db4972017-11-15 20:02:27 +0000414 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
415 int compare(size_type __pos1, size_type __n1, const _CharT* __s, size_type __n2) const
416 {
417 return substr(__pos1, __n1).compare(basic_string_view(__s, __n2));
418 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000419
Marshall Clowb7db4972017-11-15 20:02:27 +0000420 // find
421 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
422 size_type find(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
423 {
424 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
425 return __str_find<value_type, size_type, traits_type, npos>
426 (data(), size(), __s.data(), __pos, __s.size());
427 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000428
Marshall Clowb7db4972017-11-15 20:02:27 +0000429 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
430 size_type find(_CharT __c, size_type __pos = 0) const _NOEXCEPT
431 {
432 return __str_find<value_type, size_type, traits_type, npos>
433 (data(), size(), __c, __pos);
434 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000435
Marshall Clowb7db4972017-11-15 20:02:27 +0000436 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800437 size_type find(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000438 {
439 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find(): received nullptr");
440 return __str_find<value_type, size_type, traits_type, npos>
441 (data(), size(), __s, __pos, __n);
442 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000443
Marshall Clowb7db4972017-11-15 20:02:27 +0000444 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800445 size_type find(const _CharT* __s, size_type __pos = 0) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000446 {
447 _LIBCPP_ASSERT(__s != nullptr, "string_view::find(): received nullptr");
448 return __str_find<value_type, size_type, traits_type, npos>
449 (data(), size(), __s, __pos, traits_type::length(__s));
450 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000451
Marshall Clowb7db4972017-11-15 20:02:27 +0000452 // 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 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000460
Marshall Clowb7db4972017-11-15 20:02:27 +0000461 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
462 size_type rfind(_CharT __c, size_type __pos = npos) const _NOEXCEPT
463 {
464 return __str_rfind<value_type, size_type, traits_type, npos>
465 (data(), size(), __c, __pos);
466 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000467
Marshall Clowb7db4972017-11-15 20:02:27 +0000468 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800469 size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000470 {
471 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::rfind(): received nullptr");
472 return __str_rfind<value_type, size_type, traits_type, npos>
473 (data(), size(), __s, __pos, __n);
474 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000475
Marshall Clowb7db4972017-11-15 20:02:27 +0000476 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800477 size_type rfind(const _CharT* __s, size_type __pos=npos) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000478 {
479 _LIBCPP_ASSERT(__s != nullptr, "string_view::rfind(): received nullptr");
480 return __str_rfind<value_type, size_type, traits_type, npos>
481 (data(), size(), __s, __pos, traits_type::length(__s));
482 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000483
Marshall Clowb7db4972017-11-15 20:02:27 +0000484 // find_first_of
485 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
486 size_type find_first_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
487 {
488 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_of(): received nullptr");
489 return __str_find_first_of<value_type, size_type, traits_type, npos>
490 (data(), size(), __s.data(), __pos, __s.size());
491 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000492
Marshall Clowb7db4972017-11-15 20:02:27 +0000493 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
494 size_type find_first_of(_CharT __c, size_type __pos = 0) const _NOEXCEPT
495 { return find(__c, __pos); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000496
Marshall Clowb7db4972017-11-15 20:02:27 +0000497 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800498 size_type find_first_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000499 {
500 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_of(): received nullptr");
501 return __str_find_first_of<value_type, size_type, traits_type, npos>
502 (data(), size(), __s, __pos, __n);
503 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000504
Marshall Clowb7db4972017-11-15 20:02:27 +0000505 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800506 size_type find_first_of(const _CharT* __s, size_type __pos=0) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000507 {
508 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_of(): received nullptr");
509 return __str_find_first_of<value_type, size_type, traits_type, npos>
510 (data(), size(), __s, __pos, traits_type::length(__s));
511 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000512
Marshall Clowb7db4972017-11-15 20:02:27 +0000513 // find_last_of
514 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
515 size_type find_last_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
516 {
517 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_of(): received nullptr");
518 return __str_find_last_of<value_type, size_type, traits_type, npos>
519 (data(), size(), __s.data(), __pos, __s.size());
520 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000521
Marshall Clowb7db4972017-11-15 20:02:27 +0000522 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
523 size_type find_last_of(_CharT __c, size_type __pos = npos) const _NOEXCEPT
524 { return rfind(__c, __pos); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000525
Marshall Clowb7db4972017-11-15 20:02:27 +0000526 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800527 size_type find_last_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000528 {
529 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_of(): received nullptr");
530 return __str_find_last_of<value_type, size_type, traits_type, npos>
531 (data(), size(), __s, __pos, __n);
532 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000533
Marshall Clowb7db4972017-11-15 20:02:27 +0000534 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800535 size_type find_last_of(const _CharT* __s, size_type __pos=npos) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000536 {
537 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_of(): received nullptr");
538 return __str_find_last_of<value_type, size_type, traits_type, npos>
539 (data(), size(), __s, __pos, traits_type::length(__s));
540 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000541
Marshall Clowb7db4972017-11-15 20:02:27 +0000542 // find_first_not_of
543 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
544 size_type find_first_not_of(basic_string_view __s, size_type __pos=0) const _NOEXCEPT
545 {
546 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_not_of(): received nullptr");
547 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
548 (data(), size(), __s.data(), __pos, __s.size());
549 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000550
Marshall Clowb7db4972017-11-15 20:02:27 +0000551 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
552 size_type find_first_not_of(_CharT __c, size_type __pos=0) const _NOEXCEPT
553 {
554 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
555 (data(), size(), __c, __pos);
556 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000557
Marshall Clowb7db4972017-11-15 20:02:27 +0000558 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800559 size_type find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000560 {
561 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_not_of(): received nullptr");
562 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
563 (data(), size(), __s, __pos, __n);
564 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000565
Marshall Clowb7db4972017-11-15 20:02:27 +0000566 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800567 size_type find_first_not_of(const _CharT* __s, size_type __pos=0) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000568 {
569 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_not_of(): received nullptr");
570 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
571 (data(), size(), __s, __pos, traits_type::length(__s));
572 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000573
Marshall Clowb7db4972017-11-15 20:02:27 +0000574 // find_last_not_of
575 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
576 size_type find_last_not_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
577 {
578 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_not_of(): received nullptr");
579 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
580 (data(), size(), __s.data(), __pos, __s.size());
581 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000582
Marshall Clowb7db4972017-11-15 20:02:27 +0000583 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
584 size_type find_last_not_of(_CharT __c, size_type __pos=npos) const _NOEXCEPT
585 {
586 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
587 (data(), size(), __c, __pos);
588 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000589
Marshall Clowb7db4972017-11-15 20:02:27 +0000590 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800591 size_type find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000592 {
593 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_not_of(): received nullptr");
594 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
595 (data(), size(), __s, __pos, __n);
596 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000597
Marshall Clowb7db4972017-11-15 20:02:27 +0000598 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800599 size_type find_last_not_of(const _CharT* __s, size_type __pos=npos) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000600 {
601 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_not_of(): received nullptr");
602 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
603 (data(), size(), __s, __pos, traits_type::length(__s));
604 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000605
Marshall Clow18c293b2017-12-04 20:11:38 +0000606#if _LIBCPP_STD_VER > 17
607 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
608 bool starts_with(basic_string_view __s) const _NOEXCEPT
609 { return size() >= __s.size() && compare(0, __s.size(), __s) == 0; }
610
611 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
612 bool starts_with(value_type __c) const _NOEXCEPT
613 { return !empty() && _Traits::eq(front(), __c); }
614
615 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
616 bool starts_with(const value_type* __s) const _NOEXCEPT
617 { return starts_with(basic_string_view(__s)); }
618
619 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
620 bool ends_with(basic_string_view __s) const _NOEXCEPT
621 { return size() >= __s.size() && compare(size() - __s.size(), npos, __s) == 0; }
622
623 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
624 bool ends_with(value_type __c) const _NOEXCEPT
625 { return !empty() && _Traits::eq(back(), __c); }
626
627 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
628 bool ends_with(const value_type* __s) const _NOEXCEPT
629 { return ends_with(basic_string_view(__s)); }
630#endif
631
Wim Leflere023c3542021-01-19 14:33:30 -0500632#if _LIBCPP_STD_VER > 20
633 constexpr _LIBCPP_INLINE_VISIBILITY
634 bool contains(basic_string_view __sv) const noexcept
635 { return find(__sv) != npos; }
636
637 constexpr _LIBCPP_INLINE_VISIBILITY
638 bool contains(value_type __c) const noexcept
639 { return find(__c) != npos; }
640
641 constexpr _LIBCPP_INLINE_VISIBILITY
642 bool contains(const value_type* __s) const
643 { return find(__s) != npos; }
644#endif
645
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000646private:
Marshall Clowb7db4972017-11-15 20:02:27 +0000647 const value_type* __data;
648 size_type __size;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000649};
650
651
652// [string.view.comparison]
653// operator ==
654template<class _CharT, class _Traits>
655_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
656bool operator==(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000657 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000658{
Marshall Clowb7db4972017-11-15 20:02:27 +0000659 if ( __lhs.size() != __rhs.size()) return false;
660 return __lhs.compare(__rhs) == 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000661}
662
663template<class _CharT, class _Traits>
664_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
665bool operator==(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000666 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000667{
Marshall Clowb7db4972017-11-15 20:02:27 +0000668 if ( __lhs.size() != __rhs.size()) return false;
669 return __lhs.compare(__rhs) == 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000670}
671
672template<class _CharT, class _Traits>
673_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000674bool operator==(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000675 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000676{
Marshall Clowb7db4972017-11-15 20:02:27 +0000677 if ( __lhs.size() != __rhs.size()) return false;
678 return __lhs.compare(__rhs) == 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000679}
680
681
682// operator !=
683template<class _CharT, class _Traits>
684_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
685bool operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
686{
Marshall Clowb7db4972017-11-15 20:02:27 +0000687 if ( __lhs.size() != __rhs.size())
688 return true;
689 return __lhs.compare(__rhs) != 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000690}
691
692template<class _CharT, class _Traits>
693_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
694bool operator!=(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000695 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000696{
Marshall Clowb7db4972017-11-15 20:02:27 +0000697 if ( __lhs.size() != __rhs.size())
698 return true;
699 return __lhs.compare(__rhs) != 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000700}
701
702template<class _CharT, class _Traits>
703_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000704bool operator!=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000705 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000706{
Marshall Clowb7db4972017-11-15 20:02:27 +0000707 if ( __lhs.size() != __rhs.size())
708 return true;
709 return __lhs.compare(__rhs) != 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000710}
711
712
713// operator <
714template<class _CharT, class _Traits>
715_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
716bool operator<(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
717{
Marshall Clowb7db4972017-11-15 20:02:27 +0000718 return __lhs.compare(__rhs) < 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000719}
720
721template<class _CharT, class _Traits>
722_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
723bool operator<(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000724 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000725{
Marshall Clowb7db4972017-11-15 20:02:27 +0000726 return __lhs.compare(__rhs) < 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000727}
728
729template<class _CharT, class _Traits>
730_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000731bool operator<(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000732 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000733{
Marshall Clowb7db4972017-11-15 20:02:27 +0000734 return __lhs.compare(__rhs) < 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000735}
736
737
738// operator >
739template<class _CharT, class _Traits>
740_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
741bool operator> (basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
742{
Marshall Clowb7db4972017-11-15 20:02:27 +0000743 return __lhs.compare(__rhs) > 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000744}
745
746template<class _CharT, class _Traits>
747_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
748bool operator>(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000749 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000750{
Marshall Clowb7db4972017-11-15 20:02:27 +0000751 return __lhs.compare(__rhs) > 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000752}
753
754template<class _CharT, class _Traits>
755_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000756bool operator>(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000757 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000758{
Marshall Clowb7db4972017-11-15 20:02:27 +0000759 return __lhs.compare(__rhs) > 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000760}
761
762
763// operator <=
764template<class _CharT, class _Traits>
765_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
766bool operator<=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
767{
Marshall Clowb7db4972017-11-15 20:02:27 +0000768 return __lhs.compare(__rhs) <= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000769}
770
771template<class _CharT, class _Traits>
772_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
773bool operator<=(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000774 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000775{
Marshall Clowb7db4972017-11-15 20:02:27 +0000776 return __lhs.compare(__rhs) <= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000777}
778
779template<class _CharT, class _Traits>
780_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000781bool operator<=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000782 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000783{
Marshall Clowb7db4972017-11-15 20:02:27 +0000784 return __lhs.compare(__rhs) <= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000785}
786
787
788// operator >=
789template<class _CharT, class _Traits>
790_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
791bool operator>=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
792{
Marshall Clowb7db4972017-11-15 20:02:27 +0000793 return __lhs.compare(__rhs) >= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000794}
795
796
797template<class _CharT, class _Traits>
798_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
799bool operator>=(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000800 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000801{
Marshall Clowb7db4972017-11-15 20:02:27 +0000802 return __lhs.compare(__rhs) >= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000803}
804
805template<class _CharT, class _Traits>
806_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000807bool operator>=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000808 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000809{
Marshall Clowb7db4972017-11-15 20:02:27 +0000810 return __lhs.compare(__rhs) >= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000811}
812
Eric Fiselier14116922019-09-25 18:56:54 +0000813
814template<class _CharT, class _Traits>
815basic_ostream<_CharT, _Traits>&
816operator<<(basic_ostream<_CharT, _Traits>& __os,
817 basic_string_view<_CharT, _Traits> __str);
818
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000819// [string.view.hash]
Marshall Clowab313df2019-06-27 14:18:32 +0000820template<class _CharT>
821struct _LIBCPP_TEMPLATE_VIS hash<basic_string_view<_CharT, char_traits<_CharT> > >
822 : public unary_function<basic_string_view<_CharT, char_traits<_CharT> >, size_t>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000823{
Louis Dionned83f2e62018-11-28 15:22:30 +0000824 _LIBCPP_INLINE_VISIBILITY
Marshall Clowab313df2019-06-27 14:18:32 +0000825 size_t operator()(const basic_string_view<_CharT, char_traits<_CharT> > __val) const _NOEXCEPT {
Louis Dionned83f2e62018-11-28 15:22:30 +0000826 return __do_string_hash(__val.data(), __val.data() + __val.size());
827 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000828};
829
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000830
Louis Dionne173f29e2019-05-29 16:01:36 +0000831#if _LIBCPP_STD_VER > 11
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000832inline namespace literals
833{
834 inline namespace string_view_literals
835 {
836 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clowabe6daf2017-10-24 14:06:00 +0000837 basic_string_view<char> operator "" sv(const char *__str, size_t __len) _NOEXCEPT
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000838 {
839 return basic_string_view<char> (__str, __len);
840 }
841
842 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clowabe6daf2017-10-24 14:06:00 +0000843 basic_string_view<wchar_t> operator "" sv(const wchar_t *__str, size_t __len) _NOEXCEPT
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000844 {
845 return basic_string_view<wchar_t> (__str, __len);
846 }
847
Marshall Clow8732fed2018-12-11 04:35:44 +0000848#ifndef _LIBCPP_NO_HAS_CHAR8_T
849 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
850 basic_string_view<char8_t> operator "" sv(const char8_t *__str, size_t __len) _NOEXCEPT
851 {
852 return basic_string_view<char8_t> (__str, __len);
853 }
854#endif
855
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000856 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clowabe6daf2017-10-24 14:06:00 +0000857 basic_string_view<char16_t> operator "" sv(const char16_t *__str, size_t __len) _NOEXCEPT
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000858 {
859 return basic_string_view<char16_t> (__str, __len);
860 }
861
862 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clowabe6daf2017-10-24 14:06:00 +0000863 basic_string_view<char32_t> operator "" sv(const char32_t *__str, size_t __len) _NOEXCEPT
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000864 {
865 return basic_string_view<char32_t> (__str, __len);
866 }
867 }
868}
869#endif
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000870_LIBCPP_END_NAMESPACE_STD
871
Eric Fiselierf4433a32017-05-31 22:07:49 +0000872_LIBCPP_POP_MACROS
873
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000874#endif // _LIBCPP_STRING_VIEW