From 1ca7253c14c9da2b13edab366c6dc4fce7c9e8b9 Mon Sep 17 00:00:00 2001 From: Krystian Stasiowski Date: Sat, 15 Feb 2020 17:44:52 -0500 Subject: [PATCH] Make erase follow preconditions --- include/boost/static_string/static_string.hpp | 36 +++++++------------ test/constexpr_tests.hpp | 6 ++-- test/static_string.cpp | 5 --- 3 files changed, 16 insertions(+), 31 deletions(-) diff --git a/include/boost/static_string/static_string.hpp b/include/boost/static_string/static_string.hpp index bfd24e4..a8e616d 100644 --- a/include/boost/static_string/static_string.hpp +++ b/include/boost/static_string/static_string.hpp @@ -1863,8 +1863,11 @@ public: */ BOOST_STATIC_STRING_CPP14_CONSTEXPR iterator - erase( - const_iterator pos); + erase(const_iterator pos) + { + BOOST_STATIC_STRING_ASSERT(!empty()); + return erase(pos, pos + 1); + } /** Removes the characters in the range `(first, last)` @@ -1892,7 +1895,7 @@ public: void pop_back() noexcept { - BOOST_STATIC_STRING_ASSERT(size() > 0); + BOOST_STATIC_STRING_ASSERT(!empty()); this->set_size(size() - 1); term(); } @@ -5043,27 +5046,12 @@ erase( size_type count) -> basic_static_string& { - const auto curr_size = size(); - const auto curr_data = data(); BOOST_STATIC_STRING_THROW_IF( - index > curr_size, std::out_of_range{"index > size()"}); - count = (std::min)(count, curr_size - index); - traits_type::move(&curr_data[index], &curr_data[index + count], curr_size - (index + count) + 1); - this->set_size(curr_size - count); + index > size(), std::out_of_range{"index > size()"}); + erase(data() + index, data() + index + (std::min)(count, size() - index)); return *this; } -template -BOOST_STATIC_STRING_CPP14_CONSTEXPR -auto -basic_static_string:: -erase(const_iterator pos) -> - iterator -{ - erase(pos - begin(), 1); - return begin() + (pos - begin()); -} - template BOOST_STATIC_STRING_CPP14_CONSTEXPR auto @@ -5073,9 +5061,11 @@ erase( const_iterator last) -> iterator { - erase(first - begin(), - detail::distance(first, last)); - return begin() + (first - begin()); + const auto curr_data = data(); + const std::size_t index = first - curr_data; + traits_type::move(&curr_data[index], last, (end() - last) + 1); + this->set_size(size() - std::size_t(last - first)); + return curr_data + index; } template diff --git a/test/constexpr_tests.hpp b/test/constexpr_tests.hpp index cf0d88d..539ebaa 100644 --- a/test/constexpr_tests.hpp +++ b/test/constexpr_tests.hpp @@ -134,7 +134,7 @@ testConstantEvaluation() a.insert(a.begin(), {'a'}); // erase - a.erase(); + a.erase(0, 1); a.erase(a.begin()); a.erase(a.begin(), a.end()); @@ -324,7 +324,7 @@ testConstantEvaluation() a.insert(a.begin(), {'a'}); // erase - a.erase(); + a.erase(0, 1); a.erase(a.begin()); a.erase(a.begin(), a.end()); @@ -500,7 +500,7 @@ testConstantEvaluation() a.insert(a.begin(), {'a'}); // erase - a.erase(); + a.erase(0, 1); a.erase(a.begin()); a.erase(a.begin(), a.end()); diff --git a/test/static_string.cpp b/test/static_string.cpp index 58d4fa1..4defb1d 100644 --- a/test/static_string.cpp +++ b/test/static_string.cpp @@ -1608,11 +1608,6 @@ testErase() BOOST_TEST(s.erase(s.begin() + 1) == s.begin() + 1); BOOST_TEST(s == "ac"); } - { - static_string<3> s{"abc"}; - BOOST_TEST(s.erase(s.begin() + 3) == s.end()); - BOOST_TEST(s == "abc"); - } // erase(const_iterator first, const_iterator last) {