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