diff --git a/include/boost/utility/string_view.hpp b/include/boost/utility/string_view.hpp index fda078e..461073d 100644 --- a/include/boost/utility/string_view.hpp +++ b/include/boost/utility/string_view.hpp @@ -211,7 +211,7 @@ namespace boost { // find /*BOOST_CONSTEXPR*/ size_type find(basic_string_view s, size_type pos = 0) const BOOST_NOEXCEPT { - if (s.empty() && pos == size()) + if (s.empty() && pos <= size()) return pos; if (pos > size()) return npos; @@ -219,15 +219,12 @@ namespace boost { s.cbegin (), s.cend (), traits::eq ); return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter ); } - BOOST_CONSTEXPR size_type find(charT c, size_type pos = 0) const BOOST_NOEXCEPT { - return find(basic_string_view(&c, 1), pos); - } - BOOST_CONSTEXPR size_type find(const charT* s, size_type pos, size_type n) const { - return find(basic_string_view(s, n), pos); - } - BOOST_CONSTEXPR size_type find(const charT* s, size_type pos = 0) const { - return find(basic_string_view(s), pos); - } + BOOST_CONSTEXPR size_type find(charT c, size_type pos = 0) const BOOST_NOEXCEPT + { return find(basic_string_view(&c, 1), pos); } + BOOST_CONSTEXPR size_type find(const charT* s, size_type pos, size_type n) const + { return find(basic_string_view(s, n), pos); } + BOOST_CONSTEXPR size_type find(const charT* s, size_type pos = 0) const + { return find(basic_string_view(s), pos); } // rfind BOOST_CONSTEXPR size_type rfind(basic_string_view s, size_type pos = npos) @@ -236,54 +233,62 @@ namespace boost { s.crbegin (), s.crend (), traits::eq ); return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter ); } - - BOOST_CONSTEXPR size_type rfind(charT c) const BOOST_NOEXCEPT { - const_reverse_iterator iter = std::find_if ( this->crbegin (), this->crend (), - detail::string_view_traits_eq ( c )); - return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter ); - } + BOOST_CONSTEXPR size_type rfind(charT c, size_type pos = npos) const BOOST_NOEXCEPT + { return rfind(basic_string_view(&c, 1), pos); } + BOOST_CONSTEXPR size_type rfind(const charT* s, size_type pos, size_type n) const + { return rfind(basic_string_view(s, n), pos); } + BOOST_CONSTEXPR size_type rfind(const charT* s, size_type pos = npos) const + { return rfind(basic_string_view(s), pos); } // find_first_of - BOOST_CONSTEXPR size_type find_first_of(basic_string_view s) const BOOST_NOEXCEPT { + BOOST_CONSTEXPR size_type find_first_of(basic_string_view s, size_type pos = 0) const BOOST_NOEXCEPT { const_iterator iter = std::find_first_of ( this->cbegin (), this->cend (), s.cbegin (), s.cend (), traits::eq ); return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter ); } - BOOST_CONSTEXPR size_type find_first_of(charT c) const BOOST_NOEXCEPT { return find (c); } + BOOST_CONSTEXPR size_type find_first_of(charT c, size_type pos = 0) const BOOST_NOEXCEPT + { return find_first_of(basic_string_view(&c, 1), pos); } + BOOST_CONSTEXPR size_type find_first_of(const charT* s, size_type pos, size_type n) const + { return find_first_of(basic_string_view(s, n), pos); } + BOOST_CONSTEXPR size_type find_first_of(const charT* s, size_type pos = 0) const + { return find_first_of(basic_string_view(s), pos); } // find_last_of - BOOST_CONSTEXPR size_type find_last_of(basic_string_view s) const BOOST_NOEXCEPT { + BOOST_CONSTEXPR size_type find_last_of(basic_string_view s, size_type pos = npos) const BOOST_NOEXCEPT { const_reverse_iterator iter = std::find_first_of ( this->crbegin (), this->crend (), s.cbegin (), s.cend (), traits::eq ); return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter); } - BOOST_CONSTEXPR size_type find_last_of (charT c) const BOOST_NOEXCEPT { return rfind (c); } + BOOST_CONSTEXPR size_type find_last_of(charT c, size_type pos = npos) const BOOST_NOEXCEPT + { return find_last_of(basic_string_view(&c, 1), pos); } + BOOST_CONSTEXPR size_type find_last_of(const charT* s, size_type pos, size_type n) const + { return find_last_of(basic_string_view(s, n), pos); } + BOOST_CONSTEXPR size_type find_last_of(const charT* s, size_type pos = npos) const + { return find_last_of(basic_string_view(s), pos); } // find_first_not_of - BOOST_CONSTEXPR size_type find_first_not_of(basic_string_view s) const { + BOOST_CONSTEXPR size_type find_first_not_of(basic_string_view s, size_type pos = 0) const BOOST_NOEXCEPT { const_iterator iter = find_not_of ( this->cbegin (), this->cend (), s ); return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter ); } - - BOOST_CONSTEXPR size_type find_first_not_of(charT c) const BOOST_NOEXCEPT { - for ( const_iterator iter = this->cbegin (); iter != this->cend (); ++iter ) - if ( !traits::eq ( c, *iter )) - return std::distance ( this->cbegin (), iter ); - return npos; - } + BOOST_CONSTEXPR size_type find_first_not_of(charT c, size_type pos = 0) const BOOST_NOEXCEPT + { return find_first_not_of(basic_string_view(&c, 1), pos); } + BOOST_CONSTEXPR size_type find_first_not_of(const charT* s, size_type pos, size_type n) const + { return find_first_not_of(basic_string_view(s, n), pos); } + BOOST_CONSTEXPR size_type find_first_not_of(const charT* s, size_type pos = 0) const + { return find_first_not_of(basic_string_view(s), pos); } // find_last_not_of - BOOST_CONSTEXPR size_type find_last_not_of(basic_string_view s) const BOOST_NOEXCEPT { + BOOST_CONSTEXPR 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 ); return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter ); } - - BOOST_CONSTEXPR size_type find_last_not_of(charT c) const BOOST_NOEXCEPT { - for ( const_reverse_iterator iter = this->crbegin (); iter != this->crend (); ++iter ) - if ( !traits::eq ( c, *iter )) - return reverse_distance ( this->crbegin (), iter ); - return npos; - } + BOOST_CONSTEXPR size_type find_last_not_of(charT c, size_type pos = npos) const BOOST_NOEXCEPT + { return find_last_not_of(basic_string_view(&c, 1), pos); } + BOOST_CONSTEXPR size_type find_last_not_of(const charT* s, size_type pos, size_type n) const + { return find_last_not_of(basic_string_view(s, n), pos); } + BOOST_CONSTEXPR size_type find_last_not_of(const charT* s, size_type pos = npos) const + { return find_last_not_of(basic_string_view(s), pos); } private: template diff --git a/test/string_view_test3.cpp b/test/string_view_test3.cpp index c4dbbb2..ea6b510 100644 --- a/test/string_view_test3.cpp +++ b/test/string_view_test3.cpp @@ -34,6 +34,8 @@ namespace BOOST_TEST_EQ(sv2.find(sv3), s2.find(s3)); BOOST_TEST_EQ(sv3.find(sv2), s3.find(s2)); BOOST_TEST_EQ(sv1.find(sv3, 7), s1.find(s3, 7)); + BOOST_TEST_EQ(sv1.find(sv3, 5), s1.find(s3, 5)); + //std::cout << s1.find(s3, 5) << std::endl; BOOST_TEST_EQ(sv1.find(sv2), s1.find(s2)); for (std::string::size_type i = 0; i <= 8; ++i)