1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/stringprintf.h"
9 #include "base/scoped_clear_errno.h"
10 #include "base/string_util.h"
11 #include "base/utf_string_conversions.h"
17 // Overloaded wrappers around vsnprintf and vswprintf. The buf_size parameter
18 // is the size of the buffer. These return the number of characters in the
19 // formatted string excluding the NUL terminator. If the buffer is not
20 // large enough to accommodate the formatted string without truncation, they
21 // return the number of characters that would be in the fully-formatted string
22 // (vsnprintf, and vswprintf on Windows), or -1 (vswprintf on POSIX platforms).
23 inline int vsnprintfT(char* buffer
,
27 return base::vsnprintf(buffer
, buf_size
, format
, argptr
);
30 #if !defined(OS_ANDROID)
31 inline int vsnprintfT(wchar_t* buffer
,
33 const wchar_t* format
,
35 return base::vswprintf(buffer
, buf_size
, format
, argptr
);
39 // Templatized backend for StringPrintF/StringAppendF. This does not finalize
40 // the va_list, the caller is expected to do that.
41 template <class StringType
>
42 static void StringAppendVT(StringType
* dst
,
43 const typename
StringType::value_type
* format
,
45 // First try with a small fixed size buffer.
46 // This buffer size should be kept in sync with StringUtilTest.GrowBoundary
47 // and StringUtilTest.StringPrintfBounds.
48 typename
StringType::value_type stack_buf
[1024];
51 GG_VA_COPY(ap_copy
, ap
);
54 ScopedClearErrno clear_errno
;
56 int result
= vsnprintfT(stack_buf
, arraysize(stack_buf
), format
, ap_copy
);
59 if (result
>= 0 && result
< static_cast<int>(arraysize(stack_buf
))) {
61 dst
->append(stack_buf
, result
);
65 // Repeatedly increase buffer size until it fits.
66 int mem_length
= arraysize(stack_buf
);
70 // On Windows, vsnprintfT always returns the number of characters in a
71 // fully-formatted string, so if we reach this point, something else is
72 // wrong and no amount of buffer-doubling is going to fix it.
73 if (errno
!= 0 && errno
!= EOVERFLOW
)
76 // If an error other than overflow occurred, it's never going to work.
77 DLOG(WARNING
) << "Unable to printf the requested string due to error.";
80 // Try doubling the buffer size.
83 // We need exactly "result + 1" characters.
84 mem_length
= result
+ 1;
87 if (mem_length
> 32 * 1024 * 1024) {
88 // That should be plenty, don't try anything larger. This protects
89 // against huge allocations when using vsnprintfT implementations that
90 // return -1 for reasons other than overflow without setting errno.
91 DLOG(WARNING
) << "Unable to printf the requested string due to size.";
95 std::vector
<typename
StringType::value_type
> mem_buf(mem_length
);
97 // NOTE: You can only use a va_list once. Since we're in a while loop, we
98 // need to make a new copy each time so we don't use up the original.
99 GG_VA_COPY(ap_copy
, ap
);
100 result
= vsnprintfT(&mem_buf
[0], mem_length
, format
, ap_copy
);
103 if ((result
>= 0) && (result
< mem_length
)) {
105 dst
->append(&mem_buf
[0], result
);
113 std::string
StringPrintf(const char* format
, ...) {
115 va_start(ap
, format
);
117 StringAppendV(&result
, format
, ap
);
122 #if !defined(OS_ANDROID)
123 std::wstring
StringPrintf(const wchar_t* format
, ...) {
125 va_start(ap
, format
);
127 StringAppendV(&result
, format
, ap
);
133 std::string
StringPrintV(const char* format
, va_list ap
) {
135 StringAppendV(&result
, format
, ap
);
139 const std::string
& SStringPrintf(std::string
* dst
, const char* format
, ...) {
141 va_start(ap
, format
);
143 StringAppendV(dst
, format
, ap
);
148 #if !defined(OS_ANDROID)
149 const std::wstring
& SStringPrintf(std::wstring
* dst
,
150 const wchar_t* format
, ...) {
152 va_start(ap
, format
);
154 StringAppendV(dst
, format
, ap
);
160 void StringAppendF(std::string
* dst
, const char* format
, ...) {
162 va_start(ap
, format
);
163 StringAppendV(dst
, format
, ap
);
167 #if !defined(OS_ANDROID)
168 void StringAppendF(std::wstring
* dst
, const wchar_t* format
, ...) {
170 va_start(ap
, format
);
171 StringAppendV(dst
, format
, ap
);
176 void StringAppendV(std::string
* dst
, const char* format
, va_list ap
) {
177 StringAppendVT(dst
, format
, ap
);
180 #if !defined(OS_ANDROID)
181 void StringAppendV(std::wstring
* dst
, const wchar_t* format
, va_list ap
) {
182 StringAppendVT(dst
, format
, ap
);