108 static_assert(!is_array_v<_CharT>);
109 static_assert(is_trivial_v<_CharT> && is_standard_layout_v<_CharT>);
110 static_assert(is_same_v<_CharT, typename _Traits::char_type>);
115 using traits_type = _Traits;
116 using value_type = _CharT;
117 using pointer = value_type*;
118 using const_pointer =
const value_type*;
119 using reference = value_type&;
120 using const_reference =
const value_type&;
121 using const_iterator =
const value_type*;
122 using iterator = const_iterator;
125 using size_type = size_t;
126 using difference_type = ptrdiff_t;
127 static constexpr size_type npos = size_type(-1);
133 : _M_len{0}, _M_str{
nullptr}
138 [[__gnu__::__nonnull__]]
141 : _M_len{traits_type::length(__str)},
147 : _M_len{__len}, _M_str{__str}
150#if __cplusplus >= 202002L && __cpp_lib_concepts
151 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
156 noexcept(
noexcept(__last - __first))
160#if __cplusplus > 202002L
161 template<
typename _Range,
typename _DRange = remove_cvref_t<_Range>>
162 requires (!is_same_v<_DRange, basic_string_view>)
165 && is_same_v<ranges::range_value_t<_Range>, _CharT>
166 && (!is_convertible_v<_Range, const _CharT*>)
167 && (!
requires (_DRange& __d) {
168 __d.operator ::std::basic_string_view<_CharT, _Traits>();
170 && (!
requires {
typename _DRange::traits_type; }
171 || is_same_v<typename _DRange::traits_type, _Traits>)
174 noexcept(
noexcept(ranges::size(__r)) &&
noexcept(ranges::data(__r)))
175 : _M_len(ranges::size(__r)), _M_str(ranges::data(__r))
188 constexpr const_iterator
189 begin()
const noexcept
190 {
return this->_M_str; }
193 constexpr const_iterator
195 {
return this->_M_str + this->_M_len; }
198 constexpr const_iterator
199 cbegin()
const noexcept
200 {
return this->_M_str; }
203 constexpr const_iterator
204 cend()
const noexcept
205 {
return this->_M_str + this->_M_len; }
209 rbegin()
const noexcept
214 rend()
const noexcept
219 crbegin()
const noexcept
224 crend()
const noexcept
231 size()
const noexcept
232 {
return this->_M_len; }
236 length()
const noexcept
241 max_size()
const noexcept
243 return (npos -
sizeof(size_type) -
sizeof(
void*))
244 /
sizeof(value_type) / 4;
249 empty()
const noexcept
250 {
return this->_M_len == 0; }
255 constexpr const_reference
256 operator[](size_type __pos)
const noexcept
258 __glibcxx_assert(__pos < this->_M_len);
259 return *(this->_M_str + __pos);
263 constexpr const_reference
264 at(size_type __pos)
const
267 __throw_out_of_range_fmt(__N(
"basic_string_view::at: __pos "
268 "(which is %zu) >= this->size() "
269 "(which is %zu)"), __pos, this->size());
270 return *(this->_M_str + __pos);
274 constexpr const_reference
275 front()
const noexcept
277 __glibcxx_assert(this->_M_len > 0);
278 return *this->_M_str;
282 constexpr const_reference
283 back()
const noexcept
285 __glibcxx_assert(this->_M_len > 0);
286 return *(this->_M_str + this->_M_len - 1);
290 constexpr const_pointer
291 data()
const noexcept
292 {
return this->_M_str; }
297 remove_prefix(size_type __n)
noexcept
299 __glibcxx_assert(this->_M_len >= __n);
305 remove_suffix(size_type __n)
noexcept
306 { this->_M_len -= __n; }
320 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const
322 __glibcxx_requires_string_len(__str, __n);
323 __pos = std::__sv_check(size(), __pos,
"basic_string_view::copy");
324 const size_type __rlen = std::min<size_t>(__n, _M_len - __pos);
327 traits_type::copy(__str, data() + __pos, __rlen);
333 substr(size_type __pos = 0, size_type __n = npos)
const noexcept(
false)
335 __pos = std::__sv_check(size(), __pos,
"basic_string_view::substr");
336 const size_type __rlen = std::min<size_t>(__n, _M_len - __pos);
344 const size_type __rlen =
std::min(this->_M_len, __str._M_len);
345 int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
347 __ret = _S_compare(this->_M_len, __str._M_len);
354 {
return this->substr(__pos1, __n1).compare(__str); }
358 compare(size_type __pos1, size_type __n1,
361 return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
364 [[nodiscard, __gnu__::__nonnull__]]
366 compare(
const _CharT* __str)
const noexcept
369 [[nodiscard, __gnu__::__nonnull__]]
371 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const
376 compare(size_type __pos1, size_type __n1,
377 const _CharT* __str, size_type __n2)
const noexcept(
false)
379 return this->substr(__pos1, __n1)
383#if __cplusplus > 201703L
384#define __cpp_lib_starts_ends_with 201711L
388 {
return this->substr(0, __x.size()) == __x; }
392 starts_with(_CharT __x)
const noexcept
393 {
return !this->empty() && traits_type::eq(this->front(), __x); }
395 [[nodiscard, __gnu__::__nonnull__]]
397 starts_with(
const _CharT* __x)
const noexcept
404 const auto __len = this->size();
405 const auto __xlen = __x.size();
406 return __len >= __xlen
407 && traits_type::compare(end() - __xlen, __x.data(), __xlen) == 0;
412 ends_with(_CharT __x)
const noexcept
413 {
return !this->empty() && traits_type::eq(this->back(), __x); }
415 [[nodiscard, __gnu__::__nonnull__]]
417 ends_with(
const _CharT* __x)
const noexcept
421#if __cplusplus > 202002L
425# define __cpp_lib_string_contains 202011L
430 {
return this->find(__x) != npos; }
434 contains(_CharT __x)
const noexcept
435 {
return this->find(__x) != npos; }
437 [[nodiscard, __gnu__::__nonnull__]]
439 contains(
const _CharT* __x)
const noexcept
440 {
return this->find(__x) != npos; }
448 {
return this->find(__str._M_str, __pos, __str._M_len); }
452 find(_CharT __c, size_type __pos = 0)
const noexcept;
456 find(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
458 [[nodiscard, __gnu__::__nonnull__]]
460 find(
const _CharT* __str, size_type __pos = 0)
const noexcept
461 {
return this->find(__str, __pos, traits_type::length(__str)); }
466 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
470 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
474 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
476 [[nodiscard, __gnu__::__nonnull__]]
478 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
479 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
484 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
488 find_first_of(_CharT __c, size_type __pos = 0)
const noexcept
489 {
return this->find(__c, __pos); }
493 find_first_of(
const _CharT* __str, size_type __pos,
494 size_type __n)
const noexcept;
496 [[nodiscard, __gnu__::__nonnull__]]
498 find_first_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
499 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
504 size_type __pos = npos)
const noexcept
505 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
509 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
510 {
return this->rfind(__c, __pos); }
514 find_last_of(
const _CharT* __str, size_type __pos,
515 size_type __n)
const noexcept;
517 [[nodiscard, __gnu__::__nonnull__]]
519 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
520 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
525 size_type __pos = 0)
const noexcept
526 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
530 find_first_not_of(_CharT __c, size_type __pos = 0)
const noexcept;
534 find_first_not_of(
const _CharT* __str,
535 size_type __pos, size_type __n)
const noexcept;
537 [[nodiscard, __gnu__::__nonnull__]]
539 find_first_not_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
541 return this->find_first_not_of(__str, __pos,
542 traits_type::length(__str));
548 size_type __pos = npos)
const noexcept
549 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
553 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
557 find_last_not_of(
const _CharT* __str,
558 size_type __pos, size_type __n)
const noexcept;
560 [[nodiscard, __gnu__::__nonnull__]]
562 find_last_not_of(
const _CharT* __str,
563 size_type __pos = npos)
const noexcept
565 return this->find_last_not_of(__str, __pos,
566 traits_type::length(__str));
572 _S_compare(size_type __n1, size_type __n2)
noexcept
575 const difference_type __diff = __n1 - __n2;
576 if (__diff > __limits::__max)
577 return __limits::__max;
578 if (__diff < __limits::__min)
579 return __limits::__min;
580 return static_cast<int>(__diff);
584 const _CharT* _M_str;