From e90b90daf6315ab2650ea947cfaa55f86e859591 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Fri, 31 Jul 2009 21:13:19 +0000 Subject: [PATCH] * Added desperately missing Trim() method. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@32035 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/os/support/String.h | 12 ++++++++---- src/kits/support/String.cpp | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/headers/os/support/String.h b/headers/os/support/String.h index 073d35641b..cb88b17230 100644 --- a/headers/os/support/String.h +++ b/headers/os/support/String.h @@ -1,7 +1,7 @@ /* -* Copyright 2001-2009, Haiku Inc. All Rights Reserved. -* Distributed under the terms of the MIT License. -*/ + * Copyright 2001-2009, Haiku Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + */ #ifndef __BSTRING__ #define __BSTRING__ @@ -219,13 +219,17 @@ public: // Escaping and De-escaping BString& CharacterEscape(const char* original, - const char* setOfCharsToEscape, char escapeWith); + const char* setOfCharsToEscape, + char escapeWith); BString& CharacterEscape(const char* setOfCharsToEscape, char escapeWith); BString& CharacterDeescape(const char* original, char escapeChar); BString& CharacterDeescape(char escapeChar); + // Trimming + BString& Trim(); + // Insert BString& operator<<(const char* string); BString& operator<<(const BString& string); diff --git a/src/kits/support/String.cpp b/src/kits/support/String.cpp index cf3986412d..cacfb218d8 100644 --- a/src/kits/support/String.cpp +++ b/src/kits/support/String.cpp @@ -10,6 +10,7 @@ * Julun */ + /*! 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