From 146d274d81f795ef7189e0ade897e8f237bf403f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Sundstr=C3=B6m?= Date: Tue, 1 Mar 2011 20:24:04 +0000 Subject: [PATCH] Return of the on-stack buffer. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@40767 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kits/support/String.cpp | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/kits/support/String.cpp b/src/kits/support/String.cpp index f4781987fe..1e9d087917 100644 --- a/src/kits/support/String.cpp +++ b/src/kits/support/String.cpp @@ -427,23 +427,31 @@ BString::AdoptChars(BString& string, int32 charCount) BString& BString::SetToFormat(const char* format, ...) { + int32 bufferSize = 1024; + char buffer[bufferSize]; + va_list arg; va_start(arg, format); - int32 bytes = vsnprintf(LockBuffer(0), Length() + 1, format, arg); + int32 bytes = vsnprintf(buffer, bufferSize, format, arg); va_end(arg); - - if (bytes <= Length()) { - if (bytes < 0) - bytes = 0; - UnlockBuffer(bytes); - } else { - va_list arg2; - va_start(arg2, format); - bytes = vsnprintf(LockBuffer(bytes), bytes + 1, format, arg2); - va_end(arg2); - UnlockBuffer(bytes); + + if (bytes < 0) + return Truncate(0); + + if (bytes < bufferSize) { + SetTo(buffer); + return *this; } - + + va_list arg2; + va_start(arg2, format); + bytes = vsnprintf(LockBuffer(bytes), bytes + 1, format, arg2); + va_end(arg2); + + if (bytes < 0) + bytes = 0; + + UnlockBuffer(bytes); return *this; }