114 class _GLIBCXX_STRUCTURAL basic_string_view
116 static_assert(!is_array_v<_CharT>);
117 static_assert(is_trivially_copyable_v<_CharT>
118 && is_trivially_default_constructible_v<_CharT>
119 && is_standard_layout_v<_CharT>);
120 static_assert(is_same_v<_CharT, typename _Traits::char_type>);
125 using traits_type = _Traits;
126 using value_type = _CharT;
127 using pointer = value_type*;
128 using const_pointer =
const value_type*;
129 using reference = value_type&;
130 using const_reference =
const value_type&;
131 using const_iterator =
const value_type*;
132 using iterator = const_iterator;
134 using reverse_iterator = const_reverse_iterator;
135 using size_type = size_t;
136 using difference_type = ptrdiff_t;
137 static constexpr size_type npos = size_type(-1);
142 basic_string_view() noexcept
143 : _M_len{0}, _M_str{
nullptr}
146 constexpr basic_string_view(
const basic_string_view&)
noexcept =
default;
148 [[__gnu__::__nonnull__]]
150 basic_string_view(
const _CharT* __str) noexcept
151 : _M_len{traits_type::length(__str)},
156 basic_string_view(
const _CharT* __str, size_type __len) noexcept
157 : _M_len{__len}, _M_str{__str}
160#if __cplusplus >= 202002L && __cpp_lib_concepts
161 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
165 basic_string_view(_It __first, _End __last)
166 noexcept(
noexcept(__last - __first))
170#if __cplusplus > 202002L
171 template<
typename _Range,
typename _DRange = remove_cvref_t<_Range>>
172 requires (!is_same_v<_DRange, basic_string_view>)
175 && is_same_v<ranges::range_value_t<_Range>, _CharT>
176 && (!is_convertible_v<_Range, const _CharT*>)
177 && (!
requires (_DRange& __d) {
178 __d.operator ::std::basic_string_view<_CharT, _Traits>();
181 basic_string_view(_Range&& __r)
182 noexcept(
noexcept(ranges::size(__r)) &&
noexcept(ranges::data(__r)))
183 : _M_len(ranges::size(__r)), _M_str(ranges::data(__r))
186 basic_string_view(nullptr_t) =
delete;
190 constexpr basic_string_view&
191 operator=(
const basic_string_view&)
noexcept =
default;
196 constexpr const_iterator
197 begin()
const noexcept
198 {
return this->_M_str; }
201 constexpr const_iterator
203 {
return this->_M_str + this->_M_len; }
206 constexpr const_iterator
207 cbegin()
const noexcept
208 {
return this->_M_str; }
211 constexpr const_iterator
212 cend()
const noexcept
213 {
return this->_M_str + this->_M_len; }
216 constexpr const_reverse_iterator
217 rbegin()
const noexcept
218 {
return const_reverse_iterator(this->end()); }
221 constexpr const_reverse_iterator
222 rend()
const noexcept
223 {
return const_reverse_iterator(this->begin()); }
226 constexpr const_reverse_iterator
227 crbegin()
const noexcept
228 {
return const_reverse_iterator(this->end()); }
231 constexpr const_reverse_iterator
232 crend()
const noexcept
233 {
return const_reverse_iterator(this->begin()); }
239 size()
const noexcept
240 {
return this->_M_len; }
244 length()
const noexcept
249 max_size()
const noexcept
251 return (npos -
sizeof(size_type) -
sizeof(
void*))
252 /
sizeof(value_type) / 4;
257 empty()
const noexcept
258 {
return this->_M_len == 0; }
263 constexpr const_reference
264 operator[](size_type __pos)
const noexcept
266 __glibcxx_assert(__pos < this->_M_len);
267 return *(this->_M_str + __pos);
271 constexpr const_reference
272 at(size_type __pos)
const
275 __throw_out_of_range_fmt(__N(
"basic_string_view::at: __pos "
276 "(which is %zu) >= this->size() "
277 "(which is %zu)"), __pos, this->size());
278 return *(this->_M_str + __pos);
282 constexpr const_reference
283 front()
const noexcept
285 __glibcxx_assert(this->_M_len > 0);
286 return *this->_M_str;
290 constexpr const_reference
291 back()
const noexcept
293 __glibcxx_assert(this->_M_len > 0);
294 return *(this->_M_str + this->_M_len - 1);
298 constexpr const_pointer
299 data()
const noexcept
300 {
return this->_M_str; }
305 remove_prefix(size_type __n)
noexcept
307 __glibcxx_assert(this->_M_len >= __n);
313 remove_suffix(size_type __n)
noexcept
315 __glibcxx_assert(this->_M_len >= __n);
320 swap(basic_string_view& __sv)
noexcept
331 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const
333 __glibcxx_requires_string_len(__str, __n);
334 __pos = std::__sv_check(size(), __pos,
"basic_string_view::copy");
338 traits_type::copy(__str, data() + __pos, __rlen);
343 constexpr basic_string_view
344 substr(size_type __pos = 0, size_type __n = npos)
const noexcept(
false)
346 __pos = std::__sv_check(size(), __pos,
"basic_string_view::substr");
348 return basic_string_view{_M_str + __pos, __rlen};
351#ifdef __glibcxx_string_subview
353 constexpr basic_string_view
354 subview(size_type __pos = 0, size_type __n = npos)
const
355 {
return substr(__pos, __n); }
360 compare(basic_string_view __str)
const noexcept
362 const size_type __rlen =
std::min(this->_M_len, __str._M_len);
363 int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
365 __ret = _S_compare(this->_M_len, __str._M_len);
371 compare(size_type __pos1, size_type __n1, basic_string_view __str)
const
372 {
return this->substr(__pos1, __n1).compare(__str); }
376 compare(size_type __pos1, size_type __n1,
377 basic_string_view __str, size_type __pos2, size_type __n2)
const
379 return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
382 [[nodiscard, __gnu__::__nonnull__]]
384 compare(
const _CharT* __str)
const noexcept
385 {
return this->compare(basic_string_view{__str}); }
387 [[nodiscard, __gnu__::__nonnull__]]
389 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const
390 {
return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
394 compare(size_type __pos1, size_type __n1,
395 const _CharT* __str, size_type __n2)
const noexcept(
false)
397 return this->substr(__pos1, __n1)
398 .compare(basic_string_view(__str, __n2));
401#ifdef __cpp_lib_starts_ends_with
404 starts_with(basic_string_view __x)
const noexcept
406 return _M_len >= __x._M_len
407 && traits_type::compare(_M_str, __x._M_str, __x._M_len) == 0;
412 starts_with(_CharT __x)
const noexcept
413 {
return !this->empty() && traits_type::eq(this->front(), __x); }
415 [[nodiscard, __gnu__::__nonnull__]]
417 starts_with(
const _CharT* __x)
const noexcept
418 {
return this->starts_with(basic_string_view(__x)); }
422 ends_with(basic_string_view __x)
const noexcept
424 const auto __len = this->size();
425 const auto __xlen = __x.size();
426 return __len >= __xlen
427 && traits_type::compare(end() - __xlen, __x.data(), __xlen) == 0;
432 ends_with(_CharT __x)
const noexcept
433 {
return !this->empty() && traits_type::eq(this->back(), __x); }
435 [[nodiscard, __gnu__::__nonnull__]]
437 ends_with(
const _CharT* __x)
const noexcept
438 {
return this->ends_with(basic_string_view(__x)); }
441#if __cplusplus > 202002L
442#if _GLIBCXX_HOSTED && !defined(__cpp_lib_string_contains)
445# error "libstdc++ bug: string_contains not defined when it should be"
449 contains(basic_string_view __x)
const noexcept
450 {
return this->find(__x) != npos; }
454 contains(_CharT __x)
const noexcept
455 {
return this->find(__x) != npos; }
457 [[nodiscard, __gnu__::__nonnull__]]
459 contains(
const _CharT* __x)
const noexcept
460 {
return this->find(__x) != npos; }
467 find(basic_string_view __str, size_type __pos = 0)
const noexcept
468 {
return this->find(__str._M_str, __pos, __str._M_len); }
472 find(_CharT __c, size_type __pos = 0)
const noexcept;
476 find(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
478 [[nodiscard, __gnu__::__nonnull__]]
480 find(
const _CharT* __str, size_type __pos = 0)
const noexcept
481 {
return this->find(__str, __pos, traits_type::length(__str)); }
485 rfind(basic_string_view __str, size_type __pos = npos)
const noexcept
486 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
490 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
494 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
496 [[nodiscard, __gnu__::__nonnull__]]
498 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
499 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
503 find_first_of(basic_string_view __str, size_type __pos = 0)
const noexcept
504 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
508 find_first_of(_CharT __c, size_type __pos = 0)
const noexcept
509 {
return this->find(__c, __pos); }
513 find_first_of(
const _CharT* __str, size_type __pos,
514 size_type __n)
const noexcept;
516 [[nodiscard, __gnu__::__nonnull__]]
518 find_first_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
519 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
523 find_last_of(basic_string_view __str,
524 size_type __pos = npos)
const noexcept
525 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
529 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
530 {
return this->rfind(__c, __pos); }
534 find_last_of(
const _CharT* __str, size_type __pos,
535 size_type __n)
const noexcept;
537 [[nodiscard, __gnu__::__nonnull__]]
539 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
540 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
544 find_first_not_of(basic_string_view __str,
545 size_type __pos = 0)
const noexcept
546 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
550 find_first_not_of(_CharT __c, size_type __pos = 0)
const noexcept;
554 find_first_not_of(
const _CharT* __str,
555 size_type __pos, size_type __n)
const noexcept;
557 [[nodiscard, __gnu__::__nonnull__]]
559 find_first_not_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
561 return this->find_first_not_of(__str, __pos,
562 traits_type::length(__str));
567 find_last_not_of(basic_string_view __str,
568 size_type __pos = npos)
const noexcept
569 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
573 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
577 find_last_not_of(
const _CharT* __str,
578 size_type __pos, size_type __n)
const noexcept;
580 [[nodiscard, __gnu__::__nonnull__]]
582 find_last_not_of(
const _CharT* __str,
583 size_type __pos = npos)
const noexcept
585 return this->find_last_not_of(__str, __pos,
586 traits_type::length(__str));
592 _S_compare(size_type __n1, size_type __n2)
noexcept
595 const difference_type __diff = __n1 - __n2;
596 if (__diff > __limits::__max)
597 return __limits::__max;
598 if (__diff < __limits::__min)
599 return __limits::__min;
600 return static_cast<int>(__diff);
604 const _CharT* _M_str;