diff --git a/include/boost/utility/string_ref.hpp b/include/boost/utility/string_ref.hpp index d234e54..4b36ffc 100644 --- a/include/boost/utility/string_ref.hpp +++ b/include/boost/utility/string_ref.hpp @@ -181,6 +181,7 @@ namespace boost { } size_type find(basic_string_ref s) const { + if (s.empty()) return 0; const_iterator iter = std::search ( this->cbegin (), this->cend (), s.cbegin (), s.cend (), traits::eq ); return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter ); @@ -193,6 +194,7 @@ namespace boost { } size_type rfind(basic_string_ref s) const { + if (s.empty()) return 0; const_reverse_iterator iter = std::search ( this->crbegin (), this->crend (), s.crbegin (), s.crend (), traits::eq ); return iter == this->crend () ? npos : (std::distance(iter, this->crend()) - s.size()); diff --git a/test/string_ref_test2.cpp b/test/string_ref_test2.cpp index ccb5d6d..d733775 100644 --- a/test/string_ref_test2.cpp +++ b/test/string_ref_test2.cpp @@ -93,6 +93,10 @@ void find ( const char *arg ) { string_ref sr2; const char *p; +// When we search for the empty string, we find it at position 0 + BOOST_TEST ( sr1.find (sr2) == 0 ); + BOOST_TEST ( sr1.rfind(sr2) == 0 ); + // Look for each character in the string(searching from the start) p = arg; sr1 = arg; diff --git a/test/string_view_test2.cpp b/test/string_view_test2.cpp index 2549e64..135fd1a 100644 --- a/test/string_view_test2.cpp +++ b/test/string_view_test2.cpp @@ -97,6 +97,10 @@ void find ( const char *arg ) { string_view sr2; const char *p; +// When we search for the empty string, we find it at position 0 + BOOST_TEST ( sr1.find (sr2) == 0 ); + BOOST_TEST ( sr1.rfind(sr2) == 0 ); + // Look for each character in the string(searching from the start) p = arg; sr1 = arg;