102 static_assert(!is_array_v<_CharT>);
103 static_assert(is_trivial_v<_CharT> && is_standard_layout_v<_CharT>);
104 static_assert(is_same_v<_CharT, typename _Traits::char_type>);
109 using traits_type = _Traits;
110 using value_type = _CharT;
111 using pointer = value_type*;
112 using const_pointer =
const value_type*;
113 using reference = value_type&;
114 using const_reference =
const value_type&;
115 using const_iterator =
const value_type*;
116 using iterator = const_iterator;
119 using size_type = size_t;
120 using difference_type = ptrdiff_t;
121 static constexpr size_type npos = size_type(-1);
127 : _M_len{0}, _M_str{
nullptr}
132 __attribute__((__nonnull__))
constexpr
134 : _M_len{traits_type::length(__str)},
140 : _M_len{__len}, _M_str{__str}
143#if __cplusplus >= 202002L && __cpp_lib_concepts
144 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
149 noexcept(
noexcept(__last - __first))
153#if __cplusplus > 202002L
154 template<
typename _Range,
typename _DRange = remove_cvref_t<_Range>>
155 requires (!is_same_v<_DRange, basic_string_view>)
158 && is_same_v<ranges::range_value_t<_Range>, _CharT>
159 && (!is_convertible_v<_Range, const _CharT*>)
160 && (!
requires (_DRange& __d) {
161 __d.operator ::std::basic_string_view<_CharT, _Traits>();
165 noexcept(
noexcept(ranges::size(__r)) &&
noexcept(ranges::data(__r)))
166 : _M_len(ranges::size(__r)), _M_str(ranges::data(__r))
179 constexpr const_iterator
180 begin()
const noexcept
181 {
return this->_M_str; }
183 constexpr const_iterator
185 {
return this->_M_str + this->_M_len; }
187 constexpr const_iterator
188 cbegin()
const noexcept
189 {
return this->_M_str; }
191 constexpr const_iterator
192 cend()
const noexcept
193 {
return this->_M_str + this->_M_len; }
196 rbegin()
const noexcept
200 rend()
const noexcept
204 crbegin()
const noexcept
208 crend()
const noexcept
214 size()
const noexcept
215 {
return this->_M_len; }
218 length()
const noexcept
222 max_size()
const noexcept
224 return (npos -
sizeof(size_type) -
sizeof(
void*))
225 /
sizeof(value_type) / 4;
228 [[nodiscard]]
constexpr bool
229 empty()
const noexcept
230 {
return this->_M_len == 0; }
234 constexpr const_reference
235 operator[](size_type __pos)
const noexcept
237 __glibcxx_assert(__pos < this->_M_len);
238 return *(this->_M_str + __pos);
241 constexpr const_reference
242 at(size_type __pos)
const
245 __throw_out_of_range_fmt(__N(
"basic_string_view::at: __pos "
246 "(which is %zu) >= this->size() "
247 "(which is %zu)"), __pos, this->size());
248 return *(this->_M_str + __pos);
251 constexpr const_reference
252 front()
const noexcept
254 __glibcxx_assert(this->_M_len > 0);
255 return *this->_M_str;
258 constexpr const_reference
259 back()
const noexcept
261 __glibcxx_assert(this->_M_len > 0);
262 return *(this->_M_str + this->_M_len - 1);
265 constexpr const_pointer
266 data()
const noexcept
267 {
return this->_M_str; }
272 remove_prefix(size_type __n)
noexcept
274 __glibcxx_assert(this->_M_len >= __n);
280 remove_suffix(size_type __n)
noexcept
282 __glibcxx_assert(this->_M_len >= __n);
298 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const
300 __glibcxx_requires_string_len(__str, __n);
301 __pos = std::__sv_check(size(), __pos,
"basic_string_view::copy");
302 const size_type __rlen =
std::min(__n, _M_len - __pos);
305 traits_type::copy(__str, data() + __pos, __rlen);
310 substr(size_type __pos = 0, size_type __n = npos)
const noexcept(
false)
312 __pos = std::__sv_check(size(), __pos,
"basic_string_view::substr");
313 const size_type __rlen =
std::min(__n, _M_len - __pos);
320 const size_type __rlen =
std::min(this->_M_len, __str._M_len);
321 int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
323 __ret = _S_compare(this->_M_len, __str._M_len);
329 {
return this->substr(__pos1, __n1).compare(__str); }
332 compare(size_type __pos1, size_type __n1,
335 return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
338 __attribute__((__nonnull__))
constexpr int
339 compare(
const _CharT* __str)
const noexcept
342 __attribute__((__nonnull__))
constexpr int
343 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const
347 compare(size_type __pos1, size_type __n1,
348 const _CharT* __str, size_type __n2)
const noexcept(
false)
350 return this->substr(__pos1, __n1)
354#if __cplusplus > 201703L
355#define __cpp_lib_starts_ends_with 201711L
358 {
return this->substr(0, __x.size()) == __x; }
361 starts_with(_CharT __x)
const noexcept
362 {
return !this->empty() && traits_type::eq(this->front(), __x); }
365 starts_with(
const _CharT* __x)
const noexcept
371 const auto __len = this->size();
372 const auto __xlen = __x.size();
373 return __len >= __xlen
374 && traits_type::compare(end() - __xlen, __x.data(), __xlen) == 0;
378 ends_with(_CharT __x)
const noexcept
379 {
return !this->empty() && traits_type::eq(this->back(), __x); }
382 ends_with(
const _CharT* __x)
const noexcept
386#if __cplusplus > 202002L
387#define __cpp_lib_string_contains 202011L
390 {
return this->find(__x) != npos; }
393 contains(_CharT __x)
const noexcept
394 {
return this->find(__x) != npos; }
397 contains(
const _CharT* __x)
const noexcept
398 {
return this->find(__x) != npos; }
405 {
return this->find(__str._M_str, __pos, __str._M_len); }
408 find(_CharT __c, size_type __pos = 0)
const noexcept;
411 find(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
413 __attribute__((__nonnull__))
constexpr size_type
414 find(
const _CharT* __str, size_type __pos = 0)
const noexcept
415 {
return this->find(__str, __pos, traits_type::length(__str)); }
419 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
422 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
425 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
427 __attribute__((__nonnull__))
constexpr size_type
428 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
429 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
433 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
436 find_first_of(_CharT __c, size_type __pos = 0)
const noexcept
437 {
return this->find(__c, __pos); }
440 find_first_of(
const _CharT* __str, size_type __pos,
441 size_type __n)
const noexcept;
443 __attribute__((__nonnull__))
constexpr size_type
444 find_first_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
445 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
449 size_type __pos = npos)
const noexcept
450 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
453 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
454 {
return this->rfind(__c, __pos); }
457 find_last_of(
const _CharT* __str, size_type __pos,
458 size_type __n)
const noexcept;
460 __attribute__((__nonnull__))
constexpr size_type
461 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
462 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
466 size_type __pos = 0)
const noexcept
467 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
470 find_first_not_of(_CharT __c, size_type __pos = 0)
const noexcept;
473 find_first_not_of(
const _CharT* __str,
474 size_type __pos, size_type __n)
const noexcept;
476 __attribute__((__nonnull__))
constexpr size_type
477 find_first_not_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
479 return this->find_first_not_of(__str, __pos,
480 traits_type::length(__str));
485 size_type __pos = npos)
const noexcept
486 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
489 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
492 find_last_not_of(
const _CharT* __str,
493 size_type __pos, size_type __n)
const noexcept;
495 __attribute__((__nonnull__))
constexpr size_type
496 find_last_not_of(
const _CharT* __str,
497 size_type __pos = npos)
const noexcept
499 return this->find_last_not_of(__str, __pos,
500 traits_type::length(__str));
506 _S_compare(size_type __n1, size_type __n2)
noexcept
509 const difference_type __diff = __n1 - __n2;
510 if (__diff > __limits::__max)
511 return __limits::__max;
512 if (__diff < __limits::__min)
513 return __limits::__min;
514 return static_cast<int>(__diff);
518 const _CharT* _M_str;