31#ifndef ETL_STRING_VIEW_INCLUDED
32#define ETL_STRING_VIEW_INCLUDED
58 :
exception(reason_, file_name_, line_number_)
72 :
string_view_exception(ETL_ERROR_TEXT(
"basic_string_view:bounds", ETL_STRING_VIEW_FILE_ID
"A"), file_name_, line_number_)
86 :
string_view_exception(ETL_ERROR_TEXT(
"basic_string_view:uninitialised", ETL_STRING_VIEW_FILE_ID
"B"), file_name_, line_number_)
94 template <
typename T,
typename TTraits = etl::
char_traits<T> >
100 typedef TTraits traits_type;
101 typedef size_t size_type;
102 typedef const T& const_reference;
103 typedef const T* const_pointer;
104 typedef const T* const_iterator;
105 typedef ETL_OR_STD::reverse_iterator<const_iterator> const_reverse_iterator;
116 : mbegin(ETL_NULLPTR)
125 : mbegin(str.
begin())
135 , mend(begin_ + TTraits::
length(begin_))
153 , mend(begin_ + size_)
161 : mbegin(other.mbegin)
169 ETL_CONSTEXPR const_reference
front()
const
177 ETL_CONSTEXPR const_reference
back()
const
185 ETL_CONSTEXPR const_pointer
data()
const
193 ETL_CONSTEXPR const_iterator
begin()
const
201 ETL_CONSTEXPR const_iterator
cbegin()
const
209 ETL_CONSTEXPR const_iterator
end()
const
217 ETL_CONSTEXPR const_iterator cend()
const
225 ETL_CONSTEXPR const_reverse_iterator
rbegin()
const
227 return const_reverse_iterator(mend);
233 ETL_CONSTEXPR const_reverse_iterator
crbegin()
const
235 return const_reverse_iterator(mend);
241 ETL_CONSTEXPR const_reverse_iterator
rend()
const
243 return const_reverse_iterator(mbegin);
249 ETL_CONSTEXPR const_reverse_iterator
crend()
const
251 return const_reverse_iterator(mbegin);
263 return (mbegin == mend);
269 ETL_CONSTEXPR
size_t size()
const
271 return (mend - mbegin);
295 mbegin = other.mbegin;
303 ETL_CONSTEXPR14
void assign(const_pointer begin_, const_pointer end_)
312 ETL_CONSTEXPR14
void assign(const_pointer begin_,
size_t size_)
315 mend = begin_ + size_;
329 const_reference
at(
size_t i)
const
343 swap(mbegin, other.mbegin);
344 swap(mend, other.mend);
350 ETL_CONSTEXPR14 size_type
copy(T* destination, size_type count, size_type position = 0)
const
354 if (position <
size())
356 n = etl::min(count,
size() - position);
358 etl::copy(mbegin + position, mbegin + position + n, destination);
371 if (position <
size())
373 size_t n = etl::min(count,
size() - position);
402 return (*
this == view) ? 0 : ((*
this > view) ? 1 : -1);
410 ETL_CONSTEXPR14
int compare(size_type position1, size_type count1,
412 size_type position2, size_type count2)
const
414 return substr(position1, count1).
compare(view.substr(position2, count2));
417 ETL_CONSTEXPR14
int compare(
const T* text)
const
422 ETL_CONSTEXPR14
int compare(size_type position, size_type count,
const T* text)
const
427 ETL_CONSTEXPR14
int compare(size_type position, size_type count1,
const T* text, size_type count2)
const
446 ETL_CONSTEXPR14
bool starts_with(
const T* text)
const
448 size_t lengthtext = TTraits::length(text);
450 return (
size() >= lengthtext) &&
451 (
compare(0, lengthtext, text) == 0);
463 ETL_CONSTEXPR14
bool ends_with(T c)
const
468 ETL_CONSTEXPR14
bool ends_with(
const T* text)
const
470 size_t lengthtext = TTraits::length(text);
471 size_t lengthview =
size();
473 return (lengthview >= lengthtext) &&
474 (
compare(lengthview - lengthtext, lengthtext, text) == 0);
487 const_iterator iposition = etl::search(
begin() + position,
end(), view.
begin(), view.
end());
489 if (iposition ==
end())
495 return etl::distance(
begin(), iposition);
499 ETL_CONSTEXPR14 size_type
find(T c, size_type position = 0)
const
504 ETL_CONSTEXPR14 size_type
find(
const T* text, size_type position, size_type count)
const
509 ETL_CONSTEXPR14 size_type
find(
const T* text, size_type position = 0)
const
524 position = etl::min(position,
size());
526 const_iterator iposition = etl::find_end(
begin(),
531 if (iposition ==
end())
537 return etl::distance(
begin(), iposition);
541 ETL_CONSTEXPR14 size_type
rfind(T c, size_type position = npos)
const
546 ETL_CONSTEXPR14 size_type
rfind(
const T* text, size_type position, size_type count)
const
551 ETL_CONSTEXPR14 size_type
rfind(
const T* text, size_type position = npos)
const
561 const size_t lengthtext =
size();
563 if (position < lengthtext)
565 for (
size_t i = position; i < lengthtext; ++i)
567 const size_t lengthview = view.
size();
569 for (
size_t j = 0UL; j < lengthview; ++j)
571 if (mbegin[i] == view[j])
582 ETL_CONSTEXPR14 size_type
find_first_of(T c, size_type position = 0)
const
587 ETL_CONSTEXPR14 size_type
find_first_of(
const T* text, size_type position, size_type count)
const
592 ETL_CONSTEXPR14 size_type
find_first_of(
const T* text, size_type position = 0)
const
607 position = etl::min(position,
size() - 1);
609 const_reverse_iterator it =
rbegin() +
size() - position - 1;
613 const size_t viewlength = view.
size();
615 for (
size_t j = 0UL; j < viewlength; ++j)
617 if (mbegin[position] == view[j])
630 ETL_CONSTEXPR14 size_type
find_last_of(T c, size_type position = npos)
const
635 ETL_CONSTEXPR14 size_type
find_last_of(
const T* text, size_type position, size_type count)
const
640 ETL_CONSTEXPR14 size_type
find_last_of(
const T* text, size_type position = npos)
const
650 const size_t lengthtext =
size();
652 if (position < lengthtext)
654 for (
size_t i = position; i < lengthtext; ++i)
658 const size_t viewlength = view.
size();
660 for (
size_t j = 0UL; j < viewlength; ++j)
662 if (mbegin[i] == view[j])
683 ETL_CONSTEXPR14 size_type
find_first_not_of(
const T* text, size_type position, size_type count)
const
688 ETL_CONSTEXPR14 size_type
find_first_not_of(
const T* text, size_type position = 0)
const
703 position = etl::min(position,
size() - 1);
705 const_reverse_iterator it =
rbegin() +
size() - position - 1;
711 const size_t viewlength = view.
size();
713 for (
size_t j = 0UL; j < viewlength; ++j)
715 if (mbegin[position] == view[j])
733 ETL_CONSTEXPR14 size_type
find_last_not_of(T c, size_type position = npos)
const
738 ETL_CONSTEXPR14 size_type
find_last_not_of(
const T* text, size_type position, size_type count)
const
743 ETL_CONSTEXPR14 size_type
find_last_not_of(
const T* text, size_type position = npos)
const
753 return (lhs.
size() == rhs.
size()) &&
762 return !(lhs == rhs);
770 return etl::lexicographical_compare(lhs.
begin(), lhs.
end(), rhs.
begin(), rhs.
end());
799 const_pointer mbegin;
811 template<
size_t Array_Size>
820 template<
size_t Array_Size>
821 ETL_CONSTEXPR14 wstring_view
make_string_view(
const wchar_t(&text)[Array_Size])
825 return wstring_view(text, length);
829 template<
size_t Array_Size>
830 ETL_CONSTEXPR14 u16string_view
make_string_view(
const char16_t(&text)[Array_Size])
834 return u16string_view(text, length);
838 template<
size_t Array_Size>
839 ETL_CONSTEXPR14 u32string_view
make_string_view(
const char32_t(&text)[Array_Size])
843 return u32string_view(text, length);
849#if ETL_USING_8BIT_TYPES
851 struct hash<
etl::string_view>
855 return etl::private_hash::generic_hash<size_t>(
reinterpret_cast<const uint8_t*
>(text.
data()),
856 reinterpret_cast<const uint8_t*
>(text.
data() + text.
size()));
861 struct hash<
etl::wstring_view>
865 return etl::private_hash::generic_hash<size_t>(
reinterpret_cast<const uint8_t*
>(text.
data()),
866 reinterpret_cast<const uint8_t*
>(text.
data() + text.
size()));
871 struct hash<
etl::u16string_view>
875 return etl::private_hash::generic_hash<size_t>(
reinterpret_cast<const uint8_t*
>(text.
data()),
876 reinterpret_cast<const uint8_t*
>(text.
data() + text.
size()));
881 struct hash<
etl::u32string_view>
885 return etl::private_hash::generic_hash<size_t>(
reinterpret_cast<const uint8_t*
>(text.
data()),
886 reinterpret_cast<const uint8_t*
>(text.
data() + text.
size()));
895template <
typename T,
typename TTraits >
String view.
Definition: string_view.h:96
ETL_CONSTEXPR14 int compare(basic_string_view< T, TTraits > view) const
Compares two views.
Definition: string_view.h:400
ETL_CONSTEXPR basic_string_view() ETL_NOEXCEPT
Default constructor.
Definition: string_view.h:115
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:457
ETL_CONSTEXPR14 size_type copy(T *destination, size_type count, size_type position=0) const
Copies characters.
Definition: string_view.h:350
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:784
ETL_CONSTEXPR const_reverse_iterator rend() const
Returns a const reverse iterator to the end of the array.
Definition: string_view.h:241
ETL_CONSTEXPR const_reverse_iterator crbegin() const
Returns a const reverse iterator to the reverse beginning of the array.
Definition: string_view.h:233
ETL_CONSTEXPR const_reverse_iterator crend() const
Returns a const reverse iterator to the end of the array.
Definition: string_view.h:249
ETL_CONSTEXPR size_t max_size() const
Returns the maximum possible size of the array.
Definition: string_view.h:285
ETL_CONSTEXPR14 void remove_suffix(size_type n)
Shrinks the view by moving its end backward.
Definition: string_view.h:392
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:792
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:435
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:600
ETL_CONSTEXPR const_iterator begin() const
Returns a const iterator to the beginning of the array.
Definition: string_view.h:193
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:760
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:293
ETL_CONSTEXPR14 void remove_prefix(size_type n)
Shrinks the view by moving its start forward.
Definition: string_view.h:384
ETL_CONSTEXPR14 void assign(const_pointer begin_, const_pointer end_)
Assign from iterators.
Definition: string_view.h:303
ETL_CONSTEXPR14 void swap(basic_string_view &other) ETL_NOEXCEPT
Swaps with another basic_string_view.
Definition: string_view.h:339
ETL_CONSTEXPR const_pointer data() const
Returns a const pointer to the first element of the internal storage.
Definition: string_view.h:185
ETL_CONSTEXPR size_t length() const
Returns the size of the array.
Definition: string_view.h:277
ETL_CONSTEXPR const_reverse_iterator rbegin() const
Returns a const reverse iterator to the reverse beginning of the array.
Definition: string_view.h:225
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:768
ETL_CONSTEXPR const_reference back() const
Returns a const reference to the last element.
Definition: string_view.h:177
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:696
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:559
const_reference at(size_t i) const
Returns a const reference to the indexed value.
Definition: string_view.h:329
ETL_CONSTEXPR bool empty() const
Returns true if the array size is zero.
Definition: string_view.h:261
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:480
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:517
ETL_CONSTEXPR const_iterator cbegin() const
Returns a const iterator to the beginning of the array.
Definition: string_view.h:201
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:776
ETL_CONSTEXPR14 basic_string_view substr(size_type position=0, size_type count=npos) const
Returns a substring.
Definition: string_view.h:367
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:751
ETL_CONSTEXPR const_reference front() const
Returns a const reference to the first element.
Definition: string_view.h:169
ETL_CONSTEXPR const_reference operator[](size_t i) const
Returns a const reference to the indexed value.
Definition: string_view.h:321
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:648
ETL_CONSTEXPR const_iterator end() const
Returns a const iterator to the end of the array.
Definition: string_view.h:209
ETL_CONSTEXPR size_t size() const
Returns the size of the array.
Definition: string_view.h:269
Definition: basic_string.h:326
The base class for basic_string_view exceptions.
Definition: string_view.h:54
#define ETL_ASSERT(b, e)
Definition: error_handler.h:316
ETL_CONSTEXPR exception(string_type reason_, string_type, numeric_type line_)
Constructor.
Definition: exception.h:69
Definition: exception.h:47
Definition: integral_limits.h:468
Definition: string_view.h:68
Definition: string_view.h:82
bitset_ext
Definition: absolute.h:38
ETL_CONSTEXPR14 string_view make_string_view(const char(&text)[Array_Size])
make_string_view.
Definition: string_view.h:812
void swap(etl::basic_string_view< T, TTraits > &lhs, etl::basic_string_view< T, TTraits > &rhs)
Swaps the values.
Definition: string_view.h:896
Character traits for any character type.
Definition: char_traits.h:102