BString: Add SetToFormatVarArgs()
This commit is contained in:
@@ -6,10 +6,11 @@
|
|||||||
#define __BSTRING__
|
#define __BSTRING__
|
||||||
|
|
||||||
|
|
||||||
#include <BeBuild.h>
|
#include <stdarg.h>
|
||||||
#include <SupportDefs.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <SupportDefs.h>
|
||||||
|
|
||||||
|
|
||||||
class BStringList;
|
class BStringList;
|
||||||
class BStringRef;
|
class BStringRef;
|
||||||
@@ -55,6 +56,8 @@ public:
|
|||||||
BString& AdoptChars(BString& from, int32 charCount);
|
BString& AdoptChars(BString& from, int32 charCount);
|
||||||
|
|
||||||
BString& SetToFormat(const char* format, ...);
|
BString& SetToFormat(const char* format, ...);
|
||||||
|
BString& SetToFormatVarArgs(const char* format,
|
||||||
|
va_list args);
|
||||||
|
|
||||||
// Substring copying
|
// Substring copying
|
||||||
BString& CopyInto(BString& into, int32 fromOffset,
|
BString& CopyInto(BString& into, int32 fromOffset,
|
||||||
|
|||||||
@@ -428,13 +428,31 @@ BString::AdoptChars(BString& string, int32 charCount)
|
|||||||
BString&
|
BString&
|
||||||
BString::SetToFormat(const char* format, ...)
|
BString::SetToFormat(const char* format, ...)
|
||||||
{
|
{
|
||||||
|
va_list args;
|
||||||
|
va_start(args, format);
|
||||||
|
SetToFormatVarArgs(format, args);
|
||||||
|
va_end(args);
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BString&
|
||||||
|
BString::SetToFormatVarArgs(const char* format, va_list args)
|
||||||
|
{
|
||||||
|
// Use a small on-stack buffer to save a second vsnprintf() call for most
|
||||||
|
// use cases.
|
||||||
int32 bufferSize = 1024;
|
int32 bufferSize = 1024;
|
||||||
char buffer[bufferSize];
|
char buffer[bufferSize];
|
||||||
|
|
||||||
va_list arg;
|
va_list clonedArgs;
|
||||||
va_start(arg, format);
|
#if __GNUC__ == 2
|
||||||
int32 bytes = vsnprintf(buffer, bufferSize, format, arg);
|
__va_copy(clonedArgs, args);
|
||||||
va_end(arg);
|
#else
|
||||||
|
va_copy(clonedArgs, args);
|
||||||
|
#endif
|
||||||
|
int32 bytes = vsnprintf(buffer, bufferSize, format, clonedArgs);
|
||||||
|
va_end(clonedArgs);
|
||||||
|
|
||||||
if (bytes < 0)
|
if (bytes < 0)
|
||||||
return Truncate(0);
|
return Truncate(0);
|
||||||
@@ -444,11 +462,7 @@ BString::SetToFormat(const char* format, ...)
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
va_list arg2;
|
bytes = vsnprintf(LockBuffer(bytes), bytes + 1, format, args);
|
||||||
va_start(arg2, format);
|
|
||||||
bytes = vsnprintf(LockBuffer(bytes), bytes + 1, format, arg2);
|
|
||||||
va_end(arg2);
|
|
||||||
|
|
||||||
if (bytes < 0)
|
if (bytes < 0)
|
||||||
bytes = 0;
|
bytes = 0;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user