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