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