[haiku-commits] r40767 - haiku/trunk/src/kits/support

  • From: jonas@xxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Tue, 1 Mar 2011 21:24:04 +0100 (CET)

Author: kirilla
Date: 2011-03-01 21:24:04 +0100 (Tue, 01 Mar 2011)
New Revision: 40767
Changeset: http://dev.haiku-os.org/changeset/40767

Modified:
   haiku/trunk/src/kits/support/String.cpp
Log:
Return of the on-stack buffer.

Modified: haiku/trunk/src/kits/support/String.cpp
===================================================================
--- haiku/trunk/src/kits/support/String.cpp     2011-03-01 19:26:35 UTC (rev 
40766)
+++ haiku/trunk/src/kits/support/String.cpp     2011-03-01 20:24:04 UTC (rev 
40767)
@@ -427,23 +427,31 @@
 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;
 }
 


Other related posts: