Implement erase_if function and related tests

This commit is contained in:
m-peko
2020-11-25 12:00:31 +01:00
committed by Krystian Stasiowski
parent 59c4c556bb
commit 3a1efd2078
3 changed files with 73 additions and 0 deletions

View File

@ -32,6 +32,7 @@
[template include_file[path][^<'''<ulink url="../../../../'''[path]'''">'''[path]'''</ulink>'''>]]
[def __InputIterator__ [@https://en.cppreference.com/w/cpp/named_req/InputIterator ['InputIterator]]]
[def __UnaryPredicate__ [@https://en.cppreference.com/w/cpp/named_req/Predicate ['Predicate]]]
[/-----------------------------------------------------------------------------]

View File

@ -5614,6 +5614,31 @@ operator+(
std::size_t(0), +lhs);
}
//------------------------------------------------------------------------------
//
// erase_if
//
//------------------------------------------------------------------------------
template<
std::size_t N, typename CharT,
typename Traits, typename UnaryPredicate>
BOOST_STATIC_STRING_CPP14_CONSTEXPR
typename
basic_static_string<N, CharT, Traits>::size_type
erase_if(
basic_static_string<N, CharT, Traits>& str,
UnaryPredicate pred)
{
auto first = str.begin();
for (auto it = first; it != str.end(); ++it)
if (!pred(*it))
*first++ = std::move(*it);
const auto count = str.end() - first;
str.erase(first, str.end());
return count;
}
//------------------------------------------------------------------------------
//
// swap

View File

@ -16,6 +16,7 @@
#include <boost/core/lightweight_test.hpp>
#include <cstdlib>
#include <cwchar>
#include <cctype>
#include <sstream>
#include <string>
@ -1848,6 +1849,51 @@ testErase()
BOOST_TEST(testE(S("abcdefghijklmnopqrst"), 21, 0, S("can't happen")));
}
// done
static
void
testEraseIf()
{
// erase_if(static_string& str, UnaryPredicate pred)
{
static_string<3> s{""};
BOOST_TEST(erase_if(s, [](char c) { return c == 'a'; }) == 0);
BOOST_TEST(s == "");
BOOST_TEST(*s.end() == 0);
}
{
static_string<3> s{"aaa"};
BOOST_TEST(erase_if(s, [](char c) { return c == 'a'; }) == 3);
BOOST_TEST(s == "");
BOOST_TEST(*s.end() == 0);
}
{
static_string<3> s{"abc"};
BOOST_TEST(erase_if(s, [](char c) { return c == 'a'; }) == 1);
BOOST_TEST(s == "bc");
BOOST_TEST(*s.end() == 0);
}
{
static_string<3> s{"abc"};
BOOST_TEST(erase_if(s, [](char c) { return c == 'b'; }) == 1);
BOOST_TEST(s == "ac");
BOOST_TEST(*s.end() == 0);
}
{
static_string<3> s{"abc"};
BOOST_TEST(erase_if(s, [](char c) { return c == 'c'; }) == 1);
BOOST_TEST(s == "ab");
BOOST_TEST(*s.end() == 0);
}
{
static_string<3> s{"abc"};
BOOST_TEST(erase_if(s, [](char c) { return c == 'd'; }) == 0);
BOOST_TEST(s == "abc");
BOOST_TEST(*s.end() == 0);
}
}
// done
static
void
@ -7171,6 +7217,7 @@ runTests()
testClear();
testInsert();
testErase();
testEraseIf();
testPushBack();
testPopBack();
testAppend();