31 #ifndef ETL_STRING_VIEW_INCLUDED
32 #define ETL_STRING_VIEW_INCLUDED
66 :
exception(reason_, file_name_, line_number_)
80 :
string_view_exception(ETL_ERROR_TEXT(
"basic_string_view:bounds", ETL_FILE
"A"), file_name_, line_number_)
94 :
string_view_exception(ETL_ERROR_TEXT(
"basic_string_view:uninitialised", ETL_FILE
"B"), file_name_, line_number_)
102 template <
typename T,
typename TTraits = etl::
char_traits<T> >
107 typedef T value_type;
108 typedef TTraits traits_type;
109 typedef size_t size_type;
110 typedef const T& const_reference;
111 typedef const T* const_pointer;
112 typedef const T* const_iterator;
113 typedef ETL_OR_STD::reverse_iterator<const_iterator> const_reverse_iterator;
124 : mbegin(ETL_NULLPTR)
133 : mbegin(str.
begin())
143 , mend(begin_ + TTraits::
length(begin_))
161 , mend(begin_ + size_)
169 : mbegin(other.mbegin)
217 const_iterator
end()
const
225 const_iterator cend()
const
235 return const_reverse_iterator(mend);
243 return const_reverse_iterator(mend);
249 const_reverse_iterator
rend()
const
251 return const_reverse_iterator(mbegin);
257 const_reverse_iterator
crend()
const
259 return const_reverse_iterator(mbegin);
271 return (mbegin == mend);
279 return (mend - mbegin);
303 mbegin = other.mbegin;
311 template <
typename TIterator>
312 void assign(TIterator begin_, TIterator end_)
321 template <
typename TIterator>
322 void assign(TIterator begin_,
size_t size_)
339 const_reference
at(
size_t i)
const
353 swap(mbegin, other.mbegin);
354 swap(mend, other.mend);
360 size_type
copy(T* destination, size_type count, size_type position = 0)
const
364 if (position <
size())
366 n = etl::min(count,
size() - position);
368 etl::copy(mbegin + position, mbegin + position + n, destination);
381 if (position <
size())
383 size_t n = etl::min(count,
size() - position);
412 return (*
this == view) ? 0 : ((*
this > view) ? 1 : -1);
420 int compare(size_type position1, size_type count1,
422 size_type position2, size_type count2)
const
424 return substr(position1, count1).
compare(view.substr(position2, count2));
427 int compare(
const T* text)
const
432 int compare(size_type position, size_type count,
const T* text)
const
437 int compare(size_type position, size_type count1,
const T* text, size_type count2)
const
458 size_t lengthtext = TTraits::length(text);
460 return (
size() >= lengthtext) &&
461 (
compare(0, lengthtext, text) == 0);
480 size_t lengthtext = TTraits::length(text);
481 size_t lengthview =
size();
483 return (lengthview >= lengthtext) &&
484 (
compare(lengthview - lengthtext, lengthtext, text) == 0);
497 const_iterator iposition = etl::search(
begin() + position,
end(), view.
begin(), view.
end());
499 if (iposition ==
end())
505 return etl::distance(
begin(), iposition);
509 size_type
find(T c, size_type position = 0)
const
514 size_type
find(
const T* text, size_type position, size_type count)
const
519 size_type
find(
const T* text, size_type position = 0)
const
534 position = etl::min(position,
size());
536 const_iterator iposition = etl::find_end(
begin(),
541 if (iposition ==
end())
547 return etl::distance(
begin(), iposition);
551 size_type
rfind(T c, size_type position = npos)
const
556 size_type
rfind(
const T* text, size_type position, size_type count)
const
561 size_type
rfind(
const T* text, size_type position = npos)
const
571 const size_t lengthtext =
size();
573 if (position < lengthtext)
575 for (
size_t i = position; i < lengthtext; ++i)
577 const size_t lengthview = view.
size();
579 for (
size_t j = 0; j < lengthview; ++j)
581 if (mbegin[i] == view[j])
597 size_type
find_first_of(
const T* text, size_type position, size_type count)
const
602 size_type
find_first_of(
const T* text, size_type position = 0)
const
617 position = etl::min(position,
size() - 1);
619 const_reverse_iterator it =
rbegin() +
size() - position - 1;
623 const size_t viewlength = view.
size();
625 for (
size_t j = 0; j < viewlength; ++j)
627 if (mbegin[position] == view[j])
640 size_type
find_last_of(T c, size_type position = npos)
const
645 size_type
find_last_of(
const T* text, size_type position, size_type count)
const
650 size_type
find_last_of(
const T* text, size_type position = npos)
const
660 const size_t lengthtext =
size();
662 if (position < lengthtext)
664 for (
size_t i = position; i < lengthtext; ++i)
668 const size_t viewlength = view.
size();
670 for (
size_t j = 0; j < viewlength; ++j)
672 if (mbegin[i] == view[j])
693 size_type
find_first_not_of(
const T* text, size_type position, size_type count)
const
713 position = etl::min(position,
size() - 1);
715 const_reverse_iterator it =
rbegin() +
size() - position - 1;
721 const size_t viewlength = view.
size();
723 for (
size_t j = 0; j < viewlength; ++j)
725 if (mbegin[position] == view[j])
748 size_type
find_last_not_of(
const T* text, size_type position, size_type count)
const
763 return (lhs.
size() == rhs.
size()) &&
772 return !(lhs == rhs);
780 return etl::lexicographical_compare(lhs.
begin(), lhs.
end(), rhs.
begin(), rhs.
end());
821 template<
size_t ARRAY_SIZE>
827 template<
size_t ARRAY_SIZE>
830 return wstring_view(text, ARRAY_SIZE - 1U);
833 template<
size_t ARRAY_SIZE>
836 return u16string_view(text, ARRAY_SIZE - 1U);
839 template<
size_t ARRAY_SIZE>
842 return u32string_view(text, ARRAY_SIZE - 1U);
850 struct hash<
etl::string_view>
854 return etl::private_hash::generic_hash<size_t>(
reinterpret_cast<const uint8_t*
>(&text[0]),
855 reinterpret_cast<const uint8_t*
>(&text[text.
size()]));
860 struct hash<
etl::wstring_view>
864 return etl::private_hash::generic_hash<size_t>(
reinterpret_cast<const uint8_t*
>(&text[0]),
865 reinterpret_cast<const uint8_t*
>(&text[text.
size()]));
870 struct hash<
etl::u16string_view>
874 return etl::private_hash::generic_hash<size_t>(
reinterpret_cast<const uint8_t*
>(&text[0]),
875 reinterpret_cast<const uint8_t*
>(&text[text.
size()]));
880 struct hash<
etl::u32string_view>
884 return etl::private_hash::generic_hash<size_t>(
reinterpret_cast<const uint8_t*
>(&text[0]),
885 reinterpret_cast<const uint8_t*
>(&text[text.
size()]));
894 template <
typename T,
typename TTraits >
900 template <
typename T>
String view.
Definition: string_view.h:104
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:467
etl::basic_string_view< T, TTraits > & operator=(const etl::basic_string_view< T, TTraits > &other)
Assign from a view.
Definition: string_view.h:301
const_iterator cbegin() const
Returns a const iterator to the beginning of the array.
Definition: string_view.h:209
int compare(basic_string_view< T, TTraits > view) const
Compares two views.
Definition: string_view.h:410
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:445
const_reference operator[](size_t i) const
Returns a const reference to the indexed value.
Definition: string_view.h:331
const_iterator begin() const
Returns a const iterator to the beginning of the array.
Definition: string_view.h:201
size_type find(etl::basic_string_view< T, TTraits > view, size_type position=0) const
Find characters in the view.
Definition: string_view.h:490
const_reference back() const
Returns a const reference to the last element.
Definition: string_view.h:185
size_t length() const
Returns the size of the array.
Definition: string_view.h:285
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:527
friend bool operator>(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Greater-than for array views.
Definition: string_view.h:786
void swap(basic_string_view &other)
Swaps with another basic_string_view.
Definition: string_view.h:349
void assign(TIterator begin_, size_t size_)
Assign from iterator and size.
Definition: string_view.h:322
ETL_CONSTEXPR17 basic_string_view()
Default constructor.
Definition: string_view.h:123
size_t max_size() const
Returns the maximum possible size of the array.
Definition: string_view.h:293
void assign(TIterator begin_, TIterator end_)
Assign from iterators.
Definition: string_view.h:312
const_reverse_iterator crend() const
Returns a const reverse iterator to the end of the array.
Definition: string_view.h:257
basic_string_view substr(size_type position=0, size_type count=npos) const
Returns a substring.
Definition: string_view.h:377
friend bool operator<=(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Less-than-equal for array views.
Definition: string_view.h:794
const_pointer data() const
Returns a const pointer to the first element of the internal storage.
Definition: string_view.h:193
size_type copy(T *destination, size_type count, size_type position=0) const
Copies characters.
Definition: string_view.h:360
const_reverse_iterator crbegin() const
Returns a const reverse iterator to the reverse beginning of the array.
Definition: string_view.h:241
void remove_prefix(size_type n)
Shrinks the view by moving its start forward.
Definition: string_view.h:394
const_reference at(size_t i) const
Returns a const reference to the indexed value.
Definition: string_view.h:339
const_reference front() const
Returns a const reference to the first element.
Definition: string_view.h:177
bool empty() const
Returns true if the array size is zero.
Definition: string_view.h:269
const_reverse_iterator rbegin() const
Returns a const reverse iterator to the reverse beginning of the array.
Definition: string_view.h:233
friend bool operator<(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Less-than for array views.
Definition: string_view.h:778
const_reverse_iterator rend() const
Returns a const reverse iterator to the end of the array.
Definition: string_view.h:249
const_iterator end() const
Returns a const iterator to the end of the array.
Definition: string_view.h:217
friend bool operator!=(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Inequality for array views.
Definition: string_view.h:770
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:610
friend bool operator>=(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Greater-than-equal for array views.
Definition: string_view.h:802
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:569
friend bool operator==(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Equality for array views.
Definition: string_view.h:761
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:706
void remove_suffix(size_type n)
Shrinks the view by moving its end backward.
Definition: string_view.h:402
size_t size() const
Returns the size of the array.
Definition: string_view.h:277
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:658
Definition: basic_string.h:321
The base class for basic_string_view exceptions.
Definition: string_view.h:62
#define ETL_ASSERT(b, e)
Definition: error_handler.h:290
exception(string_type reason_, string_type file_, numeric_type line_)
Constructor.
Definition: exception.h:67
Definition: exception.h:47
Definition: integral_limits.h:54
T * addressof(T &t)
Definition: memory.h:61
Definition: string_view.h:76
Definition: string_view.h:90
Definition: absolute.h:37
string_view make_string_view(const char(&text)[ARRAY_SIZE])
make_string_view.
Definition: string_view.h:822
void swap(etl::basic_string_view< T, TTraits > &lhs, etl::basic_string_view< T, TTraits > &rhs)
Swaps the values.
Definition: string_view.h:895
Character traits for any character type.
Definition: char_traits.h:97