aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorMax Horn2002-09-08 11:46:42 +0000
committerMax Horn2002-09-08 11:46:42 +0000
commit78f2f9e50556e46cf32f87c23c13be70ed4ea300 (patch)
treeaeae3f9771eacb2804bfbed5a57a2d73b2b87911 /common
parent523d8e64c0bc66b75cdd7f12fc9d9d87b28a8c36 (diff)
downloadscummvm-rg350-78f2f9e50556e46cf32f87c23c13be70ed4ea300.tar.gz
scummvm-rg350-78f2f9e50556e46cf32f87c23c13be70ed4ea300.tar.bz2
scummvm-rg350-78f2f9e50556e46cf32f87c23c13be70ed4ea300.zip
Added ConstString class; not yet used, but will allow us to reduce the overhead involved when passing in string constants as parameters taking a String ref right now
svn-id: r4904
Diffstat (limited to 'common')
-rw-r--r--common/str.cpp106
-rw-r--r--common/str.h53
2 files changed, 87 insertions, 72 deletions
diff --git a/common/str.cpp b/common/str.cpp
index 2594489371..8612a02ed3 100644
--- a/common/str.cpp
+++ b/common/str.cpp
@@ -129,12 +129,57 @@ String& String::operator +=(char c)
return *this;
}
-bool String::operator ==(const String& x) const
+void String::deleteLastChar() {
+ if (_len > 0) {
+ ensureCapacity(_len - 1, true);
+ _str[--_len] = 0;
+ }
+}
+
+void String::clear()
+{
+ if (_capacity) {
+ decRefCount();
+
+ _refCount = new int(1);
+ _capacity = 0;
+ _len = 0;
+ _str = 0;
+ }
+}
+
+void String::ensureCapacity(int new_len, bool keep_old)
+{
+ // If there is not enough space, or if we are not the only owner
+ // of the current data, then we have to reallocate it.
+ if (new_len <= _capacity && *_refCount == 1)
+ return;
+
+ int newCapacity = (new_len <= _capacity) ? _capacity : new_len + 32;
+ char *newStr = (char *)calloc(1, newCapacity+1);
+
+ if (keep_old && _str)
+ memcpy(newStr, _str, _len + 1);
+ else
+ _len = 0;
+
+ decRefCount();
+
+ _refCount = new int(1);
+ _capacity = newCapacity;
+ _str = newStr;
+}
+
+
+#pragma mark -
+
+
+bool ConstString::operator ==(const ConstString& x) const
{
return (_len == x._len) && ((_len == 0) || (0 == strcmp(_str, x._str)));
}
-bool String::operator ==(const char* x) const
+bool ConstString::operator ==(const char* x) const
{
if (_str == 0)
return (x == 0) || (*x == 0);
@@ -143,12 +188,12 @@ bool String::operator ==(const char* x) const
return (0 != strcmp(_str, x));
}
-bool String::operator !=(const String& x) const
+bool ConstString::operator !=(const ConstString& x) const
{
return (_len != x._len) || ((_len != 0) && (0 != strcmp(_str, x._str)));
}
-bool String::operator !=(const char* x) const
+bool ConstString::operator !=(const char* x) const
{
if (_str == 0)
return (x != 0) && (*x != 0);
@@ -157,77 +202,36 @@ bool String::operator !=(const char* x) const
return (0 == strcmp(_str, x));
}
-bool String::operator < (const String& x) const
+bool ConstString::operator < (const ConstString& x) const
{
if (!_len || !x._len) // Any or both particpants are empty?
return !_len && x._len; // Less only if this string is empty and the other isn't
return strcmp(_str, x._str) < 0;
}
-bool String::operator <= (const String& x) const
+bool ConstString::operator <= (const ConstString& x) const
{
if (!_len || !x._len) // Any or both particpants are empty?
return !_len; // Less or equal unless the other string is empty and this one isn't
return strcmp(_str, x._str) <= 0;
}
-bool String::operator > (const String& x) const
+bool ConstString::operator > (const ConstString& x) const
{
return (x < *this);
}
-bool String::operator >= (const String& x) const
+bool ConstString::operator >= (const ConstString& x) const
{
return (x <= *this);
}
-void String::deleteLastChar() {
- if (_len > 0) {
- ensureCapacity(_len - 1, true);
- _str[--_len] = 0;
- }
-}
-
-void String::clear()
-{
- if (_capacity) {
- decRefCount();
-
- _refCount = new int(1);
- _capacity = 0;
- _len = 0;
- _str = 0;
- }
-}
-
-void String::ensureCapacity(int new_len, bool keep_old)
-{
- // If there is not enough space, or if we are not the only owner
- // of the current data, then we have to reallocate it.
- if (new_len <= _capacity && *_refCount == 1)
- return;
-
- int newCapacity = (new_len <= _capacity) ? _capacity : new_len + 32;
- char *newStr = (char *)calloc(1, newCapacity+1);
-
- if (keep_old && _str)
- memcpy(newStr, _str, _len + 1);
- else
- _len = 0;
-
- decRefCount();
-
- _refCount = new int(1);
- _capacity = newCapacity;
- _str = newStr;
-}
-
-bool operator == (const char* y, const String& x)
+bool operator == (const char* y, const ConstString& x)
{
return (x == y);
}
-bool operator != (const char* y, const String& x)
+bool operator != (const char* y, const ConstString& x)
{
return x != y;
}
diff --git a/common/str.h b/common/str.h
index 0446782aa7..fdcd70a154 100644
--- a/common/str.h
+++ b/common/str.h
@@ -37,17 +37,42 @@ namespace ScummVM {
real String object, will a malloc be issued (to this end, make String aware of
ConstString ?!?
*/
-class String {
+
+class ConstString {
+protected:
+ char *_str;
+ int _len;
+
+public:
+ ConstString() : _str(0), _len(0) {}
+ ConstString(const char *str) : _str((char*)str) { _len = str ? 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;
+
+ const char *c_str() const { return _str; }
+ int size() const { return _len; }
+
+ bool isEmpty() const { return (_len == 0); }
+};
+
+class String : public ConstString {
protected:
int *_refCount;
int _capacity;
- int _len;
- char *_str;
+
public:
- String() : _capacity(0), _len(0), _str(0) { _refCount = new int(1); }
+ String() : _capacity(0) { _refCount = new int(1); }
String(const char *str);
String(const String &str);
- ~String();
+ virtual ~String();
String& operator =(const char* str);
String& operator =(const String& str);
@@ -55,31 +80,17 @@ public:
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; }
- int size() const { return _len; }
-
void deleteLastChar();
void clear();
- bool isEmpty() const { return (_len == 0); }
-
protected:
void ensureCapacity(int new_len, bool keep_old);
void decRefCount();
};
// Some useful additional comparision operators for Strings
-bool operator == (const char* x, const String& y);
-bool operator != (const char* x, const String& y);
+bool operator == (const char* x, const ConstString& y);
+bool operator != (const char* x, const ConstString& y);
class StringList : public List<String> {
public: