[haiku-commits] r40720 - in haiku/trunk: headers/os/support src/kits/support

  • From: jonas@xxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Sun, 27 Feb 2011 10:25:26 +0100 (CET)

Author: kirilla
Date: 2011-02-27 10:25:26 +0100 (Sun, 27 Feb 2011)
New Revision: 40720
Changeset: http://dev.haiku-os.org/changeset/40720

Modified:
   haiku/trunk/headers/os/support/String.h
   haiku/trunk/src/kits/support/String.cpp
Log:
Cleanup. Efficiency makerover.

Modified: haiku/trunk/headers/os/support/String.h
===================================================================
--- haiku/trunk/headers/os/support/String.h     2011-02-27 08:34:08 UTC (rev 
40719)
+++ haiku/trunk/headers/os/support/String.h     2011-02-27 09:25:26 UTC (rev 
40720)
@@ -49,7 +49,7 @@
                        BString&                SetToChars(const BString& 
string, int32 charCount);
                        BString&                AdoptChars(BString& from, int32 
charCount);
                        
-                       BString&                SetToArguments(const char 
*format, ...);
+                       BString&                SetToArguments(const char* 
format, ...);
 
                        // Substring copying
                        BString&                CopyInto(BString& into, int32 
fromOffset,

Modified: haiku/trunk/src/kits/support/String.cpp
===================================================================
--- haiku/trunk/src/kits/support/String.cpp     2011-02-27 08:34:08 UTC (rev 
40719)
+++ haiku/trunk/src/kits/support/String.cpp     2011-02-27 09:25:26 UTC (rev 
40720)
@@ -425,20 +425,30 @@
 
 
 BString&
-BString::SetToArguments(const char *format, ...)
+BString::SetToArguments(const char* format, ...)
 {
+       int32 bufferSize = 128;
+       char buffer[bufferSize];
+       
        va_list arg;
        va_start(arg, format);
-       int32 bytes = vsnprintf(LockBuffer(0), 0, format, arg);
+       int32 bytes = vsnprintf(buffer, bufferSize, format, arg);
        va_end(arg);
-       UnlockBuffer(0);
+
+       if (bytes < 0) {
+               return *this;
+       }
        
-       va_list arg2;
-       va_start(arg2, format);
-       bytes = vsnprintf(LockBuffer(bytes), bytes + 1, format, arg2);
-       va_end(arg2);
-       UnlockBuffer(bytes);
-
+       if (bytes < bufferSize) {
+               SetTo(buffer);
+       } else {
+               va_list arg2;
+               va_start(arg2, format);
+               bytes = vsnprintf(LockBuffer(bytes), bytes + 1, format, arg2);
+               va_end(arg2);
+               UnlockBuffer(bytes);
+       }
+       
        return *this;
 }
 


Other related posts: