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/string_util.h"
10 #include "base/utf_string_conversions.h"
16 // Overloaded wrappers around vsnprintf and vswprintf. The buf_size parameter
17 // is the size of the buffer. These return the number of characters in the
18 // formatted string excluding the NUL terminator. If the buffer is not
19 // large enough to accommodate the formatted string without truncation, they
20 // return the number of characters that would be in the fully-formatted string
21 // (vsnprintf, and vswprintf on Windows), or -1 (vswprintf on POSIX platforms).
22 inline int vsnprintfT(char* buffer
,
26 return base::vsnprintf(buffer
, buf_size
, format
, argptr
);
29 #if !defined(OS_ANDROID)
30 inline int vsnprintfT(wchar_t* buffer
,
32 const wchar_t* format
,
34 return base::vswprintf(buffer
, buf_size
, format
, argptr
);
38 // Templatized backend for StringPrintF/StringAppendF. This does not finalize
39 // the va_list, the caller is expected to do that.
40 template <class StringType
>
41 static void StringAppendVT(StringType
* dst
,
42 const typename
StringType::value_type
* format
,
44 // First try with a small fixed size buffer.
45 // This buffer size should be kept in sync with StringUtilTest.GrowBoundary
46 // and StringUtilTest.StringPrintfBounds.
47 typename
StringType::value_type stack_buf
[1024];
50 GG_VA_COPY(ap_copy
, ap
);
55 int result
= vsnprintfT(stack_buf
, arraysize(stack_buf
), format
, ap_copy
);
58 if (result
>= 0 && result
< static_cast<int>(arraysize(stack_buf
))) {
60 dst
->append(stack_buf
, result
);
64 // Repeatedly increase buffer size until it fits.
65 int mem_length
= arraysize(stack_buf
);
69 // On Windows, vsnprintfT always returns the number of characters in a
70 // fully-formatted string, so if we reach this point, something else is
71 // wrong and no amount of buffer-doubling is going to fix it.
72 if (errno
!= 0 && errno
!= EOVERFLOW
)
75 // If an error other than overflow occurred, it's never going to work.
76 DLOG(WARNING
) << "Unable to printf the requested string due to error.";
79 // Try doubling the buffer size.
82 // We need exactly "result + 1" characters.
83 mem_length
= result
+ 1;
86 if (mem_length
> 32 * 1024 * 1024) {
87 // That should be plenty, don't try anything larger. This protects
88 // against huge allocations when using vsnprintfT implementations that
89 // return -1 for reasons other than overflow without setting errno.
90 DLOG(WARNING
) << "Unable to printf the requested string due to size.";
94 std::vector
<typename
StringType::value_type
> mem_buf(mem_length
);
96 // NOTE: You can only use a va_list once. Since we're in a while loop, we
97 // need to make a new copy each time so we don't use up the original.
98 GG_VA_COPY(ap_copy
, ap
);
99 result
= vsnprintfT(&mem_buf
[0], mem_length
, format
, ap_copy
);
102 if ((result
>= 0) && (result
< mem_length
)) {
104 dst
->append(&mem_buf
[0], result
);
112 std::string
StringPrintf(const char* format
, ...) {
114 va_start(ap
, format
);
116 StringAppendV(&result
, format
, ap
);
121 #if !defined(OS_ANDROID)
122 std::wstring
StringPrintf(const wchar_t* format
, ...) {
124 va_start(ap
, format
);
126 StringAppendV(&result
, format
, ap
);
132 std::string
StringPrintV(const char* format
, va_list ap
) {
134 StringAppendV(&result
, format
, ap
);
138 const std::string
& SStringPrintf(std::string
* dst
, const char* format
, ...) {
140 va_start(ap
, format
);
142 StringAppendV(dst
, format
, ap
);
147 #if !defined(OS_ANDROID)
148 const std::wstring
& SStringPrintf(std::wstring
* dst
,
149 const wchar_t* format
, ...) {
151 va_start(ap
, format
);
153 StringAppendV(dst
, format
, ap
);
159 void StringAppendF(std::string
* dst
, const char* format
, ...) {
161 va_start(ap
, format
);
162 StringAppendV(dst
, format
, ap
);
166 #if !defined(OS_ANDROID)
167 void StringAppendF(std::wstring
* dst
, const wchar_t* format
, ...) {
169 va_start(ap
, format
);
170 StringAppendV(dst
, format
, ap
);
175 void StringAppendV(std::string
* dst
, const char* format
, va_list ap
) {
176 StringAppendVT(dst
, format
, ap
);
179 #if !defined(OS_ANDROID)
180 void StringAppendV(std::wstring
* dst
, const wchar_t* format
, va_list ap
) {
181 StringAppendVT(dst
, format
, ap
);