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