[Scummvm-cvs-logs] CVS: scummvm/common str.h,1.24,1.25 str.cpp,1.33,1.34

Max Horn fingolfin at users.sourceforge.net
Sat Jan 15 13:44:00 CET 2005


Update of /cvsroot/scummvm/scummvm/common
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23725

Modified Files:
	str.h str.cpp 
Log Message:
Get rid of the ConstString class

Index: str.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/str.h,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -d -r1.24 -r1.25
--- str.h	1 Jan 2005 16:08:50 -0000	1.24
+++ str.h	15 Jan 2005 21:42:59 -0000	1.25
@@ -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 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 @@
 		_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;

Index: str.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/str.cpp,v
retrieving revision 1.33
retrieving revision 1.34
diff -u -d -r1.33 -r1.34
--- str.cpp	1 Jan 2005 16:08:50 -0000	1.33
+++ str.cpp	15 Jan 2005 21:42:59 -0000	1.34
@@ -27,7 +27,8 @@
 
 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 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 @@
 
 #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 @@
 
 #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;
 }
 





More information about the Scummvm-git-logs mailing list