From 3a1efd2078337234c2ca443ca4deec029a97a633 Mon Sep 17 00:00:00 2001 From: m-peko Date: Wed, 25 Nov 2020 12:00:31 +0100 Subject: [PATCH] Implement erase_if function and related tests --- doc/qbk/main.qbk | 1 + include/boost/static_string/static_string.hpp | 25 ++++++++++ test/static_string.cpp | 47 +++++++++++++++++++ 3 files changed, 73 insertions(+) diff --git a/doc/qbk/main.qbk b/doc/qbk/main.qbk index 35a10ae..6c2da91 100644 --- a/doc/qbk/main.qbk +++ b/doc/qbk/main.qbk @@ -32,6 +32,7 @@ [template include_file[path][^<''''''[path]''''''>]] [def __InputIterator__ [@https://en.cppreference.com/w/cpp/named_req/InputIterator ['InputIterator]]] +[def __UnaryPredicate__ [@https://en.cppreference.com/w/cpp/named_req/Predicate ['Predicate]]] [/-----------------------------------------------------------------------------] diff --git a/include/boost/static_string/static_string.hpp b/include/boost/static_string/static_string.hpp index d36cb34..e3328f4 100644 --- a/include/boost/static_string/static_string.hpp +++ b/include/boost/static_string/static_string.hpp @@ -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::size_type +erase_if( + basic_static_string& 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 diff --git a/test/static_string.cpp b/test/static_string.cpp index 98506ba..695bf39 100644 --- a/test/static_string.cpp +++ b/test/static_string.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -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();