use sprintf to generate float->string for SkString routines, removing the

worry of first converting the scalar to a fixed.



git-svn-id: http://skia.googlecode.com/svn/trunk@865 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
reed@google.com 2011-02-28 21:29:58 +00:00
parent 958c39bd05
commit fa06e52803
2 changed files with 54 additions and 34 deletions

View File

@ -19,6 +19,28 @@
#include "SkFixed.h"
#include "SkUtils.h"
#include <stdarg.h>
#include <stdio.h>
// number of bytes (on the stack) to receive the printf result
static const size_t kBufferSize = 256;
#ifdef SK_BUILD_FOR_WIN
#define VSNPRINTF _vsnprintf
#define SNPRINTF _snprintf
#else
#define VSNPRINTF vsnprintf
#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); \
} while (0)
///////////////////////////////////////////////////////////////////////////////
bool SkStrStartsWith(const char string[], const char prefix[])
{
@ -116,6 +138,9 @@ char* SkStrAppendScalar(char string[], SkScalar value)
{
SkDEBUGCODE(char* start = string;)
#ifdef SK_SCALAR_IS_FLOAT
return string + SNPRINTF(string, SkStrAppendScalar_MaxSize, "%g", value);
#else
SkFixed x = SkScalarToFixed(value);
if (x < 0)
@ -151,6 +176,7 @@ char* SkStrAppendScalar(char string[], SkScalar value)
x %= powerOfTen;
} while (x != 0);
}
#endif
SkASSERT(string - start <= SkStrAppendScalar_MaxSize);
return string;
@ -508,27 +534,6 @@ void SkString::insertScalar(size_t offset, SkScalar value)
this->insert(offset, buffer, stop - buffer);
}
///////////////////////////////////////////////////////////////////////////
#include <stdio.h>
// number of bytes (on the stack) to receive the printf result
static const size_t kBufferSize = 256;
#ifdef SK_BUILD_FOR_WIN
#define VSNPRINTF _vsnprintf
#else
#define VSNPRINTF vsnprintf
#endif
#define ARGS_TO_BUFFER(format, buffer, size) \
do { \
va_list args; \
va_start(args, format); \
VSNPRINTF(buffer, size, format, args); \
va_end(args); \
} while (0)
void SkString::printf(const char format[], ...) {
char buffer[kBufferSize];
ARGS_TO_BUFFER(format, buffer, kBufferSize);
@ -552,7 +557,7 @@ void SkString::prependf(const char format[], ...) {
#undef VSNPRINTF
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void SkString::remove(size_t offset, size_t length)
{

View File

@ -64,6 +64,21 @@ static void TestString(skiatest::Reporter* reporter) {
a.set("");
a.appendS64(-429496729612LL, 15);
REPORTER_ASSERT(reporter, a.equals("-000429496729612"));
static const struct {
SkScalar fValue;
const char* fString;
} gRec[] = {
{ 0, "0" },
{ SK_Scalar1, "1" },
{ -SK_Scalar1, "-1" },
{ SK_Scalar1/2, "0.5" },
};
for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
a.reset();
a.appendScalar(gRec[i].fValue);
REPORTER_ASSERT(reporter, a.equals(gRec[i].fString));
}
}
#include "TestClassDef.h"