aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/str.cpp17
-rw-r--r--common/str.h1
-rw-r--r--test/common/str.h34
3 files changed, 38 insertions, 14 deletions
diff --git a/common/str.cpp b/common/str.cpp
index 1b857546a8..14ba5ec530 100644
--- a/common/str.cpp
+++ b/common/str.cpp
@@ -170,7 +170,8 @@ String &String::operator =(const String &str) {
}
String& String::operator =(char c) {
- ensureCapacity(1, false);
+ decRefCount(_extern._refCount);
+ _str = _storage;
_len = 1;
_str[0] = c;
_str[1] = 0;
@@ -256,7 +257,7 @@ void String::deleteChar(uint32 p) {
// Call ensureCapacity to make sure we actually *own* the storage
// to which _str points to -- we wouldn't want to modify a storage
// which other string objects are sharing, after all.
- ensureCapacity(_len, true);
+ makeUnique();
while (p++ < _len)
_str[p-1] = _str[p];
_len--;
@@ -273,7 +274,7 @@ void String::clear() {
void String::setChar(char c, uint32 p) {
assert(p <= _len);
- ensureCapacity(_len, true);
+ makeUnique();
_str[p] = c;
}
@@ -289,18 +290,22 @@ void String::insertChar(char c, uint32 p) {
void String::toLowercase() {
// Ensure that the string is not shared
- ensureCapacity(_len, true);
+ makeUnique();
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);
+ makeUnique();
for (uint32 i = 0; i < _len; ++i)
_str[i] = toupper(_str[i]);
}
+void String::makeUnique() {
+ ensureCapacity(_len, true);
+}
+
/**
* Ensure that enough storage is available to store at least new_len
* characters plus a null byte. In addition, if we currently share
@@ -370,7 +375,7 @@ void String::trim() {
return;
// Ensure that the string is not shared
- ensureCapacity(_len, true);
+ makeUnique();
// Trim trailing whitespace
while (_len >= 1 && isspace(_str[_len-1]))
diff --git a/common/str.h b/common/str.h
index 619d295f14..3479fee8e4 100644
--- a/common/str.h
+++ b/common/str.h
@@ -202,6 +202,7 @@ public:
}
protected:
+ void makeUnique();
void ensureCapacity(uint32 new_len, bool keep_old);
void incRefCount() const;
void decRefCount(int *oldRefCount);
diff --git a/test/common/str.h b/test/common/str.h
index 7fddd0e21d..72d4df6f61 100644
--- a/test/common/str.h
+++ b/test/common/str.h
@@ -61,43 +61,61 @@ class StringTestSuite : public CxxTest::TestSuite
}
void test_refCount(void) {
+ // using internal storage
Common::String foo1("foo");
- Common::String foo2("foo");
+ Common::String foo2(foo1);
Common::String foo3(foo2);
foo3 += 'X';
- TS_ASSERT_EQUALS(foo2, foo1);
+ TS_ASSERT_EQUALS(foo1, "foo");
TS_ASSERT_EQUALS(foo2, "foo");
TS_ASSERT_EQUALS(foo3, "foo""X");
+ foo2 = 'x';
+ TS_ASSERT_EQUALS(foo1, "foo");
+ TS_ASSERT_EQUALS(foo2, "x");
+ TS_ASSERT_EQUALS(foo3, "foo""X");
}
void test_refCount2(void) {
+ // using external storage
Common::String foo1("fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
- Common::String foo2("fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
+ Common::String foo2(foo1);
Common::String foo3(foo2);
foo3 += 'X';
- TS_ASSERT_EQUALS(foo2, foo1);
+ TS_ASSERT_EQUALS(foo1, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
TS_ASSERT_EQUALS(foo2, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
TS_ASSERT_EQUALS(foo3, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd""X");
+ foo2 = 'x';
+ TS_ASSERT_EQUALS(foo1, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
+ TS_ASSERT_EQUALS(foo2, "x");
+ TS_ASSERT_EQUALS(foo3, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd""X");
}
void test_refCount3(void) {
Common::String foo1("0123456789abcdefghijk");
- Common::String foo2("0123456789abcdefghijk");
+ Common::String foo2(foo1);
Common::String foo3(foo2);
foo3 += "0123456789abcdefghijk";
- TS_ASSERT_EQUALS(foo2, foo1);
+ TS_ASSERT_EQUALS(foo1, foo2);
TS_ASSERT_EQUALS(foo2, "0123456789abcdefghijk");
TS_ASSERT_EQUALS(foo3, "0123456789abcdefghijk""0123456789abcdefghijk");
+ foo2 = 'x';
+ TS_ASSERT_EQUALS(foo1, "0123456789abcdefghijk");
+ TS_ASSERT_EQUALS(foo2, "x");
+ TS_ASSERT_EQUALS(foo3, "0123456789abcdefghijk""0123456789abcdefghijk");
}
void test_refCount4(void) {
Common::String foo1("fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
- Common::String foo2("fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
+ Common::String foo2(foo1);
Common::String foo3(foo2);
foo3 += "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd";
- TS_ASSERT_EQUALS(foo2, foo1);
+ TS_ASSERT_EQUALS(foo1, foo2);
TS_ASSERT_EQUALS(foo2, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
TS_ASSERT_EQUALS(foo3, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd""fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
+ foo2 = 'x';
+ TS_ASSERT_EQUALS(foo1, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
+ TS_ASSERT_EQUALS(foo2, "x");
+ TS_ASSERT_EQUALS(foo3, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd""fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
}
void test_hasPrefix(void) {