Browse Source

Fix a major plStringStream bug, and add an SSO-like optimization to it

Michael Hansen 12 years ago
parent
commit
11558b8301
  1. 30
      Sources/Plasma/CoreLib/plString.cpp
  2. 22
      Sources/Plasma/CoreLib/plString.h

30
Sources/Plasma/CoreLib/plString.cpp

@ -800,14 +800,25 @@ plString operator+(const char *left, const plString &right)
plStringStream &plStringStream::append(const char *data, size_t length) plStringStream &plStringStream::append(const char *data, size_t length)
{ {
if (fLength + length > fBufSize) { size_t bufSize = ICanHasHeap() ? fBufSize : 256;
char *bigger = new char[fBufSize * 2]; char *bufp = ICanHasHeap() ? fBuffer : fShort;
memcpy(bigger, fBuffer, fBufSize);
if (fLength + length > bufSize) {
size_t bigSize = bufSize;
do {
hsAssert(bigSize * 2, "plStringStream buffer too large");
bigSize *= 2;
} while (fLength + length > bigSize);
char *bigger = new char[bigSize];
memcpy(bigger, GetRawBuffer(), bufSize);
if (ICanHasHeap())
delete [] fBuffer; delete [] fBuffer;
fBuffer = bigger; fBuffer = bufp = bigger;
fBufSize *= 2; fBufSize = bigSize;
} }
memcpy(fBuffer + fLength, data, length);
memcpy(bufp + fLength, data, length);
fLength += length; fLength += length;
return *this; return *this;
} }
@ -832,6 +843,13 @@ plStringStream &plStringStream::operator<<(unsigned int num)
return operator<<(buffer); return operator<<(buffer);
} }
plStringStream &plStringStream::operator<<(double num)
{
char buffer[64];
snprintf(buffer, 64, "%f", num);
return operator<<(buffer);
}
size_t ustrlen(const UniChar *ustr, size_t max) size_t ustrlen(const UniChar *ustr, size_t max)
{ {
size_t length = 0; size_t length = 0;

22
Sources/Plasma/CoreLib/plString.h

@ -341,17 +341,19 @@ plString operator+(const char *left, const plString &right);
class plStringStream class plStringStream
{ {
public: public:
plStringStream() : fBufSize(256), fLength(0) plStringStream() : fLength(0)
{ {
fBuffer = new char[fBufSize]; fShort[0] = 0;
} }
~plStringStream() { delete [] fBuffer; } ~plStringStream() { if (ICanHasHeap()) delete [] fBuffer; }
plStringStream &append(const char *data, size_t length); plStringStream &append(const char *data, size_t length);
plStringStream &operator<<(const char *text); plStringStream &operator<<(const char *text);
plStringStream &operator<<(int num); plStringStream &operator<<(int num);
plStringStream &operator<<(unsigned int num); plStringStream &operator<<(unsigned int num);
plStringStream &operator<<(float num) { return operator<<(static_cast<double>(num)); }
plStringStream &operator<<(double num);
plStringStream &operator<<(char ch) { return append(&ch, 1); } plStringStream &operator<<(char ch) { return append(&ch, 1); }
plStringStream &operator<<(const plString &text) plStringStream &operator<<(const plString &text)
@ -359,13 +361,25 @@ public:
return append(text.c_str(), text.GetSize()); return append(text.c_str(), text.GetSize());
} }
const char *GetRawBuffer() const // WARNING: Not null-terminated!
{
return ICanHasHeap() ? fBuffer : fShort;
}
size_t GetLength() const { return fLength; } size_t GetLength() const { return fLength; }
plString GetString() { return plString::FromUtf8(fBuffer, fLength); } plString GetString() { return plString::FromUtf8(GetRawBuffer(), fLength); }
private: private:
union {
struct {
char *fBuffer; char *fBuffer;
size_t fBufSize; size_t fBufSize;
};
char fShort[256];
};
size_t fLength; size_t fLength;
bool ICanHasHeap() const { return fLength > 256; }
}; };
size_t ustrlen(const UniChar *ustr, size_t max = plString::kSizeAuto); size_t ustrlen(const UniChar *ustr, size_t max = plString::kSizeAuto);

Loading…
Cancel
Save