mirror of
https://github.com/boostorg/utility.git
synced 2025-05-09 15:04:00 +00:00
find_last_of, find_last_not_of, passing tests.
This commit is contained in:
parent
268861ff6b
commit
9c961f0577
@ -265,8 +265,14 @@ namespace boost {
|
|||||||
|
|
||||||
// find_last_of
|
// find_last_of
|
||||||
size_type find_last_of(basic_string_view s, size_type pos = npos) const BOOST_NOEXCEPT {
|
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
|
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);
|
return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter);
|
||||||
}
|
}
|
||||||
size_type find_last_of(charT c, size_type pos = npos) const BOOST_NOEXCEPT
|
size_type find_last_of(charT c, size_type pos = npos) const BOOST_NOEXCEPT
|
||||||
@ -294,7 +300,12 @@ namespace boost {
|
|||||||
|
|
||||||
// find_last_not_of
|
// find_last_not_of
|
||||||
size_type find_last_not_of(basic_string_view s, size_type pos = npos) const BOOST_NOEXCEPT {
|
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 );
|
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
|
size_type find_last_not_of(charT c, size_type pos = npos) const BOOST_NOEXCEPT
|
||||||
|
@ -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()
|
void find_first_not_of_test()
|
||||||
{
|
{
|
||||||
std::cout << "find_first_not_of test..." << std::endl;
|
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));
|
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)
|
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));
|
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)
|
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));
|
BOOST_TEST_EQ(sv1.find_first_not_of(sv3, i), s1.find_first_not_of(s3, i));
|
||||||
}
|
}
|
||||||
|
|
||||||
// second signature
|
// second signature
|
||||||
for (std::string::size_type i = 0; i <= s1.size() + 1; ++i)
|
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));
|
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 i = 0; i <= s1.size() + 1; ++i)
|
||||||
for (std::string::size_type j = 0; j <= std::strlen(s2c) + 1; ++j)
|
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));
|
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));
|
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)
|
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));
|
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
|
} // unnamed namespace
|
||||||
|
|
||||||
int cpp_main(int argc, char* argv[])
|
int cpp_main(int argc, char* argv[])
|
||||||
@ -271,7 +401,9 @@ int cpp_main(int argc, char* argv[])
|
|||||||
find_test();
|
find_test();
|
||||||
rfind_test();
|
rfind_test();
|
||||||
find_first_of_test();
|
find_first_of_test();
|
||||||
|
find_last_of_test();
|
||||||
find_first_not_of_test();
|
find_first_not_of_test();
|
||||||
|
find_last_not_of_test();
|
||||||
|
|
||||||
return boost::report_errors();
|
return boost::report_errors();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user