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>();
172 noexcept(
noexcept(ranges::size(__r)) &&
noexcept(ranges::data(__r)))
173 : _M_len(ranges::size(__r)), _M_str(ranges::data(__r))
186 constexpr const_iterator
187 begin()
const noexcept
188 {
return this->_M_str; }
191 constexpr const_iterator
193 {
return this->_M_str + this->_M_len; }
196 constexpr const_iterator
197 cbegin()
const noexcept
198 {
return this->_M_str; }
201 constexpr const_iterator
202 cend()
const noexcept
203 {
return this->_M_str + this->_M_len; }
207 rbegin()
const noexcept
212 rend()
const noexcept
217 crbegin()
const noexcept
222 crend()
const noexcept
229 size()
const noexcept
230 {
return this->_M_len; }
234 length()
const noexcept
239 max_size()
const noexcept
241 return (npos -
sizeof(size_type) -
sizeof(
void*))
242 /
sizeof(value_type) / 4;
247 empty()
const noexcept
248 {
return this->_M_len == 0; }
253 constexpr const_reference
254 operator[](size_type __pos)
const noexcept
256 __glibcxx_assert(__pos < this->_M_len);
257 return *(this->_M_str + __pos);
261 constexpr const_reference
262 at(size_type __pos)
const
265 __throw_out_of_range_fmt(__N(
"basic_string_view::at: __pos "
266 "(which is %zu) >= this->size() "
267 "(which is %zu)"), __pos, this->size());
268 return *(this->_M_str + __pos);
272 constexpr const_reference
273 front()
const noexcept
275 __glibcxx_assert(this->_M_len > 0);
276 return *this->_M_str;
280 constexpr const_reference
281 back()
const noexcept
283 __glibcxx_assert(this->_M_len > 0);
284 return *(this->_M_str + this->_M_len - 1);
288 constexpr const_pointer
289 data()
const noexcept
290 {
return this->_M_str; }
295 remove_prefix(size_type __n)
noexcept
297 __glibcxx_assert(this->_M_len >= __n);
303 remove_suffix(size_type __n)
noexcept
305 __glibcxx_assert(this->_M_len >= __n);
321 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const
323 __glibcxx_requires_string_len(__str, __n);
324 __pos = std::__sv_check(size(), __pos,
"basic_string_view::copy");
325 const size_type __rlen = std::min<size_t>(__n, _M_len - __pos);
328 traits_type::copy(__str, data() + __pos, __rlen);
334 substr(size_type __pos = 0, size_type __n = npos)
const noexcept(
false)
336 __pos = std::__sv_check(size(), __pos,
"basic_string_view::substr");
337 const size_type __rlen = std::min<size_t>(__n, _M_len - __pos);
345 const size_type __rlen =
std::min(this->_M_len, __str._M_len);
346 int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
348 __ret = _S_compare(this->_M_len, __str._M_len);
355 {
return this->substr(__pos1, __n1).compare(__str); }
359 compare(size_type __pos1, size_type __n1,
362 return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
365 [[nodiscard, __gnu__::__nonnull__]]
367 compare(
const _CharT* __str)
const noexcept
370 [[nodiscard, __gnu__::__nonnull__]]
372 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const
377 compare(size_type __pos1, size_type __n1,
378 const _CharT* __str, size_type __n2)
const noexcept(
false)
380 return this->substr(__pos1, __n1)
384#if __cplusplus > 201703L
385#define __cpp_lib_starts_ends_with 201711L
389 {
return this->substr(0, __x.size()) == __x; }
393 starts_with(_CharT __x)
const noexcept
394 {
return !this->empty() && traits_type::eq(this->front(), __x); }
396 [[nodiscard, __gnu__::__nonnull__]]
398 starts_with(
const _CharT* __x)
const noexcept
405 const auto __len = this->size();
406 const auto __xlen = __x.size();
407 return __len >= __xlen
408 && traits_type::compare(end() - __xlen, __x.data(), __xlen) == 0;
413 ends_with(_CharT __x)
const noexcept
414 {
return !this->empty() && traits_type::eq(this->back(), __x); }
416 [[nodiscard, __gnu__::__nonnull__]]
418 ends_with(
const _CharT* __x)
const noexcept
422#if __cplusplus > 202002L
426# define __cpp_lib_string_contains 202011L
431 {
return this->find(__x) != npos; }
435 contains(_CharT __x)
const noexcept
436 {
return this->find(__x) != npos; }
438 [[nodiscard, __gnu__::__nonnull__]]
440 contains(
const _CharT* __x)
const noexcept
441 {
return this->find(__x) != npos; }
449 {
return this->find(__str._M_str, __pos, __str._M_len); }
453 find(_CharT __c, size_type __pos = 0)
const noexcept;
457 find(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
459 [[nodiscard, __gnu__::__nonnull__]]
461 find(
const _CharT* __str, size_type __pos = 0)
const noexcept
462 {
return this->find(__str, __pos, traits_type::length(__str)); }
467 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
471 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
475 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
477 [[nodiscard, __gnu__::__nonnull__]]
479 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
480 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
485 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
489 find_first_of(_CharT __c, size_type __pos = 0)
const noexcept
490 {
return this->find(__c, __pos); }
494 find_first_of(
const _CharT* __str, size_type __pos,
495 size_type __n)
const noexcept;
497 [[nodiscard, __gnu__::__nonnull__]]
499 find_first_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
500 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
505 size_type __pos = npos)
const noexcept
506 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
510 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
511 {
return this->rfind(__c, __pos); }
515 find_last_of(
const _CharT* __str, size_type __pos,
516 size_type __n)
const noexcept;
518 [[nodiscard, __gnu__::__nonnull__]]
520 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
521 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
526 size_type __pos = 0)
const noexcept
527 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
531 find_first_not_of(_CharT __c, size_type __pos = 0)
const noexcept;
535 find_first_not_of(
const _CharT* __str,
536 size_type __pos, size_type __n)
const noexcept;
538 [[nodiscard, __gnu__::__nonnull__]]
540 find_first_not_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
542 return this->find_first_not_of(__str, __pos,
543 traits_type::length(__str));
549 size_type __pos = npos)
const noexcept
550 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
554 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
558 find_last_not_of(
const _CharT* __str,
559 size_type __pos, size_type __n)
const noexcept;
561 [[nodiscard, __gnu__::__nonnull__]]
563 find_last_not_of(
const _CharT* __str,
564 size_type __pos = npos)
const noexcept
566 return this->find_last_not_of(__str, __pos,
567 traits_type::length(__str));
573 _S_compare(size_type __n1, size_type __n2)
noexcept
576 const difference_type __diff = __n1 - __n2;
577 if (__diff > __limits::__max)
578 return __limits::__max;
579 if (__diff < __limits::__min)
580 return __limits::__min;
581 return static_cast<int>(__diff);
585 const _CharT* _M_str;