michael@0: // Copyright 2013 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/strings/stringprintf.h" michael@0: michael@0: #include michael@0: michael@0: #include "base/scoped_clear_errno.h" michael@0: #include "base/strings/string_util.h" michael@0: #include "base/strings/utf_string_conversions.h" michael@0: michael@0: namespace base { michael@0: michael@0: namespace { michael@0: michael@0: // Overloaded wrappers around vsnprintf and vswprintf. The buf_size parameter michael@0: // is the size of the buffer. These return the number of characters in the michael@0: // formatted string excluding the NUL terminator. If the buffer is not michael@0: // large enough to accommodate the formatted string without truncation, they michael@0: // return the number of characters that would be in the fully-formatted string michael@0: // (vsnprintf, and vswprintf on Windows), or -1 (vswprintf on POSIX platforms). michael@0: inline int vsnprintfT(char* buffer, michael@0: size_t buf_size, michael@0: const char* format, michael@0: va_list argptr) { michael@0: return base::vsnprintf(buffer, buf_size, format, argptr); michael@0: } michael@0: michael@0: #if !defined(OS_ANDROID) michael@0: inline int vsnprintfT(wchar_t* buffer, michael@0: size_t buf_size, michael@0: const wchar_t* format, michael@0: va_list argptr) { michael@0: return base::vswprintf(buffer, buf_size, format, argptr); michael@0: } michael@0: #endif michael@0: michael@0: // Templatized backend for StringPrintF/StringAppendF. This does not finalize michael@0: // the va_list, the caller is expected to do that. michael@0: template michael@0: static void StringAppendVT(StringType* dst, michael@0: const typename StringType::value_type* format, michael@0: va_list ap) { michael@0: // First try with a small fixed size buffer. michael@0: // This buffer size should be kept in sync with StringUtilTest.GrowBoundary michael@0: // and StringUtilTest.StringPrintfBounds. michael@0: typename StringType::value_type stack_buf[1024]; michael@0: michael@0: va_list ap_copy; michael@0: GG_VA_COPY(ap_copy, ap); michael@0: michael@0: #if !defined(OS_WIN) michael@0: ScopedClearErrno clear_errno; michael@0: #endif michael@0: int result = vsnprintfT(stack_buf, arraysize(stack_buf), format, ap_copy); michael@0: va_end(ap_copy); michael@0: michael@0: if (result >= 0 && result < static_cast(arraysize(stack_buf))) { michael@0: // It fit. michael@0: dst->append(stack_buf, result); michael@0: return; michael@0: } michael@0: michael@0: // Repeatedly increase buffer size until it fits. michael@0: int mem_length = arraysize(stack_buf); michael@0: while (true) { michael@0: if (result < 0) { michael@0: #if !defined(OS_WIN) michael@0: // On Windows, vsnprintfT always returns the number of characters in a michael@0: // fully-formatted string, so if we reach this point, something else is michael@0: // wrong and no amount of buffer-doubling is going to fix it. michael@0: if (errno != 0 && errno != EOVERFLOW) michael@0: #endif michael@0: { michael@0: // If an error other than overflow occurred, it's never going to work. michael@0: DLOG(WARNING) << "Unable to printf the requested string due to error."; michael@0: return; michael@0: } michael@0: // Try doubling the buffer size. michael@0: mem_length *= 2; michael@0: } else { michael@0: // We need exactly "result + 1" characters. michael@0: mem_length = result + 1; michael@0: } michael@0: michael@0: if (mem_length > 32 * 1024 * 1024) { michael@0: // That should be plenty, don't try anything larger. This protects michael@0: // against huge allocations when using vsnprintfT implementations that michael@0: // return -1 for reasons other than overflow without setting errno. michael@0: DLOG(WARNING) << "Unable to printf the requested string due to size."; michael@0: return; michael@0: } michael@0: michael@0: std::vector mem_buf(mem_length); michael@0: michael@0: // NOTE: You can only use a va_list once. Since we're in a while loop, we michael@0: // need to make a new copy each time so we don't use up the original. michael@0: GG_VA_COPY(ap_copy, ap); michael@0: result = vsnprintfT(&mem_buf[0], mem_length, format, ap_copy); michael@0: va_end(ap_copy); michael@0: michael@0: if ((result >= 0) && (result < mem_length)) { michael@0: // It fit. michael@0: dst->append(&mem_buf[0], result); michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: michael@0: } // namespace michael@0: michael@0: std::string StringPrintf(const char* format, ...) { michael@0: va_list ap; michael@0: va_start(ap, format); michael@0: std::string result; michael@0: StringAppendV(&result, format, ap); michael@0: va_end(ap); michael@0: return result; michael@0: } michael@0: michael@0: #if !defined(OS_ANDROID) michael@0: std::wstring StringPrintf(const wchar_t* format, ...) { michael@0: va_list ap; michael@0: va_start(ap, format); michael@0: std::wstring result; michael@0: StringAppendV(&result, format, ap); michael@0: va_end(ap); michael@0: return result; michael@0: } michael@0: #endif michael@0: michael@0: std::string StringPrintV(const char* format, va_list ap) { michael@0: std::string result; michael@0: StringAppendV(&result, format, ap); michael@0: return result; michael@0: } michael@0: michael@0: const std::string& SStringPrintf(std::string* dst, const char* format, ...) { michael@0: va_list ap; michael@0: va_start(ap, format); michael@0: dst->clear(); michael@0: StringAppendV(dst, format, ap); michael@0: va_end(ap); michael@0: return *dst; michael@0: } michael@0: michael@0: #if !defined(OS_ANDROID) michael@0: const std::wstring& SStringPrintf(std::wstring* dst, michael@0: const wchar_t* format, ...) { michael@0: va_list ap; michael@0: va_start(ap, format); michael@0: dst->clear(); michael@0: StringAppendV(dst, format, ap); michael@0: va_end(ap); michael@0: return *dst; michael@0: } michael@0: #endif michael@0: michael@0: void StringAppendF(std::string* dst, const char* format, ...) { michael@0: va_list ap; michael@0: va_start(ap, format); michael@0: StringAppendV(dst, format, ap); michael@0: va_end(ap); michael@0: } michael@0: michael@0: #if !defined(OS_ANDROID) michael@0: void StringAppendF(std::wstring* dst, const wchar_t* format, ...) { michael@0: va_list ap; michael@0: va_start(ap, format); michael@0: StringAppendV(dst, format, ap); michael@0: va_end(ap); michael@0: } michael@0: #endif michael@0: michael@0: void StringAppendV(std::string* dst, const char* format, va_list ap) { michael@0: StringAppendVT(dst, format, ap); michael@0: } michael@0: michael@0: #if !defined(OS_ANDROID) michael@0: void StringAppendV(std::wstring* dst, const wchar_t* format, va_list ap) { michael@0: StringAppendVT(dst, format, ap); michael@0: } michael@0: #endif michael@0: michael@0: } // namespace base