Switch to iswspace. Fix space detection at the right. Thanks Jérôme and Ingo. Please review, though.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@41084 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Clemens Zeidler
2011-03-22 20:38:13 +00:00
parent e8ac67ca07
commit b5ddb5072e
+13 -11
View File
@@ -14,15 +14,14 @@
/*! String class supporting common string operations. */ /*! String class supporting common string operations. */
#include <String.h>
#include <ctype.h> #include <ctype.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <wctype.h>
#include <Debug.h> #include <Debug.h>
#include <String.h>
#include <utf8_functions.h> #include <utf8_functions.h>
@@ -1964,27 +1963,30 @@ BString::CharacterDeescape(char escapeChar)
BString& BString&
BString::Trim() BString::Trim()
{ {
if (Length() <= 0) size_t originalLength = Length();
if (originalLength <= 0)
return *this; return *this;
const char* string = String(); const char* string = String();
int32 startCount = 0; // string is \0 terminated thus we don't need to check if we reached the end
while (isspace(string[startCount])) uint32 startCount = 0;
while (iswspace(string[startCount]))
startCount++; startCount++;
int32 endCount = 0; uint32 endCount = 0;
while (isspace(string[Length() - endCount - 1])) while (endCount < originalLength - startCount
&& iswspace(string[originalLength - endCount - 1])) {
endCount++; endCount++;
}
if (startCount == 0 && endCount == 0) if (startCount == 0 && endCount == 0)
return *this; return *this;
// We actually need to trim // We actually need to trim
ssize_t length = Length() - startCount - endCount; ssize_t length = originalLength - startCount - endCount;
if (length < 0) ASSERT(length >= 0);
length = 0;
if (startCount == 0 || length == 0) { if (startCount == 0 || length == 0) {
_MakeWritable(length, true); _MakeWritable(length, true);
} else if (_MakeWritable() == B_OK) { } else if (_MakeWritable() == B_OK) {