| 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 "base/scoped_clear_errno.h" | 9 #include "base/scoped_clear_errno.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 template <class StringType> | 41 template <class StringType> |
| 42 static void StringAppendVT(StringType* dst, | 42 static void StringAppendVT(StringType* dst, |
| 43 const typename StringType::value_type* format, | 43 const typename StringType::value_type* format, |
| 44 va_list ap) { | 44 va_list ap) { |
| 45 // First try with a small fixed size buffer. | 45 // First try with a small fixed size buffer. |
| 46 // This buffer size should be kept in sync with StringUtilTest.GrowBoundary | 46 // This buffer size should be kept in sync with StringUtilTest.GrowBoundary |
| 47 // and StringUtilTest.StringPrintfBounds. | 47 // and StringUtilTest.StringPrintfBounds. |
| 48 typename StringType::value_type stack_buf[1024]; | 48 typename StringType::value_type stack_buf[1024]; |
| 49 | 49 |
| 50 va_list ap_copy; | 50 va_list ap_copy; |
| 51 GG_VA_COPY(ap_copy, ap); | 51 va_copy(ap_copy, ap); |
| 52 | 52 |
| 53 #if !defined(OS_WIN) | 53 #if !defined(OS_WIN) |
| 54 ScopedClearErrno clear_errno; | 54 ScopedClearErrno clear_errno; |
| 55 #endif | 55 #endif |
| 56 int result = vsnprintfT(stack_buf, arraysize(stack_buf), format, ap_copy); | 56 int result = vsnprintfT(stack_buf, arraysize(stack_buf), format, ap_copy); |
| 57 va_end(ap_copy); | 57 va_end(ap_copy); |
| 58 | 58 |
| 59 if (result >= 0 && result < static_cast<int>(arraysize(stack_buf))) { | 59 if (result >= 0 && result < static_cast<int>(arraysize(stack_buf))) { |
| 60 // It fit. | 60 // It fit. |
| 61 dst->append(stack_buf, result); | 61 dst->append(stack_buf, result); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 87 // against huge allocations when using vsnprintfT implementations that | 87 // against huge allocations when using vsnprintfT implementations that |
| 88 // return -1 for reasons other than overflow without setting errno. | 88 // return -1 for reasons other than overflow without setting errno. |
| 89 DLOG(WARNING) << "Unable to printf the requested string due to size."; | 89 DLOG(WARNING) << "Unable to printf the requested string due to size."; |
| 90 return; | 90 return; |
| 91 } | 91 } |
| 92 | 92 |
| 93 std::vector<typename StringType::value_type> mem_buf(mem_length); | 93 std::vector<typename StringType::value_type> mem_buf(mem_length); |
| 94 | 94 |
| 95 // NOTE: You can only use a va_list once. Since we're in a while loop, we | 95 // NOTE: You can only use a va_list once. Since we're in a while loop, we |
| 96 // need to make a new copy each time so we don't use up the original. | 96 // need to make a new copy each time so we don't use up the original. |
| 97 GG_VA_COPY(ap_copy, ap); | 97 va_copy(ap_copy, ap); |
| 98 result = vsnprintfT(&mem_buf[0], mem_length, format, ap_copy); | 98 result = vsnprintfT(&mem_buf[0], mem_length, format, ap_copy); |
| 99 va_end(ap_copy); | 99 va_end(ap_copy); |
| 100 | 100 |
| 101 if ((result >= 0) && (result < mem_length)) { | 101 if ((result >= 0) && (result < mem_length)) { |
| 102 // It fit. | 102 // It fit. |
| 103 dst->append(&mem_buf[0], result); | 103 dst->append(&mem_buf[0], result); |
| 104 return; | 104 return; |
| 105 } | 105 } |
| 106 } | 106 } |
| 107 } | 107 } |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 StringAppendVT(dst, format, ap); | 175 StringAppendVT(dst, format, ap); |
| 176 } | 176 } |
| 177 | 177 |
| 178 #if !defined(OS_ANDROID) | 178 #if !defined(OS_ANDROID) |
| 179 void StringAppendV(std::wstring* dst, const wchar_t* format, va_list ap) { | 179 void StringAppendV(std::wstring* dst, const wchar_t* format, va_list ap) { |
| 180 StringAppendVT(dst, format, ap); | 180 StringAppendVT(dst, format, ap); |
| 181 } | 181 } |
| 182 #endif | 182 #endif |
| 183 | 183 |
| 184 } // namespace base | 184 } // namespace base |
| OLD | NEW |