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 // 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 | 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 | 18 // formatted string excluding the NUL terminator. If the buffer is not |
19 // large enough to accommodate the formatted string without truncation, they | 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 | 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). | 21 // (vsnprintf, and vswprintf on Windows), or -1 (vswprintf on POSIX platforms). |
22 inline int vsnprintfT(char* buffer, | 22 inline int vsnprintfT(char* buffer, |
23 size_t buf_size, | 23 size_t buf_size, |
24 const char* format, | 24 const char* format, |
25 va_list argptr) { | 25 va_list argptr) { |
26 return base::vsnprintf(buffer, buf_size, format, argptr); | 26 return base::vsnprintf(buffer, buf_size, format, argptr); |
27 } | 27 } |
28 | 28 |
| 29 #if !defined(OS_ANDROID) |
29 inline int vsnprintfT(wchar_t* buffer, | 30 inline int vsnprintfT(wchar_t* buffer, |
30 size_t buf_size, | 31 size_t buf_size, |
31 const wchar_t* format, | 32 const wchar_t* format, |
32 va_list argptr) { | 33 va_list argptr) { |
33 return base::vswprintf(buffer, buf_size, format, argptr); | 34 return base::vswprintf(buffer, buf_size, format, argptr); |
34 } | 35 } |
| 36 #endif |
35 | 37 |
36 // Templatized backend for StringPrintF/StringAppendF. This does not finalize | 38 // Templatized backend for StringPrintF/StringAppendF. This does not finalize |
37 // the va_list, the caller is expected to do that. | 39 // the va_list, the caller is expected to do that. |
38 template <class StringType> | 40 template <class StringType> |
39 static void StringAppendVT(StringType* dst, | 41 static void StringAppendVT(StringType* dst, |
40 const typename StringType::value_type* format, | 42 const typename StringType::value_type* format, |
41 va_list ap) { | 43 va_list ap) { |
42 // First try with a small fixed size buffer. | 44 // First try with a small fixed size buffer. |
43 // This buffer size should be kept in sync with StringUtilTest.GrowBoundary | 45 // This buffer size should be kept in sync with StringUtilTest.GrowBoundary |
44 // and StringUtilTest.StringPrintfBounds. | 46 // and StringUtilTest.StringPrintfBounds. |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 | 111 |
110 std::string StringPrintf(const char* format, ...) { | 112 std::string StringPrintf(const char* format, ...) { |
111 va_list ap; | 113 va_list ap; |
112 va_start(ap, format); | 114 va_start(ap, format); |
113 std::string result; | 115 std::string result; |
114 StringAppendV(&result, format, ap); | 116 StringAppendV(&result, format, ap); |
115 va_end(ap); | 117 va_end(ap); |
116 return result; | 118 return result; |
117 } | 119 } |
118 | 120 |
| 121 #if !defined(OS_ANDROID) |
119 std::wstring StringPrintf(const wchar_t* format, ...) { | 122 std::wstring StringPrintf(const wchar_t* format, ...) { |
120 va_list ap; | 123 va_list ap; |
121 va_start(ap, format); | 124 va_start(ap, format); |
122 std::wstring result; | 125 std::wstring result; |
123 StringAppendV(&result, format, ap); | 126 StringAppendV(&result, format, ap); |
124 va_end(ap); | 127 va_end(ap); |
125 return result; | 128 return result; |
126 } | 129 } |
| 130 #endif |
127 | 131 |
128 std::string StringPrintV(const char* format, va_list ap) { | 132 std::string StringPrintV(const char* format, va_list ap) { |
129 std::string result; | 133 std::string result; |
130 StringAppendV(&result, format, ap); | 134 StringAppendV(&result, format, ap); |
131 return result; | 135 return result; |
132 } | 136 } |
133 | 137 |
134 const std::string& SStringPrintf(std::string* dst, const char* format, ...) { | 138 const std::string& SStringPrintf(std::string* dst, const char* format, ...) { |
135 va_list ap; | 139 va_list ap; |
136 va_start(ap, format); | 140 va_start(ap, format); |
137 dst->clear(); | 141 dst->clear(); |
138 StringAppendV(dst, format, ap); | 142 StringAppendV(dst, format, ap); |
139 va_end(ap); | 143 va_end(ap); |
140 return *dst; | 144 return *dst; |
141 } | 145 } |
142 | 146 |
| 147 #if !defined(OS_ANDROID) |
143 const std::wstring& SStringPrintf(std::wstring* dst, | 148 const std::wstring& SStringPrintf(std::wstring* dst, |
144 const wchar_t* format, ...) { | 149 const wchar_t* format, ...) { |
145 va_list ap; | 150 va_list ap; |
146 va_start(ap, format); | 151 va_start(ap, format); |
147 dst->clear(); | 152 dst->clear(); |
148 StringAppendV(dst, format, ap); | 153 StringAppendV(dst, format, ap); |
149 va_end(ap); | 154 va_end(ap); |
150 return *dst; | 155 return *dst; |
151 } | 156 } |
| 157 #endif |
152 | 158 |
153 void StringAppendF(std::string* dst, const char* format, ...) { | 159 void StringAppendF(std::string* dst, const char* format, ...) { |
154 va_list ap; | 160 va_list ap; |
155 va_start(ap, format); | 161 va_start(ap, format); |
156 StringAppendV(dst, format, ap); | 162 StringAppendV(dst, format, ap); |
157 va_end(ap); | 163 va_end(ap); |
158 } | 164 } |
159 | 165 |
| 166 #if !defined(OS_ANDROID) |
160 void StringAppendF(std::wstring* dst, const wchar_t* format, ...) { | 167 void StringAppendF(std::wstring* dst, const wchar_t* format, ...) { |
161 va_list ap; | 168 va_list ap; |
162 va_start(ap, format); | 169 va_start(ap, format); |
163 StringAppendV(dst, format, ap); | 170 StringAppendV(dst, format, ap); |
164 va_end(ap); | 171 va_end(ap); |
165 } | 172 } |
| 173 #endif |
166 | 174 |
167 void StringAppendV(std::string* dst, const char* format, va_list ap) { | 175 void StringAppendV(std::string* dst, const char* format, va_list ap) { |
168 StringAppendVT(dst, format, ap); | 176 StringAppendVT(dst, format, ap); |
169 } | 177 } |
170 | 178 |
| 179 #if !defined(OS_ANDROID) |
171 void StringAppendV(std::wstring* dst, const wchar_t* format, va_list ap) { | 180 void StringAppendV(std::wstring* dst, const wchar_t* format, va_list ap) { |
172 StringAppendVT(dst, format, ap); | 181 StringAppendVT(dst, format, ap); |
173 } | 182 } |
| 183 #endif |
174 | 184 |
175 } // namespace base | 185 } // namespace base |
OLD | NEW |