Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions include/stringzilla/stringzilla.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2656,7 +2656,8 @@ class basic_string_slice {
* @return For an @b empty `other`, `min(until, size())`.
*/
size_type rfind(string_view other, size_type until) const noexcept(false) {
return until + other.size() < length_ ? substr(0, until + other.size()).rfind(other) : rfind(other);
return other.size() < length_ && until < length_ - other.size() ? substr(0, until + other.size()).rfind(other)
: rfind(other);
}

/**
Expand Down Expand Up @@ -2775,7 +2776,7 @@ class basic_string_slice {
* @param until The offset of the last character to be considered.
*/
size_type find_last_of(byteset set, size_type until) const noexcept {
auto len = sz_min_of_two(until + 1, length_);
auto len = until < length_ ? until + 1 : length_;
auto ptr = sz_rfind_byteset(start_, len, &set.raw());
return ptr ? ptr - start_ : npos;
}
Expand Down
9 changes: 0 additions & 9 deletions test/find.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,6 @@ static void check_find_unit_( //
void test_find_unit() {
std::printf(" - testing search & comparison known-answer vectors...\n");

// Default views use a null pointer, which must not turn an empty match into npos.
for (sz::string_view empty : {sz::string_view(), sz::string_view("")}) {
verify(empty.find("") == 0);
verify(empty.rfind("") == 0);
verify(empty.rfind("", sz::string_view::npos) == 0);
verify(empty.contains(""));
verify(empty.find("a") == sz::string_view::npos);
}

char const *hello = "hello world";
sz_size_t const hello_length = (sz_size_t)std::strlen(hello); // 11 bytes

Expand Down
5 changes: 5 additions & 0 deletions test/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,8 @@ void test_stl_reads_unit() {
verify(str("hello").rfind('l', 1) == str::npos);

// Substring search in normal and reverse directions.
verify(str().find("") == 0);
verify(str().rfind("") == 0);
verify(str("hello").find("ell") == 1);
verify(str("hello").find("ell", 1) == 1);
verify(str("hello").find("ell", 2) == str::npos);
Expand Down Expand Up @@ -938,6 +940,9 @@ void test_stl_reads_unit() {
}

// Boundary conditions.
auto const max_pos = std::numeric_limits<std::size_t>::max();
verify(str("hello").rfind("ell", max_pos) == 1);
verify(str("hello").find_last_of("le", max_pos) == 3);
verify(str("hello").find_first_of("ox", 4) == 4);
verify(str("hello").find_first_of("ox", 5) == str::npos);
verify(str("hello").find_last_of("ox", 4) == 4);
Expand Down
Loading