31#ifndef ETL_STRING_VIEW_INCLUDED
32#define ETL_STRING_VIEW_INCLUDED
46#if ETL_USING_STL && ETL_USING_CPP17
47 #include <string_view>
102 template <
typename T,
typename TTraits = etl::
char_traits<T> >
107 typedef T value_type;
109 typedef size_t size_type;
110 typedef T& reference;
111 typedef const T& const_reference;
113 typedef const T* const_pointer;
114 typedef const T* iterator;
115 typedef const T* const_iterator;
116 typedef ETL_OR_STD::reverse_iterator<const_iterator> const_reverse_iterator;
127 : mbegin(ETL_NULLPTR)
136 : mbegin(str.
begin())
172 : mbegin(
other.mbegin)
180 ETL_CONSTEXPR const_reference
front()
const
188 ETL_CONSTEXPR const_reference
back()
const
196 ETL_CONSTEXPR const_pointer
data()
const
204 ETL_CONSTEXPR const_iterator
begin()
const
212 ETL_CONSTEXPR const_iterator
cbegin()
const
220 ETL_CONSTEXPR const_iterator
end()
const
228 ETL_CONSTEXPR const_iterator cend()
const
236 ETL_CONSTEXPR const_reverse_iterator
rbegin()
const
238 return const_reverse_iterator(mend);
244 ETL_CONSTEXPR const_reverse_iterator
crbegin()
const
246 return const_reverse_iterator(mend);
252 ETL_CONSTEXPR const_reverse_iterator
rend()
const
254 return const_reverse_iterator(mbegin);
260 ETL_CONSTEXPR const_reverse_iterator
crend()
const
262 return const_reverse_iterator(mbegin);
274 return (mbegin == mend);
280 ETL_CONSTEXPR
size_t size()
const
282 return static_cast<size_t>(mend - mbegin);
306 mbegin =
other.mbegin;
340 const_reference
at(
size_t i)
const
352 using ETL_OR_STD::swap;
361 ETL_CONSTEXPR14 size_type
copy(
T*
destination, size_type count, size_type position = 0)
const
365 if (position <
size())
367 n = etl::min(count,
size() - position);
382 if (position <
size())
384 size_t n = etl::min(count,
size() - position);
413 return (*
this ==
view) ? 0 : ((*
this >
view) ? 1 : -1);
428 ETL_CONSTEXPR14
int compare(
const T* text)
const
461 ETL_CONSTEXPR14
int compare(size_type position, size_type count,
const T* text)
const
466 ETL_CONSTEXPR14
int compare(size_type position, size_type
count1,
const T* text, size_type
count2)
const
507 ETL_CONSTEXPR14
bool ends_with(
const T* text)
const
538 ETL_CONSTEXPR14 size_type
find(
T c, size_type position = 0)
const
543 ETL_CONSTEXPR14 size_type
find(
const T* text, size_type position, size_type count)
const
548 ETL_CONSTEXPR14 size_type
find(
const T* text, size_type position = 0)
const
563 position = etl::min(position,
size());
580 ETL_CONSTEXPR14 size_type
rfind(
T c, size_type position = npos)
const
585 ETL_CONSTEXPR14 size_type
rfind(
const T* text, size_type position, size_type count)
const
590 ETL_CONSTEXPR14 size_type
rfind(
const T* text, size_type position = npos)
const
604 for (
size_t i = position; i <
lengthtext; ++i)
610 if (mbegin[i] ==
view[
j])
621 ETL_CONSTEXPR14 size_type
find_first_of(
T c, size_type position = 0)
const
626 ETL_CONSTEXPR14 size_type
find_first_of(
const T* text, size_type position, size_type count)
const
631 ETL_CONSTEXPR14 size_type
find_first_of(
const T* text, size_type position = 0)
const
646 position = etl::min(position,
size() - 1);
648 const_reverse_iterator it =
rbegin() +
size() - position - 1;
656 if (mbegin[position] ==
view[
j])
669 ETL_CONSTEXPR14 size_type
find_last_of(
T c, size_type position = npos)
const
674 ETL_CONSTEXPR14 size_type
find_last_of(
const T* text, size_type position, size_type count)
const
679 ETL_CONSTEXPR14 size_type
find_last_of(
const T* text, size_type position = npos)
const
693 for (
size_t i = position; i <
lengthtext; ++i)
701 if (mbegin[i] ==
view[
j])
723 ETL_CONSTEXPR14 size_type
find_first_not_of(
const T* text, size_type position, size_type count)
const
728 ETL_CONSTEXPR14 size_type
find_first_not_of(
const T* text, size_type position = 0)
const
743 position = etl::min(position,
size() - 1);
745 const_reverse_iterator it =
rbegin() +
size() - position - 1;
755 if (mbegin[position] ==
view[
j])
779 ETL_CONSTEXPR14 size_type
find_last_not_of(
const T* text, size_type position, size_type count)
const
784 ETL_CONSTEXPR14 size_type
find_last_not_of(
const T* text, size_type position = npos)
const
802 return find(
s) != npos;
810 return find(
c) != npos;
818 return (
lhs.size() ==
rhs.size()) &&
819 etl::equal(
lhs.begin(),
lhs.end(),
rhs.begin());
835 return etl::lexicographical_compare(
lhs.begin(),
lhs.end(),
rhs.begin(),
rhs.end());
864 const_pointer mbegin;
877 template<
size_t Array_Size>
886 template<
size_t Array_Size>
887 ETL_CONSTEXPR14 wstring_view
make_string_view(
const wchar_t(&text)[Array_Size])
891 return wstring_view(text, length);
895 template<
size_t Array_Size>
896 ETL_CONSTEXPR14 u8string_view
make_string_view(
const char8_t(&text)[Array_Size])
900 return u8string_view(text, length);
904 template<
size_t Array_Size>
905 ETL_CONSTEXPR14 u16string_view
make_string_view(
const char16_t(&text)[Array_Size])
909 return u16string_view(text, length);
913 template<
size_t Array_Size>
914 ETL_CONSTEXPR14 u32string_view
make_string_view(
const char32_t(&text)[Array_Size])
918 return u32string_view(text, length);
924#if ETL_USING_8BIT_TYPES
926 struct hash<
etl::string_view>
930 return etl::private_hash::generic_hash<size_t>(
reinterpret_cast<const uint8_t*
>(text.
data()),
931 reinterpret_cast<const uint8_t*
>(text.
data() + text.
size()));
936 struct hash<
etl::wstring_view>
940 return etl::private_hash::generic_hash<size_t>(
reinterpret_cast<const uint8_t*
>(text.
data()),
941 reinterpret_cast<const uint8_t*
>(text.
data() + text.
size()));
946 struct hash<
etl::u16string_view>
950 return etl::private_hash::generic_hash<size_t>(
reinterpret_cast<const uint8_t*
>(text.
data()),
951 reinterpret_cast<const uint8_t*
>(text.
data() + text.
size()));
956 struct hash<
etl::u32string_view>
960 return etl::private_hash::generic_hash<size_t>(
reinterpret_cast<const uint8_t*
>(text.
data()),
961 reinterpret_cast<const uint8_t*
>(text.
data() + text.
size()));
970template <
typename T,
typename TTraits >
987std::basic_ostream<T, std::char_traits<T> > &
operator<<(std::basic_ostream<T, std::char_traits<T> > &os,
990 os.write(text.data(), text.size());
String view.
Definition string_view.h:104
ETL_CONSTEXPR14 int compare(basic_string_view< T, TTraits > view) const
Compares two views.
Definition string_view.h:411
ETL_CONSTEXPR14 void assign(const_pointer begin_, size_t size_)
Assign from iterator and size.
Definition string_view.h:323
ETL_CONSTEXPR basic_string_view() ETL_NOEXCEPT
Default constructor.
Definition string_view.h:126
ETL_CONSTEXPR14 bool ends_with(etl::basic_string_view< T, TTraits > view) const
Checks if the string view ends with the given suffix.
Definition string_view.h:496
ETL_CONSTEXPR14 size_type copy(T *destination, size_type count, size_type position=0) const
Copies characters.
Definition string_view.h:361
ETL_CONSTEXPR basic_string_view(const basic_string_view &other) ETL_NOEXCEPT
Copy constructor.
Definition string_view.h:171
friend ETL_CONSTEXPR14 bool operator<=(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Less-than-equal for string_view.
Definition string_view.h:849
ETL_CONSTEXPR const_reverse_iterator rend() const
Returns a const reverse iterator to the end of the array.
Definition string_view.h:252
ETL_CONSTEXPR const_reverse_iterator crbegin() const
Returns a const reverse iterator to the reverse beginning of the array.
Definition string_view.h:244
ETL_CONSTEXPR const_reverse_iterator crend() const
Returns a const reverse iterator to the end of the array.
Definition string_view.h:260
ETL_CONSTEXPR size_t max_size() const
Returns the maximum possible size of the array.
Definition string_view.h:296
ETL_CONSTEXPR14 void remove_suffix(size_type n)
Shrinks the view by moving its end backward.
Definition string_view.h:403
ETL_CONSTEXPR basic_string_view(const etl::ibasic_string< T > &str)
Construct from string.
Definition string_view.h:135
friend ETL_CONSTEXPR14 bool operator>=(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Greater-than-equal for string_view.
Definition string_view.h:857
ETL_CONSTEXPR14 bool starts_with(etl::basic_string_view< T, TTraits > view) const
Checks if the string view starts with the given prefix.
Definition string_view.h:474
ETL_CONSTEXPR basic_string_view(const T *begin_, const T *end_)
Construct from pointer range.
Definition string_view.h:153
bool contains(value_type c) const
Checks that character is within this string.
Definition string_view.h:808
ETL_CONSTEXPR14 size_type find_last_of(etl::basic_string_view< T, TTraits > view, size_type position=npos) const
Find last occurrence of characters.
Definition string_view.h:639
ETL_CONSTEXPR const_iterator begin() const
Returns a const iterator to the beginning of the array.
Definition string_view.h:204
friend ETL_CONSTEXPR14 bool operator!=(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Inequality for string_view.
Definition string_view.h:825
ETL_CONSTEXPR14 ETL_EXPLICIT_STRING_FROM_CHAR basic_string_view(const T *begin_)
Construct from T*.
Definition string_view.h:144
ETL_CONSTEXPR14 etl::basic_string_view< T, TTraits > & operator=(const etl::basic_string_view< T, TTraits > &other)
Assign from a view.
Definition string_view.h:304
ETL_CONSTEXPR14 void remove_prefix(size_type n)
Shrinks the view by moving its start forward.
Definition string_view.h:395
ETL_CONSTEXPR14 void assign(const_pointer begin_, const_pointer end_)
Assign from iterators.
Definition string_view.h:314
ETL_CONSTEXPR14 void swap(basic_string_view &other) ETL_NOEXCEPT
Swaps with another basic_string_view.
Definition string_view.h:350
ETL_CONSTEXPR const_pointer data() const
Returns a const pointer to the first element of the internal storage.
Definition string_view.h:196
ETL_CONSTEXPR size_t length() const
Returns the size of the array.
Definition string_view.h:288
ETL_CONSTEXPR const_reverse_iterator rbegin() const
Returns a const reverse iterator to the reverse beginning of the array.
Definition string_view.h:236
bool contains(const etl::basic_string_view< T, TTraits > &view) const
Checks that the view is within this string.
Definition string_view.h:792
friend ETL_CONSTEXPR14 bool operator<(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Less-than for string_view.
Definition string_view.h:833
bool contains(const_pointer s) const
Checks that text is within this string.
Definition string_view.h:800
ETL_CONSTEXPR const_reference back() const
Returns a const reference to the last element.
Definition string_view.h:188
ETL_CONSTEXPR14 size_type find_last_not_of(etl::basic_string_view< T, TTraits > view, size_type position=npos) const
Find last absence of characters.
Definition string_view.h:736
ETL_CONSTEXPR14 size_type find_first_of(etl::basic_string_view< T, TTraits > view, size_type position=0) const
Find first occurrence of characters.
Definition string_view.h:598
const_reference at(size_t i) const
Returns a const reference to the indexed value.
Definition string_view.h:340
ETL_CONSTEXPR bool empty() const
Returns true if the array size is zero.
Definition string_view.h:272
ETL_CONSTEXPR14 size_type find(etl::basic_string_view< T, TTraits > view, size_type position=0) const
Find characters in the view.
Definition string_view.h:519
ETL_CONSTEXPR14 size_type rfind(etl::basic_string_view< T, TTraits > view, size_type position=npos) const
Find the last occurrence of a substring.
Definition string_view.h:556
ETL_CONSTEXPR const_iterator cbegin() const
Returns a const iterator to the beginning of the array.
Definition string_view.h:212
friend ETL_CONSTEXPR14 bool operator>(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Greater-than for string_view.
Definition string_view.h:841
ETL_CONSTEXPR14 basic_string_view substr(size_type position=0, size_type count=npos) const
Returns a substring.
Definition string_view.h:378
friend ETL_CONSTEXPR14 bool operator==(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Equality for string_view.
Definition string_view.h:816
ETL_CONSTEXPR const_reference front() const
Returns a const reference to the first element.
Definition string_view.h:180
ETL_CONSTEXPR const_reference operator[](size_t i) const
Returns a const reference to the indexed value.
Definition string_view.h:332
ETL_CONSTEXPR14 size_type find_first_not_of(etl::basic_string_view< T, TTraits > view, size_type position=0) const
Find first absence of characters.
Definition string_view.h:687
ETL_CONSTEXPR const_iterator end() const
Returns a const iterator to the end of the array.
Definition string_view.h:220
ETL_CONSTEXPR basic_string_view(const T *begin_, size_t size_)
Construct from pointer/size.
Definition string_view.h:162
ETL_CONSTEXPR size_t size() const
Returns the size of the array.
Definition string_view.h:280
Definition basic_string.h:351
The base class for basic_string_view exceptions.
Definition string_view.h:62
#define ETL_ASSERT(b, e)
Definition error_handler.h:356
Definition exception.h:47
Definition integral_limits.h:516
Definition string_view.h:76
Definition string_view.h:90
bitset_ext
Definition absolute.h:38
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_even_unscaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:314
ETL_CONSTEXPR14 string_view make_string_view(const char(&text)[Array_Size])
make_string_view.
Definition string_view.h:878
std::basic_ostream< T, std::char_traits< T > > & operator<<(std::basic_ostream< T, std::char_traits< T > > &os, etl::basic_string_view< T, etl::char_traits< T > > text)
Operator overload to write to std basic_ostream.
Definition string_view.h:987
Character traits for any character type.
Definition char_traits.h:120