Improve efficiency of etl::string_view::compare with text pointer

This commit is contained in:
John Wellbelove 2025-02-17 19:11:01 +00:00
parent d1e5112e2a
commit 52c6bca34d

View File

@ -412,20 +412,48 @@ namespace etl
}
ETL_CONSTEXPR14 int compare(size_type position1, size_type count1,
basic_string_view view,
size_type position2, size_type count2) const
basic_string_view view,
size_type position2, size_type count2) const
{
return substr(position1, count1).compare(view.substr(position2, count2));
}
ETL_CONSTEXPR14 int compare(const T* text) const
{
return compare(etl::basic_string_view<T, TTraits>(text));
const T* view_itr = mbegin;
const T* text_itr = text;
while (view_itr != mend && *text_itr != T(0))
{
if (*view_itr < *text_itr)
{
return -1;
}
else if (*view_itr > *text_itr)
{
return 1;
}
++view_itr;
++text_itr;
}
if ((view_itr == mend) && (*text_itr == T(0)))
{
return 0;
}
else if (view_itr == mend)
{
return -1;
}
else
{
return 1;
}
}
ETL_CONSTEXPR14 int compare(size_type position, size_type count, const T* text) const
{
return substr(position, count).compare(etl::basic_string_view<T, TTraits>(text));
return substr(position, count).compare(text);
}
ETL_CONSTEXPR14 int compare(size_type position, size_type count1, const T* text, size_type count2) const
@ -439,7 +467,7 @@ namespace etl
ETL_CONSTEXPR14 bool starts_with(etl::basic_string_view<T, TTraits> view) const
{
return (size() >= view.size()) &&
(compare(0, view.size(), view) == 0);
(compare(0, view.size(), view) == 0);
}
ETL_CONSTEXPR14 bool starts_with(T c) const
@ -452,7 +480,7 @@ namespace etl
size_t lengthtext = TTraits::length(text);
return (size() >= lengthtext) &&
(compare(0, lengthtext, text) == 0);
(compare(0, lengthtext, text) == 0);
}
//*************************************************************************
@ -461,7 +489,7 @@ namespace etl
ETL_CONSTEXPR14 bool ends_with(etl::basic_string_view<T, TTraits> view) const
{
return (size() >= view.size()) &&
(compare(size() - view.size(), npos, view) == 0);
(compare(size() - view.size(), npos, view) == 0);
}
ETL_CONSTEXPR14 bool ends_with(T c) const
@ -475,7 +503,7 @@ namespace etl
size_t lengthview = size();
return (lengthview >= lengthtext) &&
(compare(lengthview - lengthtext, lengthtext, text) == 0);
(compare(lengthview - lengthtext, lengthtext, text) == 0);
}
//*************************************************************************