[Scummvm-cvs-logs] CVS: scummvm/common str.cpp,1.1,1.2 str.h,1.1,1.2

Max Horn fingolfin at users.sourceforge.net
Sun Sep 8 04:47:01 CEST 2002


Update of /cvsroot/scummvm/scummvm/common
In directory usw-pr-cvs1:/tmp/cvs-serv1536

Modified Files:
	str.cpp str.h 
Log Message:
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

Index: str.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/str.cpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- str.cpp	8 Sep 2002 01:08:11 -0000	1.1
+++ str.cpp	8 Sep 2002 11:46:42 -0000	1.2
@@ -129,12 +129,57 @@
 	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 @@
 	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 @@
 	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;
 }

Index: str.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/str.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- str.h	8 Sep 2002 01:08:11 -0000	1.1
+++ str.h	8 Sep 2002 11:46:42 -0000	1.2
@@ -37,17 +37,42 @@
  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 @@
 	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:





More information about the Scummvm-git-logs mailing list