110 static_assert(!is_array_v<_CharT>);
111 static_assert(is_trivially_copyable_v<_CharT>
112 && is_trivially_default_constructible_v<_CharT>
113 && is_standard_layout_v<_CharT>);
114 static_assert(is_same_v<_CharT, typename _Traits::char_type>);
119 using traits_type = _Traits;
120 using value_type = _CharT;
121 using pointer = value_type*;
122 using const_pointer =
const value_type*;
123 using reference = value_type&;
124 using const_reference =
const value_type&;
125 using const_iterator =
const value_type*;
126 using iterator = const_iterator;
129 using size_type = size_t;
130 using difference_type = ptrdiff_t;
131 static constexpr size_type npos = size_type(-1);
137 : _M_len{0}, _M_str{
nullptr}
142 [[__gnu__::__nonnull__]]
145 : _M_len{traits_type::length(__str)},
151 : _M_len{__len}, _M_str{__str}
154#if __cplusplus >= 202002L && __cpp_lib_concepts
155 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
156 requires same_as<iter_value_t<_It>, _CharT>
157 && (!convertible_to<_End, size_type>)
160 noexcept(
noexcept(__last - __first))
164#if __cplusplus > 202002L
165 template<
typename _Range,
typename _DRange = remove_cvref_t<_Range>>
166 requires (!is_same_v<_DRange, basic_string_view>)
167 && ranges::contiguous_range<_Range>
168 && ranges::sized_range<_Range>
169 && is_same_v<ranges::range_value_t<_Range>, _CharT>
170 && (!is_convertible_v<_Range, const _CharT*>)
171 && (!
requires (_DRange& __d) {
172 __d.operator ::std::basic_string_view<_CharT, _Traits>();
176 noexcept(
noexcept(ranges::size(__r)) &&
noexcept(ranges::data(__r)))
177 : _M_len(ranges::size(__r)), _M_str(ranges::data(__r))
190 constexpr const_iterator
191 begin()
const noexcept
192 {
return this->_M_str; }
195 constexpr const_iterator
197 {
return this->_M_str + this->_M_len; }
200 constexpr const_iterator
201 cbegin()
const noexcept
202 {
return this->_M_str; }
205 constexpr const_iterator
206 cend()
const noexcept
207 {
return this->_M_str + this->_M_len; }
211 rbegin()
const noexcept
216 rend()
const noexcept
221 crbegin()
const noexcept
226 crend()
const noexcept
233 size()
const noexcept
234 {
return this->_M_len; }
238 length()
const noexcept
243 max_size()
const noexcept
245 return (npos -
sizeof(size_type) -
sizeof(
void*))
246 /
sizeof(value_type) / 4;
251 empty()
const noexcept
252 {
return this->_M_len == 0; }
257 constexpr const_reference
258 operator[](size_type __pos)
const noexcept
260 __glibcxx_assert(__pos < this->_M_len);
261 return *(this->_M_str + __pos);
265 constexpr const_reference
266 at(size_type __pos)
const
269 __throw_out_of_range_fmt(__N(
"basic_string_view::at: __pos "
270 "(which is %zu) >= this->size() "
271 "(which is %zu)"), __pos, this->size());
272 return *(this->_M_str + __pos);
276 constexpr const_reference
277 front()
const noexcept
279 __glibcxx_assert(this->_M_len > 0);
280 return *this->_M_str;
284 constexpr const_reference
285 back()
const noexcept
287 __glibcxx_assert(this->_M_len > 0);
288 return *(this->_M_str + this->_M_len - 1);
292 constexpr const_pointer
293 data()
const noexcept
294 {
return this->_M_str; }
299 remove_prefix(size_type __n)
noexcept
301 __glibcxx_assert(this->_M_len >= __n);
307 remove_suffix(size_type __n)
noexcept
309 __glibcxx_assert(this->_M_len >= __n);
325 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const
327 __glibcxx_requires_string_len(__str, __n);
328 __pos = std::__sv_check(size(), __pos,
"basic_string_view::copy");
329 const size_type __rlen = std::min<size_t>(__n, _M_len - __pos);
332 traits_type::copy(__str, data() + __pos, __rlen);
338 substr(size_type __pos = 0, size_type __n = npos)
const noexcept(
false)
340 __pos = std::__sv_check(size(), __pos,
"basic_string_view::substr");
341 const size_type __rlen = std::min<size_t>(__n, _M_len - __pos);
349 const size_type __rlen =
std::min(this->_M_len, __str._M_len);
350 int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
352 __ret = _S_compare(this->_M_len, __str._M_len);
359 {
return this->substr(__pos1, __n1).compare(__str); }
363 compare(size_type __pos1, size_type __n1,
366 return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
369 [[nodiscard, __gnu__::__nonnull__]]
371 compare(
const _CharT* __str)
const noexcept
374 [[nodiscard, __gnu__::__nonnull__]]
376 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const
381 compare(size_type __pos1, size_type __n1,
382 const _CharT* __str, size_type __n2)
const noexcept(
false)
384 return this->substr(__pos1, __n1)
388#ifdef __cpp_lib_starts_ends_with
393 return _M_len >= __x._M_len
394 && traits_type::compare(_M_str, __x._M_str, __x._M_len) == 0;
399 starts_with(_CharT __x)
const noexcept
400 {
return !this->empty() && traits_type::eq(this->front(), __x); }
402 [[nodiscard, __gnu__::__nonnull__]]
404 starts_with(
const _CharT* __x)
const noexcept
411 const auto __len = this->size();
412 const auto __xlen = __x.size();
413 return __len >= __xlen
414 && traits_type::compare(end() - __xlen, __x.data(), __xlen) == 0;
419 ends_with(_CharT __x)
const noexcept
420 {
return !this->empty() && traits_type::eq(this->back(), __x); }
422 [[nodiscard, __gnu__::__nonnull__]]
424 ends_with(
const _CharT* __x)
const noexcept
428#if __cplusplus > 202002L
429#if _GLIBCXX_HOSTED && !defined(__cpp_lib_string_contains)
432# error "libstdc++ bug: string_contains not defined when it should be"
437 {
return this->find(__x) != npos; }
441 contains(_CharT __x)
const noexcept
442 {
return this->find(__x) != npos; }
444 [[nodiscard, __gnu__::__nonnull__]]
446 contains(
const _CharT* __x)
const noexcept
447 {
return this->find(__x) != npos; }
455 {
return this->find(__str._M_str, __pos, __str._M_len); }
459 find(_CharT __c, size_type __pos = 0)
const noexcept;
463 find(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
465 [[nodiscard, __gnu__::__nonnull__]]
467 find(
const _CharT* __str, size_type __pos = 0)
const noexcept
468 {
return this->find(__str, __pos, traits_type::length(__str)); }
473 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
477 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
481 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
483 [[nodiscard, __gnu__::__nonnull__]]
485 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
486 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
491 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
495 find_first_of(_CharT __c, size_type __pos = 0)
const noexcept
496 {
return this->find(__c, __pos); }
500 find_first_of(
const _CharT* __str, size_type __pos,
501 size_type __n)
const noexcept;
503 [[nodiscard, __gnu__::__nonnull__]]
505 find_first_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
506 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
511 size_type __pos = npos)
const noexcept
512 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
516 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
517 {
return this->rfind(__c, __pos); }
521 find_last_of(
const _CharT* __str, size_type __pos,
522 size_type __n)
const noexcept;
524 [[nodiscard, __gnu__::__nonnull__]]
526 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
527 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
532 size_type __pos = 0)
const noexcept
533 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
537 find_first_not_of(_CharT __c, size_type __pos = 0)
const noexcept;
541 find_first_not_of(
const _CharT* __str,
542 size_type __pos, size_type __n)
const noexcept;
544 [[nodiscard, __gnu__::__nonnull__]]
546 find_first_not_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
548 return this->find_first_not_of(__str, __pos,
549 traits_type::length(__str));
555 size_type __pos = npos)
const noexcept
556 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
560 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
564 find_last_not_of(
const _CharT* __str,
565 size_type __pos, size_type __n)
const noexcept;
567 [[nodiscard, __gnu__::__nonnull__]]
569 find_last_not_of(
const _CharT* __str,
570 size_type __pos = npos)
const noexcept
572 return this->find_last_not_of(__str, __pos,
573 traits_type::length(__str));
579 _S_compare(size_type __n1, size_type __n2)
noexcept
582 const difference_type __diff = __n1 - __n2;
583 if (__diff > __limits::__max)
584 return __limits::__max;
585 if (__diff < __limits::__min)
586 return __limits::__min;
587 return static_cast<int>(__diff);
591 const _CharT* _M_str;