From 37ffb53fb4aac12c5e091b87bd5746b85d5a0b79 Mon Sep 17 00:00:00 2001 From: Clemens Zeidler Date: Tue, 22 Mar 2011 03:05:39 +0000 Subject: [PATCH] Fix Trim() method for empty strings and strings only containing spaces. Fixes #7392. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@41076 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kits/support/String.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/kits/support/String.cpp b/src/kits/support/String.cpp index 1e9d087917..403a9060e4 100644 --- a/src/kits/support/String.cpp +++ b/src/kits/support/String.cpp @@ -1964,25 +1964,28 @@ BString::CharacterDeescape(char escapeChar) BString& BString::Trim() { + if (Length() <= 0) + return *this; + const char* string = String(); int32 startCount = 0; - while (isspace(string[startCount])) { + while (isspace(string[startCount])) startCount++; - } int32 endCount = 0; - while (isspace(string[Length() - endCount - 1])) { + while (isspace(string[Length() - endCount - 1])) endCount++; - } if (startCount == 0 && endCount == 0) return *this; // We actually need to trim - size_t length = Length() - startCount - endCount; - if (startCount == 0) { + ssize_t length = Length() - startCount - endCount; + if (length < 0) + length = 0; + if (startCount == 0 || length == 0) { _MakeWritable(length, true); } else if (_MakeWritable() == B_OK) { memmove(fPrivateData, fPrivateData + startCount, length);