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