Browse Source

Remove an unnecessary extra heap allocation in plString::Format

Michael Hansen 12 years ago
parent
commit
a7d4d42b85
  1. 14
      Sources/Plasma/CoreLib/plString.cpp

14
Sources/Plasma/CoreLib/plString.cpp

@ -512,18 +512,20 @@ plString plString::IFormat(const char *fmt, va_list vptr)
int size = 4096; int size = 4096;
for ( ;; ) { for ( ;; ) {
va_copy(vptr, vptr_save); va_copy(vptr, vptr_save);
std::auto_ptr<char> bigbuffer(new char[size]); plStringBuffer<char> bigbuffer;
chars = vsnprintf(bigbuffer.get(), size, fmt, vptr); char *data = bigbuffer.CreateWritableBuffer(size);
chars = vsnprintf(data, size, fmt, vptr);
if (chars >= 0) if (chars >= 0)
return plString::FromUtf8(bigbuffer.get(), chars); return bigbuffer;
size *= 2; size *= 2;
} }
} else if (chars >= 256) { } else if (chars >= 256) {
va_copy(vptr, vptr_save); va_copy(vptr, vptr_save);
std::unique_ptr<char> bigbuffer(new char[chars+1]); plStringBuffer<char> bigbuffer;
vsnprintf(bigbuffer.get(), chars+1, fmt, vptr); char *data = bigbuffer.CreateWritableBuffer(chars+1);
return plString::FromUtf8(bigbuffer.get(), chars); vsnprintf(data, chars+1, fmt, vptr);
return bigbuffer;
} }
return plString::FromUtf8(buffer, chars); return plString::FromUtf8(buffer, chars);

Loading…
Cancel
Save