blob: 537873d71c844435a71999c00d3cd5f7d6700565 [file] [log] [blame]
[email protected]8ad97ad2013-06-08 06:05:471// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]e6811ed52010-08-17 03:45:372// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]8ad97ad2013-06-08 06:05:475#include "base/strings/stringprintf.h"
[email protected]e6811ed52010-08-17 03:45:376
7#include <errno.h>
8
thestigd0f0a4b2014-12-18 22:07:299#include <vector>
10
[email protected]34d58a22013-05-02 23:06:2611#include "base/scoped_clear_errno.h"
[email protected]8ad97ad2013-06-08 06:05:4712#include "base/strings/string_util.h"
[email protected]a4ea1f12013-06-07 18:37:0713#include "base/strings/utf_string_conversions.h"
[email protected]e6811ed52010-08-17 03:45:3714
15namespace base {
16
17namespace {
18
19// Overloaded wrappers around vsnprintf and vswprintf. The buf_size parameter
20// is the size of the buffer. These return the number of characters in the
21// formatted string excluding the NUL terminator. If the buffer is not
22// large enough to accommodate the formatted string without truncation, they
23// return the number of characters that would be in the fully-formatted string
24// (vsnprintf, and vswprintf on Windows), or -1 (vswprintf on POSIX platforms).
25inline int vsnprintfT(char* buffer,
26 size_t buf_size,
27 const char* format,
28 va_list argptr) {
29 return base::vsnprintf(buffer, buf_size, format, argptr);
30}
31
thestigd0f0a4b2014-12-18 22:07:2932#if defined(OS_WIN)
[email protected]e6811ed52010-08-17 03:45:3733inline int vsnprintfT(wchar_t* buffer,
34 size_t buf_size,
35 const wchar_t* format,
36 va_list argptr) {
37 return base::vswprintf(buffer, buf_size, format, argptr);
38}
[email protected]90ea0782012-08-01 10:30:0239#endif
[email protected]e6811ed52010-08-17 03:45:3740
41// Templatized backend for StringPrintF/StringAppendF. This does not finalize
42// the va_list, the caller is expected to do that.
43template <class StringType>
44static void StringAppendVT(StringType* dst,
45 const typename StringType::value_type* format,
46 va_list ap) {
47 // First try with a small fixed size buffer.
48 // This buffer size should be kept in sync with StringUtilTest.GrowBoundary
49 // and StringUtilTest.StringPrintfBounds.
50 typename StringType::value_type stack_buf[1024];
51
52 va_list ap_copy;
scottmg5cfb9ef2014-12-11 23:10:1153 va_copy(ap_copy, ap);
[email protected]e6811ed52010-08-17 03:45:3754
55#if !defined(OS_WIN)
[email protected]34d58a22013-05-02 23:06:2656 ScopedClearErrno clear_errno;
[email protected]e6811ed52010-08-17 03:45:3757#endif
58 int result = vsnprintfT(stack_buf, arraysize(stack_buf), format, ap_copy);
59 va_end(ap_copy);
60
61 if (result >= 0 && result < static_cast<int>(arraysize(stack_buf))) {
62 // It fit.
63 dst->append(stack_buf, result);
64 return;
65 }
66
67 // Repeatedly increase buffer size until it fits.
68 int mem_length = arraysize(stack_buf);
69 while (true) {
70 if (result < 0) {
[email protected]30de3a32014-03-14 18:25:4871#if defined(OS_WIN)
[email protected]e6811ed52010-08-17 03:45:3772 // On Windows, vsnprintfT always returns the number of characters in a
73 // fully-formatted string, so if we reach this point, something else is
74 // wrong and no amount of buffer-doubling is going to fix it.
[email protected]30de3a32014-03-14 18:25:4875 return;
76#else
[email protected]e6811ed52010-08-17 03:45:3777 if (errno != 0 && errno != EOVERFLOW)
[email protected]e6811ed52010-08-17 03:45:3778 return;
[email protected]e6811ed52010-08-17 03:45:3779 // Try doubling the buffer size.
80 mem_length *= 2;
[email protected]30de3a32014-03-14 18:25:4881#endif
[email protected]e6811ed52010-08-17 03:45:3782 } else {
83 // We need exactly "result + 1" characters.
84 mem_length = result + 1;
85 }
86
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.";
92 return;
93 }
94
95 std::vector<typename StringType::value_type> mem_buf(mem_length);
96
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.
scottmg5cfb9ef2014-12-11 23:10:1199 va_copy(ap_copy, ap);
[email protected]e6811ed52010-08-17 03:45:37100 result = vsnprintfT(&mem_buf[0], mem_length, format, ap_copy);
101 va_end(ap_copy);
102
103 if ((result >= 0) && (result < mem_length)) {
104 // It fit.
105 dst->append(&mem_buf[0], result);
106 return;
107 }
108 }
109}
110
111} // namespace
112
113std::string StringPrintf(const char* format, ...) {
114 va_list ap;
115 va_start(ap, format);
116 std::string result;
117 StringAppendV(&result, format, ap);
118 va_end(ap);
119 return result;
120}
121
thestigd0f0a4b2014-12-18 22:07:29122#if defined(OS_WIN)
[email protected]e6811ed52010-08-17 03:45:37123std::wstring StringPrintf(const wchar_t* format, ...) {
124 va_list ap;
125 va_start(ap, format);
126 std::wstring result;
127 StringAppendV(&result, format, ap);
128 va_end(ap);
129 return result;
130}
[email protected]90ea0782012-08-01 10:30:02131#endif
[email protected]e6811ed52010-08-17 03:45:37132
133std::string StringPrintV(const char* format, va_list ap) {
134 std::string result;
135 StringAppendV(&result, format, ap);
136 return result;
137}
138
139const std::string& SStringPrintf(std::string* dst, const char* format, ...) {
140 va_list ap;
141 va_start(ap, format);
142 dst->clear();
143 StringAppendV(dst, format, ap);
144 va_end(ap);
145 return *dst;
146}
147
thestigd0f0a4b2014-12-18 22:07:29148#if defined(OS_WIN)
[email protected]e6811ed52010-08-17 03:45:37149const std::wstring& SStringPrintf(std::wstring* dst,
150 const wchar_t* format, ...) {
151 va_list ap;
152 va_start(ap, format);
153 dst->clear();
154 StringAppendV(dst, format, ap);
155 va_end(ap);
156 return *dst;
157}
[email protected]90ea0782012-08-01 10:30:02158#endif
[email protected]e6811ed52010-08-17 03:45:37159
160void StringAppendF(std::string* dst, const char* format, ...) {
161 va_list ap;
162 va_start(ap, format);
163 StringAppendV(dst, format, ap);
164 va_end(ap);
165}
166
thestigd0f0a4b2014-12-18 22:07:29167#if defined(OS_WIN)
[email protected]e6811ed52010-08-17 03:45:37168void StringAppendF(std::wstring* dst, const wchar_t* format, ...) {
169 va_list ap;
170 va_start(ap, format);
171 StringAppendV(dst, format, ap);
172 va_end(ap);
173}
[email protected]90ea0782012-08-01 10:30:02174#endif
[email protected]e6811ed52010-08-17 03:45:37175
176void StringAppendV(std::string* dst, const char* format, va_list ap) {
177 StringAppendVT(dst, format, ap);
178}
179
thestigd0f0a4b2014-12-18 22:07:29180#if defined(OS_WIN)
[email protected]e6811ed52010-08-17 03:45:37181void StringAppendV(std::wstring* dst, const wchar_t* format, va_list ap) {
182 StringAppendVT(dst, format, ap);
183}
[email protected]90ea0782012-08-01 10:30:02184#endif
[email protected]e6811ed52010-08-17 03:45:37185
186} // namespace base