3#ifndef TETL_BASIC_STRING_VIEW_STRING_VIEW_HPP
4#define TETL_BASIC_STRING_VIEW_STRING_VIEW_HPP
6#include <etl/_algorithm/clamp.hpp>
7#include <etl/_algorithm/find_end.hpp>
8#include <etl/_algorithm/lexicographical_compare.hpp>
9#include <etl/_algorithm/min.hpp>
10#include <etl/_algorithm/none_of.hpp>
11#include <etl/_concepts/emulation.hpp>
12#include <etl/_contracts/check.hpp>
13#include <etl/_iterator/begin.hpp>
14#include <etl/_iterator/data.hpp>
15#include <etl/_iterator/end.hpp>
16#include <etl/_iterator/next.hpp>
17#include <etl/_iterator/rbegin.hpp>
18#include <etl/_iterator/rend.hpp>
19#include <etl/_iterator/reverse_iterator.hpp>
20#include <etl/_iterator/size.hpp>
21#include <etl/_ranges/enable_borrowed_range.hpp>
22#include <etl/_string/char_traits.hpp>
23#include <etl/_type_traits/type_identity.hpp>
24#include <etl/_utility/swap.hpp>
34template <
typename Char,
typename Traits =
etl::char_traits<Char>>
36 using traits_type = Traits;
37 using value_type = Char;
38 using pointer = Char*;
39 using const_pointer = Char
const*;
40 using reference = Char&;
41 using const_reference = Char
const&;
42 using const_iterator = Char
const*;
43 using iterator = const_iterator;
44 using size_type =
etl::size_t;
45 using difference_type =
etl::ptrdiff_t;
46 using const_reverse_iterator =
etl::reverse_iterator<const_iterator>;
47 using reverse_iterator = const_reverse_iterator;
77 , _size{traits_type::length(str)}
87 template <
typename Iter>
88 requires(detail::RandomAccessIterator<Iter>)
124 return _begin + _size;
138 return const_reverse_iterator(
end());
147 [[
nodiscard]]
constexpr auto rend()
const noexcept -> const_reverse_iterator
160 return const_reverse_iterator(
begin());
167 TETL_PRECONDITION(pos < size());
168 return unsafe_at(pos);
175 TETL_PRECONDITION(
not empty());
183 TETL_PRECONDITION(
not empty());
184 return unsafe_at(_size - 1);
211 return size_type(-1);
224 TETL_PRECONDITION(n <= size());
233 TETL_PRECONDITION(n <= size());
240 etl::swap(_begin, v._begin);
241 etl::swap(_size, v._size);
247 [[
nodiscard]]
constexpr auto copy(Char* dest, size_type count, size_type pos = 0)
const -> size_type
249 TETL_PRECONDITION(pos <= size());
250 auto const rcount =
etl::min(count,
size() - pos);
251 traits_type::copy(dest,
data() + pos, rcount);
259 TETL_PRECONDITION(pos <= size());
260 auto const rcount =
etl::min(count,
size() - pos);
269 auto const rlen =
etl::min(
size(), v.size());
270 auto const res = traits_type::compare(
data(), v.data(), rlen);
294 return substr(pos1
, count1
).compare(v.substr(pos2, count2));
304 [[
nodiscard]]
constexpr auto compare(size_type pos1, size_type count1, Char
const* s)
const ->
int
310 [[
nodiscard]]
constexpr auto compare(size_type pos1, size_type count1, Char
const* s, size_type count2)
const ->
int
379 if (v.size() >
size() - pos) {
383 for (size_type outerIdx = pos; outerIdx <
size(); ++outerIdx) {
384 if (unsafe_at(outerIdx) == v.front()) {
386 for (size_type innerIdx = 0; innerIdx < v.size(); ++innerIdx) {
387 auto offset = outerIdx + innerIdx;
388 if (unsafe_at(offset) != v[innerIdx]) {
410 [[
nodiscard]]
constexpr auto find(Char ch, size_type pos = 0)
const noexcept -> size_type
420 constexpr auto find(Char
const* s, size_type pos, size_type count)
const -> size_type
430 constexpr auto find(Char
const* s, size_type pos = 0)
const -> size_type
443 if (sv.size() <
size() - pos) {
449 auto const* r =
etl::find_end(
data(),
data() + pos, sv.begin(), sv.end(), Traits::eq);
450 if (sv.size() > 0 && r ==
data() + pos) {
453 return static_cast<size_type>(r -
data());
473 if (Traits::eq(*--s, c)) {
474 return static_cast<size_type>(s -
data());
485 constexpr auto rfind(Char
const* s, size_type pos, size_type count)
const noexcept -> size_type
487 return rfind({s, count}, pos);
495 constexpr auto rfind(Char
const* s, size_type pos =
npos)
const noexcept -> size_type
497 return rfind({s, traits_type::length(s)}, pos);
508 for (size_type idx = pos; idx <
size(); ++idx) {
509 for (
auto const c : v) {
510 if (c == unsafe_at(idx)) {
536 constexpr auto find_first_of(Char
const* s, size_type pos, size_type count)
const -> size_type
547 constexpr auto find_first_of(Char
const* s, size_type pos = 0)
const -> size_type
561 auto const* last = str +
size();
562 for (
auto const* s = str + pos; s != last; ++s) {
563 if (Traits::find(sv.data(), sv.size(), *s) ==
nullptr) {
564 return static_cast<size_type>(s - str);
580 for (
auto const* s =
data() + pos; s != last; ++s) {
581 if (!Traits::eq(*s, c)) {
582 return static_cast<size_type>(s -
data());
596 return find_first_not_of({s, count}, pos);
606 return find_first_not_of({s, traits_type::length(s)}, pos);
619 auto offset =
etl::clamp<size_type>(pos, 0,
size() - 1);
621 auto const current = unsafe_at(offset);
622 for (
auto const ch : v) {
627 }
while (offset-- != 0);
653 constexpr auto find_last_of(Char
const* s, size_type pos, size_type count)
const -> size_type
678 auto offset =
etl::clamp<size_type>(pos, 0,
size() - 1);
680 auto equals = [&](
auto ch) {
return ch == unsafe_at(offset); };
681 if (
etl::none_of(v.begin(), v.end(), equals)) {
684 }
while (offset-- != 0);
725 return find(sv) !=
npos;
731 return find(c) !=
npos;
737 return find(s) !=
npos;
747 static constexpr size_type
npos = size_type(-1);
750 [[
nodiscard]]
constexpr auto unsafe_at(size_type pos)
const -> const_reference
755 const_pointer _begin =
nullptr;
783[[
nodiscard]]
constexpr auto operator
""_sv(
char const* str,
etl::size_t len)
noexcept ->
etl::string_view
789[[
nodiscard]]
constexpr auto operator
""_sv(
wchar_t const* str,
etl::size_t len)
noexcept ->
etl::wstring_view
795[[
nodiscard]]
constexpr auto operator
""_sv(
char8_t const* str,
etl::size_t len)
noexcept ->
etl::u8string_view
801[[
nodiscard]]
constexpr auto operator
""_sv(
char16_t const* str,
etl::size_t len)
noexcept ->
etl::u16string_view
807[[
nodiscard]]
constexpr auto operator
""_sv(
char32_t const* str,
etl::size_t len)
noexcept ->
etl::u32string_view
817template <
typename Char,
typename Traits>
818inline constexpr bool enable_borrowed_range<etl::basic_string_view<Char, Traits>> =
true;
828template <
typename Char,
typename Traits>
829[[nodiscard]]
constexpr auto
832 if (lhs.size() != rhs.size()) {
835 return lhs.compare(rhs) == 0;
844template <
typename Char,
typename Traits>
845[[nodiscard]]
constexpr auto
848 return lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
851template <
typename Char,
typename Traits,
int = 1>
852[[nodiscard]]
constexpr auto
855 return lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
858template <
typename Char,
typename Traits,
int = 2>
859[[nodiscard]]
constexpr auto
862 return lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
871template <
typename Char,
typename Traits>
872[[nodiscard]]
constexpr auto
875 return (lhs < rhs)
or (lhs == rhs);
878template <
typename Char,
typename Traits,
int = 1>
879[[nodiscard]]
constexpr auto
882 return (lhs < rhs)
or (lhs == rhs);
885template <
typename Char,
typename Traits,
int = 2>
886[[nodiscard]]
constexpr auto
889 return (lhs < rhs)
or (lhs == rhs);
898template <
typename Char,
typename Traits>
899[[nodiscard]]
constexpr auto
902 return !(lhs < rhs)
and !(lhs == rhs);
905template <
typename Char,
typename Traits,
int = 1>
906[[nodiscard]]
constexpr auto
909 return !(lhs < rhs)
and !(lhs == rhs);
912template <
typename Char,
typename Traits,
int = 2>
913[[nodiscard]]
constexpr auto
916 return !(lhs < rhs)
and !(lhs == rhs);
925template <
typename Char,
typename Traits>
926[[nodiscard]]
constexpr auto
929 return lhs > rhs
or lhs == rhs;
932template <
typename Char,
typename Traits,
int = 1>
933[[nodiscard]]
constexpr auto
936 return lhs > rhs
or lhs == rhs;
939template <
typename Char,
typename Traits,
int = 2>
940[[nodiscard]]
constexpr auto
943 return lhs > rhs
or lhs == rhs;
Definition basic_string_view.hpp:780
Definition ranges_in_fun_result.hpp:12
Definition adjacent_find.hpp:9
constexpr auto operator<(basic_string_view< Char, Traits > lhs, type_identity_t< basic_string_view< Char, Traits > > rhs) noexcept -> bool
Definition basic_string_view.hpp:860
constexpr auto operator<(basic_string_view< Char, Traits > lhs, basic_string_view< Char, Traits > rhs) noexcept -> bool
Compares two views. All comparisons are done via the compare() member function (which itself is defin...
Definition basic_string_view.hpp:846
constexpr auto operator<=(basic_string_view< Char, Traits > lhs, type_identity_t< basic_string_view< Char, Traits > > rhs) noexcept -> bool
Definition basic_string_view.hpp:887
constexpr auto operator<(type_identity_t< basic_string_view< Char, Traits > > lhs, basic_string_view< Char, Traits > rhs) noexcept -> bool
Definition basic_string_view.hpp:853
constexpr auto operator==(basic_string_view< Char, Traits > lhs, type_identity_t< basic_string_view< Char, Traits > > rhs) noexcept -> bool
Compares two views. All comparisons are done via the compare() member function (which itself is defin...
Definition basic_string_view.hpp:830
constexpr auto operator>(basic_string_view< Char, Traits > lhs, type_identity_t< basic_string_view< Char, Traits > > rhs) noexcept -> bool
Definition basic_string_view.hpp:914
constexpr auto operator<=(type_identity_t< basic_string_view< Char, Traits > > lhs, basic_string_view< Char, Traits > rhs) noexcept -> bool
Definition basic_string_view.hpp:880
constexpr auto operator>(basic_string_view< Char, Traits > lhs, basic_string_view< Char, Traits > rhs) noexcept -> bool
Compares two views. All comparisons are done via the compare() member function (which itself is defin...
Definition basic_string_view.hpp:900
constexpr auto operator>=(basic_string_view< Char, Traits > lhs, basic_string_view< Char, Traits > rhs) noexcept -> bool
Compares two views. All comparisons are done via the compare() member function (which itself is defin...
Definition basic_string_view.hpp:927
constexpr auto operator>=(type_identity_t< basic_string_view< Char, Traits > > lhs, basic_string_view< Char, Traits > rhs) noexcept -> bool
Definition basic_string_view.hpp:934
constexpr auto operator<=(basic_string_view< Char, Traits > lhs, basic_string_view< Char, Traits > rhs) noexcept -> bool
Compares two views. All comparisons are done via the compare() member function (which itself is defin...
Definition basic_string_view.hpp:873
constexpr auto operator>=(basic_string_view< Char, Traits > lhs, type_identity_t< basic_string_view< Char, Traits > > rhs) noexcept -> bool
Definition basic_string_view.hpp:941
constexpr auto operator>(type_identity_t< basic_string_view< Char, Traits > > lhs, basic_string_view< Char, Traits > rhs) noexcept -> bool
Definition basic_string_view.hpp:907
The class template basic_string_view describes an object that can refer to a constant contiguous sequ...
Definition basic_string_view.hpp:35
constexpr auto compare(size_type pos1, size_type count1, Char const *s) const -> int
Compares two character sequences. Equivalent to substr(pos1, count1).compare(basic_string_view(s)).
Definition basic_string_view.hpp:304
constexpr auto compare(size_type pos1, size_type count1, Char const *s, size_type count2) const -> int
Compares two character sequences. Equivalent to substr(pos1, count1).compare(basic_string_view(s,...
Definition basic_string_view.hpp:310
constexpr auto ends_with(Char const *str) const -> bool
Checks if the string view ends with the given suffix, where the the prefix is a null-terminated chara...
Definition basic_string_view.hpp:363
constexpr auto rfind(Char const *s, size_type pos=npos) const noexcept -> size_type
Finds the last substring equal to the given character sequence. Equivalent to rfind(basic_string_view...
Definition basic_string_view.hpp:495
constexpr auto back() const -> const_reference
Returns reference to the last character in the view.
Definition basic_string_view.hpp:181
constexpr auto compare(Char const *s) const -> int
Compares two character sequences. Equivalent to compare(basic_string_view(s)).
Definition basic_string_view.hpp:298
constexpr auto find_first_of(Char const *s, size_type pos=0) const -> size_type
Finds the first character equal to any of the characters in the given character sequence....
Definition basic_string_view.hpp:547
static constexpr size_type npos
This is a special value equal to the maximum value representable by the type size_type.
Definition basic_string_view.hpp:747
constexpr auto find_first_not_of(basic_string_view sv, size_type pos=0) const noexcept -> size_type
Finds the first character not equal to any of the characters in the given character sequence.
Definition basic_string_view.hpp:557
constexpr auto rfind(basic_string_view sv, size_type pos=npos) const noexcept -> size_type
Finds the last substring equal to the given character sequence. Finds the last occurence of v in this...
Definition basic_string_view.hpp:440
~basic_string_view() noexcept=default
constexpr basic_string_view(Char const *str, size_type size)
Constructs a view of the first count characters of the character array starting with the element poin...
Definition basic_string_view.hpp:64
constexpr auto crbegin() const noexcept -> const_reverse_iterator
Returns a reverse iterator to the first character of the reversed view. It corresponds to the last ch...
Definition basic_string_view.hpp:136
constexpr auto rfind(Char const *s, size_type pos, size_type count) const noexcept -> size_type
Finds the last substring equal to the given character sequence. Equivalent to rfind(basic_string_view...
Definition basic_string_view.hpp:485
constexpr auto starts_with(Char const *str) const -> bool
Checks if the string view begins with the given prefix, where the the prefix is a null-terminated cha...
Definition basic_string_view.hpp:335
constexpr auto find(Char const *s, size_type pos=0) const -> size_type
Finds the first substring equal to the given character sequence. Equivalent to find(basic_string_view...
Definition basic_string_view.hpp:430
constexpr auto rbegin() const noexcept -> const_reverse_iterator
Returns a reverse iterator to the first character of the reversed view. It corresponds to the last ch...
Definition basic_string_view.hpp:129
constexpr auto copy(Char *dest, size_type count, size_type pos=0) const -> size_type
Copies the substring [pos, pos + rcount) to the character array pointed to by dest,...
Definition basic_string_view.hpp:247
constexpr auto find_last_of(Char const *s, size_type pos, size_type count) const -> size_type
Finds the last character equal to one of characters in the given character sequence....
Definition basic_string_view.hpp:653
constexpr auto find(Char ch, size_type pos=0) const noexcept -> size_type
Finds the first substring equal to the given character sequence. Equivalent to find(basic_string_view...
Definition basic_string_view.hpp:410
constexpr auto compare(size_type pos1, size_type count1, basic_string_view v) const -> int
Compares two character sequences. Equivalent to substr(pos1, count1).compare(v).
Definition basic_string_view.hpp:285
constexpr auto operator=(basic_string_view const &view) noexcept -> basic_string_view &=default
Replaces the view with that of view.
constexpr auto find(basic_string_view v, size_type pos=0) const noexcept -> size_type
Finds the first substring equal to the given character sequence. Finds the first occurence of v in th...
Definition basic_string_view.hpp:373
constexpr auto find_last_of(Char const *s, size_type pos=npos) const -> size_type
Finds the last character equal to one of characters in the given character sequence....
Definition basic_string_view.hpp:666
constexpr auto find_first_not_of(Char const *s, size_type pos, size_type count) const -> size_type
Finds the first character not equal to any of the characters in the given character sequence.
Definition basic_string_view.hpp:594
constexpr auto find_last_not_of(const_pointer s, size_type pos=npos) const -> size_type
Finds the last character not equal to any of the characters in the given character sequence....
Definition basic_string_view.hpp:717
constexpr auto find_last_not_of(Char c, size_type pos=npos) const noexcept -> size_type
Finds the last character not equal to any of the characters in the given character sequence....
Definition basic_string_view.hpp:695
constexpr auto contains(basic_string_view sv) const noexcept -> bool
Checks if the string contains the given substring.
Definition basic_string_view.hpp:723
constexpr auto rfind(Char c, size_type pos=npos) const noexcept -> size_type
Finds the last substring equal to the given character sequence. Equivalent to rfind(basic_string_view...
Definition basic_string_view.hpp:461
constexpr auto find_last_not_of(const_pointer s, size_type pos, size_type count) const -> size_type
Finds the last character not equal to any of the characters in the given character sequence....
Definition basic_string_view.hpp:706
constexpr auto ends_with(Char c) const noexcept -> bool
Checks if the string view ends with the given suffix, where the prefix is a single character.
Definition basic_string_view.hpp:354
constexpr basic_string_view(nullptr_t)=delete
constexpr basic_string_view() noexcept=default
Default constructor. Constructs an empty basic_string_view. After construction, data() is equal to nu...
constexpr auto compare(basic_string_view v) const noexcept -> int
Compares two character sequences.
Definition basic_string_view.hpp:267
constexpr auto begin() const noexcept -> const_iterator
Returns an iterator to the first character of the view.
Definition basic_string_view.hpp:100
constexpr auto cbegin() const noexcept -> const_iterator
Returns an iterator to the first character of the view.
Definition basic_string_view.hpp:106
constexpr auto contains(Char c) const noexcept -> bool
Checks if the string contains the given substring.
Definition basic_string_view.hpp:729
constexpr basic_string_view(Iter first, Iter last)
Constructs a basic_string_view over the range [first, last). The behavior is undefined if [first,...
Definition basic_string_view.hpp:89
constexpr auto contains(Char const *s) const -> bool
Checks if the string contains the given substring.
Definition basic_string_view.hpp:735
constexpr auto front() const -> const_reference
Returns reference to the first character in the view.
Definition basic_string_view.hpp:173
constexpr auto empty() const noexcept -> bool
Checks if the view has no characters, i.e. whether size() == 0.
Definition basic_string_view.hpp:215
constexpr auto compare(size_type pos1, size_type count1, basic_string_view v, size_type pos2, size_type count2) const -> int
Compares two character sequences. Equivalent to substr(pos1, count1).compare(v.substr(pos2,...
Definition basic_string_view.hpp:292
constexpr auto starts_with(basic_string_view sv) const noexcept -> bool
Checks if the string view begins with the given prefix, where the prefix is a string view.
Definition basic_string_view.hpp:318
constexpr auto substr(size_type pos=0, size_type count=npos) const -> basic_string_view
Returns a view of the substring [pos, pos + rcount), where rcount is the smaller of count and size() ...
Definition basic_string_view.hpp:257
constexpr auto remove_suffix(size_type n) -> void
Moves the end of the view back by n characters.
Definition basic_string_view.hpp:231
constexpr auto find_last_of(Char c, size_type pos=npos) const noexcept -> size_type
Finds the last character equal to one of characters in the given character sequence....
Definition basic_string_view.hpp:640
constexpr auto find(Char const *s, size_type pos, size_type count) const -> size_type
Finds the first substring equal to the given character sequence. Equivalent to find(basic_string_view...
Definition basic_string_view.hpp:420
constexpr basic_string_view(basic_string_view const &other) noexcept=default
Copy constructor. Constructs a view of the same content as other. After construction,...
constexpr auto find_last_of(basic_string_view v, size_type pos=npos) const noexcept -> size_type
Finds the last character equal to one of characters in the given character sequence....
Definition basic_string_view.hpp:617
constexpr auto length() const noexcept -> size_type
Returns the number of Char elements in the view, i.e. etl::distance(begin(), end()).
Definition basic_string_view.hpp:202
constexpr auto find_first_of(Char const *s, size_type pos, size_type count) const -> size_type
Finds the first character equal to any of the characters in the given character sequence....
Definition basic_string_view.hpp:536
constexpr auto swap(basic_string_view &v) noexcept -> void
Exchanges the view with that of v.
Definition basic_string_view.hpp:238
constexpr auto max_size() const noexcept -> size_type
The largest possible number of char-like objects that can be referred to by a basic_string_view.
Definition basic_string_view.hpp:209
constexpr basic_string_view(Char const *str)
Constructs a view of the null-terminated character string pointed to by s, not including the terminat...
Definition basic_string_view.hpp:75
constexpr auto end() const noexcept -> const_iterator
Returns an iterator to the character following the last character of the view. This character acts as...
Definition basic_string_view.hpp:114
constexpr auto remove_prefix(size_type n) -> void
Moves the start of the view forward by n characters.
Definition basic_string_view.hpp:222
constexpr auto starts_with(Char c) const noexcept -> bool
Checks if the string view begins with the given prefix, where the prefix is a single character.
Definition basic_string_view.hpp:326
constexpr auto operator[](size_type pos) const -> const_reference
Returns a const reference to the character at specified location pos.
Definition basic_string_view.hpp:165
constexpr auto find_first_not_of(Char const *s, size_type pos=0) const -> size_type
Finds the first character not equal to any of the characters in the given character sequence.
Definition basic_string_view.hpp:604
constexpr auto find_last_not_of(basic_string_view v, size_type pos=npos) const noexcept -> size_type
Finds the last character not equal to any of the characters of v in this view, starting at position p...
Definition basic_string_view.hpp:676
constexpr auto find_first_of(Char c, size_type pos=0) const noexcept -> size_type
Finds the first character equal to any of the characters in the given character sequence....
Definition basic_string_view.hpp:525
constexpr auto crend() const noexcept -> const_reverse_iterator
Returns a reverse iterator to the character following the last character of the reversed view.
Definition basic_string_view.hpp:158
constexpr auto ends_with(basic_string_view sv) const noexcept -> bool
Checks if the string view ends with the given suffix, where the prefix is a string view.
Definition basic_string_view.hpp:345
constexpr auto find_first_of(basic_string_view v, size_type pos=0) const noexcept -> size_type
Finds the first character equal to any of the characters in the given character sequence....
Definition basic_string_view.hpp:506
constexpr auto data() const noexcept -> const_pointer
Returns a pointer to the underlying character array. The pointer is such that the range [data(); data...
Definition basic_string_view.hpp:190
constexpr auto rend() const noexcept -> const_reverse_iterator
Returns a reverse iterator to the character following the last character of the reversed view.
Definition basic_string_view.hpp:147
constexpr auto size() const noexcept -> size_type
Returns the number of Char elements in the view, i.e. etl::distance(begin(), end()).
Definition basic_string_view.hpp:196
constexpr auto cend() const noexcept -> const_iterator
Returns an iterator to the character following the last character of the view. This character acts as...
Definition basic_string_view.hpp:122
constexpr auto find_first_not_of(Char c, size_type pos=0) const noexcept -> size_type
Finds the first character not equal to any of the characters in the given character sequence.
Definition basic_string_view.hpp:576