diff options
author | Max Horn | 2008-07-22 14:39:26 +0000 |
---|---|---|
committer | Max Horn | 2008-07-22 14:39:26 +0000 |
commit | 705c92ddc33b050fd982aab728ef3eb7303e0af3 (patch) | |
tree | 3499f2395202128c051f5cd78e5c368606eefb55 | |
parent | c92f154b90ef5c09e500978d6e60f9d8f2db1868 (diff) | |
download | scummvm-rg350-705c92ddc33b050fd982aab728ef3eb7303e0af3.tar.gz scummvm-rg350-705c92ddc33b050fd982aab728ef3eb7303e0af3.tar.bz2 scummvm-rg350-705c92ddc33b050fd982aab728ef3eb7303e0af3.zip |
Added String::trim() method
svn-id: r33203
-rw-r--r-- | common/str.cpp | 20 | ||||
-rw-r--r-- | common/str.h | 2 | ||||
-rw-r--r-- | test/common/str.h | 6 |
3 files changed, 28 insertions, 0 deletions
diff --git a/common/str.cpp b/common/str.cpp index a2e6e0c66d..f7cb84aa05 100644 --- a/common/str.cpp +++ b/common/str.cpp @@ -363,6 +363,26 @@ void String::ensureCapacity(uint32 new_len, bool keep_old) { } } +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()); } diff --git a/common/str.h b/common/str.h index ae9cb992b6..619d295f14 100644 --- a/common/str.h +++ b/common/str.h @@ -177,6 +177,8 @@ public: void toLowercase(); void toUppercase(); + void trim(); + uint hash() const; public: diff --git a/test/common/str.h b/test/common/str.h index 76574ea70f..e9a6eae422 100644 --- a/test/common/str.h +++ b/test/common/str.h @@ -16,6 +16,12 @@ class StringTestSuite : public CxxTest::TestSuite 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() ); |