[Scummvm-cvs-logs] SF.net SVN: scummvm:[33203] scummvm/trunk

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Tue Jul 22 16:39:27 CEST 2008


Revision: 33203
          http://scummvm.svn.sourceforge.net/scummvm/?rev=33203&view=rev
Author:   fingolfin
Date:     2008-07-22 14:39:26 +0000 (Tue, 22 Jul 2008)

Log Message:
-----------
Added String::trim() method

Modified Paths:
--------------
    scummvm/trunk/common/str.cpp
    scummvm/trunk/common/str.h
    scummvm/trunk/test/common/str.h

Modified: scummvm/trunk/common/str.cpp
===================================================================
--- scummvm/trunk/common/str.cpp	2008-07-22 14:38:54 UTC (rev 33202)
+++ scummvm/trunk/common/str.cpp	2008-07-22 14:39:26 UTC (rev 33203)
@@ -363,6 +363,26 @@
 	}
 }
 
+void String::trim() {
+	if (_len == 0)
+		return;
+
+	// Trim trailing whitespace
+	while (_len >= 1 && isspace(_str[_len-1]))
+		_len--;
+	_str[_len] = 0;
+
+	// Trim leading whitespace
+	char *t = _str;
+	while (isspace(*t))
+		t++;
+
+	if (t != _str) {
+		_len -= t - _str;
+		memmove(_str, t, _len + 1);
+	}
+}
+
 uint String::hash() const {
 	return hashit(c_str());
 }

Modified: scummvm/trunk/common/str.h
===================================================================
--- scummvm/trunk/common/str.h	2008-07-22 14:38:54 UTC (rev 33202)
+++ scummvm/trunk/common/str.h	2008-07-22 14:39:26 UTC (rev 33203)
@@ -177,6 +177,8 @@
 	void toLowercase();
 	void toUppercase();
 	
+	void trim();
+
 	uint hash() const;
 
 public:

Modified: scummvm/trunk/test/common/str.h
===================================================================
--- scummvm/trunk/test/common/str.h	2008-07-22 14:38:54 UTC (rev 33202)
+++ scummvm/trunk/test/common/str.h	2008-07-22 14:39:26 UTC (rev 33203)
@@ -16,6 +16,12 @@
 		TS_ASSERT( str == "str" );
 	}
 
+	void test_trim(void) {
+		Common::String str("  This is a s tring with spaces  ");
+		str.trim();
+		TS_ASSERT( str == "This is a s tring with spaces" );
+	}
+
 	void test_empty_clear(void) {
 		Common::String str("test");
 		TS_ASSERT( !str.empty() );


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list