110 static_assert(!is_array_v<_CharT>);
111 static_assert(is_trivial_v<_CharT> && is_standard_layout_v<_CharT>);
112 static_assert(is_same_v<_CharT, typename _Traits::char_type>);
117 using traits_type = _Traits;
118 using value_type = _CharT;
119 using pointer = value_type*;
120 using const_pointer =
const value_type*;
121 using reference = value_type&;
122 using const_reference =
const value_type&;
123 using const_iterator =
const value_type*;
124 using iterator = const_iterator;
127 using size_type = size_t;
128 using difference_type = ptrdiff_t;
129 static constexpr size_type npos = size_type(-1);
135 : _M_len{0}, _M_str{
nullptr}
140 [[__gnu__::__nonnull__]]
143 : _M_len{traits_type::length(__str)},
149 : _M_len{__len}, _M_str{__str}
152#if __cplusplus >= 202002L && __cpp_lib_concepts
153 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
154 requires same_as<iter_value_t<_It>, _CharT>
155 && (!convertible_to<_End, size_type>)
158 noexcept(
noexcept(__last - __first))
162#if __cplusplus > 202002L
163 template<
typename _Range,
typename _DRange = remove_cvref_t<_Range>>
164 requires (!is_same_v<_DRange, basic_string_view>)
165 && ranges::contiguous_range<_Range>
166 && ranges::sized_range<_Range>
167 && is_same_v<ranges::range_value_t<_Range>, _CharT>
168 && (!is_convertible_v<_Range, const _CharT*>)
169 && (!
requires (_DRange& __d) {
170 __d.operator ::std::basic_string_view<_CharT, _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
307 __glibcxx_assert(this->_M_len >= __n);
323 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const
325 __glibcxx_requires_string_len(__str, __n);
326 __pos = std::__sv_check(size(), __pos,
"basic_string_view::copy");
327 const size_type __rlen = std::min<size_t>(__n, _M_len - __pos);
330 traits_type::copy(__str, data() + __pos, __rlen);
336 substr(size_type __pos = 0, size_type __n = npos)
const noexcept(
false)
338 __pos = std::__sv_check(size(), __pos,
"basic_string_view::substr");
339 const size_type __rlen = std::min<size_t>(__n, _M_len - __pos);
347 const size_type __rlen =
std::min(this->_M_len, __str._M_len);
348 int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
350 __ret = _S_compare(this->_M_len, __str._M_len);
357 {
return this->substr(__pos1, __n1).compare(__str); }
361 compare(size_type __pos1, size_type __n1,
364 return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
367 [[nodiscard, __gnu__::__nonnull__]]
369 compare(
const _CharT* __str)
const noexcept
372 [[nodiscard, __gnu__::__nonnull__]]
374 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const
379 compare(size_type __pos1, size_type __n1,
380 const _CharT* __str, size_type __n2)
const noexcept(
false)
382 return this->substr(__pos1, __n1)
386#ifdef __cpp_lib_starts_ends_with
391 return _M_len >= __x._M_len
392 && traits_type::compare(_M_str, __x._M_str, __x._M_len) == 0;
397 starts_with(_CharT __x)
const noexcept
398 {
return !this->empty() && traits_type::eq(this->front(), __x); }
400 [[nodiscard, __gnu__::__nonnull__]]
402 starts_with(
const _CharT* __x)
const noexcept
409 const auto __len = this->size();
410 const auto __xlen = __x.size();
411 return __len >= __xlen
412 && traits_type::compare(end() - __xlen, __x.data(), __xlen) == 0;
417 ends_with(_CharT __x)
const noexcept
418 {
return !this->empty() && traits_type::eq(this->back(), __x); }
420 [[nodiscard, __gnu__::__nonnull__]]
422 ends_with(
const _CharT* __x)
const noexcept
426#if __cplusplus > 202002L
427#if _GLIBCXX_HOSTED && !defined(__cpp_lib_string_contains)
430# error "libstdc++ bug: string_contains not defined when it should be"
435 {
return this->find(__x) != npos; }
439 contains(_CharT __x)
const noexcept
440 {
return this->find(__x) != npos; }
442 [[nodiscard, __gnu__::__nonnull__]]
444 contains(
const _CharT* __x)
const noexcept
445 {
return this->find(__x) != npos; }
453 {
return this->find(__str._M_str, __pos, __str._M_len); }
457 find(_CharT __c, size_type __pos = 0)
const noexcept;
461 find(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
463 [[nodiscard, __gnu__::__nonnull__]]
465 find(
const _CharT* __str, size_type __pos = 0)
const noexcept
466 {
return this->find(__str, __pos, traits_type::length(__str)); }
471 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
475 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
479 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
481 [[nodiscard, __gnu__::__nonnull__]]
483 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
484 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
489 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
493 find_first_of(_CharT __c, size_type __pos = 0)
const noexcept
494 {
return this->find(__c, __pos); }
498 find_first_of(
const _CharT* __str, size_type __pos,
499 size_type __n)
const noexcept;
501 [[nodiscard, __gnu__::__nonnull__]]
503 find_first_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
504 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
509 size_type __pos = npos)
const noexcept
510 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
514 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
515 {
return this->rfind(__c, __pos); }
519 find_last_of(
const _CharT* __str, size_type __pos,
520 size_type __n)
const noexcept;
522 [[nodiscard, __gnu__::__nonnull__]]
524 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
525 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
530 size_type __pos = 0)
const noexcept
531 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
535 find_first_not_of(_CharT __c, size_type __pos = 0)
const noexcept;
539 find_first_not_of(
const _CharT* __str,
540 size_type __pos, size_type __n)
const noexcept;
542 [[nodiscard, __gnu__::__nonnull__]]
544 find_first_not_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
546 return this->find_first_not_of(__str, __pos,
547 traits_type::length(__str));
553 size_type __pos = npos)
const noexcept
554 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
558 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
562 find_last_not_of(
const _CharT* __str,
563 size_type __pos, size_type __n)
const noexcept;
565 [[nodiscard, __gnu__::__nonnull__]]
567 find_last_not_of(
const _CharT* __str,
568 size_type __pos = npos)
const noexcept
570 return this->find_last_not_of(__str, __pos,
571 traits_type::length(__str));
577 _S_compare(size_type __n1, size_type __n2)
noexcept
580 const difference_type __diff = __n1 - __n2;
581 if (__diff > __limits::__max)
582 return __limits::__max;
583 if (__diff < __limits::__min)
584 return __limits::__min;
585 return static_cast<int>(__diff);
589 const _CharT* _M_str;