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