98 class basic_string_view
100 static_assert(!is_array_v<_CharT>);
101 static_assert(is_trivial_v<_CharT> && is_standard_layout_v<_CharT>);
102 static_assert(is_same_v<_CharT, typename _Traits::char_type>);
107 using traits_type = _Traits;
108 using value_type = _CharT;
109 using pointer = value_type*;
110 using const_pointer =
const value_type*;
111 using reference = value_type&;
112 using const_reference =
const value_type&;
113 using const_iterator =
const value_type*;
114 using iterator = const_iterator;
116 using reverse_iterator = const_reverse_iterator;
117 using size_type = size_t;
118 using difference_type = ptrdiff_t;
119 static constexpr size_type npos = size_type(-1);
124 basic_string_view() noexcept
125 : _M_len{0}, _M_str{
nullptr}
128 constexpr basic_string_view(
const basic_string_view&)
noexcept =
default;
130 __attribute__((__nonnull__))
constexpr
131 basic_string_view(
const _CharT* __str) noexcept
132 : _M_len{traits_type::length(__str)},
137 basic_string_view(
const _CharT* __str, size_type __len) noexcept
138 : _M_len{__len}, _M_str{__str}
141#if __cplusplus >= 202002L && __cpp_lib_concepts
142 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
143 requires same_as<iter_value_t<_It>, _CharT>
144 && (!convertible_to<_End, size_type>)
146 basic_string_view(_It __first, _End __last)
147 noexcept(
noexcept(__last - __first))
148 : _M_len(__last - __first), _M_str(std::to_address(__first))
151#if __cplusplus > 202002L
152 template<
typename _Range,
typename _DRange = remove_cvref_t<_Range>>
153 requires (!is_same_v<_DRange, basic_string_view>)
154 && ranges::contiguous_range<_Range>
155 && ranges::sized_range<_Range>
156 && is_same_v<ranges::range_value_t<_Range>, _CharT>
157 && (!is_convertible_v<_Range, const _CharT*>)
158 && (!
requires (_DRange& __d) {
159 __d.operator ::std::basic_string_view<_CharT, _Traits>();
161 && (!
requires {
typename _DRange::traits_type; }
162 || is_same_v<typename _DRange::traits_type, _Traits>)
164 basic_string_view(_Range&& __r)
165 noexcept(
noexcept(ranges::size(__r)) &&
noexcept(ranges::data(__r)))
166 : _M_len(ranges::size(__r)), _M_str(ranges::data(__r))
171 constexpr basic_string_view&
172 operator=(
const basic_string_view&)
noexcept =
default;
176 constexpr const_iterator
177 begin()
const noexcept
178 {
return this->_M_str; }
180 constexpr const_iterator
182 {
return this->_M_str + this->_M_len; }
184 constexpr const_iterator
185 cbegin()
const noexcept
186 {
return this->_M_str; }
188 constexpr const_iterator
189 cend()
const noexcept
190 {
return this->_M_str + this->_M_len; }
192 constexpr const_reverse_iterator
193 rbegin()
const noexcept
194 {
return const_reverse_iterator(this->end()); }
196 constexpr const_reverse_iterator
197 rend()
const noexcept
198 {
return const_reverse_iterator(this->begin()); }
200 constexpr const_reverse_iterator
201 crbegin()
const noexcept
202 {
return const_reverse_iterator(this->end()); }
204 constexpr const_reverse_iterator
205 crend()
const noexcept
206 {
return const_reverse_iterator(this->begin()); }
211 size()
const noexcept
212 {
return this->_M_len; }
215 length()
const noexcept
219 max_size()
const noexcept
221 return (npos -
sizeof(size_type) -
sizeof(
void*))
222 /
sizeof(value_type) / 4;
225 [[nodiscard]]
constexpr bool
226 empty()
const noexcept
227 {
return this->_M_len == 0; }
231 constexpr const_reference
232 operator[](size_type __pos)
const noexcept
234 __glibcxx_assert(__pos < this->_M_len);
235 return *(this->_M_str + __pos);
238 constexpr const_reference
239 at(size_type __pos)
const
242 __throw_out_of_range_fmt(__N(
"basic_string_view::at: __pos "
243 "(which is %zu) >= this->size() "
244 "(which is %zu)"), __pos, this->size());
245 return *(this->_M_str + __pos);
248 constexpr const_reference
249 front()
const noexcept
251 __glibcxx_assert(this->_M_len > 0);
252 return *this->_M_str;
255 constexpr const_reference
256 back()
const noexcept
258 __glibcxx_assert(this->_M_len > 0);
259 return *(this->_M_str + this->_M_len - 1);
262 constexpr const_pointer
263 data()
const noexcept
264 {
return this->_M_str; }
269 remove_prefix(size_type __n)
noexcept
271 __glibcxx_assert(this->_M_len >= __n);
277 remove_suffix(size_type __n)
noexcept
279 __glibcxx_assert(this->_M_len >= __n);
284 swap(basic_string_view& __sv)
noexcept
295 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const
297 __glibcxx_requires_string_len(__str, __n);
298 __pos = std::__sv_check(size(), __pos,
"basic_string_view::copy");
299 const size_type __rlen =
std::min(__n, _M_len - __pos);
302 traits_type::copy(__str, data() + __pos, __rlen);
306 constexpr basic_string_view
307 substr(size_type __pos = 0, size_type __n = npos)
const noexcept(
false)
309 __pos = std::__sv_check(size(), __pos,
"basic_string_view::substr");
310 const size_type __rlen =
std::min(__n, _M_len - __pos);
311 return basic_string_view{_M_str + __pos, __rlen};
315 compare(basic_string_view __str)
const noexcept
317 const size_type __rlen =
std::min(this->_M_len, __str._M_len);
318 int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
320 __ret = _S_compare(this->_M_len, __str._M_len);
325 compare(size_type __pos1, size_type __n1, basic_string_view __str)
const
326 {
return this->substr(__pos1, __n1).compare(__str); }
329 compare(size_type __pos1, size_type __n1,
330 basic_string_view __str, size_type __pos2, size_type __n2)
const
332 return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
335 __attribute__((__nonnull__))
constexpr int
336 compare(
const _CharT* __str)
const noexcept
337 {
return this->compare(basic_string_view{__str}); }
339 __attribute__((__nonnull__))
constexpr int
340 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const
341 {
return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
344 compare(size_type __pos1, size_type __n1,
345 const _CharT* __str, size_type __n2)
const noexcept(
false)
347 return this->substr(__pos1, __n1)
348 .compare(basic_string_view(__str, __n2));
351#if __cplusplus > 201703L
352#define __cpp_lib_starts_ends_with 201711L
354 starts_with(basic_string_view __x)
const noexcept
355 {
return this->substr(0, __x.size()) == __x; }
358 starts_with(_CharT __x)
const noexcept
359 {
return !this->empty() && traits_type::eq(this->front(), __x); }
362 starts_with(
const _CharT* __x)
const noexcept
363 {
return this->starts_with(basic_string_view(__x)); }
366 ends_with(basic_string_view __x)
const noexcept
368 const auto __len = this->size();
369 const auto __xlen = __x.size();
370 return __len >= __xlen
371 && traits_type::compare(end() - __xlen, __x.data(), __xlen) == 0;
375 ends_with(_CharT __x)
const noexcept
376 {
return !this->empty() && traits_type::eq(this->back(), __x); }
379 ends_with(
const _CharT* __x)
const noexcept
380 {
return this->ends_with(basic_string_view(__x)); }
383#if __cplusplus > 202002L
384#define __cpp_lib_string_contains 202011L
386 contains(basic_string_view __x)
const noexcept
387 {
return this->find(__x) != npos; }
390 contains(_CharT __x)
const noexcept
391 {
return this->find(__x) != npos; }
394 contains(
const _CharT* __x)
const noexcept
395 {
return this->find(__x) != npos; }
401 find(basic_string_view __str, size_type __pos = 0)
const noexcept
402 {
return this->find(__str._M_str, __pos, __str._M_len); }
405 find(_CharT __c, size_type __pos = 0)
const noexcept;
408 find(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
410 __attribute__((__nonnull__))
constexpr size_type
411 find(
const _CharT* __str, size_type __pos = 0)
const noexcept
412 {
return this->find(__str, __pos, traits_type::length(__str)); }
415 rfind(basic_string_view __str, size_type __pos = npos)
const noexcept
416 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
419 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
422 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
424 __attribute__((__nonnull__))
constexpr size_type
425 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
426 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
429 find_first_of(basic_string_view __str, size_type __pos = 0)
const noexcept
430 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
433 find_first_of(_CharT __c, size_type __pos = 0)
const noexcept
434 {
return this->find(__c, __pos); }
437 find_first_of(
const _CharT* __str, size_type __pos,
438 size_type __n)
const noexcept;
440 __attribute__((__nonnull__))
constexpr size_type
441 find_first_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
442 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
445 find_last_of(basic_string_view __str,
446 size_type __pos = npos)
const noexcept
447 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
450 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
451 {
return this->rfind(__c, __pos); }
454 find_last_of(
const _CharT* __str, size_type __pos,
455 size_type __n)
const noexcept;
457 __attribute__((__nonnull__))
constexpr size_type
458 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
459 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
462 find_first_not_of(basic_string_view __str,
463 size_type __pos = 0)
const noexcept
464 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
467 find_first_not_of(_CharT __c, size_type __pos = 0)
const noexcept;
470 find_first_not_of(
const _CharT* __str,
471 size_type __pos, size_type __n)
const noexcept;
473 __attribute__((__nonnull__))
constexpr size_type
474 find_first_not_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
476 return this->find_first_not_of(__str, __pos,
477 traits_type::length(__str));
481 find_last_not_of(basic_string_view __str,
482 size_type __pos = npos)
const noexcept
483 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
486 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
489 find_last_not_of(
const _CharT* __str,
490 size_type __pos, size_type __n)
const noexcept;
492 __attribute__((__nonnull__))
constexpr size_type
493 find_last_not_of(
const _CharT* __str,
494 size_type __pos = npos)
const noexcept
496 return this->find_last_not_of(__str, __pos,
497 traits_type::length(__str));
503 _S_compare(size_type __n1, size_type __n2)
noexcept
506 const difference_type __diff = __n1 - __n2;
507 if (__diff > __limits::__max)
508 return __limits::__max;
509 if (__diff < __limits::__min)
510 return __limits::__min;
511 return static_cast<int>(__diff);
515 const _CharT* _M_str;