From 976386b75d883eebaa4d2f19c4ae0222f3f85c0a Mon Sep 17 00:00:00 2001 From: joshualitt Date: Thu, 23 Oct 2014 18:23:32 -0700 Subject: [PATCH] Possible optimization to SkString BUG=skia: Review URL: https://codereview.chromium.org/679433003 --- src/core/SkString.cpp | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/core/SkString.cpp b/src/core/SkString.cpp index 48459dbbd8..d3a28e248b 100644 --- a/src/core/SkString.cpp +++ b/src/core/SkString.cpp @@ -26,12 +26,13 @@ static const size_t kBufferSize = 1024; #define SNPRINTF snprintf #endif -#define ARGS_TO_BUFFER(format, buffer, size) \ - do { \ - va_list args; \ - va_start(args, format); \ - VSNPRINTF(buffer, size, format, args); \ - va_end(args); \ +#define ARGS_TO_BUFFER(format, buffer, size, written) \ + do { \ + va_list args; \ + va_start(args, format); \ + written = VSNPRINTF(buffer, size, format, args); \ + SkASSERT(written >= 0 && written < SkToInt(size)); \ + va_end(args); \ } while (0) /////////////////////////////////////////////////////////////////////////////// @@ -557,37 +558,42 @@ void SkString::insertScalar(size_t offset, SkScalar value) { void SkString::printf(const char format[], ...) { char buffer[kBufferSize]; - ARGS_TO_BUFFER(format, buffer, kBufferSize); + int length; + ARGS_TO_BUFFER(format, buffer, kBufferSize, length); - this->set(buffer, strlen(buffer)); + this->set(buffer, length); } void SkString::appendf(const char format[], ...) { char buffer[kBufferSize]; - ARGS_TO_BUFFER(format, buffer, kBufferSize); + int length; + ARGS_TO_BUFFER(format, buffer, kBufferSize, length); - this->append(buffer, strlen(buffer)); + this->append(buffer, length); } void SkString::appendVAList(const char format[], va_list args) { char buffer[kBufferSize]; - VSNPRINTF(buffer, kBufferSize, format, args); + int length = VSNPRINTF(buffer, kBufferSize, format, args); + SkASSERT(length >= 0 && length < SkToInt(kBufferSize)); - this->append(buffer, strlen(buffer)); + this->append(buffer, length); } void SkString::prependf(const char format[], ...) { char buffer[kBufferSize]; - ARGS_TO_BUFFER(format, buffer, kBufferSize); + int length; + ARGS_TO_BUFFER(format, buffer, kBufferSize, length); - this->prepend(buffer, strlen(buffer)); + this->prepend(buffer, length); } void SkString::prependVAList(const char format[], va_list args) { char buffer[kBufferSize]; - VSNPRINTF(buffer, kBufferSize, format, args); + int length = VSNPRINTF(buffer, kBufferSize, format, args); + SkASSERT(length >= 0 && length < SkToInt(kBufferSize)); - this->prepend(buffer, strlen(buffer)); + this->prepend(buffer, length); } @@ -635,7 +641,8 @@ void SkString::swap(SkString& other) { SkString SkStringPrintf(const char* format, ...) { SkString formattedOutput; char buffer[kBufferSize]; - ARGS_TO_BUFFER(format, buffer, kBufferSize); + SK_UNUSED int length; + ARGS_TO_BUFFER(format, buffer, kBufferSize, length); formattedOutput.set(buffer); return formattedOutput; }