mirror of
https://github.com/ETLCPP/etl.git
synced 2026-10-28 18:07:10 +08:00
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:
parent
22fc884d45
commit
1a72c88d47
@ -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;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
|
||||
@ -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)
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user