Author: czeidler Date: 2011-03-22 04:05:39 +0100 (Tue, 22 Mar 2011) New Revision: 41076 Changeset: https://dev.haiku-os.org/changeset/41076 Ticket: https://dev.haiku-os.org/ticket/7392 Modified: haiku/trunk/src/kits/support/String.cpp Log: Fix Trim() method for empty strings and strings only containing spaces. Fixes #7392. Modified: haiku/trunk/src/kits/support/String.cpp =================================================================== --- haiku/trunk/src/kits/support/String.cpp 2011-03-22 01:35:38 UTC (rev 41075) +++ haiku/trunk/src/kits/support/String.cpp 2011-03-22 03:05:39 UTC (rev 41076) @@ -1964,25 +1964,28 @@ 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);