From b5ddb5072e9a9f545adfc493e5395f6b8f2b1f1c Mon Sep 17 00:00:00 2001 From: Clemens Zeidler Date: Tue, 22 Mar 2011 20:38:13 +0000 Subject: [PATCH] =?UTF-8?q?Switch=20to=20iswspace.=20Fix=20space=20detecti?= =?UTF-8?q?on=20at=20the=20right.=20Thanks=20J=C3=A9r=C3=B4me=20and=20Ingo?= =?UTF-8?q?.=20Please=20review,=20though.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@41084 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kits/support/String.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/kits/support/String.cpp b/src/kits/support/String.cpp index 403a9060e4..ffa7e58190 100644 --- a/src/kits/support/String.cpp +++ b/src/kits/support/String.cpp @@ -14,15 +14,14 @@ /*! String class supporting common string operations. */ - -#include - #include #include #include #include +#include #include +#include #include @@ -1964,27 +1963,30 @@ BString::CharacterDeescape(char escapeChar) BString& BString::Trim() { - if (Length() <= 0) + size_t originalLength = Length(); + if (originalLength <= 0) return *this; const char* string = String(); - int32 startCount = 0; - while (isspace(string[startCount])) + // string is \0 terminated thus we don't need to check if we reached the end + uint32 startCount = 0; + while (iswspace(string[startCount])) startCount++; - int32 endCount = 0; - while (isspace(string[Length() - endCount - 1])) + uint32 endCount = 0; + while (endCount < originalLength - startCount + && iswspace(string[originalLength - endCount - 1])) { endCount++; + } if (startCount == 0 && endCount == 0) return *this; // We actually need to trim - ssize_t length = Length() - startCount - endCount; - if (length < 0) - length = 0; + ssize_t length = originalLength - startCount - endCount; + ASSERT(length >= 0); if (startCount == 0 || length == 0) { _MakeWritable(length, true); } else if (_MakeWritable() == B_OK) {