string_view: clamp n in remove_prefix and remove_suffix (#1513)

* remove_prefix, premove_suffix, clamp n to size()

* Add function documentation

* Add ETL_ASSERT

* Remove ETL_NOEXCEPT_IF

---------

Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
This commit is contained in:
Mike Bloom 2026-08-22 15:09:07 -04:00 committed by GitHub
parent 22fc884d45
commit 1a72c88d47
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 58 additions and 4 deletions

View File

@ -425,18 +425,28 @@ namespace etl
//*************************************************************************
/// Shrinks the view by moving its start forward.
/// When n >= size(), the view is set to empty.
//*************************************************************************
ETL_CONSTEXPR14 void remove_prefix(size_type n) ETL_NOEXCEPT
ETL_CONSTEXPR14 void remove_prefix(size_type n)
{
mbegin += n;
ETL_ASSERT(n <= size(), ETL_ERROR(string_view_bounds));
if (n < size())
mbegin += n;
else
mbegin = mend;
}
//*************************************************************************
/// Shrinks the view by moving its end backward.
/// When n >= size(), the view is set to empty.
//*************************************************************************
ETL_CONSTEXPR14 void remove_suffix(size_type n) ETL_NOEXCEPT
ETL_CONSTEXPR14 void remove_suffix(size_type n)
{
mend -= n;
ETL_ASSERT(n <= size(), ETL_ERROR(string_view_bounds));
if (n < size())
mend -= n;
else
mend = mbegin;
}
//*************************************************************************

View File

@ -542,6 +542,50 @@ namespace
CHECK(isEqual);
}
//*************************************************************************
TEST(test_remove_prefix_empty)
{
std::string original = "Hello World";
View view(original.c_str());
// remove n == size() should result in an empty view
view.remove_prefix(original.size());
CHECK_EQUAL(view.begin(), view.end());
CHECK(view.empty());
}
//*************************************************************************
TEST(test_remove_prefix_assert)
{
std::string original = "Hello World";
View view(original.c_str());
// remove n > size() should trigger ETL_ASSERT
CHECK_THROW(view.remove_prefix(original.size() + 1), etl::string_view_bounds);
}
//*************************************************************************
TEST(test_remove_suffix_empty)
{
std::string original = "Hello World";
View view(original.c_str());
// remove n == size() should result in an empty view
view.remove_suffix(original.size());
CHECK_EQUAL(view.begin(), view.end());
CHECK(view.empty());
}
//*************************************************************************
TEST(test_remove_suffix_assert)
{
std::string original = "Hello World";
View view(original.c_str());
// remove n > size() should trigger ETL_ASSERT
CHECK_THROW(view.remove_suffix(original.size() + 1), etl::string_view_bounds);
}
//*************************************************************************
TEST(test_copy)
{