COMMON: fix String::rfind for default value of pos (max value of size_t)

This commit is contained in:
Sergio 2022-11-22 22:55:59 +01:00 committed by Filippos Karapetis
parent f7b2bb0e66
commit d136a5f54c
2 changed files with 25 additions and 3 deletions

View File

@ -325,9 +325,15 @@ size_t String::rfind(const char *s) const {
}
size_t String::rfind(char c, size_t pos) const {
for (int idx = MIN((int)_size - 1, (int)pos); idx >= 0; --idx) {
if ((*this)[idx] == c)
return idx;
if (pos == npos || pos > _size)
pos = _size;
else
++pos;
while (pos > 0) {
--pos;
if ((*this)[pos] == c)
return pos;
}
return npos;

View File

@ -570,6 +570,22 @@ class StringTestSuite : public CxxTest::TestSuite
TS_ASSERT_EQUALS(s4, "TestTestTestTestTestTestTestTestTestTestTest");
}
void test_find() {
Common::String a("0123012"), b;
TS_ASSERT_EQUALS(a.find('1'), 1u);
TS_ASSERT_EQUALS(a.find('3'), 3u);
TS_ASSERT_EQUALS(a.find('1', 3), 5u);
TS_ASSERT_EQUALS(b.find('*'), Common::String::npos);
TS_ASSERT_EQUALS(b.find('*', 1), Common::String::npos);
TS_ASSERT_EQUALS(a.rfind('1'), 5u);
TS_ASSERT_EQUALS(a.rfind('3'), 3u);
TS_ASSERT_EQUALS(a.rfind('1', 3), 1u);
TS_ASSERT_EQUALS(b.rfind('*'), Common::String::npos);
TS_ASSERT_EQUALS(b.rfind('*', 1), Common::String::npos);
}
void test_setChar() {
Common::String testString("123456");
testString.setChar('2', 0);