diff options
author | reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2011-02-28 21:29:58 +0000 |
---|---|---|
committer | reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2011-02-28 21:29:58 +0000 |
commit | fa06e528035dd9c4d01c1d1e17e9df1cfe99b855 (patch) | |
tree | cd481831d60051ff1ec318aa0f6f48e03992f2a4 | |
parent | 958c39bd0560c4cd25089083ca9d8fd2db6097ef (diff) |
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
-rw-r--r-- | src/core/SkString.cpp | 73 | ||||
-rw-r--r-- | tests/StringTest.cpp | 15 |
2 files changed, 54 insertions, 34 deletions
diff --git a/src/core/SkString.cpp b/src/core/SkString.cpp index cdce160adc..7f490cc97d 100644 --- a/src/core/SkString.cpp +++ b/src/core/SkString.cpp @@ -2,16 +2,16 @@ ** ** Copyright 2006, The Android Open Source Project ** -** Licensed under the Apache License, Version 2.0 (the "License"); -** you may not use this file except in compliance with the License. -** You may obtain a copy of the License at +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at ** -** http://www.apache.org/licenses/LICENSE-2.0 +** http://www.apache.org/licenses/LICENSE-2.0 ** -** Unless required by applicable law or agreed to in writing, software -** distributed under the License is distributed on an "AS IS" BASIS, -** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -** See the License for the specific language governing permissions and +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and ** limitations under the License. */ @@ -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,7 +176,8 @@ char* SkStrAppendScalar(char string[], SkScalar value) x %= powerOfTen; } while (x != 0); } - +#endif + SkASSERT(string - start <= SkStrAppendScalar_MaxSize); return string; } @@ -483,7 +509,7 @@ void SkString::insertS64(size_t offset, int64_t dec, int minDigits) void SkString::insertHex(size_t offset, uint32_t hex, int minDigits) { minDigits = SkPin32(minDigits, 0, 8); - + static const char gHex[] = "0123456789ABCDEF"; char buffer[8]; @@ -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); @@ -539,20 +544,20 @@ void SkString::printf(const char format[], ...) { void SkString::appendf(const char format[], ...) { char buffer[kBufferSize]; ARGS_TO_BUFFER(format, buffer, kBufferSize); - + this->append(buffer, strlen(buffer)); } void SkString::prependf(const char format[], ...) { char buffer[kBufferSize]; ARGS_TO_BUFFER(format, buffer, kBufferSize); - + this->prepend(buffer, strlen(buffer)); } #undef VSNPRINTF -/////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// void SkString::remove(size_t offset, size_t length) { diff --git a/tests/StringTest.cpp b/tests/StringTest.cpp index 2e691f4809..781fed8bf7 100644 --- a/tests/StringTest.cpp +++ b/tests/StringTest.cpp @@ -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" |