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 class string_view_empty :
public string_view_exception
106 string_view_empty(string_type file_name_, numeric_type line_number_)
107 : string_view_exception(ETL_ERROR_TEXT(
"basic_string_view:empty", ETL_STRING_VIEW_FILE_ID
"C"), file_name_, line_number_)
115 template <
typename T,
typename TTraits = etl::
char_traits<T> >
120 typedef T value_type;
121 typedef TTraits traits_type;
122 typedef size_t size_type;
123 typedef T& reference;
124 typedef const T& const_reference;
126 typedef const T* const_pointer;
127 typedef const T* iterator;
128 typedef const T* const_iterator;
129 typedef ETL_OR_STD::reverse_iterator<const_iterator> const_reverse_iterator;
140 : mbegin(ETL_NULLPTR)
149 : mbegin(str.begin())
159 , mend(begin_ + TTraits::length(begin_))
177 , mend(begin_ + size_)
185 : mbegin(other.mbegin)
195 ETL_CONSTEXPR const_reference
front()
const
197#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_EXTRA
210 ETL_CONSTEXPR const_reference
back()
const
212#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_EXTRA
223 ETL_CONSTEXPR const_pointer
data() const ETL_NOEXCEPT
231 ETL_CONSTEXPR const_iterator
begin() const ETL_NOEXCEPT
239 ETL_CONSTEXPR const_iterator
cbegin() const ETL_NOEXCEPT
247 ETL_CONSTEXPR const_iterator
end() const ETL_NOEXCEPT
263 ETL_CONSTEXPR const_reverse_iterator
rbegin() const ETL_NOEXCEPT
265 return const_reverse_iterator(mend);
271 ETL_CONSTEXPR const_reverse_iterator
crbegin() const ETL_NOEXCEPT
273 return const_reverse_iterator(mend);
279 ETL_CONSTEXPR const_reverse_iterator
rend() const ETL_NOEXCEPT
281 return const_reverse_iterator(mbegin);
287 ETL_CONSTEXPR const_reverse_iterator
crend() const ETL_NOEXCEPT
289 return const_reverse_iterator(mbegin);
299 ETL_CONSTEXPR
bool empty() const ETL_NOEXCEPT
301 return (mbegin == mend);
307 ETL_CONSTEXPR
size_t size() const ETL_NOEXCEPT
309 return static_cast<size_t>(mend - mbegin);
315 ETL_CONSTEXPR
size_t length() const ETL_NOEXCEPT
333 mbegin = other.mbegin;
341 ETL_CONSTEXPR14
void assign(const_pointer begin_, const_pointer end_) ETL_NOEXCEPT
350 ETL_CONSTEXPR14
void assign(const_pointer begin_,
size_t size_) ETL_NOEXCEPT
353 mend = begin_ + size_;
361 ETL_CONSTEXPR const_reference
operator[](
size_t i)
const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_INDEX_OPERATOR)
363#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_INDEX_OPERATOR
374 const_reference
at(
size_t i)
const
386 using ETL_OR_STD::swap;
388 swap(mbegin, other.mbegin);
389 swap(mend, other.mend);
395 ETL_CONSTEXPR14 size_type
copy(T* destination, size_type count, size_type position = 0) const ETL_NOEXCEPT
399 if (position <
size())
401 n = etl::min(count,
size() - position);
416 if (position <
size())
418 size_t n = etl::min(count,
size() - position);
447 return (*
this == view) ? 0 : ((*
this > view) ? 1 : -1);
455 ETL_CONSTEXPR14
int compare(size_type position1, size_type count1,
basic_string_view view, size_type position2, size_type count2)
const
458 return substr(position1, count1).
compare(view.substr(position2, count2));
461 ETL_CONSTEXPR14
int compare(
const T* text)
const ETL_NOEXCEPT
463 const T* view_itr = mbegin;
464 const T* text_itr = text;
466 while (view_itr != mend && *text_itr != T(0))
468 if (*view_itr < *text_itr)
472 else if (*view_itr > *text_itr)
480 if ((view_itr == mend) && (*text_itr == T(0)))
484 else if (view_itr == mend)
494 ETL_CONSTEXPR14
int compare(size_type position, size_type count,
const T* text)
const ETL_NOEXCEPT
499 ETL_CONSTEXPR14
int compare(size_type position, size_type count1,
const T* text, size_type count2)
const ETL_NOEXCEPT
501 return substr(position, count1).
compare(etl::basic_string_view<T, TTraits>(text, count2));
509 return (
size() >= view.size()) && (
compare(0, view.size(), view) == 0);
512 ETL_CONSTEXPR14
bool starts_with(T c)
const ETL_NOEXCEPT
517 ETL_CONSTEXPR14
bool starts_with(
const T* text)
const ETL_NOEXCEPT
519 size_t lengthtext = TTraits::length(text);
521 return (
size() >= lengthtext) && (
compare(0, lengthtext, text) == 0);
529 return (
size() >= view.size()) && (
compare(
size() - view.size(), npos, view) == 0);
532 ETL_CONSTEXPR14
bool ends_with(T c)
const
537 ETL_CONSTEXPR14
bool ends_with(
const T* text)
const
539 size_t lengthtext = TTraits::length(text);
540 size_t lengthview =
size();
542 return (lengthview >= lengthtext) && (
compare(lengthview - lengthtext, lengthtext, text) == 0);
555 const_iterator iposition = etl::search(
begin() + position,
end(), view.
begin(), view.
end());
557 if (iposition ==
end())
563 return static_cast<size_type
>(etl::distance(
begin(), iposition));
572 ETL_CONSTEXPR14 size_type
find(
const T* text, size_type position, size_type count)
const ETL_NOEXCEPT
577 ETL_CONSTEXPR14 size_type
find(
const T* text, size_type position = 0) const ETL_NOEXCEPT
579 return find(etl::basic_string_view<T, TTraits>(text), position);
587 if ((
size() < view.size()))
592 position = etl::min(position,
size());
594 const_iterator iposition = etl::find_end(
begin(),
begin() + position, view.begin(), view.end());
596 if (iposition ==
end())
602 return static_cast<size_type
>(etl::distance(
begin(), iposition));
611 ETL_CONSTEXPR14 size_type
rfind(
const T* text, size_type position, size_type count)
const ETL_NOEXCEPT
616 ETL_CONSTEXPR14 size_type
rfind(
const T* text, size_type position = npos)
const ETL_NOEXCEPT
618 return rfind(etl::basic_string_view<T, TTraits>(text), position);
626 const size_t lengthtext =
size();
628 if (position < lengthtext)
630 for (
size_t i = position; i < lengthtext; ++i)
632 const size_t lengthview = view.
size();
634 for (
size_t j = 0UL; j < lengthview; ++j)
636 if (mbegin[i] == view[j])
652 ETL_CONSTEXPR14 size_type
find_first_of(
const T* text, size_type position, size_type count)
const ETL_NOEXCEPT
657 ETL_CONSTEXPR14 size_type
find_first_of(
const T* text, size_type position = 0) const ETL_NOEXCEPT
659 return find_first_of(etl::basic_string_view<T, TTraits>(text), position);
672 position = etl::min(position,
size() - 1);
674 const_reverse_iterator it =
rbegin() +
static_cast<ptrdiff_t
>(
size() - position - 1);
678 const size_t viewlength = view.size();
680 for (
size_t j = 0UL; j < viewlength; ++j)
682 if (mbegin[position] == view[j])
700 ETL_CONSTEXPR14 size_type
find_last_of(
const T* text, size_type position, size_type count)
const ETL_NOEXCEPT
705 ETL_CONSTEXPR14 size_type
find_last_of(
const T* text, size_type position = npos)
const ETL_NOEXCEPT
707 return find_last_of(etl::basic_string_view<T, TTraits>(text), position);
715 const size_t lengthtext =
size();
717 if (position < lengthtext)
719 for (
size_t i = position; i < lengthtext; ++i)
723 const size_t viewlength = view.
size();
725 for (
size_t j = 0UL; j < viewlength; ++j)
727 if (mbegin[i] == view[j])
749 ETL_CONSTEXPR14 size_type
find_first_not_of(
const T* text, size_type position, size_type count)
const ETL_NOEXCEPT
754 ETL_CONSTEXPR14 size_type
find_first_not_of(
const T* text, size_type position = 0) const ETL_NOEXCEPT
769 position = etl::min(position,
size() - 1);
771 const_reverse_iterator it =
rbegin() +
static_cast<ptrdiff_t
>(
size() - position - 1);
777 const size_t viewlength = view.size();
779 for (
size_t j = 0UL; j < viewlength; ++j)
781 if (mbegin[position] == view[j])
805 ETL_CONSTEXPR14 size_type
find_last_not_of(
const T* text, size_type position, size_type count)
const ETL_NOEXCEPT
810 ETL_CONSTEXPR14 size_type
find_last_not_of(
const T* text, size_type position = npos)
const ETL_NOEXCEPT
812 return find_last_not_of(etl::basic_string_view<T, TTraits>(text), position);
820 return find(view) != npos;
828 return find(s) != npos;
836 return find(c) != npos;
852 return !(lhs == rhs);
860 return etl::lexicographical_compare(lhs.
begin(), lhs.
end(), rhs.
begin(), rhs.
end());
889 const_pointer mbegin;
902 template <
size_t Array_Size>
905 size_t length = etl::char_traits<char>::length(text, Array_Size - 1U);
907 return string_view(text, length);
911 template <
size_t Array_Size>
912 ETL_CONSTEXPR14 wstring_view
make_string_view(
const wchar_t (&text)[Array_Size]) ETL_NOEXCEPT
914 size_t length = etl::char_traits<wchar_t>::length(text, Array_Size - 1U);
916 return wstring_view(text, length);
920 template <
size_t Array_Size>
921 ETL_CONSTEXPR14 u8string_view
make_string_view(
const char8_t (&text)[Array_Size]) ETL_NOEXCEPT
923 size_t length = etl::char_traits<char8_t>::length(text, Array_Size - 1U);
925 return u8string_view(text, length);
929 template <
size_t Array_Size>
930 ETL_CONSTEXPR14 u16string_view
make_string_view(
const char16_t (&text)[Array_Size]) ETL_NOEXCEPT
932 size_t length = etl::char_traits<char16_t>::length(text, Array_Size - 1U);
934 return u16string_view(text, length);
938 template <
size_t Array_Size>
939 ETL_CONSTEXPR14 u32string_view
make_string_view(
const char32_t (&text)[Array_Size]) ETL_NOEXCEPT
941 size_t length = etl::char_traits<char32_t>::length(text, Array_Size - 1U);
943 return u32string_view(text, length);
949#if ETL_USING_8BIT_TYPES
951 struct hash<etl::string_view>
953 size_t operator()(
const etl::string_view& text)
const
955 return etl::private_hash::generic_hash<size_t>(
reinterpret_cast<const uint8_t*
>(text.
data()),
956 reinterpret_cast<const uint8_t*
>(text.
data() + text.
size()));
961 struct hash<etl::wstring_view>
963 size_t operator()(
const etl::wstring_view& text)
const
965 return etl::private_hash::generic_hash<size_t>(
reinterpret_cast<const uint8_t*
>(text.
data()),
966 reinterpret_cast<const uint8_t*
>(text.
data() + text.
size()));
971 struct hash<etl::u16string_view>
973 size_t operator()(
const etl::u16string_view& text)
const
975 return etl::private_hash::generic_hash<size_t>(
reinterpret_cast<const uint8_t*
>(text.
data()),
976 reinterpret_cast<const uint8_t*
>(text.
data() + text.
size()));
981 struct hash<etl::u32string_view>
983 size_t operator()(
const etl::u32string_view& text)
const
985 return etl::private_hash::generic_hash<size_t>(
reinterpret_cast<const uint8_t*
>(text.
data()),
986 reinterpret_cast<const uint8_t*
>(text.
data() + text.
size()));
995template <
typename T,
typename TTraits >
1001template <
typename T>
1010#if ETL_USING_STD_OSTREAM
1011template <
typename T>
1012std::basic_ostream<T, std::char_traits<T> >&
operator<<(std::basic_ostream<T, std::char_traits<T> >& os,
1015 os.write(text.data(),
static_cast<std::streamsize
>(text.size()));
String view.
Definition string_view.h:117
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:548
ETL_CONSTEXPR basic_string_view() ETL_NOEXCEPT
Default constructor.
Definition string_view.h:139
ETL_CONSTEXPR const_iterator cbegin() const ETL_NOEXCEPT
Returns a const iterator to the beginning of the array.
Definition string_view.h:239
ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT
Returns the size of the array.
Definition string_view.h:307
ETL_CONSTEXPR const_reverse_iterator rend() const ETL_NOEXCEPT
Returns a const reverse iterator to the end of the array.
Definition string_view.h:279
ETL_CONSTEXPR const_reverse_iterator crend() const ETL_NOEXCEPT
Returns a const reverse iterator to the end of the array.
Definition string_view.h:287
ETL_CONSTEXPR basic_string_view(const basic_string_view &other) ETL_NOEXCEPT
Copy constructor.
Definition string_view.h:184
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:874
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:331
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:762
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:507
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:882
ETL_CONSTEXPR14 void remove_prefix(size_type n) ETL_NOEXCEPT
Shrinks the view by moving its start forward.
Definition string_view.h:429
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:624
ETL_CONSTEXPR const_pointer data() const ETL_NOEXCEPT
Returns a const pointer to the first element of the internal storage.
Definition string_view.h:223
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:818
ETL_CONSTEXPR size_t max_size() const ETL_NOEXCEPT
Returns the maximum possible size of the array.
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)
Inequality for string_view.
Definition string_view.h:850
ETL_CONSTEXPR size_t length() const ETL_NOEXCEPT
Returns the size of the array.
Definition string_view.h:315
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:527
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:665
ETL_CONSTEXPR14 void swap(basic_string_view &other) ETL_NOEXCEPT
Swaps with another basic_string_view.
Definition string_view.h:384
bool contains(const_pointer s) const ETL_NOEXCEPT
Checks that text is within this string.
Definition string_view.h:826
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:858
ETL_CONSTEXPR const_reference back() const
Definition string_view.h:210
ETL_CONSTEXPR14 int compare(basic_string_view< T, TTraits > view) const ETL_NOEXCEPT
Compares two views.
Definition string_view.h:445
ETL_CONSTEXPR const_iterator end() const ETL_NOEXCEPT
Returns a const iterator to the end of the array.
Definition string_view.h:247
const_reference at(size_t i) const
Returns a const reference to the indexed value.
Definition string_view.h:374
ETL_CONSTEXPR14 void remove_suffix(size_type n) ETL_NOEXCEPT
Shrinks the view by moving its end backward.
Definition string_view.h:437
ETL_CONSTEXPR14 void assign(const_pointer begin_, const_pointer end_) ETL_NOEXCEPT
Assign from iterators.
Definition string_view.h:341
bool contains(value_type c) const ETL_NOEXCEPT
Checks that character is within this string.
Definition string_view.h:834
ETL_CONSTEXPR basic_string_view(const T *begin_, size_t size_) ETL_NOEXCEPT
Construct from pointer/size.
Definition string_view.h:175
ETL_CONSTEXPR basic_string_view(const etl::ibasic_string< T > &str) ETL_NOEXCEPT
Construct from string.
Definition string_view.h:148
ETL_CONSTEXPR14 void assign(const_pointer begin_, size_t size_) ETL_NOEXCEPT
Assign from iterator and size.
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)
Greater-than for string_view.
Definition string_view.h:866
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:713
ETL_CONSTEXPR14 ETL_EXPLICIT_STRING_FROM_CHAR basic_string_view(const T *begin_) ETL_NOEXCEPT
Construct from T*.
Definition string_view.h:157
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:842
ETL_CONSTEXPR const_iterator begin() const ETL_NOEXCEPT
Returns a const iterator to the beginning of the array.
Definition string_view.h:231
ETL_CONSTEXPR const_reference front() const
Definition string_view.h:195
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:271
ETL_CONSTEXPR14 basic_string_view substr(size_type position=0, size_type count=npos) const ETL_NOEXCEPT
Returns a substring.
Definition string_view.h:412
ETL_CONSTEXPR const_reference operator[](size_t i) const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS||ETL_NOT_CHECKING_INDEX_OPERATOR)
Definition string_view.h:361
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:585
ETL_CONSTEXPR14 size_type copy(T *destination, size_type count, size_type position=0) const ETL_NOEXCEPT
Copies characters.
Definition string_view.h:395
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:263
ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT
Returns true if the array size is zero.
Definition string_view.h:299
ETL_CONSTEXPR basic_string_view(const T *begin_, const T *end_) ETL_NOEXCEPT
Construct from pointer range.
Definition string_view.h:166
Definition basic_string.h:350
Definition string_view.h:76
The exception thrown when the view is empty.
Definition string_view.h:103
Definition string_view.h:90
#define ETL_ASSERT(b, e)
Definition error_handler.h:511
ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type, numeric_type)
Constructor.
Definition exception.h:81
Definition integral_limits.h:518
bitset_ext
Definition absolute.h:40
ETL_CONSTEXPR14 string_view make_string_view(const char(&text)[Array_Size]) ETL_NOEXCEPT
make_string_view.
Definition string_view.h:903
T * mem_copy(const T *sb, const T *se, T *db) ETL_NOEXCEPT
Definition memory.h:2835
ETL_CONSTEXPR TContainer::const_iterator cend(const TContainer &container)
Definition iterator.h:1017
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:996
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:1012
Character traits for any character type.
Definition char_traits.h:128