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