31#ifndef ETL_STRING_VIEW_INCLUDED
32#define ETL_STRING_VIEW_INCLUDED
46#if ETL_USING_STL && ETL_USING_CPP17
47 #include <string_view>
50#if ETL_USING_STD_OSTREAM
65 string_view_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
66 :
exception(reason_, file_name_, line_number_)
75 class string_view_bounds :
public string_view_exception
79 string_view_bounds(string_type file_name_, numeric_type line_number_)
80 : string_view_exception(ETL_ERROR_TEXT(
"basic_string_view:bounds", ETL_STRING_VIEW_FILE_ID
"A"), file_name_, line_number_)
89 class string_view_uninitialised :
public string_view_exception
93 string_view_uninitialised(string_type file_name_, numeric_type line_number_)
94 : string_view_exception(ETL_ERROR_TEXT(
"basic_string_view:uninitialised", ETL_STRING_VIEW_FILE_ID
"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 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())
146 , mend(begin_ + TTraits::length(begin_))
164 , mend(begin_ + size_)
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 ETL_NOEXCEPT
204 ETL_CONSTEXPR const_iterator
begin() const ETL_NOEXCEPT
212 ETL_CONSTEXPR const_iterator
cbegin() const ETL_NOEXCEPT
220 ETL_CONSTEXPR const_iterator
end() const ETL_NOEXCEPT
236 ETL_CONSTEXPR const_reverse_iterator
rbegin() const ETL_NOEXCEPT
238 return const_reverse_iterator(mend);
244 ETL_CONSTEXPR const_reverse_iterator
crbegin() const ETL_NOEXCEPT
246 return const_reverse_iterator(mend);
252 ETL_CONSTEXPR const_reverse_iterator
rend() const ETL_NOEXCEPT
254 return const_reverse_iterator(mbegin);
260 ETL_CONSTEXPR const_reverse_iterator
crend() const ETL_NOEXCEPT
262 return const_reverse_iterator(mbegin);
272 ETL_CONSTEXPR
bool empty() const ETL_NOEXCEPT
274 return (mbegin == mend);
280 ETL_CONSTEXPR
size_t size() const ETL_NOEXCEPT
282 return static_cast<size_t>(mend - mbegin);
288 ETL_CONSTEXPR
size_t length() const ETL_NOEXCEPT
306 mbegin = other.mbegin;
314 ETL_CONSTEXPR14
void assign(const_pointer begin_, const_pointer end_) ETL_NOEXCEPT
323 ETL_CONSTEXPR14
void assign(const_pointer begin_,
size_t size_) ETL_NOEXCEPT
326 mend = begin_ + size_;
332 ETL_CONSTEXPR const_reference
operator[](
size_t i)
const ETL_NOEXCEPT
340 const_reference
at(
size_t i)
const
352 using ETL_OR_STD::swap;
354 swap(mbegin, other.mbegin);
355 swap(mend, other.mend);
361 ETL_CONSTEXPR14 size_type
copy(T* destination, size_type count, size_type position = 0) const ETL_NOEXCEPT
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);
421 ETL_CONSTEXPR14
int compare(size_type position1, size_type count1,
423 size_type position2, size_type count2)
const ETL_NOEXCEPT
425 return substr(position1, count1).
compare(view.substr(position2, count2));
428 ETL_CONSTEXPR14
int compare(
const T* text)
const ETL_NOEXCEPT
430 const T* view_itr = mbegin;
431 const T* text_itr = text;
433 while (view_itr != mend && *text_itr != T(0))
435 if (*view_itr < *text_itr)
439 else if (*view_itr > *text_itr)
447 if ((view_itr == mend) && (*text_itr == T(0)))
451 else if (view_itr == mend)
461 ETL_CONSTEXPR14
int compare(size_type position, size_type count,
const T* text)
const ETL_NOEXCEPT
466 ETL_CONSTEXPR14
int compare(size_type position, size_type count1,
const T* text, size_type count2)
const ETL_NOEXCEPT
468 return substr(position, count1).
compare(etl::basic_string_view<T, TTraits>(text, count2));
476 return (
size() >= view.size()) &&
477 (
compare(0, view.size(), view) == 0);
480 ETL_CONSTEXPR14
bool starts_with(T c)
const ETL_NOEXCEPT
485 ETL_CONSTEXPR14
bool starts_with(
const T* text)
const ETL_NOEXCEPT
487 size_t lengthtext = TTraits::length(text);
489 return (
size() >= lengthtext) &&
490 (
compare(0, lengthtext, text) == 0);
498 return (
size() >= view.size()) &&
502 ETL_CONSTEXPR14
bool ends_with(T c)
const
507 ETL_CONSTEXPR14
bool ends_with(
const T* text)
const
509 size_t lengthtext = TTraits::length(text);
510 size_t lengthview =
size();
512 return (lengthview >= lengthtext) &&
513 (
compare(lengthview - lengthtext, lengthtext, text) == 0);
526 const_iterator iposition = etl::search(
begin() + position,
end(), view.
begin(), view.
end());
528 if (iposition ==
end())
534 return etl::distance(
begin(), iposition);
543 ETL_CONSTEXPR14 size_type
find(
const T* text, size_type position, size_type count)
const ETL_NOEXCEPT
548 ETL_CONSTEXPR14 size_type
find(
const T* text, size_type position = 0) const ETL_NOEXCEPT
550 return find(etl::basic_string_view<T, TTraits>(text), position);
558 if ((
size() < view.size()))
563 position = etl::min(position,
size());
565 const_iterator iposition = etl::find_end(
begin(),
570 if (iposition ==
end())
576 return etl::distance(
begin(), iposition);
585 ETL_CONSTEXPR14 size_type
rfind(
const T* text, size_type position, size_type count)
const ETL_NOEXCEPT
590 ETL_CONSTEXPR14 size_type
rfind(
const T* text, size_type position = npos)
const ETL_NOEXCEPT
592 return rfind(etl::basic_string_view<T, TTraits>(text), position);
600 const size_t lengthtext =
size();
602 if (position < lengthtext)
604 for (
size_t i = position; i < lengthtext; ++i)
606 const size_t lengthview = view.
size();
608 for (
size_t j = 0UL; j < lengthview; ++j)
610 if (mbegin[i] == view[j])
626 ETL_CONSTEXPR14 size_type
find_first_of(
const T* text, size_type position, size_type count)
const ETL_NOEXCEPT
631 ETL_CONSTEXPR14 size_type
find_first_of(
const T* text, size_type position = 0) const ETL_NOEXCEPT
633 return find_first_of(etl::basic_string_view<T, TTraits>(text), position);
646 position = etl::min(position,
size() - 1);
648 const_reverse_iterator it =
rbegin() +
size() - position - 1;
652 const size_t viewlength = view.size();
654 for (
size_t j = 0UL; j < viewlength; ++j)
656 if (mbegin[position] == view[j])
674 ETL_CONSTEXPR14 size_type
find_last_of(
const T* text, size_type position, size_type count)
const ETL_NOEXCEPT
679 ETL_CONSTEXPR14 size_type
find_last_of(
const T* text, size_type position = npos)
const ETL_NOEXCEPT
681 return find_last_of(etl::basic_string_view<T, TTraits>(text), position);
689 const size_t lengthtext =
size();
691 if (position < lengthtext)
693 for (
size_t i = position; i < lengthtext; ++i)
697 const size_t viewlength = view.
size();
699 for (
size_t j = 0UL; j < viewlength; ++j)
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 ETL_NOEXCEPT
728 ETL_CONSTEXPR14 size_type
find_first_not_of(
const T* text, size_type position = 0) const ETL_NOEXCEPT
743 position = etl::min(position,
size() - 1);
745 const_reverse_iterator it =
rbegin() +
size() - position - 1;
751 const size_t viewlength = view.size();
753 for (
size_t j = 0UL; j < viewlength; ++j)
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 ETL_NOEXCEPT
784 ETL_CONSTEXPR14 size_type
find_last_not_of(
const T* text, size_type position = npos)
const ETL_NOEXCEPT
786 return find_last_not_of(etl::basic_string_view<T, TTraits>(text), position);
794 return find(view) != npos;
802 return find(s) != npos;
810 return find(c) != npos;
818 return (lhs.
size() == rhs.
size()) &&
827 return !(lhs == rhs);
835 return etl::lexicographical_compare(lhs.
begin(), lhs.
end(), rhs.
begin(), rhs.
end());
864 const_pointer mbegin;
877 template<
size_t Array_Size>
880 size_t length = etl::char_traits<char>::length(text, Array_Size - 1U);
882 return string_view(text, length);
886 template<
size_t Array_Size>
887 ETL_CONSTEXPR14 wstring_view
make_string_view(
const wchar_t(&text)[Array_Size]) ETL_NOEXCEPT
889 size_t length = etl::char_traits<wchar_t>::length(text, Array_Size - 1U);
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]) ETL_NOEXCEPT
898 size_t length = etl::char_traits<char8_t>::length(text, Array_Size - 1U);
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]) ETL_NOEXCEPT
907 size_t length = etl::char_traits<char16_t>::length(text, Array_Size - 1U);
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]) ETL_NOEXCEPT
916 size_t length = etl::char_traits<char32_t>::length(text, Array_Size - 1U);
918 return u32string_view(text, length);
924#if ETL_USING_8BIT_TYPES
926 struct hash<etl::string_view>
928 size_t operator()(
const etl::string_view& text)
const
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>
938 size_t operator()(
const etl::wstring_view& text)
const
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>
948 size_t operator()(
const etl::u16string_view& text)
const
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>
958 size_t operator()(
const etl::u32string_view& text)
const
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 >
985#if ETL_USING_STD_OSTREAM
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 size_type find(etl::basic_string_view< T, TTraits > view, size_type position=0) const ETL_NOEXCEPT
Find characters in the view.
Definition string_view.h:519
ETL_CONSTEXPR const_reference operator[](size_t i) const ETL_NOEXCEPT
Returns a const reference to the indexed value.
Definition string_view.h:332
ETL_CONSTEXPR basic_string_view() ETL_NOEXCEPT
Default constructor.
Definition string_view.h:126
ETL_CONSTEXPR const_iterator cbegin() const ETL_NOEXCEPT
Returns a const iterator to the beginning of the array.
Definition string_view.h:212
ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT
Returns the size of the array.
Definition string_view.h:280
ETL_CONSTEXPR const_reverse_iterator rend() const ETL_NOEXCEPT
Returns a const reverse iterator to the end of the array.
Definition string_view.h:252
ETL_CONSTEXPR const_reverse_iterator crend() const ETL_NOEXCEPT
Returns a const reverse iterator to the end of the array.
Definition string_view.h:260
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_CONSTEXPR14 etl::basic_string_view< T, TTraits > & operator=(const etl::basic_string_view< T, TTraits > &other) ETL_NOEXCEPT
Assign from a view.
Definition string_view.h:304
ETL_CONSTEXPR14 size_type find_last_not_of(etl::basic_string_view< T, TTraits > view, size_type position=npos) const ETL_NOEXCEPT
Find last absence of characters.
Definition string_view.h:736
ETL_CONSTEXPR14 bool starts_with(etl::basic_string_view< T, TTraits > view) const ETL_NOEXCEPT
Checks if the string view starts with the given prefix.
Definition string_view.h:474
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 void remove_prefix(size_type n) ETL_NOEXCEPT
Shrinks the view by moving its start forward.
Definition string_view.h:395
ETL_CONSTEXPR14 size_type find_first_of(etl::basic_string_view< T, TTraits > view, size_type position=0) const ETL_NOEXCEPT
Find first occurrence of characters.
Definition string_view.h:598
ETL_CONSTEXPR const_pointer data() const ETL_NOEXCEPT
Returns a const pointer to the first element of the internal storage.
Definition string_view.h:196
bool contains(const etl::basic_string_view< T, TTraits > &view) const ETL_NOEXCEPT
Checks that the view is within this string.
Definition string_view.h:792
ETL_CONSTEXPR size_t max_size() const ETL_NOEXCEPT
Returns the maximum possible size of the array.
Definition string_view.h:296
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_CONSTEXPR size_t length() const ETL_NOEXCEPT
Returns the size of the array.
Definition string_view.h:288
ETL_CONSTEXPR14 bool ends_with(etl::basic_string_view< T, TTraits > view) const ETL_NOEXCEPT
Checks if the string view ends with the given suffix.
Definition string_view.h:496
ETL_CONSTEXPR14 size_type find_last_of(etl::basic_string_view< T, TTraits > view, size_type position=npos) const ETL_NOEXCEPT
Find last occurrence of characters.
Definition string_view.h:639
ETL_CONSTEXPR14 void swap(basic_string_view &other) ETL_NOEXCEPT
Swaps with another basic_string_view.
Definition string_view.h:350
bool contains(const_pointer s) const ETL_NOEXCEPT
Checks that text is within this string.
Definition string_view.h:800
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
ETL_CONSTEXPR const_reference back() const
Returns a const reference to the last element.
Definition string_view.h:188
ETL_CONSTEXPR14 int compare(basic_string_view< T, TTraits > view) const ETL_NOEXCEPT
Compares two views.
Definition string_view.h:411
ETL_CONSTEXPR const_iterator end() const ETL_NOEXCEPT
Returns a const iterator to the end of the array.
Definition string_view.h:220
const_reference at(size_t i) const
Returns a const reference to the indexed value.
Definition string_view.h:340
ETL_CONSTEXPR14 void remove_suffix(size_type n) ETL_NOEXCEPT
Shrinks the view by moving its end backward.
Definition string_view.h:403
ETL_CONSTEXPR14 void assign(const_pointer begin_, const_pointer end_) ETL_NOEXCEPT
Assign from iterators.
Definition string_view.h:314
bool contains(value_type c) const ETL_NOEXCEPT
Checks that character is within this string.
Definition string_view.h:808
ETL_CONSTEXPR basic_string_view(const T *begin_, size_t size_) ETL_NOEXCEPT
Construct from pointer/size.
Definition string_view.h:162
ETL_CONSTEXPR basic_string_view(const etl::ibasic_string< T > &str) ETL_NOEXCEPT
Construct from string.
Definition string_view.h:135
ETL_CONSTEXPR14 void assign(const_pointer begin_, size_t size_) ETL_NOEXCEPT
Assign from iterator and size.
Definition string_view.h:323
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 size_type find_first_not_of(etl::basic_string_view< T, TTraits > view, size_type position=0) const ETL_NOEXCEPT
Find first absence of characters.
Definition string_view.h:687
ETL_CONSTEXPR14 ETL_EXPLICIT_STRING_FROM_CHAR basic_string_view(const T *begin_) ETL_NOEXCEPT
Construct from T*.
Definition string_view.h:144
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_iterator begin() const ETL_NOEXCEPT
Returns a const iterator to the beginning of the array.
Definition string_view.h:204
ETL_CONSTEXPR const_reference front() const
Returns a const reference to the first element.
Definition string_view.h:180
ETL_CONSTEXPR const_reverse_iterator crbegin() const ETL_NOEXCEPT
Returns a const reverse iterator to the reverse beginning of the array.
Definition string_view.h:244
ETL_CONSTEXPR14 basic_string_view substr(size_type position=0, size_type count=npos) const ETL_NOEXCEPT
Returns a substring.
Definition string_view.h:378
ETL_CONSTEXPR14 size_type rfind(etl::basic_string_view< T, TTraits > view, size_type position=npos) const ETL_NOEXCEPT
Find the last occurrence of a substring.
Definition string_view.h:556
ETL_CONSTEXPR14 size_type copy(T *destination, size_type count, size_type position=0) const ETL_NOEXCEPT
Copies characters.
Definition string_view.h:361
ETL_CONSTEXPR const_reverse_iterator rbegin() const ETL_NOEXCEPT
Returns a const reverse iterator to the reverse beginning of the array.
Definition string_view.h:236
ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT
Returns true if the array size is zero.
Definition string_view.h:272
ETL_CONSTEXPR basic_string_view(const T *begin_, const T *end_) ETL_NOEXCEPT
Construct from pointer range.
Definition string_view.h:153
Definition basic_string.h:351
Definition string_view.h:76
Definition string_view.h:90
#define ETL_ASSERT(b, e)
Definition error_handler.h:356
ETL_CONSTEXPR exception(string_type reason_, string_type, numeric_type line_)
Constructor.
Definition exception.h:69
Definition integral_limits.h:516
bitset_ext
Definition absolute.h:39
ETL_CONSTEXPR14 string_view make_string_view(const char(&text)[Array_Size]) ETL_NOEXCEPT
make_string_view.
Definition string_view.h:878
T * mem_copy(const T *sb, const T *se, T *db) ETL_NOEXCEPT
Definition memory.h:2289
ETL_CONSTEXPR TContainer::const_iterator cend(const TContainer &container)
Definition iterator.h:1012
void swap(etl::basic_string_view< T, TTraits > &lhs, etl::basic_string_view< T, TTraits > &rhs) ETL_NOEXCEPT
Swaps the values.
Definition string_view.h:971
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