109 class basic_string_view
111 static_assert(!is_array_v<_CharT>);
112 static_assert(is_trivially_copyable_v<_CharT>
113 && is_trivially_default_constructible_v<_CharT>
114 && is_standard_layout_v<_CharT>);
115 static_assert(is_same_v<_CharT, typename _Traits::char_type>);
120 using traits_type = _Traits;
121 using value_type = _CharT;
122 using pointer = value_type*;
123 using const_pointer =
const value_type*;
124 using reference = value_type&;
125 using const_reference =
const value_type&;
126 using const_iterator =
const value_type*;
127 using iterator = const_iterator;
129 using reverse_iterator = const_reverse_iterator;
130 using size_type = size_t;
131 using difference_type = ptrdiff_t;
132 static constexpr size_type npos = size_type(-1);
137 basic_string_view() noexcept
138 : _M_len{0}, _M_str{
nullptr}
141 constexpr basic_string_view(
const basic_string_view&)
noexcept =
default;
143 [[__gnu__::__nonnull__]]
145 basic_string_view(
const _CharT* __str) noexcept
146 : _M_len{traits_type::length(__str)},
151 basic_string_view(
const _CharT* __str, size_type __len) noexcept
152 : _M_len{__len}, _M_str{__str}
155#if __cplusplus >= 202002L && __cpp_lib_concepts
156 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
160 basic_string_view(_It __first, _End __last)
161 noexcept(
noexcept(__last - __first))
165#if __cplusplus > 202002L
166 template<
typename _Range,
typename _DRange = remove_cvref_t<_Range>>
167 requires (!is_same_v<_DRange, basic_string_view>)
170 && is_same_v<ranges::range_value_t<_Range>, _CharT>
171 && (!is_convertible_v<_Range, const _CharT*>)
172 && (!
requires (_DRange& __d) {
173 __d.operator ::std::basic_string_view<_CharT, _Traits>();
176 basic_string_view(_Range&& __r)
177 noexcept(
noexcept(ranges::size(__r)) &&
noexcept(ranges::data(__r)))
178 : _M_len(ranges::size(__r)), _M_str(ranges::data(__r))
181 basic_string_view(nullptr_t) =
delete;
185 constexpr basic_string_view&
186 operator=(
const basic_string_view&)
noexcept =
default;
191 constexpr const_iterator
192 begin()
const noexcept
193 {
return this->_M_str; }
196 constexpr const_iterator
198 {
return this->_M_str + this->_M_len; }
201 constexpr const_iterator
202 cbegin()
const noexcept
203 {
return this->_M_str; }
206 constexpr const_iterator
207 cend()
const noexcept
208 {
return this->_M_str + this->_M_len; }
211 constexpr const_reverse_iterator
212 rbegin()
const noexcept
213 {
return const_reverse_iterator(this->end()); }
216 constexpr const_reverse_iterator
217 rend()
const noexcept
218 {
return const_reverse_iterator(this->begin()); }
221 constexpr const_reverse_iterator
222 crbegin()
const noexcept
223 {
return const_reverse_iterator(this->end()); }
226 constexpr const_reverse_iterator
227 crend()
const noexcept
228 {
return const_reverse_iterator(this->begin()); }
234 size()
const noexcept
235 {
return this->_M_len; }
239 length()
const noexcept
244 max_size()
const noexcept
246 return (npos -
sizeof(size_type) -
sizeof(
void*))
247 /
sizeof(value_type) / 4;
252 empty()
const noexcept
253 {
return this->_M_len == 0; }
258 constexpr const_reference
259 operator[](size_type __pos)
const noexcept
261 __glibcxx_assert(__pos < this->_M_len);
262 return *(this->_M_str + __pos);
266 constexpr const_reference
267 at(size_type __pos)
const
270 __throw_out_of_range_fmt(__N(
"basic_string_view::at: __pos "
271 "(which is %zu) >= this->size() "
272 "(which is %zu)"), __pos, this->size());
273 return *(this->_M_str + __pos);
277 constexpr const_reference
278 front()
const noexcept
280 __glibcxx_assert(this->_M_len > 0);
281 return *this->_M_str;
285 constexpr const_reference
286 back()
const noexcept
288 __glibcxx_assert(this->_M_len > 0);
289 return *(this->_M_str + this->_M_len - 1);
293 constexpr const_pointer
294 data()
const noexcept
295 {
return this->_M_str; }
300 remove_prefix(size_type __n)
noexcept
302 __glibcxx_assert(this->_M_len >= __n);
308 remove_suffix(size_type __n)
noexcept
310 __glibcxx_assert(this->_M_len >= __n);
315 swap(basic_string_view& __sv)
noexcept
326 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const
328 __glibcxx_requires_string_len(__str, __n);
329 __pos = std::__sv_check(size(), __pos,
"basic_string_view::copy");
333 traits_type::copy(__str, data() + __pos, __rlen);
338 constexpr basic_string_view
339 substr(size_type __pos = 0, size_type __n = npos)
const noexcept(
false)
341 __pos = std::__sv_check(size(), __pos,
"basic_string_view::substr");
343 return basic_string_view{_M_str + __pos, __rlen};
346#ifdef __glibcxx_string_subview
348 constexpr basic_string_view
349 subview(size_type __pos = 0, size_type __n = npos)
const
350 {
return substr(__pos, __n); }
355 compare(basic_string_view __str)
const noexcept
357 const size_type __rlen =
std::min(this->_M_len, __str._M_len);
358 int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
360 __ret = _S_compare(this->_M_len, __str._M_len);
366 compare(size_type __pos1, size_type __n1, basic_string_view __str)
const
367 {
return this->substr(__pos1, __n1).compare(__str); }
371 compare(size_type __pos1, size_type __n1,
372 basic_string_view __str, size_type __pos2, size_type __n2)
const
374 return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
377 [[nodiscard, __gnu__::__nonnull__]]
379 compare(
const _CharT* __str)
const noexcept
380 {
return this->compare(basic_string_view{__str}); }
382 [[nodiscard, __gnu__::__nonnull__]]
384 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const
385 {
return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
389 compare(size_type __pos1, size_type __n1,
390 const _CharT* __str, size_type __n2)
const noexcept(
false)
392 return this->substr(__pos1, __n1)
393 .compare(basic_string_view(__str, __n2));
396#ifdef __cpp_lib_starts_ends_with
399 starts_with(basic_string_view __x)
const noexcept
401 return _M_len >= __x._M_len
402 && traits_type::compare(_M_str, __x._M_str, __x._M_len) == 0;
407 starts_with(_CharT __x)
const noexcept
408 {
return !this->empty() && traits_type::eq(this->front(), __x); }
410 [[nodiscard, __gnu__::__nonnull__]]
412 starts_with(
const _CharT* __x)
const noexcept
413 {
return this->starts_with(basic_string_view(__x)); }
417 ends_with(basic_string_view __x)
const noexcept
419 const auto __len = this->size();
420 const auto __xlen = __x.size();
421 return __len >= __xlen
422 && traits_type::compare(end() - __xlen, __x.data(), __xlen) == 0;
427 ends_with(_CharT __x)
const noexcept
428 {
return !this->empty() && traits_type::eq(this->back(), __x); }
430 [[nodiscard, __gnu__::__nonnull__]]
432 ends_with(
const _CharT* __x)
const noexcept
433 {
return this->ends_with(basic_string_view(__x)); }
436#if __cplusplus > 202002L
437#if _GLIBCXX_HOSTED && !defined(__cpp_lib_string_contains)
440# error "libstdc++ bug: string_contains not defined when it should be"
444 contains(basic_string_view __x)
const noexcept
445 {
return this->find(__x) != npos; }
449 contains(_CharT __x)
const noexcept
450 {
return this->find(__x) != npos; }
452 [[nodiscard, __gnu__::__nonnull__]]
454 contains(
const _CharT* __x)
const noexcept
455 {
return this->find(__x) != npos; }
462 find(basic_string_view __str, size_type __pos = 0)
const noexcept
463 {
return this->find(__str._M_str, __pos, __str._M_len); }
467 find(_CharT __c, size_type __pos = 0)
const noexcept;
471 find(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
473 [[nodiscard, __gnu__::__nonnull__]]
475 find(
const _CharT* __str, size_type __pos = 0)
const noexcept
476 {
return this->find(__str, __pos, traits_type::length(__str)); }
480 rfind(basic_string_view __str, size_type __pos = npos)
const noexcept
481 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
485 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
489 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
491 [[nodiscard, __gnu__::__nonnull__]]
493 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
494 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
498 find_first_of(basic_string_view __str, size_type __pos = 0)
const noexcept
499 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
503 find_first_of(_CharT __c, size_type __pos = 0)
const noexcept
504 {
return this->find(__c, __pos); }
508 find_first_of(
const _CharT* __str, size_type __pos,
509 size_type __n)
const noexcept;
511 [[nodiscard, __gnu__::__nonnull__]]
513 find_first_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
514 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
518 find_last_of(basic_string_view __str,
519 size_type __pos = npos)
const noexcept
520 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
524 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
525 {
return this->rfind(__c, __pos); }
529 find_last_of(
const _CharT* __str, size_type __pos,
530 size_type __n)
const noexcept;
532 [[nodiscard, __gnu__::__nonnull__]]
534 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
535 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
539 find_first_not_of(basic_string_view __str,
540 size_type __pos = 0)
const noexcept
541 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
545 find_first_not_of(_CharT __c, size_type __pos = 0)
const noexcept;
549 find_first_not_of(
const _CharT* __str,
550 size_type __pos, size_type __n)
const noexcept;
552 [[nodiscard, __gnu__::__nonnull__]]
554 find_first_not_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
556 return this->find_first_not_of(__str, __pos,
557 traits_type::length(__str));
562 find_last_not_of(basic_string_view __str,
563 size_type __pos = npos)
const noexcept
564 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
568 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
572 find_last_not_of(
const _CharT* __str,
573 size_type __pos, size_type __n)
const noexcept;
575 [[nodiscard, __gnu__::__nonnull__]]
577 find_last_not_of(
const _CharT* __str,
578 size_type __pos = npos)
const noexcept
580 return this->find_last_not_of(__str, __pos,
581 traits_type::length(__str));
587 _S_compare(size_type __n1, size_type __n2)
noexcept
590 const difference_type __diff = __n1 - __n2;
591 if (__diff > __limits::__max)
592 return __limits::__max;
593 if (__diff < __limits::__min)
594 return __limits::__min;
595 return static_cast<int>(__diff);
599 const _CharT* _M_str;