* Fixed a race condition in the former _Detach*() functions: since atomic_get()

was used, two different threads could decide to share the same mutable string.
* Renamed some functions to make clearer what they do, ie. _Detach() is now
  called _MakeWritable().
* Cleaned up some questionable semantics, like the const char* parameter in
  _DetachWith() - you can now choose to copy the original string or not with
  a boolean. This also makes sure that the string is actually copied when it
  has to, which wasn't the case before (but that was no problem with the way
  that function was used).
* Made the header compliant with our style guide.
* Further cleanup.
* All BString related unit tests are passed, so I guess I didn't break too
  much :-)


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@30980 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2009-06-06 11:23:17 +00:00
parent a55f3fb022
commit e52400cf24
2 changed files with 449 additions and 371 deletions
+227 -197
View File
@@ -1,5 +1,5 @@
/*
* Copyright 2001-2008, Haiku Inc. All Rights Reserved.
* Copyright 2001-2009, Haiku Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef __BSTRING__
@@ -16,251 +16,281 @@ class BStringRef;
class BString {
public:
BString();
BString(const char* string);
BString(const BString& string);
BString(const char* string, int32 maxLength);
~BString();
BString();
BString(const char* string);
BString(const BString& string);
BString(const char* string, int32 maxLength);
~BString();
// Access
const char* String() const;
int32 Length() const;
int32 CountChars() const;
// Access
const char* String() const;
int32 Length() const;
int32 CountChars() const;
// Assignment
BString& operator=(const BString& string);
BString& operator=(const char* string);
BString& operator=(char c);
// Assignment
BString& operator=(const BString& string);
BString& operator=(const char* string);
BString& operator=(char c);
BString& SetTo(const char* string);
BString& SetTo(const char* string, int32 maxLength);
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);
BString& Adopt(BString& from);
BString& SetTo(const BString& string, int32 maxLength);
BString& Adopt(BString& from, int32 maxLength);
BString& SetTo(const BString& string, int32 maxLength);
BString& Adopt(BString& from, int32 maxLength);
BString& SetTo(char c, int32 count);
BString& SetTo(char c, int32 count);
// Substring copying
BString& CopyInto(BString& into, int32 fromOffset, int32 length) const;
void CopyInto(char* into, int32 fromOffset, int32 length) const;
// Substring copying
BString& CopyInto(BString& into, int32 fromOffset,
int32 length) const;
void CopyInto(char* into, int32 fromOffset,
int32 length) const;
// Appending
BString& operator+=(const BString& string);
BString& operator+=(const char* string);
BString& operator+=(char c);
// Appending
BString& operator+=(const BString& string);
BString& operator+=(const char* string);
BString& operator+=(char c);
BString& Append(const BString& string);
BString& Append(const char* string);
BString& Append(const BString& string);
BString& Append(const char* string);
BString& Append(const BString& string, int32 length);
BString& Append(const char* string, int32 length);
BString& Append(char c, int32 count);
BString& Append(const BString& string, int32 length);
BString& Append(const char* string, int32 length);
BString& Append(char c, int32 count);
// 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);
// 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 position);
BString& Insert(const char* string, int32 length, int32 position);
BString& Insert(const char* string, int32 fromOffset, int32 length,
int32 position);
BString& Insert(const BString& string, int32 position);
BString& Insert(const BString& string, int32 length, int32 position);
BString& Insert(const BString& string, int32 fromOffset, int32 length,
int32 position);
BString& Insert(char c, int32 count, int32 position);
// Inserting
BString& Insert(const char* string, int32 position);
BString& Insert(const char* string, int32 length,
int32 position);
BString& Insert(const char* string, int32 fromOffset,
int32 length, int32 position);
BString& Insert(const BString& string, int32 position);
BString& Insert(const BString& string, int32 length,
int32 position);
BString& Insert(const BString& string, int32 fromOffset,
int32 length, int32 position);
BString& Insert(char c, int32 count, int32 position);
// Removing
BString& Truncate(int32 newLength, bool lazy = true);
BString& Remove(int32 from, int32 length);
// Removing
BString& Truncate(int32 newLength, bool lazy = true);
BString& Remove(int32 from, int32 length);
BString& RemoveFirst(const BString& string);
BString& RemoveLast(const BString& string);
BString& RemoveAll(const BString& string);
BString& RemoveFirst(const BString& string);
BString& RemoveLast(const BString& string);
BString& RemoveAll(const BString& string);
BString& RemoveFirst(const char* string);
BString& RemoveLast(const char* string);
BString& RemoveAll(const char* string);
BString& RemoveFirst(const char* string);
BString& RemoveLast(const char* string);
BString& RemoveAll(const char* string);
BString& RemoveSet(const char* setOfCharsToRemove);
BString& RemoveSet(const char* setOfCharsToRemove);
BString& MoveInto(BString& into, int32 from, int32 length);
void MoveInto(char* into, int32 from, int32 length);
BString& MoveInto(BString& into, int32 from, int32 length);
void MoveInto(char* into, int32 from, int32 length);
// 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;
// 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;
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;
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;
// 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;
// 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;
// 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;
// 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;
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 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 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;
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, 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,
int32 maxReplaceCount, int32 fromOffset = 0);
// 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,
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, int32 maxReplaceCount,
int32 fromOffset = 0);
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, 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,
int32 maxReplaceCount, int32 fromOffset = 0);
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,
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, int32 maxReplaceCount,
int32 fromOffset = 0);
BString& ReplaceSet(const char* setOfChars, char with);
BString& ReplaceSet(const char* setOfChars, const char* with);
BString& ReplaceSet(const char* setOfChars, char with);
BString& ReplaceSet(const char* setOfChars, const char* with);
// Unchecked char access
char operator[](int32 index) const;
// Unchecked char access
char operator[](int32 index) const;
#if __GNUC__ > 3
BStringRef operator[](int32 index);
BStringRef operator[](int32 index);
#else
char& operator[](int32 index);
char& operator[](int32 index);
#endif
// Checked char access
char ByteAt(int32 index) const;
// Checked char access
char ByteAt(int32 index) const;
// Fast low-level manipulation
char* LockBuffer(int32 maxLength);
BString& UnlockBuffer(int32 length = -1);
// Fast low-level manipulation
char* LockBuffer(int32 maxLength);
BString& UnlockBuffer(int32 length = -1);
// Upercase <-> Lowercase
BString& ToLower();
BString& ToUpper();
// Upercase <-> Lowercase
BString& ToLower();
BString& ToUpper();
BString& Capitalize();
BString& CapitalizeEachWord();
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);
BString& CharacterDeescape(const char* original, char escapeChar);
BString& CharacterDeescape(char escapeChar);
// Escaping and De-escaping
BString& CharacterEscape(const char* original,
const char* setOfCharsToEscape, char escapeWith);
BString& CharacterEscape(const char* setOfCharsToEscape,
char escapeWith);
BString& CharacterDeescape(const char* original,
char escapeChar);
BString& CharacterDeescape(char escapeChar);
// Insert
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);
// float output hardcodes %.2f style formatting
BString& operator<<(float value);
// Insert
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);
// float output hardcodes %.2f style formatting
BString& operator<<(float value);
private:
class PosVect;
friend class BStringRef;
// Management
status_t _Detach();
char* _Alloc(int32 length, bool adoptReferenceCount = true);
char* _Realloc(int32 length);
void _Init(const char* src, int32 length);
char* _Clone(const char* data, int32 length);
char* _OpenAtBy(int32 offset, int32 length);
char* _ShrinkAtBy(int32 offset, int32 length);
status_t _DetachWith(const char* string, int32 length);
// Management
status_t _MakeWritable();
status_t _MakeWritable(int32 length, bool copy);
char* _Allocate(int32 length);
char* _Resize(int32 length);
void _Init(const char* src, int32 length);
char* _Clone(const char* data, int32 length);
char* _OpenAtBy(int32 offset, int32 length);
char* _ShrinkAtBy(int32 offset, int32 length);
// Data
void _SetLength(int32 length);
bool _DoAppend(const char* string, int32 length);
bool _DoPrepend(const char* string, int32 length);
bool _DoInsert(const char* string, int32 offset, int32 length);
// Data
void _SetLength(int32 length);
bool _DoAppend(const char* string, int32 length);
bool _DoPrepend(const char* string, int32 length);
bool _DoInsert(const char* string, int32 offset,
int32 length);
// Search
int32 _ShortFindAfter(const char* string, int32 len) const;
int32 _FindAfter(const char* string, int32 offset, int32 strlen) const;
int32 _IFindAfter(const char* string, int32 offset, int32 strlen) const;
// Search
int32 _ShortFindAfter(const char* string,
int32 length) const;
int32 _FindAfter(const char* string, int32 offset,
int32 length) const;
int32 _IFindAfter(const char* string, int32 offset,
int32 length) const;
int32 _FindBefore(const char* string, int32 offset,
int32 length) const;
int32 _IFindBefore(const char* string, int32 offset,
int32 length) const;
int32 _FindBefore(const char* string, int32 offset, int32 strlen) const;
int32 _IFindBefore(const char* string, int32 offset, int32 strlen) const;
// Escape
BString& _DoCharacterEscape(const char* string,
const char *setOfCharsToEscape, char escapeChar);
BString& _DoCharacterDeescape(const char* string,
char escapeChar);
// Escape
BString& _DoCharacterEscape(const char* string,
const char *setOfCharsToEscape, char escapeChar);
BString& _DoCharacterDeescape(const char* string, char escapeChar);
// Replace
BString& _DoReplace(const char* findThis, const char* replaceWith,
int32 maxReplaceCount, int32 fromOffset, bool ignoreCase);
void _ReplaceAtPositions(const PosVect* positions, int32 searchLen,
const char* with, int32 withLen);
// Replace
BString& _DoReplace(const char* findThis,
const char* replaceWith, int32 maxReplaceCount,
int32 fromOffset, bool ignoreCase);
void _ReplaceAtPositions(const PosVect* positions,
int32 searchLength, const char* with,
int32 withLength);
private:
int32& _ReferenceCount();
const int32& _ReferenceCount() const;
bool _IsShareable() const;
void _FreePrivateData();
vint32& _ReferenceCount();
const vint32& _ReferenceCount() const;
bool _IsShareable() const;
void _FreePrivateData();
char* fPrivateData;
char* fPrivateData;
};
@@ -442,7 +472,7 @@ class BStringRef {
public:
BStringRef(BString& string, int32 position);
~BStringRef() {}
operator char() const;
char* operator&();