forked from boostorg/static_string
Implement erase_if function and related tests
This commit is contained in:
committed by
Krystian Stasiowski
parent
59c4c556bb
commit
3a1efd2078
@ -32,6 +32,7 @@
|
|||||||
[template include_file[path][^<'''<ulink url="../../../../'''[path]'''">'''[path]'''</ulink>'''>]]
|
[template include_file[path][^<'''<ulink url="../../../../'''[path]'''">'''[path]'''</ulink>'''>]]
|
||||||
|
|
||||||
[def __InputIterator__ [@https://en.cppreference.com/w/cpp/named_req/InputIterator ['InputIterator]]]
|
[def __InputIterator__ [@https://en.cppreference.com/w/cpp/named_req/InputIterator ['InputIterator]]]
|
||||||
|
[def __UnaryPredicate__ [@https://en.cppreference.com/w/cpp/named_req/Predicate ['Predicate]]]
|
||||||
|
|
||||||
[/-----------------------------------------------------------------------------]
|
[/-----------------------------------------------------------------------------]
|
||||||
|
|
||||||
|
@ -5614,6 +5614,31 @@ operator+(
|
|||||||
std::size_t(0), +lhs);
|
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
|
// swap
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
#include <boost/core/lightweight_test.hpp>
|
#include <boost/core/lightweight_test.hpp>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cwchar>
|
#include <cwchar>
|
||||||
|
#include <cctype>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@ -1848,6 +1849,51 @@ testErase()
|
|||||||
BOOST_TEST(testE(S("abcdefghijklmnopqrst"), 21, 0, S("can't happen")));
|
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
|
// done
|
||||||
static
|
static
|
||||||
void
|
void
|
||||||
@ -7171,6 +7217,7 @@ runTests()
|
|||||||
testClear();
|
testClear();
|
||||||
testInsert();
|
testInsert();
|
||||||
testErase();
|
testErase();
|
||||||
|
testEraseIf();
|
||||||
testPushBack();
|
testPushBack();
|
||||||
testPopBack();
|
testPopBack();
|
||||||
testAppend();
|
testAppend();
|
||||||
|
Reference in New Issue
Block a user