[Haiku-commits] r30980 - in haiku/trunk: headers/os/support src/kits/support
axeld at BerliOS
axeld at mail.berlios.de
Sat Jun 6 13:23:19 CEST 2009
Author: axeld
Date: 2009-06-06 13:23:17 +0200 (Sat, 06 Jun 2009)
New Revision: 30980
ViewCVS: http://svn.berlios.de/viewcvs/haiku?rev=30980&view=rev
Modified:
haiku/trunk/headers/os/support/String.h
haiku/trunk/src/kits/support/String.cpp
Log:
* 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 :-)
Modified: haiku/trunk/headers/os/support/String.h
===================================================================
--- haiku/trunk/headers/os/support/String.h 2009-06-06 10:35:14 UTC (rev 30979)
+++ haiku/trunk/headers/os/support/String.h 2009-06-06 11:23:17 UTC (rev 30980)
@@ -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 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 searchLength, const char* with,
+ int32 withLength);
- // 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);
-
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 @@
public:
BStringRef(BString& string, int32 position);
~BStringRef() {}
-
+
operator char() const;
char* operator&();
Modified: haiku/trunk/src/kits/support/String.cpp
===================================================================
--- haiku/trunk/src/kits/support/String.cpp 2009-06-06 10:35:14 UTC (rev 30979)
+++ haiku/trunk/src/kits/support/String.cpp 2009-06-06 11:23:17 UTC (rev 30980)
@@ -1,19 +1,18 @@
/*
-* Copyright 2001-2008, Haiku, Inc. All Rights Reserved.
-* Distributed under the terms of the MIT License.
-*
-* Authors:
-* Marc Flerackers (mflerackers at androme.be)
-* Stefano Ceccherini (burton666 at libero.it)
-* Oliver Tappe (openbeos at hirschkaefer.de)
-* Axel Dörfler, axeld at pinc-software.de
-* Julun <host.haiku at gmx.de>
-*/
+ * Copyright 2001-2009, Haiku, Inc. All Rights Reserved.
+ * Distributed under the terms of the MIT License.
+ *
+ * Authors:
+ * Marc Flerackers (mflerackers at androme.be)
+ * Stefano Ceccherini (burton666 at libero.it)
+ * Oliver Tappe (openbeos at hirschkaefer.de)
+ * Axel Dörfler, axeld at pinc-software.de
+ * Julun <host.haiku at gmx.de>
+ */
/*! String class supporting common string operations. */
-#include <Debug.h>
#include <String.h>
#include <ctype.h>
@@ -21,9 +20,8 @@
#include <stdio.h>
#include <stdlib.h>
+#include <Debug.h>
-// Set this to 1 to make some private methods inline
-#define ENABLE_INLINES 0
// define proper names for case-option of _DoReplace()
#define KEEP_CASE false
@@ -35,7 +33,7 @@
const uint32 kPrivateDataOffset = 2 * sizeof(int32);
-const char *B_EMPTY_STRING = "";
+const char* B_EMPTY_STRING = "";
// helper function, returns minimum of two given values (but clamps to 0):
@@ -49,7 +47,7 @@
}
-//! helper function, returns length of given string (but clamps to given maximum):
+//! Returns length of given string (but clamps to given maximum).
static inline int32
strlen_clamp(const char* str, int32 max)
{
@@ -62,23 +60,48 @@
}
+//! Helper function for strlen() that can handle NULL strings.
+static inline size_t
+string_length(const char* string)
+{
+ return string != NULL ? strlen(string) : 0;
+}
+
+
//! helper function, massages given pointer into a legal c-string:
-static inline const char *
+static inline const char*
safestr(const char* str)
{
return str ? str : "";
}
+static inline vint32&
+data_reference_count(char* data)
+{
+ return *(((int32 *)data) - 2);
+}
+
+
+static inline int32&
+data_length(char* data)
+{
+ return *(((int32*)data) - 1);
+}
+
+
// #pragma mark - PosVect
class BString::PosVect {
public:
PosVect()
- : fSize(0),
+ :
+ fSize(0),
fBufferSize(20),
- fBuffer(NULL) { }
+ fBuffer(NULL)
+ {
+ }
~PosVect()
{
@@ -92,7 +115,7 @@
fBufferSize *= 2;
int32* newBuffer = NULL;
- newBuffer = (int32 *)realloc(fBuffer, fBufferSize * sizeof(int32));
+ newBuffer = (int32*)realloc(fBuffer, fBufferSize * sizeof(int32));
if (newBuffer == NULL)
return false;
@@ -104,9 +127,14 @@
}
inline int32 ItemAt(int32 index) const
- { return fBuffer[index]; }
+ {
+ return fBuffer[index];
+ }
+
inline int32 CountItems() const
- { return fSize; }
+ {
+ return fSize;
+ }
private:
int32 fSize;
@@ -133,7 +161,7 @@
BStringRef&
BStringRef::operator=(char c)
{
- fString._Detach();
+ fString._MakeWritable();
fString.fPrivateData[fPosition] = c;
return *this;
}
@@ -156,7 +184,7 @@
char*
BStringRef::operator&()
{
- if (fString._Detach() != B_OK)
+ if (fString._MakeWritable() != B_OK)
return NULL;
fString._ReferenceCount() = -1;
@@ -169,21 +197,24 @@
BString::BString()
- : fPrivateData(NULL)
+ :
+ fPrivateData(NULL)
{
_Init("", 0);
}
BString::BString(const char* string)
- : fPrivateData(NULL)
+ :
+ fPrivateData(NULL)
{
_Init(string, strlen(safestr(string)));
}
BString::BString(const BString& string)
- : fPrivateData(NULL)
+ :
+ fPrivateData(NULL)
{
// check if source is sharable - if so, share else clone
if (string._IsShareable()) {
@@ -217,8 +248,8 @@
{
int32 count = 0;
- const char *start = fPrivateData;
- const char *end = fPrivateData + Length();
+ const char* start = fPrivateData;
+ const char* end = fPrivateData + Length();
while (start++ != end) {
count++;
@@ -262,8 +293,10 @@
{
if (maxLength < 0)
maxLength = INT32_MAX;
+
maxLength = strlen_clamp(safestr(string), maxLength);
- if (_DetachWith("", maxLength) == B_OK)
+
+ if (_MakeWritable(maxLength, false) == B_OK)
memcpy(fPrivateData, string, maxLength);
return *this;
@@ -318,7 +351,7 @@
// make sure we reassing in case length is different
|| (fPrivateData == string.fPrivateData && Length() > maxLength)) {
maxLength = min_clamp0(maxLength, string.Length());
- if (_DetachWith("", maxLength) == B_OK)
+ if (_MakeWritable(maxLength, false) == B_OK)
memcpy(fPrivateData, string.String(), maxLength);
}
return *this;
@@ -341,7 +374,7 @@
if (count < 0)
count = 0;
- if (_DetachWith("", count) == B_OK)
+ if (_MakeWritable(count, false) == B_OK)
memset(fPrivateData, c, count);
return *this;
}
@@ -480,7 +513,7 @@
BString&
BString::Insert(const char* string, int32 position)
{
- if (string && position <= Length()) {
+ if (string != NULL && position <= Length()) {
int32 len = int32(strlen(string));
if (position < 0) {
int32 skipLen = min_clamp0(-1 * position, len);
@@ -499,7 +532,7 @@
BString&
BString::Insert(const char* string, int32 length, int32 position)
{
- if (string && position <= Length()) {
+ if (string != NULL && position <= Length()) {
int32 len = strlen_clamp(string, length);
if (position < 0) {
int32 skipLen = min_clamp0(-1 * position, len);
@@ -578,9 +611,10 @@
if (newLength < 0)
newLength = 0;
- if (newLength < Length())
+ if (newLength < Length()) {
// ignore lazy, since we might detach
- _DetachWith(fPrivateData, newLength);
+ _MakeWritable(newLength, true);
+ }
return *this;
}
@@ -624,7 +658,7 @@
if (string.Length() == 0 || Length() == 0 || FindFirst(string) < 0)
return *this;
- if (_Detach() != B_OK)
+ if (_MakeWritable() != B_OK)
return *this;
return _DoReplace(string.String(), "", REPLACE_ALL, 0, KEEP_CASE);
@@ -663,7 +697,7 @@
if (!string || Length() == 0 || FindFirst(string) < 0)
return *this;
- if (_Detach() != B_OK)
+ if (_MakeWritable() != B_OK)
return *this;
return _DoReplace(string, "", REPLACE_ALL, 0, KEEP_CASE);
@@ -843,8 +877,8 @@
int32
BString::FindFirst(char c) const
{
- const char *start = String();
- const char *end = String() + Length();
+ const char* start = String();
+ const char* end = String() + Length();
// Scans the string until we found the
// character, or we reach the string's start
@@ -865,8 +899,8 @@
if (fromOffset < 0)
return B_ERROR;
- const char *start = String() + min_clamp0(fromOffset, Length());
- const char *end = String() + Length();
+ const char* start = String() + min_clamp0(fromOffset, Length());
+ const char* end = String() + Length();
// Scans the string until we found the
// character, or we reach the string's start
@@ -926,8 +960,8 @@
int32
BString::FindLast(char c) const
{
- const char *start = String();
- const char *end = String() + Length();
+ const char* start = String();
+ const char* end = String() + Length();
// Scans the string backwards until we found
// the character, or we reach the string's start
@@ -948,8 +982,8 @@
if (beforeOffset < 0)
return B_ERROR;
- const char *start = String();
- const char *end = String() + min_clamp0(beforeOffset, Length());
+ const char* start = String();
+ const char* end = String() + min_clamp0(beforeOffset, Length());
// Scans the string backwards until we found
// the character, or we reach the string's start
@@ -1055,7 +1089,7 @@
BString::ReplaceFirst(char replaceThis, char withThis)
{
int32 pos = FindFirst(replaceThis);
- if (pos >= 0 && _Detach() == B_OK)
+ if (pos >= 0 && _MakeWritable() == B_OK)
fPrivateData[pos] = withThis;
return *this;
}
@@ -1065,7 +1099,7 @@
BString::ReplaceLast(char replaceThis, char withThis)
{
int32 pos = FindLast(replaceThis);
- if (pos >= 0 && _Detach() == B_OK)
+ if (pos >= 0 && _MakeWritable() == B_OK)
fPrivateData[pos] = withThis;
return *this;
}
@@ -1078,7 +1112,7 @@
int32 pos = FindFirst(replaceThis, fromOffset);
// detach and set first match
- if (pos >= 0 && _Detach() == B_OK) {
+ if (pos >= 0 && _MakeWritable() == B_OK) {
fPrivateData[pos] = withThis;
for (pos = pos;;) {
pos = FindFirst(replaceThis, pos);
@@ -1098,7 +1132,7 @@
fromOffset = min_clamp0(fromOffset, Length());
int32 pos = FindFirst(replaceThis, fromOffset);
- if (maxReplaceCount > 0 && pos >= 0 && _Detach() == B_OK) {
+ if (maxReplaceCount > 0 && pos >= 0 && _MakeWritable() == B_OK) {
maxReplaceCount--;
fPrivateData[pos] = withThis;
for (pos = pos; maxReplaceCount > 0; maxReplaceCount--) {
@@ -1118,7 +1152,7 @@
if (!replaceThis || !withThis || FindFirst(replaceThis) < 0)
return *this;
- if (_Detach() != B_OK)
+ if (_MakeWritable() != B_OK)
return *this;
return _DoReplace(replaceThis, withThis, 1, 0, KEEP_CASE);
@@ -1145,7 +1179,7 @@
if (!_ShrinkAtBy(pos, -difference))
return *this;
} else {
- if (_Detach() != B_OK)
+ if (_MakeWritable() != B_OK)
return *this;
}
memcpy(fPrivateData + pos, withThis, withThisLength);
@@ -1162,7 +1196,7 @@
if (!replaceThis || !withThis || FindFirst(replaceThis) < 0)
return *this;
- if (_Detach() != B_OK)
+ if (_MakeWritable() != B_OK)
return *this;
return _DoReplace(replaceThis, withThis, REPLACE_ALL,
@@ -1178,7 +1212,7 @@
|| FindFirst(replaceThis) < 0)
return *this;
- if (_Detach() != B_OK)
+ if (_MakeWritable() != B_OK)
return *this;
return _DoReplace(replaceThis, withThis, maxReplaceCount,
@@ -1192,7 +1226,7 @@
char tmp[2] = { replaceThis, '\0' };
int32 pos = _IFindAfter(tmp, 0, 1);
- if (pos >= 0 && _Detach() == B_OK)
+ if (pos >= 0 && _MakeWritable() == B_OK)
fPrivateData[pos] = withThis;
return *this;
}
@@ -1204,7 +1238,7 @@
char tmp[2] = { replaceThis, '\0' };
int32 pos = _IFindBefore(tmp, Length(), 1);
- if (pos >= 0 && _Detach() == B_OK)
+ if (pos >= 0 && _MakeWritable() == B_OK)
fPrivateData[pos] = withThis;
return *this;
}
@@ -1217,7 +1251,7 @@
fromOffset = min_clamp0(fromOffset, Length());
int32 pos = _IFindAfter(tmp, fromOffset, 1);
- if (pos >= 0 && _Detach() == B_OK) {
+ if (pos >= 0 && _MakeWritable() == B_OK) {
fPrivateData[pos] = withThis;
for (pos = pos;;) {
pos = _IFindAfter(tmp, pos, 1);
@@ -1238,7 +1272,7 @@
fromOffset = min_clamp0(fromOffset, Length());
int32 pos = _IFindAfter(tmp, fromOffset, 1);
- if (maxReplaceCount > 0 && pos >= 0 && _Detach() == B_OK) {
+ if (maxReplaceCount > 0 && pos >= 0 && _MakeWritable() == B_OK) {
fPrivateData[pos] = withThis;
maxReplaceCount--;
for (pos = pos; maxReplaceCount > 0; maxReplaceCount--) {
@@ -1259,7 +1293,7 @@
if (!replaceThis || !withThis || IFindFirst(replaceThis) < 0)
return *this;
- if (_Detach() != B_OK)
+ if (_MakeWritable() != B_OK)
return *this;
return _DoReplace(replaceThis, withThis, 1, 0, IGNORE_CASE);
}
@@ -1285,7 +1319,7 @@
if (!_ShrinkAtBy(pos, -difference))
return *this;
} else {
- if (_Detach() != B_OK)
+ if (_MakeWritable() != B_OK)
return *this;
}
memcpy(fPrivateData + pos, withThis, withThisLength);
@@ -1302,7 +1336,7 @@
if (!replaceThis || !withThis || IFindFirst(replaceThis) < 0)
return *this;
- if (_Detach() != B_OK)
+ if (_MakeWritable() != B_OK)
return *this;
return _DoReplace(replaceThis, withThis, REPLACE_ALL,
@@ -1318,7 +1352,7 @@
|| FindFirst(replaceThis) < 0)
return *this;
- if (_Detach() != B_OK)
+ if (_MakeWritable() != B_OK)
return *this;
return _DoReplace(replaceThis, withThis, maxReplaceCount,
@@ -1332,7 +1366,7 @@
if (!setOfChars || strcspn(fPrivateData, setOfChars) >= uint32(Length()))
return *this;
- if (_Detach() != B_OK)
+ if (_MakeWritable() != B_OK)
return *this;
int32 offset = 0;
@@ -1364,7 +1398,7 @@
if (withLen == 1)
return ReplaceSet(setOfChars, *with);
- if (_Detach() != B_OK)
+ if (_MakeWritable() != B_OK)
return *this;
int32 pos = 0;
@@ -1398,7 +1432,7 @@
char&
BString::operator[](int32 index)
{
- if (_Detach() != B_OK) {
+ if (_MakeWritable() != B_OK) {
static char invalid;
return invalid;
}
@@ -1421,7 +1455,7 @@
if (maxLength > length)
length = maxLength;
- if (_DetachWith(fPrivateData, length) == B_OK) {
+ if (_MakeWritable(length, true) == B_OK) {
_ReferenceCount() = -1;
// mark unshareable
}
@@ -1436,10 +1470,10 @@
[... truncated: 510 lines follow ...]
More information about the Haiku-commits
mailing list