From 3a1f6a06cc706b35d9d6086ffbf5135cbf42bf8a Mon Sep 17 00:00:00 2001 From: "tomhudson@google.com" Date: Thu, 30 Jun 2011 14:39:52 +0000 Subject: [PATCH] SkStringPrintf helper fucntion + a new unit test for it. git-svn-id: http://skia.googlecode.com/svn/trunk@1766 2bbb7eff-a529-9590-31e7-b0007b416f81 --- include/core/SkString.h | 3 +++ src/core/SkString.cpp | 15 +++++++++++++-- tests/StringTest.cpp | 2 ++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/include/core/SkString.h b/include/core/SkString.h index 0295b7536d..abfdd24ca5 100644 --- a/include/core/SkString.h +++ b/include/core/SkString.h @@ -197,4 +197,7 @@ private: uint16_t* fUCS2; }; +/// Creates a new string and writes into it using a printf()-style format. +SkString SkStringPrintf(const char* format, ...); + #endif diff --git a/src/core/SkString.cpp b/src/core/SkString.cpp index f461a7a44a..0d7defec8a 100644 --- a/src/core/SkString.cpp +++ b/src/core/SkString.cpp @@ -542,8 +542,6 @@ void SkString::prependf(const char format[], ...) { this->prepend(buffer, strlen(buffer)); } -#undef VSNPRINTF - /////////////////////////////////////////////////////////////////////////////// void SkString::remove(size_t offset, size_t length) { @@ -605,3 +603,16 @@ SkAutoUCS2::SkAutoUCS2(const char utf8[]) { SkAutoUCS2::~SkAutoUCS2() { sk_free(fUCS2); } + +/////////////////////////////////////////////////////////////////////////////// + +SkString SkStringPrintf(const char* format, ...) { + SkString formattedOutput; + char buffer[kBufferSize]; + ARGS_TO_BUFFER(format, buffer, kBufferSize); + formattedOutput.set(buffer); + return formattedOutput; +} + +#undef VSNPRINTF + diff --git a/tests/StringTest.cpp b/tests/StringTest.cpp index 270ccfd3a4..02510d56dc 100644 --- a/tests/StringTest.cpp +++ b/tests/StringTest.cpp @@ -85,6 +85,8 @@ static void TestString(skiatest::Reporter* reporter) { // SkDebugf(" received <%s> expected <%s>\n", a.c_str(), gRec[i].fString); REPORTER_ASSERT(reporter, a.equals(gRec[i].fString)); } + + REPORTER_ASSERT(reporter, SkStringPrintf("%i", 0).equals("0")); } #include "TestClassDef.h"