112 class basic_string_view
114 static_assert(!is_array_v<_CharT>);
115 static_assert(is_trivially_copyable_v<_CharT>
116 && is_trivially_default_constructible_v<_CharT>
117 && is_standard_layout_v<_CharT>);
118 static_assert(is_same_v<_CharT, typename _Traits::char_type>);
123 using traits_type = _Traits;
124 using value_type = _CharT;
125 using pointer = value_type*;
126 using const_pointer =
const value_type*;
127 using reference = value_type&;
128 using const_reference =
const value_type&;
129 using const_iterator =
const value_type*;
130 using iterator = const_iterator;
132 using reverse_iterator = const_reverse_iterator;
133 using size_type = size_t;
134 using difference_type = ptrdiff_t;
135 static constexpr size_type npos = size_type(-1);
140 basic_string_view() noexcept
141 : _M_len{0}, _M_str{
nullptr}
144 constexpr basic_string_view(
const basic_string_view&)
noexcept =
default;
146 [[__gnu__::__nonnull__]]
148 basic_string_view(
const _CharT* __str) noexcept
149 : _M_len{traits_type::length(__str)},
154 basic_string_view(
const _CharT* __str, size_type __len) noexcept
155 : _M_len{__len}, _M_str{__str}
158#if __cplusplus >= 202002L && __cpp_lib_concepts
159 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
163 basic_string_view(_It __first, _End __last)
164 noexcept(
noexcept(__last - __first))
168#if __cplusplus > 202002L
169 template<
typename _Range,
typename _DRange = remove_cvref_t<_Range>>
170 requires (!is_same_v<_DRange, basic_string_view>)
173 && is_same_v<ranges::range_value_t<_Range>, _CharT>
174 && (!is_convertible_v<_Range, const _CharT*>)
175 && (!
requires (_DRange& __d) {
176 __d.operator ::std::basic_string_view<_CharT, _Traits>();
179 basic_string_view(_Range&& __r)
180 noexcept(
noexcept(ranges::size(__r)) &&
noexcept(ranges::data(__r)))
181 : _M_len(ranges::size(__r)), _M_str(ranges::data(__r))
184 basic_string_view(nullptr_t) =
delete;
188 constexpr basic_string_view&
189 operator=(
const basic_string_view&)
noexcept =
default;
194 constexpr const_iterator
195 begin()
const noexcept
196 {
return this->_M_str; }
199 constexpr const_iterator
201 {
return this->_M_str + this->_M_len; }
204 constexpr const_iterator
205 cbegin()
const noexcept
206 {
return this->_M_str; }
209 constexpr const_iterator
210 cend()
const noexcept
211 {
return this->_M_str + this->_M_len; }
214 constexpr const_reverse_iterator
215 rbegin()
const noexcept
216 {
return const_reverse_iterator(this->end()); }
219 constexpr const_reverse_iterator
220 rend()
const noexcept
221 {
return const_reverse_iterator(this->begin()); }
224 constexpr const_reverse_iterator
225 crbegin()
const noexcept
226 {
return const_reverse_iterator(this->end()); }
229 constexpr const_reverse_iterator
230 crend()
const noexcept
231 {
return const_reverse_iterator(this->begin()); }
237 size()
const noexcept
238 {
return this->_M_len; }
242 length()
const noexcept
247 max_size()
const noexcept
249 return (npos -
sizeof(size_type) -
sizeof(
void*))
250 /
sizeof(value_type) / 4;
255 empty()
const noexcept
256 {
return this->_M_len == 0; }
261 constexpr const_reference
262 operator[](size_type __pos)
const noexcept
264 __glibcxx_assert(__pos < this->_M_len);
265 return *(this->_M_str + __pos);
269 constexpr const_reference
270 at(size_type __pos)
const
273 __throw_out_of_range_fmt(__N(
"basic_string_view::at: __pos "
274 "(which is %zu) >= this->size() "
275 "(which is %zu)"), __pos, this->size());
276 return *(this->_M_str + __pos);
280 constexpr const_reference
281 front()
const noexcept
283 __glibcxx_assert(this->_M_len > 0);
284 return *this->_M_str;
288 constexpr const_reference
289 back()
const noexcept
291 __glibcxx_assert(this->_M_len > 0);
292 return *(this->_M_str + this->_M_len - 1);
296 constexpr const_pointer
297 data()
const noexcept
298 {
return this->_M_str; }
303 remove_prefix(size_type __n)
noexcept
305 __glibcxx_assert(this->_M_len >= __n);
311 remove_suffix(size_type __n)
noexcept
313 __glibcxx_assert(this->_M_len >= __n);
318 swap(basic_string_view& __sv)
noexcept
329 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const
331 __glibcxx_requires_string_len(__str, __n);
332 __pos = std::__sv_check(size(), __pos,
"basic_string_view::copy");
336 traits_type::copy(__str, data() + __pos, __rlen);
341 constexpr basic_string_view
342 substr(size_type __pos = 0, size_type __n = npos)
const noexcept(
false)
344 __pos = std::__sv_check(size(), __pos,
"basic_string_view::substr");
346 return basic_string_view{_M_str + __pos, __rlen};
349#ifdef __glibcxx_string_subview
351 constexpr basic_string_view
352 subview(size_type __pos = 0, size_type __n = npos)
const
353 {
return substr(__pos, __n); }
358 compare(basic_string_view __str)
const noexcept
360 const size_type __rlen =
std::min(this->_M_len, __str._M_len);
361 int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
363 __ret = _S_compare(this->_M_len, __str._M_len);
369 compare(size_type __pos1, size_type __n1, basic_string_view __str)
const
370 {
return this->substr(__pos1, __n1).compare(__str); }
374 compare(size_type __pos1, size_type __n1,
375 basic_string_view __str, size_type __pos2, size_type __n2)
const
377 return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
380 [[nodiscard, __gnu__::__nonnull__]]
382 compare(
const _CharT* __str)
const noexcept
383 {
return this->compare(basic_string_view{__str}); }
385 [[nodiscard, __gnu__::__nonnull__]]
387 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const
388 {
return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
392 compare(size_type __pos1, size_type __n1,
393 const _CharT* __str, size_type __n2)
const noexcept(
false)
395 return this->substr(__pos1, __n1)
396 .compare(basic_string_view(__str, __n2));
399#ifdef __cpp_lib_starts_ends_with
402 starts_with(basic_string_view __x)
const noexcept
404 return _M_len >= __x._M_len
405 && traits_type::compare(_M_str, __x._M_str, __x._M_len) == 0;
410 starts_with(_CharT __x)
const noexcept
411 {
return !this->empty() && traits_type::eq(this->front(), __x); }
413 [[nodiscard, __gnu__::__nonnull__]]
415 starts_with(
const _CharT* __x)
const noexcept
416 {
return this->starts_with(basic_string_view(__x)); }
420 ends_with(basic_string_view __x)
const noexcept
422 const auto __len = this->size();
423 const auto __xlen = __x.size();
424 return __len >= __xlen
425 && traits_type::compare(end() - __xlen, __x.data(), __xlen) == 0;
430 ends_with(_CharT __x)
const noexcept
431 {
return !this->empty() && traits_type::eq(this->back(), __x); }
433 [[nodiscard, __gnu__::__nonnull__]]
435 ends_with(
const _CharT* __x)
const noexcept
436 {
return this->ends_with(basic_string_view(__x)); }
439#if __cplusplus > 202002L
440#if _GLIBCXX_HOSTED && !defined(__cpp_lib_string_contains)
443# error "libstdc++ bug: string_contains not defined when it should be"
447 contains(basic_string_view __x)
const noexcept
448 {
return this->find(__x) != npos; }
452 contains(_CharT __x)
const noexcept
453 {
return this->find(__x) != npos; }
455 [[nodiscard, __gnu__::__nonnull__]]
457 contains(
const _CharT* __x)
const noexcept
458 {
return this->find(__x) != npos; }
465 find(basic_string_view __str, size_type __pos = 0)
const noexcept
466 {
return this->find(__str._M_str, __pos, __str._M_len); }
470 find(_CharT __c, size_type __pos = 0)
const noexcept;
474 find(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
476 [[nodiscard, __gnu__::__nonnull__]]
478 find(
const _CharT* __str, size_type __pos = 0)
const noexcept
479 {
return this->find(__str, __pos, traits_type::length(__str)); }
483 rfind(basic_string_view __str, size_type __pos = npos)
const noexcept
484 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
488 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
492 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
494 [[nodiscard, __gnu__::__nonnull__]]
496 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
497 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
501 find_first_of(basic_string_view __str, size_type __pos = 0)
const noexcept
502 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
506 find_first_of(_CharT __c, size_type __pos = 0)
const noexcept
507 {
return this->find(__c, __pos); }
511 find_first_of(
const _CharT* __str, size_type __pos,
512 size_type __n)
const noexcept;
514 [[nodiscard, __gnu__::__nonnull__]]
516 find_first_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
517 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
521 find_last_of(basic_string_view __str,
522 size_type __pos = npos)
const noexcept
523 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
527 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
528 {
return this->rfind(__c, __pos); }
532 find_last_of(
const _CharT* __str, size_type __pos,
533 size_type __n)
const noexcept;
535 [[nodiscard, __gnu__::__nonnull__]]
537 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
538 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
542 find_first_not_of(basic_string_view __str,
543 size_type __pos = 0)
const noexcept
544 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
548 find_first_not_of(_CharT __c, size_type __pos = 0)
const noexcept;
552 find_first_not_of(
const _CharT* __str,
553 size_type __pos, size_type __n)
const noexcept;
555 [[nodiscard, __gnu__::__nonnull__]]
557 find_first_not_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
559 return this->find_first_not_of(__str, __pos,
560 traits_type::length(__str));
565 find_last_not_of(basic_string_view __str,
566 size_type __pos = npos)
const noexcept
567 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
571 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
575 find_last_not_of(
const _CharT* __str,
576 size_type __pos, size_type __n)
const noexcept;
578 [[nodiscard, __gnu__::__nonnull__]]
580 find_last_not_of(
const _CharT* __str,
581 size_type __pos = npos)
const noexcept
583 return this->find_last_not_of(__str, __pos,
584 traits_type::length(__str));
590 _S_compare(size_type __n1, size_type __n2)
noexcept
593 const difference_type __diff = __n1 - __n2;
594 if (__diff > __limits::__max)
595 return __limits::__max;
596 if (__diff < __limits::__min)
597 return __limits::__min;
598 return static_cast<int>(__diff);
602 const _CharT* _M_str;