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