diff options
-rw-r--r-- | common/str.cpp | 5 | ||||
-rw-r--r-- | test/common/str.h | 86 |
2 files changed, 51 insertions, 40 deletions
diff --git a/common/str.cpp b/common/str.cpp index f7cb84aa05..1b857546a8 100644 --- a/common/str.cpp +++ b/common/str.cpp @@ -288,12 +288,14 @@ void String::insertChar(char c, uint32 p) { } void String::toLowercase() { + // Ensure that the string is not shared ensureCapacity(_len, true); for (uint32 i = 0; i < _len; ++i) _str[i] = tolower(_str[i]); } void String::toUppercase() { + // Ensure that the string is not shared ensureCapacity(_len, true); for (uint32 i = 0; i < _len; ++i) _str[i] = toupper(_str[i]); @@ -367,6 +369,9 @@ void String::trim() { if (_len == 0) return; + // Ensure that the string is not shared + ensureCapacity(_len, true); + // Trim trailing whitespace while (_len >= 1 && isspace(_str[_len-1])) _len--; diff --git a/test/common/str.h b/test/common/str.h index e9a6eae422..7fddd0e21d 100644 --- a/test/common/str.h +++ b/test/common/str.h @@ -7,55 +7,57 @@ class StringTestSuite : public CxxTest::TestSuite public: void test_constructors(void) { Common::String str("test-string"); - TS_ASSERT( str == "test-string" ); + TS_ASSERT( str == "test-string"); str = Common::String(str.c_str()+5, 3); - TS_ASSERT( str == "str" ); + TS_ASSERT( str == "str"); str = "test-string"; - TS_ASSERT( str == "test-string" ); + TS_ASSERT( str == "test-string"); str = Common::String(str.c_str()+5, str.c_str()+8); - TS_ASSERT( str == "str" ); + TS_ASSERT( str == "str"); } void test_trim(void) { Common::String str(" This is a s tring with spaces "); + Common::String str2 = str; str.trim(); - TS_ASSERT( str == "This is a s tring with spaces" ); + TS_ASSERT( str == "This is a s tring with spaces"); + TS_ASSERT( str2 == " This is a s tring with spaces "); } void test_empty_clear(void) { Common::String str("test"); - TS_ASSERT( !str.empty() ); + TS_ASSERT( !str.empty()); str.clear(); - TS_ASSERT( str.empty() ); + TS_ASSERT( str.empty()); } void test_lastChar(void) { Common::String str; - TS_ASSERT_EQUALS( str.lastChar(), '\0' ); + TS_ASSERT_EQUALS(str.lastChar(), '\0'); str = "test"; - TS_ASSERT_EQUALS( str.lastChar(), 't' ); + TS_ASSERT_EQUALS(str.lastChar(), 't'); Common::String str2("bar"); - TS_ASSERT_EQUALS( str2.lastChar(), 'r' ); + TS_ASSERT_EQUALS(str2.lastChar(), 'r'); } void test_concat1(void) { Common::String str("foo"); Common::String str2("bar"); str += str2; - TS_ASSERT_EQUALS( str, "foobar" ); - TS_ASSERT_EQUALS( str2, "bar" ); + TS_ASSERT_EQUALS(str, "foobar"); + TS_ASSERT_EQUALS(str2, "bar"); } void test_concat2(void) { Common::String str("foo"); str += "bar"; - TS_ASSERT_EQUALS( str, "foobar" ); + TS_ASSERT_EQUALS(str, "foobar"); } void test_concat3(void) { Common::String str("foo"); str += 'X'; - TS_ASSERT_EQUALS( str, "fooX" ); + TS_ASSERT_EQUALS(str, "fooX"); } void test_refCount(void) { @@ -63,9 +65,9 @@ class StringTestSuite : public CxxTest::TestSuite Common::String foo2("foo"); Common::String foo3(foo2); foo3 += 'X'; - TS_ASSERT_EQUALS( foo2, foo1 ); - TS_ASSERT_EQUALS( foo2, "foo" ); - TS_ASSERT_EQUALS( foo3, "foo""X" ); + TS_ASSERT_EQUALS(foo2, foo1); + TS_ASSERT_EQUALS(foo2, "foo"); + TS_ASSERT_EQUALS(foo3, "foo""X"); } void test_refCount2(void) { @@ -73,9 +75,9 @@ class StringTestSuite : public CxxTest::TestSuite Common::String foo2("fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd"); Common::String foo3(foo2); foo3 += 'X'; - TS_ASSERT_EQUALS( foo2, foo1 ); - TS_ASSERT_EQUALS( foo2, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd" ); - TS_ASSERT_EQUALS( foo3, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd""X" ); + TS_ASSERT_EQUALS(foo2, foo1); + TS_ASSERT_EQUALS(foo2, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd"); + TS_ASSERT_EQUALS(foo3, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd""X"); } void test_refCount3(void) { @@ -83,9 +85,9 @@ class StringTestSuite : public CxxTest::TestSuite Common::String foo2("0123456789abcdefghijk"); Common::String foo3(foo2); foo3 += "0123456789abcdefghijk"; - TS_ASSERT_EQUALS( foo2, foo1 ); - TS_ASSERT_EQUALS( foo2, "0123456789abcdefghijk" ); - TS_ASSERT_EQUALS( foo3, "0123456789abcdefghijk""0123456789abcdefghijk" ); + TS_ASSERT_EQUALS(foo2, foo1); + TS_ASSERT_EQUALS(foo2, "0123456789abcdefghijk"); + TS_ASSERT_EQUALS(foo3, "0123456789abcdefghijk""0123456789abcdefghijk"); } void test_refCount4(void) { @@ -93,44 +95,48 @@ class StringTestSuite : public CxxTest::TestSuite Common::String foo2("fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd"); Common::String foo3(foo2); foo3 += "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd"; - TS_ASSERT_EQUALS( foo2, foo1 ); - TS_ASSERT_EQUALS( foo2, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd" ); - TS_ASSERT_EQUALS( foo3, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd""fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd" ); + TS_ASSERT_EQUALS(foo2, foo1); + TS_ASSERT_EQUALS(foo2, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd"); + TS_ASSERT_EQUALS(foo3, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd""fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd"); } void test_hasPrefix(void) { Common::String str("this/is/a/test, haha"); - TS_ASSERT_EQUALS( str.hasPrefix(""), true ); - TS_ASSERT_EQUALS( str.hasPrefix("this"), true ); - TS_ASSERT_EQUALS( str.hasPrefix("thit"), false ); - TS_ASSERT_EQUALS( str.hasPrefix("foo"), false ); + TS_ASSERT_EQUALS(str.hasPrefix(""), true); + TS_ASSERT_EQUALS(str.hasPrefix("this"), true); + TS_ASSERT_EQUALS(str.hasPrefix("thit"), false); + TS_ASSERT_EQUALS(str.hasPrefix("foo"), false); } void test_hasSuffix(void) { Common::String str("this/is/a/test, haha"); - TS_ASSERT_EQUALS( str.hasSuffix(""), true ); - TS_ASSERT_EQUALS( str.hasSuffix("haha"), true ); - TS_ASSERT_EQUALS( str.hasSuffix("hahb"), false ); - TS_ASSERT_EQUALS( str.hasSuffix("hahah"), false ); + TS_ASSERT_EQUALS(str.hasSuffix(""), true); + TS_ASSERT_EQUALS(str.hasSuffix("haha"), true); + TS_ASSERT_EQUALS(str.hasSuffix("hahb"), false); + TS_ASSERT_EQUALS(str.hasSuffix("hahah"), false); } void test_contains(void) { Common::String str("this/is/a/test, haha"); - TS_ASSERT_EQUALS( str.contains(""), true ); - TS_ASSERT_EQUALS( str.contains("haha"), true ); - TS_ASSERT_EQUALS( str.contains("hahb"), false ); - TS_ASSERT_EQUALS( str.contains("test"), true ); + TS_ASSERT_EQUALS(str.contains(""), true); + TS_ASSERT_EQUALS(str.contains("haha"), true); + TS_ASSERT_EQUALS(str.contains("hahb"), false); + TS_ASSERT_EQUALS(str.contains("test"), true); } void test_toLowercase(void) { Common::String str("Test it, NOW! 42"); + Common::String str2 = str; str.toLowercase(); - TS_ASSERT_EQUALS( str, "test it, now! 42" ); + TS_ASSERT_EQUALS(str, "test it, now! 42"); + TS_ASSERT_EQUALS(str2, "Test it, NOW! 42"); } void test_toUppercase(void) { Common::String str("Test it, NOW! 42"); + Common::String str2 = str; str.toUppercase(); - TS_ASSERT_EQUALS( str, "TEST IT, NOW! 42" ); + TS_ASSERT_EQUALS(str, "TEST IT, NOW! 42"); + TS_ASSERT_EQUALS(str2, "Test it, NOW! 42"); } }; |