diff options
author | Max Horn | 2005-01-15 21:42:59 +0000 |
---|---|---|
committer | Max Horn | 2005-01-15 21:42:59 +0000 |
commit | fcad363886dd2d79bc39058ae5affe2b3098352e (patch) | |
tree | 8979bc765164c0019db91b9e6a433766929e6f94 /common | |
parent | f7c14e04d275015b257c88ae2fb9758bcc8803a1 (diff) | |
download | scummvm-rg350-fcad363886dd2d79bc39058ae5affe2b3098352e.tar.gz scummvm-rg350-fcad363886dd2d79bc39058ae5affe2b3098352e.tar.bz2 scummvm-rg350-fcad363886dd2d79bc39058ae5affe2b3098352e.zip |
Get rid of the ConstString class
svn-id: r16564
Diffstat (limited to 'common')
-rw-r--r-- | common/str.cpp | 39 | ||||
-rw-r--r-- | common/str.h | 83 |
2 files changed, 36 insertions, 86 deletions
diff --git a/common/str.cpp b/common/str.cpp index 3dcf652be8..cbfdc4f029 100644 --- a/common/str.cpp +++ b/common/str.cpp @@ -27,7 +27,8 @@ namespace Common { const String String::emptyString; -String::String(const char *str, int len) { +String::String(const char *str, int len) + : _str(0), _len(0) { _refCount = new int(1); if (str && len != 0) { if (len > 0) @@ -43,20 +44,8 @@ String::String(const char *str, int len) { } } -String::String(const ConstString &str) { - printf("String::String(const ConstString &str)\n"); - _refCount = new int(1); - if (str._str) { - _capacity = _len = strlen(str._str); - _str = (char *)calloc(1, _capacity+1); - memcpy(_str, str._str, _len+1); - } else { - _capacity = _len = 0; - _str = 0; - } -} - -String::String(const String &str) : ConstString() { +String::String(const String &str) + : _str(0), _len(0) { ++(*str._refCount); _refCount = str._refCount; @@ -225,37 +214,37 @@ void String::ensureCapacity(int new_len, bool keep_old) { #pragma mark - -bool ConstString::operator ==(const ConstString &x) const { +bool String::operator ==(const String &x) const { return (0 == strcmp(c_str(), x.c_str())); } -bool ConstString::operator ==(const char *x) const { +bool String::operator ==(const char *x) const { assert(x != 0); return (0 == strcmp(c_str(), x)); } -bool ConstString::operator !=(const ConstString &x) const { +bool String::operator !=(const String &x) const { return (0 != strcmp(c_str(), x.c_str())); } -bool ConstString::operator !=(const char *x) const { +bool String::operator !=(const char *x) const { assert(x != 0); return (0 != strcmp(c_str(), x)); } -bool ConstString::operator < (const ConstString &x) const { +bool String::operator < (const String &x) const { return strcmp(c_str(), x.c_str()) < 0; } -bool ConstString::operator <= (const ConstString &x) const { +bool String::operator <= (const String &x) const { return strcmp(c_str(), x.c_str()) <= 0; } -bool ConstString::operator > (const ConstString &x) const { +bool String::operator > (const String &x) const { return (x < *this); } -bool ConstString::operator >= (const ConstString &x) const { +bool String::operator >= (const String &x) const { return (x <= *this); } @@ -281,11 +270,11 @@ String operator +(const String &x, const char *y) { #pragma mark - -bool operator == (const char* y, const ConstString &x) { +bool operator == (const char* y, const String &x) { return (x == y); } -bool operator != (const char* y, const ConstString &x) { +bool operator != (const char* y, const String &x) { return x != y; } diff --git a/common/str.h b/common/str.h index c452fa5106..5a96f7939e 100644 --- a/common/str.h +++ b/common/str.h @@ -29,77 +29,43 @@ namespace Common { -/* - TODO - Add a class ConstString which is a light weight base class of String. - It will be immutable, and *not* copy the char pointer it gets when created. - Only constructor: ConstString(const char *ptr) - Then whenever you now use "const String &" in a parameter, use "const ConstString &" - instead (mayhaps make a typedef even). Thus, parameters are passed w/o - causing a free/malloc. Then only for permanent storage, when we assign it to a - real String object, will a malloc be issued (to this end, make String aware of - ConstString ?!? -*/ - -class ConstString { - friend class String; +class String { protected: char *_str; - int _len; - -public: - ConstString() : _str(0), _len(0) {} -// ConstString(const char *str, int len = -1) : _str((char *)str) { _len = str ? (len >= 0 ? len : strlen(str)) : 0; } - virtual ~ConstString() {} - - bool operator ==(const ConstString &x) const; - bool operator ==(const char *x) const; - bool operator !=(const ConstString &x) const; - bool operator !=(const char *x) const; - bool operator <(const ConstString &x) const; - bool operator <=(const ConstString &x) const; - bool operator >(const ConstString &x) const; - bool operator >=(const ConstString &x) const; - - char operator [](int idx) const { - assert(_str && idx >= 0 && idx < _len); - return _str[idx]; - } - - const char *c_str() const { return _str ? _str : ""; } - uint size() const { return _len; } - - bool isEmpty() const { return (_len == 0); } - char lastChar() const { return (_len > 0) ? _str[_len-1] : 0; } -}; - -class String : public ConstString { -protected: - int *_refCount; - int _capacity; + int _len; + int *_refCount; + int _capacity; public: static const String emptyString; - String() : _capacity(0) { _refCount = new int(1); } + String() : _str(0), _len(0), _capacity(0) { _refCount = new int(1); } String(const char *str, int len = -1); - String(const ConstString &str); String(const String &str); virtual ~String(); String &operator =(const char *str); -// TODO - We should use RTTI here - that is, not real C++ RTTI but maybe some magic -// constant in each string object. We want to be able to optimize the case when -// a real 'String' object is passed to a function as a ConstString obj and then -// assigned to a 'String' object. -// An alternative would be to add private clone() and cloneMutable methods that -// would do the right thing. String &operator =(const String &str); String &operator =(char c); String &operator +=(const char *str); String &operator +=(const String &str); String &operator +=(char c); + bool operator ==(const String &x) const; + bool operator ==(const char *x) const; + bool operator !=(const String &x) const; + bool operator !=(const char *x) const; + bool operator <(const String &x) const; + bool operator <=(const String &x) const; + bool operator >(const String &x) const; + bool operator >=(const String &x) const; + + const char *c_str() const { return _str ? _str : ""; } + uint size() const { return _len; } + + bool isEmpty() const { return (_len == 0); } + char lastChar() const { return (_len > 0) ? _str[_len-1] : 0; } + char operator [](int idx) const { assert(_str && idx >= 0 && idx < _len); return _str[idx]; @@ -129,8 +95,8 @@ String operator +(const char *x, const String &y); String operator +(const String &x, const char *y); // Some useful additional comparision operators for Strings -bool operator == (const char *x, const ConstString &y); -bool operator != (const char *x, const ConstString &y); +bool operator == (const char *x, const String &y); +bool operator != (const char *x, const String &y); class StringList : public Array<String> { public: @@ -139,11 +105,6 @@ public: _data[_size++] = str; } - void push_back(const ConstString &str) { - ensureCapacity(_size + 1); - _data[_size++] = str; - } - void push_back(const String &str) { ensureCapacity(_size + 1); _data[_size++] = str; |