Skip to content

Commit

Permalink
Fix std::out_of_range from string_view::find (#7)
Browse files Browse the repository at this point in the history
The string_view::find operation was unexpectedly throwing an
out_of_range exception in cases where the position was nonzero
and the string wasn't found. 

This corrects those cases by accounting for the position, and adds
unit tests for validation.

Fixes #6
  • Loading branch information
xgcssch committed Mar 13, 2021
1 parent f582eaf commit fa51c8f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/bpstd/detail/string_view.inl
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ namespace bpstd {
}

const auto offset = pos;
const auto increments = size() - v.size();
const auto increments = size() - v.size() - offset;

for (auto i = 0u; i <= increments; ++i) {
const auto j = i + offset;
Expand Down
2 changes: 1 addition & 1 deletion single_include/bpstd/string_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ namespace bpstd {
}

const auto offset = pos;
const auto increments = size() - v.size();
const auto increments = size() - v.size() - offset;

for (auto i = 0u; i <= increments; ++i) {
const auto j = i + offset;
Expand Down
17 changes: 17 additions & 0 deletions tests/bpstd/string_view.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,23 @@ TEST_CASE("string_view::find", "[operations]")
REQUIRE( result == 0u );
}
}
SECTION("Argument is string, offset in string, no match")
{
bpstd::string_view s1 = "01234567890ABCDEFGHIJ";
bpstd::string_view s2 = "01";

auto result = s1.find(s2);
SECTION("prefix find ok")
{
REQUIRE(result != bpstd::string_view::npos);
REQUIRE(s1.size() > 10);
}
auto result1 = s1.find(s2, 10);
SECTION("nonmatching substring")
{
REQUIRE(result1 == bpstd::string_view::npos);
}
}
}
}

Expand Down

0 comments on commit fa51c8f

Please sign in to comment.