mirror of
https://foundry.openuru.org/gitblit/r/CWE-ou-minkata.git
synced 2025-07-18 11:19:10 +00:00
Add Truncate() method to plStringStream, which truncates the buffer without reallocating memory
This commit is contained in:
@ -822,17 +822,16 @@ plString operator+(const char *left, const plString &right)
|
||||
|
||||
plStringStream &plStringStream::append(const char *data, size_t length)
|
||||
{
|
||||
size_t bufSize = ICanHasHeap() ? fBufSize : STRING_STACK_SIZE;
|
||||
char *bufp = ICanHasHeap() ? fBuffer : fShort;
|
||||
|
||||
if (fLength + length > bufSize) {
|
||||
size_t bigSize = bufSize;
|
||||
if (fLength + length > fBufSize) {
|
||||
size_t bigSize = fBufSize;
|
||||
do {
|
||||
bigSize *= 2;
|
||||
} while (fLength + length > bigSize);
|
||||
|
||||
char *bigger = new char[bigSize];
|
||||
memcpy(bigger, GetRawBuffer(), bufSize);
|
||||
memcpy(bigger, GetRawBuffer(), fBufSize);
|
||||
if (ICanHasHeap())
|
||||
delete [] fBuffer;
|
||||
fBuffer = bufp = bigger;
|
||||
|
@ -594,7 +594,7 @@ public:
|
||||
/** Construct a new empty string stream. The first STRING_STACK_SIZE
|
||||
* bytes are allocated on the stack for further efficiency.
|
||||
*/
|
||||
plStringStream() : fLength(0) { }
|
||||
plStringStream() : fBufSize(STRING_STACK_SIZE), fLength(0) { }
|
||||
|
||||
/** Destructor, frees any allocated heap memory owned by the stream. */
|
||||
~plStringStream() { if (ICanHasHeap()) delete [] fBuffer; }
|
||||
@ -638,19 +638,23 @@ public:
|
||||
size_t GetLength() const { return fLength; }
|
||||
|
||||
/** Convert the stream's data to a UTF-8 string. */
|
||||
plString GetString() { return plString::FromUtf8(GetRawBuffer(), fLength); }
|
||||
plString GetString() const { return plString::FromUtf8(GetRawBuffer(), fLength); }
|
||||
|
||||
/** Reset the stream's append pointer back to the beginning.
|
||||
* This does not incur a reallocation of the buffer -- it is left
|
||||
* with as much space as it had before, making this method more
|
||||
* useful for re-using string streams in loops.
|
||||
*/
|
||||
void Truncate() { fLength = 0; }
|
||||
|
||||
private:
|
||||
union {
|
||||
struct {
|
||||
char *fBuffer;
|
||||
size_t fBufSize;
|
||||
};
|
||||
char *fBuffer;
|
||||
char fShort[STRING_STACK_SIZE];
|
||||
};
|
||||
size_t fLength;
|
||||
size_t fBufSize, fLength;
|
||||
|
||||
bool ICanHasHeap() const { return fLength > STRING_STACK_SIZE; }
|
||||
bool ICanHasHeap() const { return fBufSize > STRING_STACK_SIZE; }
|
||||
};
|
||||
|
||||
/** \p strlen implementation for UniChar based C-style string buffers. */
|
||||
|
Reference in New Issue
Block a user