| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/strings/stringprintf.h" | 5 #include "base/strings/stringprintf.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 | 8 |
| 9 #include <vector> |
| 10 |
| 9 #include "base/scoped_clear_errno.h" | 11 #include "base/scoped_clear_errno.h" |
| 10 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 12 | 14 |
| 13 namespace base { | 15 namespace base { |
| 14 | 16 |
| 15 namespace { | 17 namespace { |
| 16 | 18 |
| 17 // Overloaded wrappers around vsnprintf and vswprintf. The buf_size parameter | 19 // Overloaded wrappers around vsnprintf and vswprintf. The buf_size parameter |
| 18 // is the size of the buffer. These return the number of characters in the | 20 // is the size of the buffer. These return the number of characters in the |
| 19 // formatted string excluding the NUL terminator. If the buffer is not | 21 // formatted string excluding the NUL terminator. If the buffer is not |
| 20 // large enough to accommodate the formatted string without truncation, they | 22 // large enough to accommodate the formatted string without truncation, they |
| 21 // return the number of characters that would be in the fully-formatted string | 23 // return the number of characters that would be in the fully-formatted string |
| 22 // (vsnprintf, and vswprintf on Windows), or -1 (vswprintf on POSIX platforms). | 24 // (vsnprintf, and vswprintf on Windows), or -1 (vswprintf on POSIX platforms). |
| 23 inline int vsnprintfT(char* buffer, | 25 inline int vsnprintfT(char* buffer, |
| 24 size_t buf_size, | 26 size_t buf_size, |
| 25 const char* format, | 27 const char* format, |
| 26 va_list argptr) { | 28 va_list argptr) { |
| 27 return base::vsnprintf(buffer, buf_size, format, argptr); | 29 return base::vsnprintf(buffer, buf_size, format, argptr); |
| 28 } | 30 } |
| 29 | 31 |
| 30 #if !defined(OS_ANDROID) | 32 #if defined(OS_WIN) |
| 31 inline int vsnprintfT(wchar_t* buffer, | 33 inline int vsnprintfT(wchar_t* buffer, |
| 32 size_t buf_size, | 34 size_t buf_size, |
| 33 const wchar_t* format, | 35 const wchar_t* format, |
| 34 va_list argptr) { | 36 va_list argptr) { |
| 35 return base::vswprintf(buffer, buf_size, format, argptr); | 37 return base::vswprintf(buffer, buf_size, format, argptr); |
| 36 } | 38 } |
| 37 #endif | 39 #endif |
| 38 | 40 |
| 39 // Templatized backend for StringPrintF/StringAppendF. This does not finalize | 41 // Templatized backend for StringPrintF/StringAppendF. This does not finalize |
| 40 // the va_list, the caller is expected to do that. | 42 // the va_list, the caller is expected to do that. |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 | 112 |
| 111 std::string StringPrintf(const char* format, ...) { | 113 std::string StringPrintf(const char* format, ...) { |
| 112 va_list ap; | 114 va_list ap; |
| 113 va_start(ap, format); | 115 va_start(ap, format); |
| 114 std::string result; | 116 std::string result; |
| 115 StringAppendV(&result, format, ap); | 117 StringAppendV(&result, format, ap); |
| 116 va_end(ap); | 118 va_end(ap); |
| 117 return result; | 119 return result; |
| 118 } | 120 } |
| 119 | 121 |
| 120 #if !defined(OS_ANDROID) | 122 #if defined(OS_WIN) |
| 121 std::wstring StringPrintf(const wchar_t* format, ...) { | 123 std::wstring StringPrintf(const wchar_t* format, ...) { |
| 122 va_list ap; | 124 va_list ap; |
| 123 va_start(ap, format); | 125 va_start(ap, format); |
| 124 std::wstring result; | 126 std::wstring result; |
| 125 StringAppendV(&result, format, ap); | 127 StringAppendV(&result, format, ap); |
| 126 va_end(ap); | 128 va_end(ap); |
| 127 return result; | 129 return result; |
| 128 } | 130 } |
| 129 #endif | 131 #endif |
| 130 | 132 |
| 131 std::string StringPrintV(const char* format, va_list ap) { | 133 std::string StringPrintV(const char* format, va_list ap) { |
| 132 std::string result; | 134 std::string result; |
| 133 StringAppendV(&result, format, ap); | 135 StringAppendV(&result, format, ap); |
| 134 return result; | 136 return result; |
| 135 } | 137 } |
| 136 | 138 |
| 137 const std::string& SStringPrintf(std::string* dst, const char* format, ...) { | 139 const std::string& SStringPrintf(std::string* dst, const char* format, ...) { |
| 138 va_list ap; | 140 va_list ap; |
| 139 va_start(ap, format); | 141 va_start(ap, format); |
| 140 dst->clear(); | 142 dst->clear(); |
| 141 StringAppendV(dst, format, ap); | 143 StringAppendV(dst, format, ap); |
| 142 va_end(ap); | 144 va_end(ap); |
| 143 return *dst; | 145 return *dst; |
| 144 } | 146 } |
| 145 | 147 |
| 146 #if !defined(OS_ANDROID) | 148 #if defined(OS_WIN) |
| 147 const std::wstring& SStringPrintf(std::wstring* dst, | 149 const std::wstring& SStringPrintf(std::wstring* dst, |
| 148 const wchar_t* format, ...) { | 150 const wchar_t* format, ...) { |
| 149 va_list ap; | 151 va_list ap; |
| 150 va_start(ap, format); | 152 va_start(ap, format); |
| 151 dst->clear(); | 153 dst->clear(); |
| 152 StringAppendV(dst, format, ap); | 154 StringAppendV(dst, format, ap); |
| 153 va_end(ap); | 155 va_end(ap); |
| 154 return *dst; | 156 return *dst; |
| 155 } | 157 } |
| 156 #endif | 158 #endif |
| 157 | 159 |
| 158 void StringAppendF(std::string* dst, const char* format, ...) { | 160 void StringAppendF(std::string* dst, const char* format, ...) { |
| 159 va_list ap; | 161 va_list ap; |
| 160 va_start(ap, format); | 162 va_start(ap, format); |
| 161 StringAppendV(dst, format, ap); | 163 StringAppendV(dst, format, ap); |
| 162 va_end(ap); | 164 va_end(ap); |
| 163 } | 165 } |
| 164 | 166 |
| 165 #if !defined(OS_ANDROID) | 167 #if defined(OS_WIN) |
| 166 void StringAppendF(std::wstring* dst, const wchar_t* format, ...) { | 168 void StringAppendF(std::wstring* dst, const wchar_t* format, ...) { |
| 167 va_list ap; | 169 va_list ap; |
| 168 va_start(ap, format); | 170 va_start(ap, format); |
| 169 StringAppendV(dst, format, ap); | 171 StringAppendV(dst, format, ap); |
| 170 va_end(ap); | 172 va_end(ap); |
| 171 } | 173 } |
| 172 #endif | 174 #endif |
| 173 | 175 |
| 174 void StringAppendV(std::string* dst, const char* format, va_list ap) { | 176 void StringAppendV(std::string* dst, const char* format, va_list ap) { |
| 175 StringAppendVT(dst, format, ap); | 177 StringAppendVT(dst, format, ap); |
| 176 } | 178 } |
| 177 | 179 |
| 178 #if !defined(OS_ANDROID) | 180 #if defined(OS_WIN) |
| 179 void StringAppendV(std::wstring* dst, const wchar_t* format, va_list ap) { | 181 void StringAppendV(std::wstring* dst, const wchar_t* format, va_list ap) { |
| 180 StringAppendVT(dst, format, ap); | 182 StringAppendVT(dst, format, ap); |
| 181 } | 183 } |
| 182 #endif | 184 #endif |
| 183 | 185 |
| 184 } // namespace base | 186 } // namespace base |
| OLD | NEW |