From 9c961f057799f5f85ce5df85965df66212fb787a Mon Sep 17 00:00:00 2001 From: Beman Date: Mon, 13 Jul 2015 17:23:23 -0400 Subject: [PATCH] find_last_of, find_last_not_of, passing tests. --- include/boost/utility/string_view.hpp | 15 ++- test/string_view_test3.cpp | 142 +++++++++++++++++++++++++- 2 files changed, 150 insertions(+), 7 deletions(-) diff --git a/include/boost/utility/string_view.hpp b/include/boost/utility/string_view.hpp index f7d3312..1280ffb 100644 --- a/include/boost/utility/string_view.hpp +++ b/include/boost/utility/string_view.hpp @@ -265,8 +265,14 @@ namespace boost { // find_last_of size_type find_last_of(basic_string_view s, size_type pos = npos) const BOOST_NOEXCEPT { + if (s.len_ == 0u) + return npos; + if (pos >= len_) + pos = 0; + else + pos = len_ - (pos+1); const_reverse_iterator iter = std::find_first_of - ( this->crbegin (), this->crend (), s.cbegin (), s.cend (), traits::eq ); + ( this->crbegin () + pos, this->crend (), s.cbegin (), s.cend (), traits::eq ); return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter); } size_type find_last_of(charT c, size_type pos = npos) const BOOST_NOEXCEPT @@ -294,7 +300,12 @@ namespace boost { // find_last_not_of size_type find_last_not_of(basic_string_view s, size_type pos = npos) const BOOST_NOEXCEPT { - const_reverse_iterator iter = find_not_of ( this->crbegin (), this->crend (), s ); + if (pos >= len_) + pos = len_ - 1;; + if (s.len_ == 0u) + return pos; + pos = len_ - (pos+1); + const_reverse_iterator iter = find_not_of ( this->crbegin () + pos, this->crend (), s ); return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter ); } size_type find_last_not_of(charT c, size_type pos = npos) const BOOST_NOEXCEPT diff --git a/test/string_view_test3.cpp b/test/string_view_test3.cpp index 8ddcd61..c8cd519 100644 --- a/test/string_view_test3.cpp +++ b/test/string_view_test3.cpp @@ -192,6 +192,71 @@ namespace } } + void find_last_of_test() + { + std::cout << "find_last_of test..." << std::endl; + + // find_last_of - test two modified and two new signatures + std::string s1("Hello World!"); + boost::string_view sv1(s1); + std::string s2("o"); + boost::string_view sv2(s2); + const char* s2c = "Good Bye"; + boost::string_view sv2c(s2c); + std::string s3; + boost::string_view sv3(s3); + + // smoke test + BOOST_TEST_EQ(sv1.find_last_of(sv2), s1.find_last_of(s2)); + BOOST_TEST_EQ(sv1.find_last_of(sv2, 5), s1.find_last_of(s2, 5)); + BOOST_TEST_EQ(sv1.find_last_of(s2c), s1.find_last_of(s2c)); + BOOST_TEST_EQ(sv1.find_last_of(s2c, 0, 4), s1.find_last_of(s2c, 0, 4)); + + // first signature + BOOST_TEST_EQ(sv3.find_last_of(sv3), s3.find_last_of(s3)); // both strings empty + BOOST_TEST_EQ(sv1.find_last_of(sv3), s1.find_last_of(s3)); // search string empty + BOOST_TEST_EQ(sv3.find_last_of(sv2), s3.find_last_of(s2)); // searched string empty + BOOST_TEST_EQ(sv1.find_last_of(sv3, s1.size() + 2), s1.find_last_of(s3, s1.size() + 2)); + BOOST_TEST_EQ(sv1.find_last_of(sv3, s1.size() + 1), s1.find_last_of(s3, s1.size() + 1)); + BOOST_TEST_EQ(sv1.find_last_of(sv3, s1.size()), s1.find_last_of(s3, s1.size())); + BOOST_TEST_EQ(sv1.find_last_of(sv3, s1.size() - 1), s1.find_last_of(s3, s1.size() - 1)); + + BOOST_TEST_EQ(sv1.find_last_of(sv2), s1.find_last_of(s2)); + for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) + { + //std::cout << i << ": " << sv1.find_last_of(sv2, i) << " " << s1.find_last_of(s2, i) << std::endl; + BOOST_TEST_EQ(sv1.find_last_of(sv2, i), s1.find_last_of(s2, i)); + } + for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) + { + //std::cout << i << ": " << sv1.find_last_of(sv3, i) << " " << s1.find_last_of(s3, i) << std::endl; + BOOST_TEST_EQ(sv1.find_last_of(sv3, i), s1.find_last_of(s3, i)); + } + + // second signature + for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) + { + //std::cout << i << ": " << sv1.find_last_of('o', i) << " " << s1.find_last_of('o', i) << std::endl; + BOOST_TEST_EQ(sv1.find_last_of('o', i), s1.find_last_of('o', i)); + } + + // third signature + for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) + for (std::string::size_type j = 0; j <= std::strlen(s2c) + 1; ++j) + { + //std::cout << i << "," << j << ": " << sv1.find_last_of(s2c, i, j) << " " << s1.find_last_of(s2c, i, j) << std::endl; + BOOST_TEST_EQ(sv1.find_last_of(s2c, i, j), s1.find_last_of(s2c, i, j)); + } + + // fourth signature + BOOST_TEST_EQ(sv1.find_last_of(s2c), s1.find_last_of(s2c)); + for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) + { + //std::cout << i << ": " << sv1.find_last_of(s2c, i) << " " << s1.find_last_of(s2c, i) << std::endl; + BOOST_TEST_EQ(sv1.find_last_of(s2c, i), s1.find_last_of(s2c, i)); + } + } + void find_first_not_of_test() { std::cout << "find_first_not_of test..." << std::endl; @@ -224,19 +289,19 @@ namespace BOOST_TEST_EQ(sv1.find_first_not_of(sv2), s1.find_first_not_of(s2)); for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) { - std::cout << i << ": " << sv1.find_first_not_of(sv2, i) << " " << s1.find_first_not_of(s2, i) << std::endl; + //std::cout << i << ": " << sv1.find_first_not_of(sv2, i) << " " << s1.find_first_not_of(s2, i) << std::endl; BOOST_TEST_EQ(sv1.find_first_not_of(sv2, i), s1.find_first_not_of(s2, i)); } for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) { - std::cout << i << ": " << sv1.find_first_not_of(sv3, i) << " " << s1.find_first_not_of(s3, i) << std::endl; + //std::cout << i << ": " << sv1.find_first_not_of(sv3, i) << " " << s1.find_first_not_of(s3, i) << std::endl; BOOST_TEST_EQ(sv1.find_first_not_of(sv3, i), s1.find_first_not_of(s3, i)); } // second signature for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) { - std::cout << i << ": " << sv1.find_first_not_of('o', i) << " " << s1.find_first_not_of('o', i) << std::endl; + //std::cout << i << ": " << sv1.find_first_not_of('o', i) << " " << s1.find_first_not_of('o', i) << std::endl; BOOST_TEST_EQ(sv1.find_first_not_of('o', i), s1.find_first_not_of('o', i)); } @@ -244,7 +309,7 @@ namespace for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) for (std::string::size_type j = 0; j <= std::strlen(s2c) + 1; ++j) { - std::cout << i << "," << j << ": " << sv1.find_first_not_of(s2c, i, j) << " " << s1.find_first_not_of(s2c, i, j) << std::endl; + //std::cout << i << "," << j << ": " << sv1.find_first_not_of(s2c, i, j) << " " << s1.find_first_not_of(s2c, i, j) << std::endl; BOOST_TEST_EQ(sv1.find_first_not_of(s2c, i, j), s1.find_first_not_of(s2c, i, j)); } @@ -252,11 +317,76 @@ namespace BOOST_TEST_EQ(sv1.find_first_not_of(s2c), s1.find_first_not_of(s2c)); for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) { - std::cout << i << ": " << sv1.find_first_not_of(s2c, i) << " " << s1.find_first_not_of(s2c, i) << std::endl; + //std::cout << i << ": " << sv1.find_first_not_of(s2c, i) << " " << s1.find_first_not_of(s2c, i) << std::endl; BOOST_TEST_EQ(sv1.find_first_not_of(s2c, i), s1.find_first_not_of(s2c, i)); } } + void find_last_not_of_test() + { + std::cout << "find_last_not_of test..." << std::endl; + + // find_last_not_of - test two modified and two new signatures + std::string s1("Hello World!"); + boost::string_view sv1(s1); + std::string s2("o"); + boost::string_view sv2(s2); + const char* s2c = "Good Bye"; + boost::string_view sv2c(s2c); + std::string s3; + boost::string_view sv3(s3); + + // smoke test + BOOST_TEST_EQ(sv1.find_last_not_of(sv2), s1.find_last_not_of(s2)); + BOOST_TEST_EQ(sv1.find_last_not_of(sv2, 5), s1.find_last_not_of(s2, 5)); + BOOST_TEST_EQ(sv1.find_last_not_of(s2c), s1.find_last_not_of(s2c)); + BOOST_TEST_EQ(sv1.find_last_not_of(s2c, 0, 4), s1.find_last_not_of(s2c, 0, 4)); + + // first signature + BOOST_TEST_EQ(sv3.find_last_not_of(sv3), s3.find_last_not_of(s3)); // both strings empty + BOOST_TEST_EQ(sv1.find_last_not_of(sv3), s1.find_last_not_of(s3)); // search string empty + BOOST_TEST_EQ(sv3.find_last_not_of(sv2), s3.find_last_not_of(s2)); // searched string empty + BOOST_TEST_EQ(sv1.find_last_not_of(sv3, s1.size() + 2), s1.find_last_not_of(s3, s1.size() + 2)); + BOOST_TEST_EQ(sv1.find_last_not_of(sv3, s1.size() + 1), s1.find_last_not_of(s3, s1.size() + 1)); + BOOST_TEST_EQ(sv1.find_last_not_of(sv3, s1.size()), s1.find_last_not_of(s3, s1.size())); + BOOST_TEST_EQ(sv1.find_last_not_of(sv3, s1.size() - 1), s1.find_last_not_of(s3, s1.size() - 1)); + + BOOST_TEST_EQ(sv1.find_last_not_of(sv2), s1.find_last_not_of(s2)); + for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) + { + //std::cout << i << ": " << sv1.find_last_not_of(sv2, i) << " " << s1.find_last_not_of(s2, i) << std::endl; + BOOST_TEST_EQ(sv1.find_last_not_of(sv2, i), s1.find_last_not_of(s2, i)); + } + for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) + { + //std::cout << i << ": " << sv1.find_last_not_of(sv3, i) << " " << s1.find_last_not_of(s3, i) << std::endl; + BOOST_TEST_EQ(sv1.find_last_not_of(sv3, i), s1.find_last_not_of(s3, i)); + } + + // second signature + for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) + { + //std::cout << i << ": " << sv1.find_last_not_of('o', i) << " " << s1.find_last_not_of('o', i) << std::endl; + BOOST_TEST_EQ(sv1.find_last_not_of('o', i), s1.find_last_not_of('o', i)); + } + + // third signature + for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) + for (std::string::size_type j = 0; j <= std::strlen(s2c) + 1; ++j) + { + //std::cout << i << "," << j << ": " << sv1.find_last_not_of(s2c, i, j) << " " << s1.find_last_not_of(s2c, i, j) << std::endl; + BOOST_TEST_EQ(sv1.find_last_not_of(s2c, i, j), s1.find_last_not_of(s2c, i, j)); + } + + // fourth signature + BOOST_TEST_EQ(sv1.find_last_not_of(s2c), s1.find_last_not_of(s2c)); + for (std::string::size_type i = 0; i <= s1.size() + 1; ++i) + { + //std::cout << i << ": " << sv1.find_last_not_of(s2c, i) << " " << s1.find_last_not_of(s2c, i) << std::endl; + BOOST_TEST_EQ(sv1.find_last_not_of(s2c, i), s1.find_last_not_of(s2c, i)); + } + } + } // unnamed namespace int cpp_main(int argc, char* argv[]) @@ -271,7 +401,9 @@ int cpp_main(int argc, char* argv[]) find_test(); rfind_test(); find_first_of_test(); + find_last_of_test(); find_first_not_of_test(); + find_last_not_of_test(); return boost::report_errors(); }