OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/stringprintf.h" | 5 #include "base/stringprintf.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 | 8 |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 // Overloaded wrappers around vsnprintf and vswprintf. The buf_size parameter | 16 // Wrapper around vsnprintf. The buf_size parameter is the size of the buffer. |
17 // is the size of the buffer. These return the number of characters in the | 17 // These return the number of characters in the formatted string excluding the |
18 // formatted string excluding the NUL terminator. If the buffer is not | 18 // NUL terminator. If the buffer is not large enough to accommodate the |
19 // large enough to accommodate the formatted string without truncation, they | 19 // formatted string without truncation, they return the number of characters |
20 // return the number of characters that would be in the fully-formatted string | 20 // 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, | 21 inline int vsnprintfT(char* buffer, |
brettw
2012/06/01 19:27:13
I think we can just remove this and call base::vsn
| |
23 size_t buf_size, | 22 size_t buf_size, |
24 const char* format, | 23 const char* format, |
25 va_list argptr) { | 24 va_list argptr) { |
26 return base::vsnprintf(buffer, buf_size, format, argptr); | 25 return base::vsnprintf(buffer, buf_size, format, argptr); |
27 } | 26 } |
28 | 27 |
29 inline int vsnprintfT(wchar_t* buffer, | |
30 size_t buf_size, | |
31 const wchar_t* format, | |
32 va_list argptr) { | |
33 return base::vswprintf(buffer, buf_size, format, argptr); | |
34 } | |
35 | |
36 // Templatized backend for StringPrintF/StringAppendF. This does not finalize | 28 // Templatized backend for StringPrintF/StringAppendF. This does not finalize |
37 // the va_list, the caller is expected to do that. | 29 // the va_list, the caller is expected to do that. |
38 template <class StringType> | 30 template <class StringType> |
39 static void StringAppendVT(StringType* dst, | 31 static void StringAppendVT(StringType* dst, |
40 const typename StringType::value_type* format, | 32 const typename StringType::value_type* format, |
41 va_list ap) { | 33 va_list ap) { |
42 // First try with a small fixed size buffer. | 34 // First try with a small fixed size buffer. |
43 // This buffer size should be kept in sync with StringUtilTest.GrowBoundary | 35 // This buffer size should be kept in sync with StringUtilTest.GrowBoundary |
44 // and StringUtilTest.StringPrintfBounds. | 36 // and StringUtilTest.StringPrintfBounds. |
45 typename StringType::value_type stack_buf[1024]; | 37 typename StringType::value_type stack_buf[1024]; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 | 101 |
110 std::string StringPrintf(const char* format, ...) { | 102 std::string StringPrintf(const char* format, ...) { |
111 va_list ap; | 103 va_list ap; |
112 va_start(ap, format); | 104 va_start(ap, format); |
113 std::string result; | 105 std::string result; |
114 StringAppendV(&result, format, ap); | 106 StringAppendV(&result, format, ap); |
115 va_end(ap); | 107 va_end(ap); |
116 return result; | 108 return result; |
117 } | 109 } |
118 | 110 |
119 std::wstring StringPrintf(const wchar_t* format, ...) { | |
120 va_list ap; | |
121 va_start(ap, format); | |
122 std::wstring result; | |
123 StringAppendV(&result, format, ap); | |
124 va_end(ap); | |
125 return result; | |
126 } | |
127 | |
128 std::string StringPrintV(const char* format, va_list ap) { | 111 std::string StringPrintV(const char* format, va_list ap) { |
129 std::string result; | 112 std::string result; |
130 StringAppendV(&result, format, ap); | 113 StringAppendV(&result, format, ap); |
131 return result; | 114 return result; |
132 } | 115 } |
133 | 116 |
134 const std::string& SStringPrintf(std::string* dst, const char* format, ...) { | 117 const std::string& SStringPrintf(std::string* dst, const char* format, ...) { |
135 va_list ap; | 118 va_list ap; |
136 va_start(ap, format); | 119 va_start(ap, format); |
137 dst->clear(); | 120 dst->clear(); |
138 StringAppendV(dst, format, ap); | 121 StringAppendV(dst, format, ap); |
139 va_end(ap); | 122 va_end(ap); |
140 return *dst; | 123 return *dst; |
141 } | 124 } |
142 | 125 |
143 const std::wstring& SStringPrintf(std::wstring* dst, | |
144 const wchar_t* format, ...) { | |
145 va_list ap; | |
146 va_start(ap, format); | |
147 dst->clear(); | |
148 StringAppendV(dst, format, ap); | |
149 va_end(ap); | |
150 return *dst; | |
151 } | |
152 | |
153 void StringAppendF(std::string* dst, const char* format, ...) { | 126 void StringAppendF(std::string* dst, const char* format, ...) { |
154 va_list ap; | 127 va_list ap; |
155 va_start(ap, format); | 128 va_start(ap, format); |
156 StringAppendV(dst, format, ap); | 129 StringAppendV(dst, format, ap); |
157 va_end(ap); | 130 va_end(ap); |
158 } | 131 } |
159 | 132 |
160 void StringAppendF(std::wstring* dst, const wchar_t* format, ...) { | |
161 va_list ap; | |
162 va_start(ap, format); | |
163 StringAppendV(dst, format, ap); | |
164 va_end(ap); | |
165 } | |
166 | |
167 void StringAppendV(std::string* dst, const char* format, va_list ap) { | 133 void StringAppendV(std::string* dst, const char* format, va_list ap) { |
168 StringAppendVT(dst, format, ap); | 134 StringAppendVT(dst, format, ap); |
169 } | 135 } |
170 | 136 |
171 void StringAppendV(std::wstring* dst, const wchar_t* format, va_list ap) { | |
172 StringAppendVT(dst, format, ap); | |
173 } | |
174 | |
175 } // namespace base | 137 } // namespace base |
OLD | NEW |