Some new methods (Removing), some bug fixes, some optimisations.
git-svn-id: file:///srv/svn/repos/haiku/trunk/current@1018 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
+121
-15
@@ -62,7 +62,8 @@ BString::BString(const char *str, int32 maxLength)
|
|||||||
:_privateData(NULL)
|
:_privateData(NULL)
|
||||||
{
|
{
|
||||||
if (str) {
|
if (str) {
|
||||||
int32 len = min(maxLength, (int32)strlen(str));
|
int32 len = (int32)strlen(str);
|
||||||
|
len = min(len, maxLength);
|
||||||
_Init(str, len);
|
_Init(str, len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -123,8 +124,10 @@ BString::operator=(char c)
|
|||||||
BString&
|
BString&
|
||||||
BString::SetTo(const char *str, int32 length)
|
BString::SetTo(const char *str, int32 length)
|
||||||
{
|
{
|
||||||
if (str)
|
if (str) {
|
||||||
_DoAssign(str, min(length, (int32)strlen(str)));
|
int32 len = (int32)strlen(str);
|
||||||
|
_DoAssign(str, min(length, len));
|
||||||
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,8 +236,10 @@ BString::Append(const BString &string, int32 length)
|
|||||||
BString&
|
BString&
|
||||||
BString::Append(const char *str, int32 length)
|
BString::Append(const char *str, int32 length)
|
||||||
{
|
{
|
||||||
if (str)
|
if (str) {
|
||||||
_DoAppend(str, min(length, (int32)strlen(str)));
|
int32 len = (int32)strlen(str);
|
||||||
|
_DoAppend(str, min(len, length));
|
||||||
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -263,16 +268,19 @@ BString::Prepend(const char *str)
|
|||||||
BString&
|
BString&
|
||||||
BString::Prepend(const BString &string)
|
BString::Prepend(const BString &string)
|
||||||
{
|
{
|
||||||
_DoPrepend(string.String(), string.Length());
|
if (&string != this)
|
||||||
|
_DoPrepend(string.String(), string.Length());
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
BString&
|
BString&
|
||||||
BString::Prepend(const char *str, int32 len)
|
BString::Prepend(const char *str, int32 length)
|
||||||
{
|
{
|
||||||
if (str)
|
if (str) {
|
||||||
_DoPrepend(str, min(len, (int32)strlen(str)));
|
int32 len = (int32)strlen(str);
|
||||||
|
_DoPrepend(str, min(len, length));
|
||||||
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -280,7 +288,8 @@ BString::Prepend(const char *str, int32 len)
|
|||||||
BString&
|
BString&
|
||||||
BString::Prepend(const BString &string, int32 len)
|
BString::Prepend(const BString &string, int32 len)
|
||||||
{
|
{
|
||||||
_DoPrepend(string.String(), min(len, string.Length()));
|
if (&string != this)
|
||||||
|
_DoPrepend(string.String(), min(len, string.Length()));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -312,7 +321,8 @@ BString&
|
|||||||
BString::Insert(const char *str, int32 length, int32 pos)
|
BString::Insert(const char *str, int32 length, int32 pos)
|
||||||
{
|
{
|
||||||
if (str) {
|
if (str) {
|
||||||
int32 len = min(length, (int32)strlen(str));
|
int32 len = (int32)strlen(str);
|
||||||
|
len = min(len, length);
|
||||||
_privateData = _OpenAtBy(pos, len);
|
_privateData = _OpenAtBy(pos, len);
|
||||||
memcpy(_privateData + pos, str, len);
|
memcpy(_privateData + pos, str, len);
|
||||||
}
|
}
|
||||||
@@ -324,7 +334,8 @@ BString&
|
|||||||
BString::Insert(const char *str, int32 fromOffset, int32 length, int32 pos)
|
BString::Insert(const char *str, int32 fromOffset, int32 length, int32 pos)
|
||||||
{
|
{
|
||||||
if (str) {
|
if (str) {
|
||||||
int32 len = min(length, (int32)strlen(str));
|
int32 len = (int32)strlen(str);
|
||||||
|
len = min(len, length);
|
||||||
_privateData = _OpenAtBy(pos, len);
|
_privateData = _OpenAtBy(pos, len);
|
||||||
memcpy(_privateData + pos, str + fromOffset, len);
|
memcpy(_privateData + pos, str + fromOffset, len);
|
||||||
}
|
}
|
||||||
@@ -378,7 +389,7 @@ BString::Truncate(int32 newLength, bool lazy = true)
|
|||||||
if (newLength < Length()) {
|
if (newLength < Length()) {
|
||||||
#if 0
|
#if 0
|
||||||
if (lazy)
|
if (lazy)
|
||||||
; //ToDo: Fix this
|
; //ToDo: Implement
|
||||||
else
|
else
|
||||||
#endif
|
#endif
|
||||||
_privateData = _GrowBy(newLength - Length()); // Negative value
|
_privateData = _GrowBy(newLength - Length()); // Negative value
|
||||||
@@ -396,6 +407,101 @@ BString::Remove(int32 from, int32 length)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BString&
|
||||||
|
BString::RemoveFirst(const BString &string)
|
||||||
|
{
|
||||||
|
int32 pos = _FindAfter(string.String(), 0, -1);
|
||||||
|
|
||||||
|
if (pos >= 0)
|
||||||
|
_privateData = _ShrinkAtBy(pos, string.Length());
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BString&
|
||||||
|
BString::RemoveLast(const BString &string)
|
||||||
|
{
|
||||||
|
//TODO: Implement
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BString&
|
||||||
|
BString::RemoveAll(const BString &string)
|
||||||
|
{
|
||||||
|
int32 pos = B_ERROR;
|
||||||
|
while ((pos = _FindAfter(string.String(), 0, -1)) >= 0)
|
||||||
|
_privateData = _ShrinkAtBy(pos, string.Length());
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BString&
|
||||||
|
BString::RemoveFirst(const char *str)
|
||||||
|
{
|
||||||
|
if (str) {
|
||||||
|
int32 pos = _FindAfter(str, 0, -1);
|
||||||
|
int32 len = strlen(str);
|
||||||
|
if (pos >= 0)
|
||||||
|
_privateData = _ShrinkAtBy(pos, len);
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BString&
|
||||||
|
BString::RemoveLast(const char *str)
|
||||||
|
{
|
||||||
|
//TODO: Implement
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BString&
|
||||||
|
BString::RemoveAll(const char *str)
|
||||||
|
{
|
||||||
|
if (str) {
|
||||||
|
int32 pos = B_ERROR;
|
||||||
|
int32 len = strlen(str);
|
||||||
|
while ((pos = _FindAfter(str, 0, -1)) >= 0)
|
||||||
|
_privateData = _ShrinkAtBy(pos, len);
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BString&
|
||||||
|
BString::RemoveSet(const char *setOfCharsToRemove)
|
||||||
|
{
|
||||||
|
if (setOfCharsToRemove) {
|
||||||
|
int32 pos;
|
||||||
|
while ((pos = strcspn(String(), setOfCharsToRemove)) < Length())
|
||||||
|
_privateData = _ShrinkAtBy(pos, 1);
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BString&
|
||||||
|
BString::MoveInto(BString &into, int32 from, int32 length)
|
||||||
|
{
|
||||||
|
//TODO: Implement
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
BString::MoveInto(char *into, int32 from, int32 length)
|
||||||
|
{
|
||||||
|
//TODO: Test
|
||||||
|
if (into && (from + length <= Length())) {
|
||||||
|
memcpy(into, String() + from, length);
|
||||||
|
_privateData = _ShrinkAtBy(from, length);
|
||||||
|
into[length] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*---- Compare functions ---------------------------------------------------*/
|
/*---- Compare functions ---------------------------------------------------*/
|
||||||
// Implemented inline in the header file
|
// Implemented inline in the header file
|
||||||
/*---- strcmp-style compare functions --------------------------------------*/
|
/*---- strcmp-style compare functions --------------------------------------*/
|
||||||
@@ -709,14 +815,14 @@ void
|
|||||||
BString::_DoPrepend(const char *str, int32 count)
|
BString::_DoPrepend(const char *str, int32 count)
|
||||||
{
|
{
|
||||||
assert(str != NULL);
|
assert(str != NULL);
|
||||||
_privateData = _GrowBy(count);
|
_privateData = _OpenAtBy(0, count);
|
||||||
strncpy(_privateData, str, count);
|
strncpy(_privateData, str, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int32
|
int32
|
||||||
BString::_FindAfter(const char *str, int32 offset, int32) const
|
BString::_FindAfter(const char *str, int32 offset, int32) const
|
||||||
{
|
{
|
||||||
if (offset > Length())
|
if (offset > Length())
|
||||||
return B_ERROR;
|
return B_ERROR;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user