From 15c610a069c4f86f921791584fa0248a9e26b9c2 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Wed, 23 Jul 2008 16:33:53 +0000 Subject: Added convenience method String::makeUnique(); simplified String::operator=(char c); extended String unit tests svn-id: r33246 --- common/str.cpp | 17 +++++++++++------ common/str.h | 1 + 2 files changed, 12 insertions(+), 6 deletions(-) (limited to 'common') 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); -- cgit v1.2.3