* Added desperately missing Trim() method.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@32035 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2009-07-31 21:13:19 +00:00
parent 8bf23e3caa
commit e90b90daf6
2 changed files with 45 additions and 4 deletions

View File

@ -1,7 +1,7 @@
/* /*
* Copyright 2001-2009, Haiku Inc. All Rights Reserved. * Copyright 2001-2009, Haiku Inc. All Rights Reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#ifndef __BSTRING__ #ifndef __BSTRING__
#define __BSTRING__ #define __BSTRING__
@ -219,13 +219,17 @@ public:
// Escaping and De-escaping // Escaping and De-escaping
BString& CharacterEscape(const char* original, BString& CharacterEscape(const char* original,
const char* setOfCharsToEscape, char escapeWith); const char* setOfCharsToEscape,
char escapeWith);
BString& CharacterEscape(const char* setOfCharsToEscape, BString& CharacterEscape(const char* setOfCharsToEscape,
char escapeWith); char escapeWith);
BString& CharacterDeescape(const char* original, BString& CharacterDeescape(const char* original,
char escapeChar); char escapeChar);
BString& CharacterDeescape(char escapeChar); BString& CharacterDeescape(char escapeChar);
// Trimming
BString& Trim();
// Insert // Insert
BString& operator<<(const char* string); BString& operator<<(const char* string);
BString& operator<<(const BString& string); BString& operator<<(const BString& string);

View File

@ -10,6 +10,7 @@
* Julun <host.haiku@gmx.de> * Julun <host.haiku@gmx.de>
*/ */
/*! String class supporting common string operations. */ /*! String class supporting common string operations. */
@ -1613,6 +1614,42 @@ BString::CharacterDeescape(char escapeChar)
} }
// #pragma mark - Trimming
BString&
BString::Trim()
{
const char* string = String();
int32 startCount = 0;
while (isspace(string[startCount])) {
startCount++;
}
int32 endCount = 0;
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) {
_MakeWritable(length, true);
} else if (_MakeWritable() == B_OK) {
memmove(fPrivateData, fPrivateData + startCount, length);
fPrivateData[length] = '\0';
_SetLength(length);
}
return *this;
}
// #pragma mark - Insert // #pragma mark - Insert