aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/str.cpp20
-rw-r--r--common/str.h2
-rw-r--r--test/common/str.h6
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() );