Style and performance fixes. Thanks Ingo and Stephan.

This commit is contained in:
czeidler
2011-12-17 14:36:36 +13:00
parent 7e38e5101f
commit 14fc524be2
2 changed files with 40 additions and 24 deletions
+1 -1
View File
@@ -26,7 +26,7 @@ public:
bool Remove(const BString& string, bool Remove(const BString& string,
bool ignoreCase = false); bool ignoreCase = false);
void Remove(const BStringList& list, bool Remove(const BStringList& list,
bool ignoreCase = false); bool ignoreCase = false);
BString Remove(int32 index); BString Remove(int32 index);
bool Remove(int32 index, int32 count); bool Remove(int32 index, int32 count);
+39 -23
View File
@@ -1,15 +1,17 @@
/* /*
* Copyright 2011, Ingo Weinhold, [email protected]. * Copyright 2011, Ingo Weinhold, [email protected]
* Copyright 2011, Clemens Zeidler <[email protected]>
*
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#include <StringList.h> #include <StringList.h>
#include <TypeConstants.h>
#include <algorithm> #include <algorithm>
#include <StringPrivate.h> #include <StringPrivate.h>
#include <TypeConstants.h>
static int static int
@@ -127,11 +129,15 @@ BStringList::Remove(const BString& string, bool ignoreCase)
} }
void bool
BStringList::Remove(const BStringList& list, bool ignoreCase) BStringList::Remove(const BStringList& list, bool ignoreCase)
{ {
for (int32 i = 0; i < list.CountStrings(); i++) bool removedAnything = false;
Remove(list.StringAt(i), ignoreCase); int32 stringCount = list.CountStrings();
for (int32 i = 0; i < stringCount; i++)
removedAnything |= Remove(list.StringAt(i), ignoreCase);
return removedAnything;
} }
@@ -346,28 +352,30 @@ ssize_t
BStringList::FlattenedSize() const BStringList::FlattenedSize() const
{ {
ssize_t size = 0; ssize_t size = 0;
for (int32 i = 0; i < CountStrings(); i++) { int32 stringCount = CountStrings();
const char* str = StringAt(i).String(); for (int32 i = 0; i < stringCount; i++)
size += strlen(str) + 1; size += StringAt(i).Length() + 1;
}
return size; return size;
} }
status_t status_t
BStringList::Flatten(void* buffer, ssize_t size) const BStringList::Flatten(void* buf, ssize_t size) const
{ {
const char* buffer = (const char*)buf;
if (size < FlattenedSize()) if (size < FlattenedSize())
return B_NO_MEMORY; return B_NO_MEMORY;
for (int32 i = 0; i < CountStrings(); i++) { int32 stringCount = CountStrings();
const char* str = StringAt(i).String(); for (int32 i = 0; i < stringCount; i++) {
ssize_t storeSize = strlen(str) + 1; BString item = StringAt(i);
memcpy(buffer, str, storeSize); ssize_t storeSize = item.Length() + 1;
buffer = (void*)((const char*)buffer + storeSize); memcpy((void*)buffer, (const void*)item.String(), storeSize);
buffer += storeSize;
} }
return B_OK; return B_OK;
} }
@@ -377,13 +385,21 @@ BStringList::Unflatten(type_code code, const void* buffer, ssize_t size)
{ {
if (code != B_STRING_LIST_TYPE) if (code != B_STRING_LIST_TYPE)
return B_ERROR; return B_ERROR;
const char* bufferStart = (const char*)buffer;
const char* str = (const char*)buffer;
for (off_t offset = 0; offset < size; offset++) { MakeEmpty();
if (((int8*)buffer)[offset] == 0) {
Add(str); off_t offset = 0;
str = (const char*)buffer + offset + 1; while (offset < size) {
} const char* cstring = bufferStart + offset;
size_t restSize = size - offset;
size_t read = strnlen(cstring, restSize);
if (read == restSize)
return B_BAD_VALUE;
if (!Add(cstring))
return B_NO_MEMORY;
offset += read + 1;
} }
return B_OK; return B_OK;