113 class basic_string_view
115 static_assert(!is_array_v<_CharT>);
116 static_assert(is_trivially_copyable_v<_CharT>
117 && is_trivially_default_constructible_v<_CharT>
118 && is_standard_layout_v<_CharT>);
119 static_assert(is_same_v<_CharT, typename _Traits::char_type>);
124 using traits_type = _Traits;
125 using value_type = _CharT;
126 using pointer = value_type*;
127 using const_pointer =
const value_type*;
128 using reference = value_type&;
129 using const_reference =
const value_type&;
130 using const_iterator =
const value_type*;
131 using iterator = const_iterator;
133 using reverse_iterator = const_reverse_iterator;
134 using size_type = size_t;
135 using difference_type = ptrdiff_t;
136 static constexpr size_type npos = size_type(-1);
141 basic_string_view() noexcept
142 : _M_len{0}, _M_str{
nullptr}
145 constexpr basic_string_view(
const basic_string_view&)
noexcept =
default;
147 [[__gnu__::__nonnull__]]
149 basic_string_view(
const _CharT* __str) noexcept
150 : _M_len{traits_type::length(__str)},
155 basic_string_view(
const _CharT* __str, size_type __len) noexcept
156 : _M_len{__len}, _M_str{__str}
159#if __cplusplus >= 202002L && __cpp_lib_concepts
160 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
164 basic_string_view(_It __first, _End __last)
165 noexcept(
noexcept(__last - __first))
169#if __cplusplus > 202002L
170 template<
typename _Range,
typename _DRange = remove_cvref_t<_Range>>
171 requires (!is_same_v<_DRange, basic_string_view>)
174 && is_same_v<ranges::range_value_t<_Range>, _CharT>
175 && (!is_convertible_v<_Range, const _CharT*>)
176 && (!
requires (_DRange& __d) {
177 __d.operator ::std::basic_string_view<_CharT, _Traits>();
180 basic_string_view(_Range&& __r)
181 noexcept(
noexcept(ranges::size(__r)) &&
noexcept(ranges::data(__r)))
182 : _M_len(ranges::size(__r)), _M_str(ranges::data(__r))
185 basic_string_view(nullptr_t) =
delete;
189 constexpr basic_string_view&
190 operator=(
const basic_string_view&)
noexcept =
default;
195 constexpr const_iterator
196 begin()
const noexcept
197 {
return this->_M_str; }
200 constexpr const_iterator
202 {
return this->_M_str + this->_M_len; }
205 constexpr const_iterator
206 cbegin()
const noexcept
207 {
return this->_M_str; }
210 constexpr const_iterator
211 cend()
const noexcept
212 {
return this->_M_str + this->_M_len; }
215 constexpr const_reverse_iterator
216 rbegin()
const noexcept
217 {
return const_reverse_iterator(this->end()); }
220 constexpr const_reverse_iterator
221 rend()
const noexcept
222 {
return const_reverse_iterator(this->begin()); }
225 constexpr const_reverse_iterator
226 crbegin()
const noexcept
227 {
return const_reverse_iterator(this->end()); }
230 constexpr const_reverse_iterator
231 crend()
const noexcept
232 {
return const_reverse_iterator(this->begin()); }
238 size()
const noexcept
239 {
return this->_M_len; }
243 length()
const noexcept
248 max_size()
const noexcept
250 return (npos -
sizeof(size_type) -
sizeof(
void*))
251 /
sizeof(value_type) / 4;
256 empty()
const noexcept
257 {
return this->_M_len == 0; }
262 constexpr const_reference
263 operator[](size_type __pos)
const noexcept
265 __glibcxx_assert(__pos < this->_M_len);
266 return *(this->_M_str + __pos);
270 constexpr const_reference
271 at(size_type __pos)
const
274 __throw_out_of_range_fmt(__N(
"basic_string_view::at: __pos "
275 "(which is %zu) >= this->size() "
276 "(which is %zu)"), __pos, this->size());
277 return *(this->_M_str + __pos);
281 constexpr const_reference
282 front()
const noexcept
284 __glibcxx_assert(this->_M_len > 0);
285 return *this->_M_str;
289 constexpr const_reference
290 back()
const noexcept
292 __glibcxx_assert(this->_M_len > 0);
293 return *(this->_M_str + this->_M_len - 1);
297 constexpr const_pointer
298 data()
const noexcept
299 {
return this->_M_str; }
304 remove_prefix(size_type __n)
noexcept
306 __glibcxx_assert(this->_M_len >= __n);
312 remove_suffix(size_type __n)
noexcept
314 __glibcxx_assert(this->_M_len >= __n);
319 swap(basic_string_view& __sv)
noexcept
330 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const
332 __glibcxx_requires_string_len(__str, __n);
333 __pos = std::__sv_check(size(), __pos,
"basic_string_view::copy");
337 traits_type::copy(__str, data() + __pos, __rlen);
342 constexpr basic_string_view
343 substr(size_type __pos = 0, size_type __n = npos)
const noexcept(
false)
345 __pos = std::__sv_check(size(), __pos,
"basic_string_view::substr");
347 return basic_string_view{_M_str + __pos, __rlen};
350#ifdef __glibcxx_string_subview
352 constexpr basic_string_view
353 subview(size_type __pos = 0, size_type __n = npos)
const
354 {
return substr(__pos, __n); }
359 compare(basic_string_view __str)
const noexcept
361 const size_type __rlen =
std::min(this->_M_len, __str._M_len);
362 int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
364 __ret = _S_compare(this->_M_len, __str._M_len);
370 compare(size_type __pos1, size_type __n1, basic_string_view __str)
const
371 {
return this->substr(__pos1, __n1).compare(__str); }
375 compare(size_type __pos1, size_type __n1,
376 basic_string_view __str, size_type __pos2, size_type __n2)
const
378 return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
381 [[nodiscard, __gnu__::__nonnull__]]
383 compare(
const _CharT* __str)
const noexcept
384 {
return this->compare(basic_string_view{__str}); }
386 [[nodiscard, __gnu__::__nonnull__]]
388 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const
389 {
return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
393 compare(size_type __pos1, size_type __n1,
394 const _CharT* __str, size_type __n2)
const noexcept(
false)
396 return this->substr(__pos1, __n1)
397 .compare(basic_string_view(__str, __n2));
400#ifdef __cpp_lib_starts_ends_with
403 starts_with(basic_string_view __x)
const noexcept
405 return _M_len >= __x._M_len
406 && traits_type::compare(_M_str, __x._M_str, __x._M_len) == 0;
411 starts_with(_CharT __x)
const noexcept
412 {
return !this->empty() && traits_type::eq(this->front(), __x); }
414 [[nodiscard, __gnu__::__nonnull__]]
416 starts_with(
const _CharT* __x)
const noexcept
417 {
return this->starts_with(basic_string_view(__x)); }
421 ends_with(basic_string_view __x)
const noexcept
423 const auto __len = this->size();
424 const auto __xlen = __x.size();
425 return __len >= __xlen
426 && traits_type::compare(end() - __xlen, __x.data(), __xlen) == 0;
431 ends_with(_CharT __x)
const noexcept
432 {
return !this->empty() && traits_type::eq(this->back(), __x); }
434 [[nodiscard, __gnu__::__nonnull__]]
436 ends_with(
const _CharT* __x)
const noexcept
437 {
return this->ends_with(basic_string_view(__x)); }
440#if __cplusplus > 202002L
441#if _GLIBCXX_HOSTED && !defined(__cpp_lib_string_contains)
444# error "libstdc++ bug: string_contains not defined when it should be"
448 contains(basic_string_view __x)
const noexcept
449 {
return this->find(__x) != npos; }
453 contains(_CharT __x)
const noexcept
454 {
return this->find(__x) != npos; }
456 [[nodiscard, __gnu__::__nonnull__]]
458 contains(
const _CharT* __x)
const noexcept
459 {
return this->find(__x) != npos; }
466 find(basic_string_view __str, size_type __pos = 0)
const noexcept
467 {
return this->find(__str._M_str, __pos, __str._M_len); }
471 find(_CharT __c, size_type __pos = 0)
const noexcept;
475 find(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
477 [[nodiscard, __gnu__::__nonnull__]]
479 find(
const _CharT* __str, size_type __pos = 0)
const noexcept
480 {
return this->find(__str, __pos, traits_type::length(__str)); }
484 rfind(basic_string_view __str, size_type __pos = npos)
const noexcept
485 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
489 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
493 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
495 [[nodiscard, __gnu__::__nonnull__]]
497 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
498 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
502 find_first_of(basic_string_view __str, size_type __pos = 0)
const noexcept
503 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
507 find_first_of(_CharT __c, size_type __pos = 0)
const noexcept
508 {
return this->find(__c, __pos); }
512 find_first_of(
const _CharT* __str, size_type __pos,
513 size_type __n)
const noexcept;
515 [[nodiscard, __gnu__::__nonnull__]]
517 find_first_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
518 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
522 find_last_of(basic_string_view __str,
523 size_type __pos = npos)
const noexcept
524 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
528 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
529 {
return this->rfind(__c, __pos); }
533 find_last_of(
const _CharT* __str, size_type __pos,
534 size_type __n)
const noexcept;
536 [[nodiscard, __gnu__::__nonnull__]]
538 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
539 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
543 find_first_not_of(basic_string_view __str,
544 size_type __pos = 0)
const noexcept
545 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
549 find_first_not_of(_CharT __c, size_type __pos = 0)
const noexcept;
553 find_first_not_of(
const _CharT* __str,
554 size_type __pos, size_type __n)
const noexcept;
556 [[nodiscard, __gnu__::__nonnull__]]
558 find_first_not_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
560 return this->find_first_not_of(__str, __pos,
561 traits_type::length(__str));
566 find_last_not_of(basic_string_view __str,
567 size_type __pos = npos)
const noexcept
568 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
572 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
576 find_last_not_of(
const _CharT* __str,
577 size_type __pos, size_type __n)
const noexcept;
579 [[nodiscard, __gnu__::__nonnull__]]
581 find_last_not_of(
const _CharT* __str,
582 size_type __pos = npos)
const noexcept
584 return this->find_last_not_of(__str, __pos,
585 traits_type::length(__str));
591 _S_compare(size_type __n1, size_type __n2)
noexcept
594 const difference_type __diff = __n1 - __n2;
595 if (__diff > __limits::__max)
596 return __limits::__max;
597 if (__diff < __limits::__min)
598 return __limits::__min;
599 return static_cast<int>(__diff);
603 const _CharT* _M_str;