Skip to content

Commit

Permalink
[fix] last not of: fix start parameter was ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
biojppm committed Jan 5, 2022
1 parent 31a1443 commit 01c926c
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 8 deletions.
20 changes: 13 additions & 7 deletions src/c4/substr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,8 @@ struct C4CORE_EXPORT basic_substring
size_t last_of(const C c, size_t start=npos) const
{
C4_ASSERT(start == npos || (start >= 0 && start <= len));
if(start == npos) start = len;
if(start == npos)
start = len;
for(size_t i = start-1; i != size_t(-1); --i)
{
if(str[i] == c)
Expand Down Expand Up @@ -785,7 +786,8 @@ struct C4CORE_EXPORT basic_substring
size_t last_of(ro_substr chars, size_t start=npos) const
{
C4_ASSERT(start == npos || (start >= 0 && start <= len));
if(start == npos) start = len;
if(start == npos)
start = len;
for(size_t i = start-1; i != size_t(-1); --i)
{
for(size_t j = 0; j < chars.len; ++j)
Expand Down Expand Up @@ -813,7 +815,8 @@ struct C4CORE_EXPORT basic_substring
size_t last_not_of(const C c, size_t start=npos) const
{
C4_ASSERT(start == npos || (start >= 0 && start <= len));
if(start == npos) start = len;
if(start == npos)
start = len;
for(size_t i = start-1; i != size_t(-1); --i)
{
if(str[i] != c)
Expand Down Expand Up @@ -847,8 +850,9 @@ struct C4CORE_EXPORT basic_substring
size_t last_not_of(ro_substr chars, size_t start=npos) const
{
C4_ASSERT(start == npos || (start >= 0 && start <= len));
if(start == npos) start = len;
for(size_t i = len-1; i != size_t(-1); --i)
if(start == npos)
start = len;
for(size_t i = start-1; i != size_t(-1); --i)
{
bool gotit = true;
for(size_t j = 0; j < chars.len; ++j)
Expand Down Expand Up @@ -880,9 +884,11 @@ struct C4CORE_EXPORT basic_substring
basic_substring pair_range(CC open, CC close) const
{
size_t b = find(open);
if(b == npos) return basic_substring();
if(b == npos)
return basic_substring();
size_t e = find(close, b+1);
if(e == npos) return basic_substring();
if(e == npos)
return basic_substring();
basic_substring ret = range(b, e+1);
C4_ASSERT(ret.sub(1).find(open) == npos);
return ret;
Expand Down
109 changes: 108 additions & 1 deletion test/test_substr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,19 @@ TEST_CASE("substr.first_of")
CHECK_EQ(csubstr("012345").first_of("012345"), 0u);
CHECK_EQ(csubstr("012345").first_of("543210"), 0u);

CHECK_EQ(csubstr("012345").first_of('0', 2u), npos);
CHECK_EQ(csubstr("012345").first_of("0", 2u), npos);
CHECK_EQ(csubstr("012345").first_of("01", 2u), npos);
CHECK_EQ(csubstr("012345").first_of("10", 2u), npos);
CHECK_EQ(csubstr("012345").first_of("012", 2u), 2u);
CHECK_EQ(csubstr("012345").first_of("210", 2u), 2u);
CHECK_EQ(csubstr("012345").first_of("0123", 2u), 2u);
CHECK_EQ(csubstr("012345").first_of("3210", 2u), 2u);
CHECK_EQ(csubstr("012345").first_of("01234", 2u), 2u);
CHECK_EQ(csubstr("012345").first_of("43210", 2u), 2u);
CHECK_EQ(csubstr("012345").first_of("012345", 2u), 2u);
CHECK_EQ(csubstr("012345").first_of("543210", 2u), 2u);

CHECK_EQ(csubstr("012345").first_of('5'), 5u);
CHECK_EQ(csubstr("012345").first_of("5"), 5u);
CHECK_EQ(csubstr("012345").first_of("45"), 4u);
Expand All @@ -575,6 +588,21 @@ TEST_CASE("substr.first_of")
CHECK_EQ(csubstr("012345").first_of("012345"), 0u);
CHECK_EQ(csubstr("012345").first_of("543210"), 0u);

CHECK_EQ(csubstr("012345").first_of('5', 2u), 5u);
CHECK_EQ(csubstr("012345").first_of("5", 2u), 5u);
CHECK_EQ(csubstr("012345").first_of("45", 2u), 4u);
CHECK_EQ(csubstr("012345").first_of("54", 2u), 4u);
CHECK_EQ(csubstr("012345").first_of("345", 2u), 3u);
CHECK_EQ(csubstr("012345").first_of("543", 2u), 3u);
CHECK_EQ(csubstr("012345").first_of("2345", 2u), 2u);
CHECK_EQ(csubstr("012345").first_of("5432", 2u), 2u);
CHECK_EQ(csubstr("012345").first_of("12345", 2u), 2u);
CHECK_EQ(csubstr("012345").first_of("54321", 2u), 2u);
CHECK_EQ(csubstr("012345").first_of("012345", 2u), 2u);
CHECK_EQ(csubstr("012345").first_of("543210", 2u), 2u);

CHECK_EQ(csubstr{}.first_of('0'), npos);
CHECK_EQ(csubstr{}.first_of('0', 0u), npos);
CHECK_EQ(csubstr("012345").first_of('0', 6u), npos);
CHECK_EQ(csubstr("012345").first_of('5', 6u), npos);
CHECK_EQ(csubstr("012345").first_of("012345", 6u), npos);
Expand All @@ -600,6 +628,19 @@ TEST_CASE("substr.last_of")
CHECK_EQ(csubstr("012345").last_of("012345"), 5u);
CHECK_EQ(csubstr("012345").last_of("543210"), 5u);

CHECK_EQ(csubstr("012345").last_of('0', 2u), 0u);
CHECK_EQ(csubstr("012345").last_of("0", 2u), 0u);
CHECK_EQ(csubstr("012345").last_of("01", 2u), 1u);
CHECK_EQ(csubstr("012345").last_of("10", 2u), 1u);
CHECK_EQ(csubstr("012345").last_of("012", 2u), 1u);
CHECK_EQ(csubstr("012345").last_of("210", 2u), 1u);
CHECK_EQ(csubstr("012345").last_of("0123", 2u), 1u);
CHECK_EQ(csubstr("012345").last_of("3210", 2u), 1u);
CHECK_EQ(csubstr("012345").last_of("01234", 2u), 1u);
CHECK_EQ(csubstr("012345").last_of("43210", 2u), 1u);
CHECK_EQ(csubstr("012345").last_of("012345", 2u), 1u);
CHECK_EQ(csubstr("012345").last_of("543210", 2u), 1u);

CHECK_EQ(csubstr("012345").last_of('5'), 5u);
CHECK_EQ(csubstr("012345").last_of("5"), 5u);
CHECK_EQ(csubstr("012345").last_of("45"), 5u);
Expand All @@ -613,6 +654,20 @@ TEST_CASE("substr.last_of")
CHECK_EQ(csubstr("012345").last_of("012345"), 5u);
CHECK_EQ(csubstr("012345").last_of("543210"), 5u);

CHECK_EQ(csubstr("012345").last_of('5', 2u), npos);
CHECK_EQ(csubstr("012345").last_of("5", 2u), npos);
CHECK_EQ(csubstr("012345").last_of("45", 2u), npos);
CHECK_EQ(csubstr("012345").last_of("54", 2u), npos);
CHECK_EQ(csubstr("012345").last_of("345", 2u), npos);
CHECK_EQ(csubstr("012345").last_of("543", 2u), npos);
CHECK_EQ(csubstr("012345").last_of("2345", 2u), npos);
CHECK_EQ(csubstr("012345").last_of("5432", 2u), npos);
CHECK_EQ(csubstr("012345").last_of("12345", 2u), 1u);
CHECK_EQ(csubstr("012345").last_of("54321", 2u), 1u);
CHECK_EQ(csubstr("012345").last_of("012345", 2u), 1u);
CHECK_EQ(csubstr("012345").last_of("543210", 2u), 1u);

CHECK_EQ(csubstr{}.last_of('?'), npos);
CHECK_EQ(csubstr("012345").last_of('0', 6u), 0u);
CHECK_EQ(csubstr("012345").last_of('5', 6u), 5u);
CHECK_EQ(csubstr("012345").last_of("012345", 6u), 5u);
Expand All @@ -638,6 +693,19 @@ TEST_CASE("substr.first_not_of")
CHECK_EQ(csubstr("012345").first_not_of("012345"), npos);
CHECK_EQ(csubstr("012345").first_not_of("543210"), npos);

CHECK_EQ(csubstr("012345").first_not_of('0', 2u), 2u);
CHECK_EQ(csubstr("012345").first_not_of("0", 2u), 2u);
CHECK_EQ(csubstr("012345").first_not_of("01", 2u), 2u);
CHECK_EQ(csubstr("012345").first_not_of("10", 2u), 2u);
CHECK_EQ(csubstr("012345").first_not_of("012", 2u), 3u);
CHECK_EQ(csubstr("012345").first_not_of("210", 2u), 3u);
CHECK_EQ(csubstr("012345").first_not_of("0123", 2u), 4u);
CHECK_EQ(csubstr("012345").first_not_of("3210", 2u), 4u);
CHECK_EQ(csubstr("012345").first_not_of("01234", 2u), 5u);
CHECK_EQ(csubstr("012345").first_not_of("43210", 2u), 5u);
CHECK_EQ(csubstr("012345").first_not_of("012345", 2u), npos);
CHECK_EQ(csubstr("012345").first_not_of("543210", 2u), npos);

CHECK_EQ(csubstr("012345").first_not_of('5'), 0u);
CHECK_EQ(csubstr("012345").first_not_of("5"), 0u);
CHECK_EQ(csubstr("012345").first_not_of("45"), 0u);
Expand All @@ -651,6 +719,19 @@ TEST_CASE("substr.first_not_of")
CHECK_EQ(csubstr("012345").first_not_of("012345"), npos);
CHECK_EQ(csubstr("012345").first_not_of("543210"), npos);

CHECK_EQ(csubstr("012345").first_not_of('5', 2u), 2u);
CHECK_EQ(csubstr("012345").first_not_of("5", 2u), 2u);
CHECK_EQ(csubstr("012345").first_not_of("45", 2u), 2u);
CHECK_EQ(csubstr("012345").first_not_of("54", 2u), 2u);
CHECK_EQ(csubstr("012345").first_not_of("345", 2u), 2u);
CHECK_EQ(csubstr("012345").first_not_of("543", 2u), 2u);
CHECK_EQ(csubstr("012345").first_not_of("2345", 2u), npos);
CHECK_EQ(csubstr("012345").first_not_of("5432", 2u), npos);
CHECK_EQ(csubstr("012345").first_not_of("12345", 2u), npos);
CHECK_EQ(csubstr("012345").first_not_of("54321", 2u), npos);
CHECK_EQ(csubstr("012345").first_not_of("012345", 2u), npos);
CHECK_EQ(csubstr("012345").first_not_of("543210", 2u), npos);

CHECK_EQ(csubstr("").first_not_of('0', 0u), npos);
CHECK_EQ(csubstr("012345").first_not_of('5', 6u), npos);
CHECK_EQ(csubstr("012345").first_not_of("012345", 6u), npos);
Expand All @@ -676,6 +757,19 @@ TEST_CASE("substr.last_not_of")
CHECK_EQ(csubstr("012345").last_not_of("012345"), npos);
CHECK_EQ(csubstr("012345").last_not_of("543210"), npos);

CHECK_EQ(csubstr("012345").last_not_of('5', 2u), 1u);
CHECK_EQ(csubstr("012345").last_not_of("5", 2u), 1u);
CHECK_EQ(csubstr("012345").last_not_of("45", 2u), 1u);
CHECK_EQ(csubstr("012345").last_not_of("54", 2u), 1u);
CHECK_EQ(csubstr("012345").last_not_of("345", 2u), 1u);
CHECK_EQ(csubstr("012345").last_not_of("543", 2u), 1u);
CHECK_EQ(csubstr("012345").last_not_of("2345", 2u), 1u);
CHECK_EQ(csubstr("012345").last_not_of("5432", 2u), 1u);
CHECK_EQ(csubstr("012345").last_not_of("12345", 2u), 0u);
CHECK_EQ(csubstr("012345").last_not_of("54321", 2u), 0u);
CHECK_EQ(csubstr("012345").last_not_of("012345", 2u), npos);
CHECK_EQ(csubstr("012345").last_not_of("543210", 2u), npos);

CHECK_EQ(csubstr("012345").last_not_of('0'), 5u);
CHECK_EQ(csubstr("012345").last_not_of("0"), 5u);
CHECK_EQ(csubstr("012345").last_not_of("01"), 5u);
Expand All @@ -689,8 +783,21 @@ TEST_CASE("substr.last_not_of")
CHECK_EQ(csubstr("012345").last_not_of("012345"), npos);
CHECK_EQ(csubstr("012345").last_not_of("543210"), npos);

CHECK_EQ(csubstr("012345").last_not_of('0', 2u), 1u);
CHECK_EQ(csubstr("012345").last_not_of("0", 2u), 1u);
CHECK_EQ(csubstr("012345").last_not_of("01", 2u), npos);
CHECK_EQ(csubstr("012345").last_not_of("10", 2u), npos);
CHECK_EQ(csubstr("012345").last_not_of("012", 2u), npos);
CHECK_EQ(csubstr("012345").last_not_of("210", 2u), npos);
CHECK_EQ(csubstr("012345").last_not_of("0123", 2u), npos);
CHECK_EQ(csubstr("012345").last_not_of("3210", 2u), npos);
CHECK_EQ(csubstr("012345").last_not_of("01234", 2u), npos);
CHECK_EQ(csubstr("012345").last_not_of("43210", 2u), npos);
CHECK_EQ(csubstr("012345").last_not_of("012345", 2u), npos);
CHECK_EQ(csubstr("012345").last_not_of("543210", 2u), npos);

CHECK_EQ(csubstr("").last_not_of('0', 0u), npos);
CHECK_EQ(csubstr("012345").last_not_of('5', 6u), 4);
CHECK_EQ(csubstr("012345").last_not_of('5', 6u), 4u);
}

TEST_CASE("substr.left_of")
Expand Down

0 comments on commit 01c926c

Please sign in to comment.