This source file includes following definitions.
- vsnprintfT
- vsnprintfT
- StringAppendVT
- StringPrintf
- StringPrintf
- StringPrintV
- SStringPrintf
- SStringPrintf
- StringAppendF
- StringAppendF
- StringAppendV
- StringAppendV
#include "base/strings/stringprintf.h"
#include <errno.h>
#include "base/scoped_clear_errno.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
namespace base {
namespace {
inline int vsnprintfT(char* buffer,
size_t buf_size,
const char* format,
va_list argptr) {
return base::vsnprintf(buffer, buf_size, format, argptr);
}
#if !defined(OS_ANDROID)
inline int vsnprintfT(wchar_t* buffer,
size_t buf_size,
const wchar_t* format,
va_list argptr) {
return base::vswprintf(buffer, buf_size, format, argptr);
}
#endif
template <class StringType>
static void StringAppendVT(StringType* dst,
const typename StringType::value_type* format,
va_list ap) {
typename StringType::value_type stack_buf[1024];
va_list ap_copy;
GG_VA_COPY(ap_copy, ap);
#if !defined(OS_WIN)
ScopedClearErrno clear_errno;
#endif
int result = vsnprintfT(stack_buf, arraysize(stack_buf), format, ap_copy);
va_end(ap_copy);
if (result >= 0 && result < static_cast<int>(arraysize(stack_buf))) {
dst->append(stack_buf, result);
return;
}
int mem_length = arraysize(stack_buf);
while (true) {
if (result < 0) {
#if defined(OS_WIN)
return;
#else
if (errno != 0 && errno != EOVERFLOW)
return;
mem_length *= 2;
#endif
} else {
mem_length = result + 1;
}
if (mem_length > 32 * 1024 * 1024) {
DLOG(WARNING) << "Unable to printf the requested string due to size.";
return;
}
std::vector<typename StringType::value_type> mem_buf(mem_length);
GG_VA_COPY(ap_copy, ap);
result = vsnprintfT(&mem_buf[0], mem_length, format, ap_copy);
va_end(ap_copy);
if ((result >= 0) && (result < mem_length)) {
dst->append(&mem_buf[0], result);
return;
}
}
}
}
std::string StringPrintf(const char* format, ...) {
va_list ap;
va_start(ap, format);
std::string result;
StringAppendV(&result, format, ap);
va_end(ap);
return result;
}
#if !defined(OS_ANDROID)
std::wstring StringPrintf(const wchar_t* format, ...) {
va_list ap;
va_start(ap, format);
std::wstring result;
StringAppendV(&result, format, ap);
va_end(ap);
return result;
}
#endif
std::string StringPrintV(const char* format, va_list ap) {
std::string result;
StringAppendV(&result, format, ap);
return result;
}
const std::string& SStringPrintf(std::string* dst, const char* format, ...) {
va_list ap;
va_start(ap, format);
dst->clear();
StringAppendV(dst, format, ap);
va_end(ap);
return *dst;
}
#if !defined(OS_ANDROID)
const std::wstring& SStringPrintf(std::wstring* dst,
const wchar_t* format, ...) {
va_list ap;
va_start(ap, format);
dst->clear();
StringAppendV(dst, format, ap);
va_end(ap);
return *dst;
}
#endif
void StringAppendF(std::string* dst, const char* format, ...) {
va_list ap;
va_start(ap, format);
StringAppendV(dst, format, ap);
va_end(ap);
}
#if !defined(OS_ANDROID)
void StringAppendF(std::wstring* dst, const wchar_t* format, ...) {
va_list ap;
va_start(ap, format);
StringAppendV(dst, format, ap);
va_end(ap);
}
#endif
void StringAppendV(std::string* dst, const char* format, va_list ap) {
StringAppendVT(dst, format, ap);
}
#if !defined(OS_ANDROID)
void StringAppendV(std::wstring* dst, const wchar_t* format, va_list ap) {
StringAppendVT(dst, format, ap);
}
#endif
}