diff --git a/headers/os/support/String.h b/headers/os/support/String.h index f5bf7e62ad..1b46f81903 100644 --- a/headers/os/support/String.h +++ b/headers/os/support/String.h @@ -1,360 +1,281 @@ -//------------------------------------------------------------------------------ -// Copyright (c) 2001-2003, OpenBeOS -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. -// -// File Name: String.h -// Author(s): Stefano Ceccherini (burton666@libero.it) -// Description: String class supporting common string operations. -//------------------------------------------------------------------------------ - - - +/* + * Copyright 2001-2007, Haiku Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + */ #ifndef __BSTRING__ #define __BSTRING__ + #include #include #include + class BString { -public: - BString(); - BString(const char *); - BString(const BString &); - BString(const char *, int32 maxLength); - - ~BString(); - -/*---- Access --------------------------------------------------------------*/ - const char *String() const; - /* returns null-terminated string */ + public: + BString(); + BString(const char* string); + BString(const BString& string); + BString(const char* string, int32 maxLength); + ~BString(); - int32 Length() const; - /* length of corresponding string */ + // Access + const char* String() const; - int32 CountChars() const; - /* returns number of UTF8 characters in string */ -/*---- Assignment ----------------------------------------------------------*/ - BString &operator=(const BString &); - BString &operator=(const char *); - BString &operator=(char); - - BString &SetTo(const char *); - BString &SetTo(const char *, int32 length); + int32 Length() const; + // length of corresponding string + int32 CountChars() const; + // returns number of UTF8 characters in string - BString &SetTo(const BString &from); - BString &Adopt(BString &from); - /* leaves empty, avoiding a copy */ - - BString &SetTo(const BString &, int32 length); - BString &Adopt(BString &from, int32 length); - /* leaves empty, avoiding a copy */ - - BString &SetTo(char, int32 count); + // Assignment + BString& operator=(const BString& string); + BString& operator=(const char* string); + BString& operator=(char c); -/*---- Substring copying ---------------------------------------------------*/ - BString &CopyInto(BString &into, int32 fromOffset, + BString& SetTo(const char* string); + BString& SetTo(const char* string, int32 maxLength); + + BString& SetTo(const BString& string); + BString& Adopt(BString& from); + + BString& SetTo(const BString& string, int32 maxLength); + BString& Adopt(BString& from, int32 maxLength); + + BString& SetTo(char c, int32 count); + + // Substring copying + BString& CopyInto(BString& into, int32 fromOffset, int32 length) const; - /* returns ref as it's result; doesn't do - * anything if is - */ - - void CopyInto(char *into, int32 fromOffset, + void CopyInto(char* into, int32 fromOffset, int32 length) const; - /* caller guarantees that is large enough */ -/*---- Appending -----------------------------------------------------------*/ - BString &operator+=(const BString &); - BString &operator+=(const char *); - BString &operator+=(char); - - BString &Append(const BString &); - BString &Append(const char *); + // Appending + BString& operator+=(const BString& string); + BString& operator+=(const char* string); + BString& operator+=(char c); - BString &Append(const BString &, int32 length); - BString &Append(const char *, int32 length); - BString &Append(char, int32 count); + BString& Append(const BString& string); + BString& Append(const char* string); -/*---- Prepending ----------------------------------------------------------*/ - BString &Prepend(const char *); - BString &Prepend(const BString &); - BString &Prepend(const char *, int32); - BString &Prepend(const BString &, int32); - BString &Prepend(char, int32 count); + BString& Append(const BString& string, int32 length); + BString& Append(const char* string, int32 length); + BString& Append(char c, int32 count); -/*---- Inserting ----------------------------------------------------------*/ - BString &Insert(const char *, int32 pos); - BString &Insert(const char *, int32 length, int32 pos); - BString &Insert(const char *, int32 fromOffset, + // Prepending + BString& Prepend(const char* string); + BString& Prepend(const BString& string); + BString& Prepend(const char* string, int32 length); + BString& Prepend(const BString& string, int32 length); + BString& Prepend(char c, int32 count); + + // Inserting + BString& Insert(const char* string, int32 pos); + BString& Insert(const char* string, int32 length, int32 pos); + BString& Insert(const char* string, int32 fromOffset, int32 length, int32 pos); - BString &Insert(const BString &, int32 pos); - BString &Insert(const BString &, int32 length, int32 pos); - BString &Insert(const BString &, int32 fromOffset, + BString& Insert(const BString& string, int32 pos); + BString& Insert(const BString& string, int32 length, int32 pos); + BString& Insert(const BString& string, int32 fromOffset, int32 length, int32 pos); - BString &Insert(char, int32 count, int32 pos); + BString& Insert(char, int32 count, int32 pos); -/*---- Removing -----------------------------------------------------------*/ - BString &Truncate(int32 newLength, bool lazy = true); - /* pass false in to ensure freeing up the - * truncated memory - */ - - BString &Remove(int32 from, int32 length); + // Removing + BString& Truncate(int32 newLength, bool lazy = true); + BString& Remove(int32 from, int32 length); - BString &RemoveFirst(const BString &); - BString &RemoveLast(const BString &); - BString &RemoveAll(const BString &); + BString& RemoveFirst(const BString& string); + BString& RemoveLast(const BString& string); + BString& RemoveAll(const BString& string); - BString &RemoveFirst(const char *); - BString &RemoveLast(const char *); - BString &RemoveAll(const char *); - - BString &RemoveSet(const char *setOfCharsToRemove); - - BString &MoveInto(BString &into, int32 from, int32 length); - void MoveInto(char *into, int32 from, int32 length); - /* caller guarantees that is large enough */ + BString& RemoveFirst(const char* string); + BString& RemoveLast(const char* string); + BString& RemoveAll(const char* string); + BString& RemoveSet(const char* setOfCharsToRemove); -/*---- Compare functions ---------------------------------------------------*/ - bool operator<(const BString &) const; - bool operator<=(const BString &) const; - bool operator==(const BString &) const; - bool operator>=(const BString &) const; - bool operator>(const BString &) const; - bool operator!=(const BString &) const; - - bool operator<(const char *) const; - bool operator<=(const char *) const; - bool operator==(const char *) const; - bool operator>=(const char *) const; - bool operator>(const char *) const; - bool operator!=(const char *) const; - -/*---- strcmp-style compare functions --------------------------------------*/ - int Compare(const BString &) const; - int Compare(const char *) const; - int Compare(const BString &, int32 n) const; - int Compare(const char *, int32 n) const; - int ICompare(const BString &) const; - int ICompare(const char *) const; - int ICompare(const BString &, int32 n) const; - int ICompare(const char *, int32 n) const; - -/*---- Searching -----------------------------------------------------------*/ - int32 FindFirst(const BString &) const; - int32 FindFirst(const char *) const; - int32 FindFirst(const BString &, int32 fromOffset) const; - int32 FindFirst(const char *, int32 fromOffset) const; - int32 FindFirst(char) const; - int32 FindFirst(char, int32 fromOffset) const; + BString& MoveInto(BString& into, int32 from, int32 length); + void MoveInto(char* into, int32 from, int32 length); - int32 FindLast(const BString &) const; - int32 FindLast(const char *) const; - int32 FindLast(const BString &, int32 beforeOffset) const; - int32 FindLast(const char *, int32 beforeOffset) const; - int32 FindLast(char) const; - int32 FindLast(char, int32 beforeOffset) const; + // Compare functions + bool operator<(const BString& string) const; + bool operator<=(const BString& string) const; + bool operator==(const BString& string) const; + bool operator>=(const BString& string) const; + bool operator>(const BString& string) const; + bool operator!=(const BString& string) const; - int32 IFindFirst(const BString &) const; - int32 IFindFirst(const char *) const; - int32 IFindFirst(const BString &, int32 fromOffset) const; - int32 IFindFirst(const char *, int32 fromOffset) const; + bool operator<(const char* string) const; + bool operator<=(const char* string) const; + bool operator==(const char* string) const; + bool operator>=(const char* string) const; + bool operator>(const char* string) const; + bool operator!=(const char* string) const; - int32 IFindLast(const BString &) const; - int32 IFindLast(const char *) const; - int32 IFindLast(const BString &, int32 beforeOffset) const; - int32 IFindLast(const char *, int32 beforeOffset) const; + // strcmp()-style compare functions + int Compare(const BString& string) const; + int Compare(const char* string) const; + int Compare(const BString& string, int32 length) const; + int Compare(const char* string, int32 length) const; + int ICompare(const BString& string) const; + int ICompare(const char* string) const; + int ICompare(const BString& string, int32 length) const; + int ICompare(const char* string, int32 length) const; -/*---- Replacing -----------------------------------------------------------*/ + // Searching + int32 FindFirst(const BString& string) const; + int32 FindFirst(const char* string) const; + int32 FindFirst(const BString& string, int32 fromOffset) const; + int32 FindFirst(const char* string, int32 fromOffset) const; + int32 FindFirst(char c) const; + int32 FindFirst(char c, int32 fromOffset) const; - BString &ReplaceFirst(char replaceThis, char withThis); - BString &ReplaceLast(char replaceThis, char withThis); - BString &ReplaceAll(char replaceThis, char withThis, + int32 FindLast(const BString& string) const; + int32 FindLast(const char* string) const; + int32 FindLast(const BString& string, int32 beforeOffset) const; + int32 FindLast(const char* string, int32 beforeOffset) const; + int32 FindLast(char c) const; + int32 FindLast(char c, int32 beforeOffset) const; + + int32 IFindFirst(const BString& string) const; + int32 IFindFirst(const char* string) const; + int32 IFindFirst(const BString& string, int32 fromOffset) const; + int32 IFindFirst(const char* string, int32 fromOffset) const; + + int32 IFindLast(const BString& string) const; + int32 IFindLast(const char* string) const; + int32 IFindLast(const BString& string, int32 beforeOffset) const; + int32 IFindLast(const char* string, int32 beforeOffset) const; + + // Replacing + BString& ReplaceFirst(char replaceThis, char withThis); + BString& ReplaceLast(char replaceThis, char withThis); + BString& ReplaceAll(char replaceThis, char withThis, int32 fromOffset = 0); - BString &Replace(char replaceThis, char withThis, + BString& Replace(char replaceThis, char withThis, int32 maxReplaceCount, int32 fromOffset = 0); - BString &ReplaceFirst(const char *replaceThis, - const char *withThis); - BString &ReplaceLast(const char *replaceThis, - const char *withThis); - BString &ReplaceAll(const char *replaceThis, - const char *withThis, int32 fromOffset = 0); - BString &Replace(const char *replaceThis, const char *withThis, + BString& ReplaceFirst(const char* replaceThis, + const char* withThis); + BString& ReplaceLast(const char* replaceThis, + const char* withThis); + BString& ReplaceAll(const char* replaceThis, + const char* withThis, int32 fromOffset = 0); + BString& Replace(const char* replaceThis, const char* withThis, int32 maxReplaceCount, int32 fromOffset = 0); - BString &IReplaceFirst(char replaceThis, char withThis); - BString &IReplaceLast(char replaceThis, char withThis); - BString &IReplaceAll(char replaceThis, char withThis, + BString& IReplaceFirst(char replaceThis, char withThis); + BString& IReplaceLast(char replaceThis, char withThis); + BString& IReplaceAll(char replaceThis, char withThis, int32 fromOffset = 0); - BString &IReplace(char replaceThis, char withThis, + BString& IReplace(char replaceThis, char withThis, int32 maxReplaceCount, int32 fromOffset = 0); - BString &IReplaceFirst(const char *replaceThis, - const char *withThis); - BString &IReplaceLast(const char *replaceThis, - const char *withThis); - BString &IReplaceAll(const char *replaceThis, - const char *withThis, int32 fromOffset = 0); - BString &IReplace(const char *replaceThis, const char *withThis, + BString& IReplaceFirst(const char* replaceThis, + const char* withThis); + BString& IReplaceLast(const char* replaceThis, + const char* withThis); + BString& IReplaceAll(const char* replaceThis, + const char* withThis, int32 fromOffset = 0); + BString& IReplace(const char* replaceThis, const char* withThis, int32 maxReplaceCount, int32 fromOffset = 0); - - BString &ReplaceSet(const char *setOfChars, char with); - BString &ReplaceSet(const char *setOfChars, const char *with); -/*---- Unchecked char access -----------------------------------------------*/ - char operator[](int32 index) const; - char &operator[](int32 index); -/*---- Checked char access -------------------------------------------------*/ - char ByteAt(int32 index) const; + BString& ReplaceSet(const char* setOfChars, char with); + BString& ReplaceSet(const char* setOfChars, const char *with); -/*---- Fast low-level manipulation -----------------------------------------*/ - char *LockBuffer(int32 maxLength); - - /* Make room for characters to be added by C-string like manipulation. - * Returns the equivalent of String(), includes space for - * trailing zero while used as C-string, it is illegal to call other - * BString routines that rely on data/length consistency until - * UnlockBuffer sets things up again. - */ + // Unchecked char access + char operator[](int32 index) const; + char& operator[](int32 index); - BString &UnlockBuffer(int32 length = -1); - - /* Finish using BString as C-string, adjusting length. If no length - * passed in, strlen of internal data is used to determine it. - * BString is in consistent state after this. - */ - -/*---- Upercase<->Lowercase ------------------------------------------------*/ - BString &ToLower(); - BString &ToUpper(); + // Checked char access + char ByteAt(int32 index) const; - BString &Capitalize(); - /* Converts first character to upper-case, rest to - * lower-case - */ + // Fast low-level manipulation + char* LockBuffer(int32 maxLength); + BString& UnlockBuffer(int32 length = -1); - BString &CapitalizeEachWord(); - /* Converts first character in each - * non-alphabethycal-character-separated - * word to upper-case, rest to lower-case - */ -/*----- Escaping and Deescaping --------------------------------------------*/ - BString &CharacterEscape(const char *original, - const char *setOfCharsToEscape, char escapeWith); - /* copies original into , escaping characters - * specified in by prepending - * them with - */ - BString &CharacterEscape(const char *setOfCharsToEscape, + // Upercase <-> Lowercase + BString& ToLower(); + BString& ToUpper(); + + BString& Capitalize(); + BString& CapitalizeEachWord(); + + // Escaping and De-escaping + BString& CharacterEscape(const char* original, + const char* setOfCharsToEscape, char escapeWith); + BString& CharacterEscape(const char* setOfCharsToEscape, char escapeWith); - /* escapes characters specified in - * by prepending them with - */ - BString &CharacterDeescape(const char *original, char escapeChar); - /* copy into the string removing the escaping - * characters - */ - BString &CharacterDeescape(char escapeChar); - /* remove the escaping characters from - * the string - */ + BString& CharacterDeescape(const char* original, char escapeChar); + BString& CharacterDeescape(char escapeChar); -/*---- Simple sprintf replacement calls ------------------------------------*/ -/*---- Slower than sprintf but type and overflow safe ----------------------*/ - BString &operator<<(const char *); - BString &operator<<(const BString &); - BString &operator<<(char); - BString &operator<<(int); - BString &operator<<(unsigned int); - BString &operator<<(uint32); - BString &operator<<(int32); - BString &operator<<(uint64); - BString &operator<<(int64); - BString &operator<<(float); - /* float output hardcodes %.2f style formatting */ - -/*----- Private or reserved ------------------------------------------------*/ -private: - void _Init(const char *, int32); - void _DoAssign(const char *, int32); - void _DoAppend(const char *, int32); - char *_GrowBy(int32); - char *_OpenAtBy(int32, int32); - char *_ShrinkAtBy(int32, int32); - void _DoPrepend(const char *, int32); - - int32 _FindAfter(const char *, int32, int32) const; - int32 _IFindAfter(const char *, int32, int32) const; - int32 _ShortFindAfter(const char *, int32) const; - int32 _FindBefore(const char *, int32, int32) const; - int32 _IFindBefore(const char *, int32, int32) const; - BString &_DoReplace(const char *, const char *, int32, int32, - bool); - void _SetLength(int32); + // Slower than sprintf() but type and overflow safe + BString& operator<<(const char* string); + BString& operator<<(const BString& string); + BString& operator<<(char c); + BString& operator<<(int value); + BString& operator<<(unsigned int value); + BString& operator<<(uint32 value); + BString& operator<<(int32 value); + BString& operator<<(uint64 value); + BString& operator<<(int64 value); + BString& operator<<(float value); + // float output hardcodes %.2f style formatting + + private: + void _Init(const char *, int32); + void _DoAssign(const char *, int32); + void _DoAppend(const char *, int32); + char* _GrowBy(int32); + char* _OpenAtBy(int32, int32); + char* _ShrinkAtBy(int32, int32); + void _DoPrepend(const char *, int32); + + int32 _FindAfter(const char *, int32, int32) const; + int32 _IFindAfter(const char *, int32, int32) const; + int32 _ShortFindAfter(const char *, int32) const; + int32 _FindBefore(const char *, int32, int32) const; + int32 _IFindBefore(const char *, int32, int32) const; + BString& _DoReplace(const char *, const char *, int32, int32, + bool); + void _SetLength(int32); #if DEBUG - void _SetUsingAsCString(bool); - void _AssertNotUsingAsCString() const; + void _SetUsingAsCString(bool); + void _AssertNotUsingAsCString() const; #else - void _SetUsingAsCString(bool) {} - void _AssertNotUsingAsCString() const {} + void _SetUsingAsCString(bool) {} + void _AssertNotUsingAsCString() const {} #endif - char *_Alloc( int32); + char* _Alloc(int32 size); - struct PosVect; - void _ReplaceAtPositions( const PosVect* positions, - int32 searchLen, - const char* with, - int32 withLen); + class PosVect; + void _ReplaceAtPositions(const PosVect* positions, + int32 searchLength, const char* with, int32 withLen); -protected: - char *_privateData; + protected: + char* fPrivateData; }; -/*----- Comutative compare operators --------------------------------------*/ -bool operator<(const char *, const BString &); -bool operator<=(const char *, const BString &); -bool operator==(const char *, const BString &); -bool operator>(const char *, const BString &); -bool operator>=(const char *, const BString &); -bool operator!=(const char *, const BString &); +// Commutative compare operators +bool operator<(const char* a, const BString& b); +bool operator<=(const char* a, const BString& b); +bool operator==(const char* a, const BString& b); +bool operator>(const char* a, const BString& b); +bool operator>=(const char* a, const BString& b); +bool operator!=(const char* a, const BString& b); -/*----- Non-member compare for sorting, etc. ------------------------------*/ -int Compare(const BString &, const BString &); -int ICompare(const BString &, const BString &); -int Compare(const BString *, const BString *); -int ICompare(const BString *, const BString *); +// Non-member compare for sorting, etc. +int Compare(const BString& a, const BString& b); +int ICompare(const BString& a, const BString& b); +int Compare(const BString* a, const BString* b); +int ICompare(const BString* a, const BString* b); - - -/*-------------------------------------------------------------------------*/ -/*---- No user serviceable parts after this -------------------------------*/ - inline int32 BString::Length() const { - return _privateData ? (*((int32 *)_privateData - 1) & 0x7fffffff) : 0; + return fPrivateData ? (*((int32 *)fPrivateData - 1) & 0x7fffffff) : 0; /* the most significant bit is reserved; accessing * it in any way will cause the computer to explode */ @@ -363,29 +284,29 @@ BString::Length() const inline const char * BString::String() const { - if (!_privateData) + if (!fPrivateData) return ""; - return _privateData; + return fPrivateData; } inline BString & -BString::SetTo(const char *str) +BString::SetTo(const char *string) { - return operator=(str); + return operator=(string); } inline char BString::operator[](int32 index) const { - return _privateData[index]; + return fPrivateData[index]; } inline char BString::ByteAt(int32 index) const { - if (!_privateData || index < 0 || index > Length()) + if (!fPrivateData || index < 0 || index > Length()) return 0; - return _privateData[index]; + return fPrivateData[index]; } inline BString & diff --git a/src/kits/support/String.cpp b/src/kits/support/String.cpp index e76d40f4ce..4181d97cb1 100644 --- a/src/kits/support/String.cpp +++ b/src/kits/support/String.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2001-2006, Haiku, Inc. All Rights Reserved. + * Copyright 2001-2007, Haiku, Inc. All Rights Reserved. * Distributed under the terms of the MIT License. * * Authors: @@ -31,86 +31,6 @@ #define REPLACE_ALL 0x7FFFFFFF -const char *B_EMPTY_STRING = ""; - - -// helper function, returns minimum of two given values (but clamps to 0): -static inline int32 -min_clamp0(int32 num1, int32 num2) -{ - if (num1 < num2) - return num1 > 0 ? num1 : 0; - - return num2 > 0 ? num2 : 0; -} - - -// helper function, returns length of given string (but clamps to given maximum): -static inline int32 -strlen_clamp(const char* str, int32 max) -{ // this should yield 0 for max<0: - int32 len=0; - while( len 0 ? num1 : 0; + + return num2 > 0 ? num2 : 0; +} + + +//! helper function, returns length of given string (but clamps to given maximum): +static inline int32 +strlen_clamp(const char* str, int32 max) +{ + // this should yield 0 for max<0: + int32 length = 0; + while (length < max && *str++) { + length++; + } + return length; +} + + +//! helper function, massages given pointer into a legal c-string: +static inline const char * +safestr(const char* str) +{ + return str ? str : ""; +} + + // #pragma mark - +BString::PosVect::PosVect() + : + fSize(0), + fBufferSize(20), + fBuffer(NULL) +{ +} + + +BString::PosVect::~PosVect() +{ + free(fBuffer); +} + + +bool +BString::PosVect::Add(int32 pos) +{ + if (fBuffer == NULL || fSize == fBufferSize) { + if (fBuffer != NULL) + fBufferSize *= 2; + + int32* newBuffer = (int32 *)realloc(fBuffer, fBufferSize * sizeof(int32)); + if (newBuffer == NULL) + return false; + + fBuffer = newBuffer; + } + + fBuffer[fSize++] = pos; + return true; +} + + +// #pragma mark - BString + -// constructor BString::BString() - : _privateData(NULL) + : fPrivateData(NULL) { } -// constructor -BString::BString(const char* str) - : _privateData(NULL) +BString::BString(const char* string) + : fPrivateData(NULL) { - if (str != NULL) - _Init(str, strlen(str)); + if (string != NULL) + _Init(string, strlen(string)); } -// copy constructor BString::BString(const BString &string) - : _privateData(NULL) + : fPrivateData(NULL) { _Init(string.String(), string.Length()); } -// constructor -BString::BString(const char *str, int32 maxLength) - : _privateData(NULL) +BString::BString(const char *string, int32 maxLength) + : fPrivateData(NULL) { - if (str != NULL) - _Init(str, strlen_clamp(str, maxLength)); + if (string != NULL) + _Init(string, strlen_clamp(string, maxLength)); } -// destructor BString::~BString() { - if (_privateData) - free(_privateData - sizeof(int32)); + if (fPrivateData) + free(fPrivateData - sizeof(int32)); } -/*---- Access --------------------------------------------------------------*/ -// String, implemented inline in the header - -// Length, implemented inline in the header +// #pragma mark - Access -// CountChars int32 BString::CountChars() const { int32 count = 0; - const char *start = _privateData; - const char *end = _privateData + Length(); + const char *start = fPrivateData; + const char *end = fPrivateData + Length(); while (start++ != end) { count++; @@ -208,8 +210,9 @@ BString::CountChars() const } -/*---- Assignment ----------------------------------------------------------*/ -// equal operator +// #pragma mark - Assignment + + BString& BString::operator=(const BString &string) { @@ -219,7 +222,6 @@ BString::operator=(const BString &string) } -// equal operator BString& BString::operator=(const char *str) { @@ -232,7 +234,6 @@ BString::operator=(const char *str) } -// equal operator BString& BString::operator=(char c) { @@ -241,7 +242,6 @@ BString::operator=(char c) } -// SetTo BString& BString::SetTo(const char *str, int32 maxLength) { @@ -254,17 +254,17 @@ BString::SetTo(const char *str, int32 maxLength) } -// SetTo BString& BString::SetTo(const BString &from) { - if (&from != this) // Avoid auto-assignment + if (&from != this) { + // Avoid auto-assignment _DoAssign(from.String(), from.Length()); + } return *this; } -// Adopt BString& BString::Adopt(BString &from) { @@ -273,18 +273,17 @@ BString::Adopt(BString &from) return *this; } - if (_privateData) - free(_privateData - sizeof(int32)); + if (fPrivateData) + free(fPrivateData - sizeof(int32)); /* "steal" the data from the given BString */ - _privateData = from._privateData; - from._privateData = NULL; + fPrivateData = from.fPrivateData; + from.fPrivateData = NULL; return *this; } -// SetTo BString& BString::SetTo(const BString &string, int32 length) { @@ -294,7 +293,6 @@ BString::SetTo(const BString &string, int32 length) } -// Adopt BString& BString::Adopt(BString &from, int32 length) { @@ -303,12 +301,12 @@ BString::Adopt(BString &from, int32 length) int32 len = min_clamp0(length, from.Length()); - if (_privateData) - free(_privateData - sizeof(int32)); + if (fPrivateData) + free(fPrivateData - sizeof(int32)); /* "steal" the data from the given BString */ - _privateData = from._privateData; - from._privateData = NULL; + fPrivateData = from.fPrivateData; + from.fPrivateData = NULL; if (len < Length()) _Alloc(len); @@ -317,7 +315,6 @@ BString::Adopt(BString &from, int32 length) } -// SetTo BString& BString::SetTo(char c, int32 count) { @@ -326,14 +323,14 @@ BString::SetTo(char c, int32 count) int32 curLen = Length(); if (curLen == count || _GrowBy(count - curLen)) - memset(_privateData, c, count); + memset(fPrivateData, c, count); return *this; } -/*---- Substring copying ---------------------------------------------------*/ +// #pragma mark - Substring copying + -// CopyInto BString & BString::CopyInto(BString &into, int32 fromOffset, int32 length) const { @@ -348,7 +345,6 @@ BString::CopyInto(BString &into, int32 fromOffset, int32 length) const } -// CopyInto void BString::CopyInto(char *into, int32 fromOffset, int32 length) const { @@ -356,13 +352,14 @@ BString::CopyInto(char *into, int32 fromOffset, int32 length) const CHECK_PARAM_VOID(fromOffset >= 0, "'fromOffset' must not be negative!"); CHECK_PARAM_VOID(fromOffset <= Length(), "'fromOffset' exceeds length!"); int32 len = min_clamp0(length, Length() - fromOffset); - memcpy(into, _privateData + fromOffset, len); + memcpy(into, fPrivateData + fromOffset, len); } } -/*---- Appending -----------------------------------------------------------*/ -// plus operator +// #pragma mark - Appending + + BString& BString::operator+=(const char *str) { @@ -372,7 +369,6 @@ BString::operator+=(const char *str) } -// plus operator BString& BString::operator+=(char c) { @@ -381,7 +377,6 @@ BString::operator+=(char c) } -// Append BString& BString::Append(const BString &string, int32 length) { @@ -390,7 +385,6 @@ BString::Append(const BString &string, int32 length) } -// Append BString& BString::Append(const char *str, int32 length) { @@ -402,13 +396,12 @@ BString::Append(const char *str, int32 length) } -// Append BString& BString::Append(char c, int32 count) { int32 len = Length(); if (count > 0 && _GrowBy(count)) - memset(_privateData + len, c, count); + memset(fPrivateData + len, c, count); return *this; } @@ -463,7 +456,7 @@ BString& BString::Prepend(char c, int32 count) { if (count > 0 && _OpenAtBy(0, count)) - memset(_privateData, c, count); + memset(fPrivateData, c, count); return *this; } @@ -486,7 +479,7 @@ BString::Insert(const char *str, int32 pos) } else pos = min_clamp0(pos, Length()); if (_OpenAtBy(pos, len)) - memcpy(_privateData + pos, str, len); + memcpy(fPrivateData + pos, str, len); } return *this; } @@ -507,7 +500,7 @@ BString::Insert(const char *str, int32 length, int32 pos) } else pos = min_clamp0(pos, Length()); if (_OpenAtBy(pos, len)) - memcpy(_privateData + pos, str, len); + memcpy(fPrivateData + pos, str, len); } return *this; } @@ -566,7 +559,7 @@ BString::Insert(char c, int32 count, int32 pos) pos = min_clamp0(pos, Length()); if (count > 0 && _OpenAtBy(pos, count)) - memset(_privateData + pos, c, count); + memset(fPrivateData + pos, c, count); return *this; } @@ -587,7 +580,7 @@ BString::Truncate(int32 newLength, bool lazy) if (lazy) { // don't free memory yet, just set new length _SetLength(newLength); - _privateData[newLength] = '\0'; + fPrivateData[newLength] = '\0'; } else _Alloc(newLength); } @@ -699,8 +692,8 @@ BString::MoveInto(BString &into, int32 from, int32 length) if (&into == this) { /* TODO: [zooey]: to be activated later (>R1): // strings are identical, just move the data: - if (from>0 && _privateData) - memmove( _privateData, _privateData+from, len); + if (from>0 && fPrivateData) + memmove( fPrivateData, fPrivateData+from, len); Truncate( len); */ return *this; @@ -1088,7 +1081,7 @@ BString::ReplaceFirst(char replaceThis, char withThis) { int32 pos = FindFirst(replaceThis); if (pos >= 0) - _privateData[pos] = withThis; + fPrivateData[pos] = withThis; return *this; } @@ -1099,7 +1092,7 @@ BString::ReplaceLast(char replaceThis, char withThis) { int32 pos = FindLast(replaceThis); if (pos >= 0) - _privateData[pos] = withThis; + fPrivateData[pos] = withThis; return *this; } @@ -1113,7 +1106,7 @@ BString::ReplaceAll(char replaceThis, char withThis, int32 fromOffset) pos = FindFirst(replaceThis, pos); if (pos < 0) break; - _privateData[pos] = withThis; + fPrivateData[pos] = withThis; } return *this; @@ -1130,7 +1123,7 @@ BString::Replace(char replaceThis, char withThis, int32 maxReplaceCount, int32 f pos = FindFirst(replaceThis, pos); if (pos < 0) break; - _privateData[pos] = withThis; + fPrivateData[pos] = withThis; } } return *this; @@ -1164,7 +1157,7 @@ BString::ReplaceLast(const char *replaceThis, const char *withThis) if (!_ShrinkAtBy(pos, -difference)) return *this; } - memcpy(_privateData + pos, withThis, len); + memcpy(fPrivateData + pos, withThis, len); } return *this; @@ -1196,7 +1189,7 @@ BString::IReplaceFirst(char replaceThis, char withThis) int32 pos = _IFindAfter(tmp, 0, 1); if (pos >= 0) - _privateData[pos] = withThis; + fPrivateData[pos] = withThis; return *this; } @@ -1209,7 +1202,7 @@ BString::IReplaceLast(char replaceThis, char withThis) int32 pos = _IFindBefore(tmp, Length(), 1); if (pos >= 0) - _privateData[pos] = withThis; + fPrivateData[pos] = withThis; return *this; } @@ -1226,7 +1219,7 @@ BString::IReplaceAll(char replaceThis, char withThis, int32 fromOffset) pos = _IFindAfter(tmp, pos, 1); if (pos < 0) break; - _privateData[pos] = withThis; + fPrivateData[pos] = withThis; } return *this; } @@ -1239,7 +1232,7 @@ BString::IReplace(char replaceThis, char withThis, int32 maxReplaceCount, int32 char tmp[2] = { replaceThis, '\0' }; - if (_privateData == NULL) + if (fPrivateData == NULL) return *this; for (int32 pos = min_clamp0(fromOffset,Length()); @@ -1247,7 +1240,7 @@ BString::IReplace(char replaceThis, char withThis, int32 maxReplaceCount, int32 pos = _IFindAfter(tmp, pos, 1); if (pos < 0) break; - _privateData[pos] = withThis; + fPrivateData[pos] = withThis; } return *this; } @@ -1280,7 +1273,7 @@ BString::IReplaceLast(const char *replaceThis, const char *withThis) if (!_ShrinkAtBy(pos, -difference)) return *this; } - memcpy(_privateData + pos, withThis, len); + memcpy(fPrivateData + pos, withThis, len); } return *this; @@ -1322,7 +1315,7 @@ BString::ReplaceSet(const char *setOfChars, char with) if (offset >= length) break; - _privateData[offset] = with; + fPrivateData[offset] = with; offset++; } @@ -1339,7 +1332,7 @@ BString::ReplaceSet(const char *setOfChars, const char *with) return ReplaceSet(setOfChars, *with); } - if (setOfChars == NULL || _privateData == NULL) + if (setOfChars == NULL || fPrivateData == NULL) return *this; PosVect positions; @@ -1348,7 +1341,7 @@ BString::ReplaceSet(const char *setOfChars, const char *with) int32 len = Length(); int32 pos = 0; for (int32 offset = 0; offset < len; offset += (pos+searchLen)) { - pos = strcspn(_privateData + offset, setOfChars); + pos = strcspn(fPrivateData + offset, setOfChars); if (pos + offset >= len) break; if (!positions.Add(offset + pos)) @@ -1366,7 +1359,7 @@ BString::ReplaceSet(const char *setOfChars, const char *with) char & BString::operator[](int32 index) { - return _privateData[index]; + return fPrivateData[index]; } @@ -1381,16 +1374,16 @@ BString::LockBuffer(int32 maxLength) if (maxLength > len) { if (!_GrowBy(maxLength - len)) return NULL; - if (!len && _privateData) + if (!len && fPrivateData) // if string was empty before call to LockBuffer(), we make sure the // buffer represents an empty c-string: - *_privateData = '\0'; + *fPrivateData = '\0'; } else if (!maxLength && !len) { // special case for unallocated string, we return an empty c-string: return const_cast(String()); } - return _privateData; + return fPrivateData; } @@ -1400,7 +1393,7 @@ BString::UnlockBuffer(int32 length) _SetUsingAsCString(false); if (length < 0) - length = (_privateData == NULL) ? 0 : strlen(_privateData); + length = (fPrivateData == NULL) ? 0 : strlen(fPrivateData); if (length != Length()) _GrowBy(length - Length()); @@ -1416,7 +1409,7 @@ BString::ToLower() { int32 length = Length(); for (int32 count = 0; count < length; count++) { - _privateData[count] = tolower(_privateData[count]); + fPrivateData[count] = tolower(fPrivateData[count]); } return *this; @@ -1429,7 +1422,7 @@ BString::ToUpper() { int32 length = Length(); for (int32 count = 0; count < length; count++) { - _privateData[count] = toupper(_privateData[count]); + fPrivateData[count] = toupper(fPrivateData[count]); } return *this; @@ -1440,14 +1433,14 @@ BString::ToUpper() BString& BString::Capitalize() { - if (_privateData == NULL) + if (fPrivateData == NULL) return *this; - _privateData[0] = toupper(_privateData[0]); + fPrivateData[0] = toupper(fPrivateData[0]); int32 length = Length(); for (int32 count = 1; count < length; count++) { - _privateData[count] = tolower(_privateData[count]); + fPrivateData[count] = tolower(fPrivateData[count]); } return *this; @@ -1458,7 +1451,7 @@ BString::Capitalize() BString& BString::CapitalizeEachWord() { - if (_privateData == NULL) + if (fPrivateData == NULL) return *this; int32 count = 0; @@ -1467,9 +1460,9 @@ BString::CapitalizeEachWord() do { // Find the first alphabetical character... for (; count < length; count++) { - if (isalpha(_privateData[count])) { + if (isalpha(fPrivateData[count])) { // ...found! Convert it to uppercase. - _privateData[count] = toupper(_privateData[count]); + fPrivateData[count] = toupper(fPrivateData[count]); count++; break; } @@ -1478,8 +1471,8 @@ BString::CapitalizeEachWord() // Now find the first non-alphabetical character, // and meanwhile, turn to lowercase all the alphabetical ones for (; count < length; count++) { - if (isalpha(_privateData[count])) - _privateData[count] = tolower(_privateData[count]); + if (isalpha(fPrivateData[count])) + fPrivateData[count] = tolower(fPrivateData[count]); else break; } @@ -1504,14 +1497,14 @@ BString::CharacterEscape(const char *original, const char *setOfCharsToEscape, BString& BString::CharacterEscape(const char *setOfCharsToEscape, char escapeWith) { - if (setOfCharsToEscape == NULL || _privateData == NULL) + if (setOfCharsToEscape == NULL || fPrivateData == NULL) return *this; PosVect positions; int32 len = Length(); int32 pos = 0; for (int32 offset = 0; offset < len; offset += pos + 1) { - if ((pos = strcspn(_privateData + offset, setOfCharsToEscape)) < len - offset) { + if ((pos = strcspn(fPrivateData + offset, setOfCharsToEscape)) < len - offset) { if (!positions.Add(offset + pos)) return *this; } @@ -1525,7 +1518,7 @@ BString::CharacterEscape(const char *setOfCharsToEscape, char escapeWith) } int32 lastPos = 0; - char* oldAdr = _privateData; + char* oldAdr = fPrivateData; char* newData = (char*)malloc(newLength + sizeof(int32) + 1); if (newData) { newData += sizeof(int32); @@ -1546,9 +1539,9 @@ BString::CharacterEscape(const char *setOfCharsToEscape, char escapeWith) if (len > 0) memcpy(newAdr, oldAdr, len); - free(_privateData - sizeof(int32)); - _privateData = newData; - _privateData[newLength] = 0; + free(fPrivateData - sizeof(int32)); + fPrivateData = newData; + fPrivateData[newLength] = 0; _SetLength( newLength); } @@ -1684,17 +1677,17 @@ BString::operator<<(float f) char * BString::_Alloc(int32 dataLength) { - char *dataPtr = _privateData ? _privateData - sizeof(int32) : NULL; + char *dataPtr = fPrivateData ? fPrivateData - sizeof(int32) : NULL; if (dataLength <= 0) { // release buffer #if 0 free(dataPtr); - _privateData = NULL; + fPrivateData = NULL; return NULL; #else // TODO: think about removing this work-around again; it lets // BeOS R5 NetPositive run on Haiku - this is obviously ignoring - // the fact, that _privateData could be NULL at one point + // the fact, that fPrivateData could be NULL at one point // (while building the menus from resources). dataLength = 0; #endif @@ -1704,10 +1697,10 @@ BString::_Alloc(int32 dataLength) dataPtr = (char *)realloc(dataPtr, allocLength); if (dataPtr) { dataPtr += sizeof(int32); - _privateData = dataPtr; + fPrivateData = dataPtr; _SetLength(dataLength); - _privateData[dataLength] = '\0'; + fPrivateData[dataLength] = '\0'; } return dataPtr; @@ -1717,7 +1710,7 @@ void BString::_Init(const char *str, int32 len) { if (_Alloc(len)) - memcpy(_privateData, str, len); + memcpy(fPrivateData, str, len); } @@ -1730,7 +1723,7 @@ BString::_DoAssign(const char *str, int32 len) int32 curLen = Length(); if (len == curLen || _GrowBy(len - curLen)) - memcpy(_privateData, str, len); + memcpy(fPrivateData, str, len); } @@ -1742,7 +1735,7 @@ BString::_DoAppend(const char *str, int32 len) { int32 length = Length(); if (_GrowBy(len)) - memcpy(_privateData + length, str, len); + memcpy(fPrivateData + length, str, len); } @@ -1760,7 +1753,7 @@ BString::_OpenAtBy(int32 offset, int32 length) char* newData = _Alloc(oldLength + length); if (newData != NULL) { - memmove(_privateData + offset + length, _privateData + offset, + memmove(fPrivateData + offset + length, fPrivateData + offset, oldLength - offset); } @@ -1771,12 +1764,12 @@ BString::_OpenAtBy(int32 offset, int32 length) char* BString::_ShrinkAtBy(int32 offset, int32 length) { - if (!_privateData) + if (!fPrivateData) return NULL; int32 oldLength = Length(); - memmove(_privateData + offset, _privateData + offset + length, + memmove(fPrivateData + offset, fPrivateData + offset + length, oldLength - offset - length); // the following actually should never fail, since we are reducing the size... @@ -1791,7 +1784,7 @@ void BString::_DoPrepend(const char *str, int32 count) { if (_OpenAtBy(0, count)) - memcpy(_privateData, str, count); + memcpy(fPrivateData, str, count); } @@ -1835,12 +1828,12 @@ BString::_ShortFindAfter(const char *str, int32 len) const int32 BString::_FindBefore(const char *str, int32 offset, int32 strlen) const { - if (_privateData) { - const char *ptr = _privateData + offset - strlen; + if (fPrivateData) { + const char *ptr = fPrivateData + offset - strlen; - while (ptr >= _privateData) { + while (ptr >= fPrivateData) { if (!memcmp(ptr, str, strlen)) - return ptr - _privateData; + return ptr - fPrivateData; ptr--; } } @@ -1851,12 +1844,12 @@ BString::_FindBefore(const char *str, int32 offset, int32 strlen) const int32 BString::_IFindBefore(const char *str, int32 offset, int32 strlen) const { - if (_privateData) { - char *ptr1 = _privateData + offset - strlen; + if (fPrivateData) { + char *ptr1 = fPrivateData + offset - strlen; - while (ptr1 >= _privateData) { + while (ptr1 >= fPrivateData) { if (!strncasecmp(ptr1, str, strlen)) - return ptr1 - _privateData; + return ptr1 - fPrivateData; ptr1--; } } @@ -1908,7 +1901,7 @@ BString::_ReplaceAtPositions(const PosVect* positions, int32 pos; int32 lastPos = 0; - char *oldAdr = _privateData; + char *oldAdr = fPrivateData; char *newData = (char *)malloc(newLength + sizeof(int32) + 1); if (newData) { newData += sizeof(int32); @@ -1930,9 +1923,9 @@ BString::_ReplaceAtPositions(const PosVect* positions, if (len > 0) memcpy(newAdr, oldAdr, len); - free(_privateData - sizeof(int32)); - _privateData = newData; - _privateData[newLength] = 0; + free(fPrivateData - sizeof(int32)); + fPrivateData = newData; + fPrivateData[newLength] = 0; _SetLength( newLength); } } @@ -1944,7 +1937,7 @@ inline void BString::_SetLength(int32 length) { - *((int32*)_privateData - 1) = length & 0x7fffffff; + *((int32*)fPrivateData - 1) = length & 0x7fffffff; }