1 // Copyright (c) 2010 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 inline int vsnprintfT(wchar_t* buffer
,
31 const wchar_t* format
,
33 return base::vswprintf(buffer
, buf_size
, format
, argptr
);
36 // Templatized backend for StringPrintF/StringAppendF. This does not finalize
37 // the va_list, the caller is expected to do that.
38 template <class StringType
>
39 static void StringAppendVT(StringType
* dst
,
40 const typename
StringType::value_type
* format
,
42 // First try with a small fixed size buffer.
43 // This buffer size should be kept in sync with StringUtilTest.GrowBoundary
44 // and StringUtilTest.StringPrintfBounds.
45 typename
StringType::value_type stack_buf
[1024];
48 GG_VA_COPY(ap_copy
, ap
);
53 int result
= vsnprintfT(stack_buf
, arraysize(stack_buf
), format
, ap_copy
);
56 if (result
>= 0 && result
< static_cast<int>(arraysize(stack_buf
))) {
58 dst
->append(stack_buf
, result
);
62 // Repeatedly increase buffer size until it fits.
63 int mem_length
= arraysize(stack_buf
);
67 // On Windows, vsnprintfT always returns the number of characters in a
68 // fully-formatted string, so if we reach this point, something else is
69 // wrong and no amount of buffer-doubling is going to fix it.
70 if (errno
!= 0 && errno
!= EOVERFLOW
)
73 // If an error other than overflow occurred, it's never going to work.
74 DLOG(WARNING
) << "Unable to printf the requested string due to error.";
77 // Try doubling the buffer size.
80 // We need exactly "result + 1" characters.
81 mem_length
= result
+ 1;
84 if (mem_length
> 32 * 1024 * 1024) {
85 // That should be plenty, don't try anything larger. This protects
86 // against huge allocations when using vsnprintfT implementations that
87 // return -1 for reasons other than overflow without setting errno.
88 DLOG(WARNING
) << "Unable to printf the requested string due to size.";
92 std::vector
<typename
StringType::value_type
> mem_buf(mem_length
);
94 // NOTE: You can only use a va_list once. Since we're in a while loop, we
95 // need to make a new copy each time so we don't use up the original.
96 GG_VA_COPY(ap_copy
, ap
);
97 result
= vsnprintfT(&mem_buf
[0], mem_length
, format
, ap_copy
);
100 if ((result
>= 0) && (result
< mem_length
)) {
102 dst
->append(&mem_buf
[0], result
);
110 std::string
StringPrintf(const char* format
, ...) {
112 va_start(ap
, format
);
114 StringAppendV(&result
, format
, ap
);
119 std::wstring
StringPrintf(const wchar_t* format
, ...) {
121 va_start(ap
, format
);
123 StringAppendV(&result
, format
, ap
);
128 std::string
StringPrintV(const char* format
, va_list ap
) {
130 StringAppendV(&result
, format
, ap
);
134 const std::string
& SStringPrintf(std::string
* dst
, const char* format
, ...) {
136 va_start(ap
, format
);
138 StringAppendV(dst
, format
, ap
);
143 const std::wstring
& SStringPrintf(std::wstring
* dst
,
144 const wchar_t* format
, ...) {
146 va_start(ap
, format
);
148 StringAppendV(dst
, format
, ap
);
153 void StringAppendF(std::string
* dst
, const char* format
, ...) {
155 va_start(ap
, format
);
156 StringAppendV(dst
, format
, ap
);
160 void StringAppendF(std::wstring
* dst
, const wchar_t* format
, ...) {
162 va_start(ap
, format
);
163 StringAppendV(dst
, format
, ap
);
167 void StringAppendV(std::string
* dst
, const char* format
, va_list ap
) {
168 StringAppendVT(dst
, format
, ap
);
171 void StringAppendV(std::wstring
* dst
, const wchar_t* format
, va_list ap
) {
172 StringAppendVT(dst
, format
, ap
);