[Scummvm-git-logs] scummvm master -> d136a5f54ceff2c6328235fbc477ab9107da4763

bluegr noreply at scummvm.org
Sat Nov 26 21:13:45 UTC 2022


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

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


Commit: d136a5f54ceff2c6328235fbc477ab9107da4763
    https://github.com/scummvm/scummvm/commit/d136a5f54ceff2c6328235fbc477ab9107da4763
Author: Sergio (sergio256256 at gmail.com)
Date: 2022-11-26T23:13:42+02:00

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

Changed paths:
    common/str.cpp
    test/common/str.h


diff --git a/common/str.cpp b/common/str.cpp
index cbd87bbdc9c..9994f209b32 100644
--- a/common/str.cpp
+++ b/common/str.cpp
@@ -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;
diff --git a/test/common/str.h b/test/common/str.h
index 9321f38bd72..a3952927659 100644
--- a/test/common/str.h
+++ b/test/common/str.h
@@ -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);




More information about the Scummvm-git-logs mailing list