mirror of
https://github.com/boostorg/utility.git
synced 2025-05-09 15:04:00 +00:00
Fix string_ref::find/rfind's handling of empty strings. Thanks to 'reluctantbugreporter' for the bug report
This commit is contained in:
parent
3d2a7f0c17
commit
db05c11f50
@ -181,6 +181,7 @@ namespace boost {
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_type find(basic_string_ref s) const {
|
size_type find(basic_string_ref s) const {
|
||||||
|
if (s.empty()) return 0;
|
||||||
const_iterator iter = std::search ( this->cbegin (), this->cend (),
|
const_iterator iter = std::search ( this->cbegin (), this->cend (),
|
||||||
s.cbegin (), s.cend (), traits::eq );
|
s.cbegin (), s.cend (), traits::eq );
|
||||||
return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
|
return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
|
||||||
@ -193,6 +194,7 @@ namespace boost {
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_type rfind(basic_string_ref s) const {
|
size_type rfind(basic_string_ref s) const {
|
||||||
|
if (s.empty()) return 0;
|
||||||
const_reverse_iterator iter = std::search ( this->crbegin (), this->crend (),
|
const_reverse_iterator iter = std::search ( this->crbegin (), this->crend (),
|
||||||
s.crbegin (), s.crend (), traits::eq );
|
s.crbegin (), s.crend (), traits::eq );
|
||||||
return iter == this->crend () ? npos : (std::distance(iter, this->crend()) - s.size());
|
return iter == this->crend () ? npos : (std::distance(iter, this->crend()) - s.size());
|
||||||
|
@ -93,6 +93,10 @@ void find ( const char *arg ) {
|
|||||||
string_ref sr2;
|
string_ref sr2;
|
||||||
const char *p;
|
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)
|
// Look for each character in the string(searching from the start)
|
||||||
p = arg;
|
p = arg;
|
||||||
sr1 = arg;
|
sr1 = arg;
|
||||||
|
@ -97,6 +97,10 @@ void find ( const char *arg ) {
|
|||||||
string_view sr2;
|
string_view sr2;
|
||||||
const char *p;
|
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)
|
// Look for each character in the string(searching from the start)
|
||||||
p = arg;
|
p = arg;
|
||||||
sr1 = arg;
|
sr1 = arg;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user