Skip to content

Commit

Permalink
Solved bug in starts_with() and ends_with()
Browse files Browse the repository at this point in the history
  • Loading branch information
torrentg committed Jan 8, 2025
1 parent cc0a6b5 commit ae8af81
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
Binary file removed cstring-example
Binary file not shown.
6 changes: 5 additions & 1 deletion cstring-tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,10 @@ TEST_CASE("cstring") {
CHECK(!cstring("hello world").starts_with(cstring("ello")));

const char *aux = "hello";
CHECK(cstring("hello world").starts_with(""sv));
CHECK(cstring("hello world").starts_with(cstring::basic_cstring_view(aux)));
CHECK(!cstring("hello world").starts_with(cstring::basic_cstring_view(aux, 1)));
CHECK(cstring("hello world").starts_with(cstring::basic_cstring_view(aux, 1)));
CHECK(!cstring("hello world").starts_with("hellow world!"sv));

CHECK(cstring("hello world").starts_with(nullptr));
CHECK(cstring("hello world").starts_with(""));
Expand All @@ -304,8 +306,10 @@ TEST_CASE("cstring") {
CHECK(!cstring("hello world").ends_with(cstring("worlds")));

const char *aux = "world";
CHECK(cstring("hello world").ends_with(""sv));
CHECK(cstring("hello world").ends_with(cstring::basic_cstring_view(aux)));
CHECK(!cstring("hello world").ends_with(cstring::basic_cstring_view(aux, 4)));
CHECK(!cstring("hello world").ends_with("a hello world"sv));

CHECK(cstring("hello world").ends_with(nullptr));
CHECK(cstring("hello world").ends_with(""));
Expand Down
6 changes: 3 additions & 3 deletions cstring.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace gto {
* @see https://github.com/torrentg/cstring
*
* @note This class is immutable.
* @version 1.0.3
* @version 1.0.4
*/
template<typename Char,
typename Traits = std::char_traits<Char>,
Expand Down Expand Up @@ -375,7 +375,7 @@ class basic_cstring
}
bool starts_with(const basic_cstring_view sv) const noexcept {
auto len = sv.length();
return (compare(0, len, sv.data()) == 0);
return (compare(0, len, sv.data(), len) == 0);
}
bool starts_with(const_pointer str) const noexcept {
return starts_with(basic_cstring_view(sanitize(str)));
Expand All @@ -390,7 +390,7 @@ class basic_cstring
bool ends_with(const basic_cstring_view sv) const noexcept {
size_type len1 = length();
size_type len2 = sv.length();
return (len1 >= len2 && compare(len1-len2, len2, sv.data()) == 0);
return (len1 >= len2 && compare(len1-len2, len2, sv.data(), len2) == 0);
}
bool ends_with(const_pointer str) const noexcept {
return ends_with(basic_cstring_view(sanitize(str)));
Expand Down

0 comments on commit ae8af81

Please sign in to comment.