Skip to content

Commit

Permalink
fix sstring.find(): make find("") compatible with std::string
Browse files Browse the repository at this point in the history
  • Loading branch information
longqimin committed Jan 18, 2022
1 parent 777e4b3 commit 7bb0754
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/seastar/core/sstring.hh
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ public:
const char_type* c_str = s.str();
const char_type* c_str_end = s.str() + s.size();

if (c_str == c_str_end) {
/* see https://en.cppreference.com/w/cpp/string/basic_string/find
* - an empty substring is found at pos if and only if pos <= size()
*/
return (pos <= size()) ? pos : npos;
}

while (it < end) {
auto i = it;
auto j = c_str;
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/sstring_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ BOOST_AUTO_TEST_CASE(test_add_literal_to_sstring) {
BOOST_AUTO_TEST_CASE(test_find_sstring) {
BOOST_REQUIRE_EQUAL(sstring("abcde").find('b'), 1u);
BOOST_REQUIRE_EQUAL(sstring("babcde").find('b',1), 2u);
BOOST_REQUIRE_EQUAL(sstring("").find("", 0), 0u);
BOOST_REQUIRE_EQUAL(sstring("").find("", 1), sstring::npos);
BOOST_REQUIRE_EQUAL(sstring("abcde").find("", 0), 0u);
BOOST_REQUIRE_EQUAL(sstring("abcde").find("", 1), 1u);
BOOST_REQUIRE_EQUAL(sstring("abcde").find("", 5), 5u);
BOOST_REQUIRE_EQUAL(sstring("abcde").find("", 6), sstring::npos);
}

BOOST_AUTO_TEST_CASE(test_not_find_sstring) {
Expand Down

0 comments on commit 7bb0754

Please sign in to comment.