diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/str.cpp | 20 | ||||
-rw-r--r-- | common/str.h | 2 |
2 files changed, 22 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: |