scalar->string uses %.8g. Floats have at most 8 significant digits, plus we add
more space for sign, decimal point and exponent.
add tests to check these limits.
git-svn-id: http://skia.googlecode.com/svn/trunk@872
2bbb7eff-a529-9590-31e7-
b0007b416f81
char* SkStrAppendS32(char buffer[], int32_t);
#define SkStrAppendS64_MaxSize 20
char* SkStrAppendS64(char buffer[], int64_t, int minDigits);
-#define SkStrAppendScalar_MaxSize 11
-char* SkStrAppendScalar(char buffer[], SkScalar);
+
+/**
+ * Floats have at most 8 significant digits, so we limit our %g to that.
+ * However, the total string could be 14 characters: -1.2345678e+38
+ */
+#define SkStrAppendScalar_MaxSize 14
+
+/**
+ * Write the scaler in decimal format into buffer, and return a pointer to
+ * the next char after the last one written. Note: a terminating 0 is not
+ * written into buffer, which must be at least SkStrAppendScalar_MaxSize.
+ * Thus if the caller wants to add a 0 at the end, buffer must be at least
+ * SkStrAppendScalar_MaxSize + 1 bytes large.
+ */
+char* SkStrAppendScalar(char buffer[], SkScalar);
/** \class SkString
to never fail or throw.
*/
void swap(SkString& other);
-
+
private:
struct Rec {
public:
SkDEBUGCODE(char* start = string;)
#ifdef SK_SCALAR_IS_FLOAT
- return string + SNPRINTF(string, SkStrAppendScalar_MaxSize, "%g", value);
+ // since floats have at most 8 significant digits, we limit our %g to that.
+ static const char gFormat[] = "%.8g";
+ // make it 1 larger for the terminating 0
+ char buffer[SkStrAppendScalar_MaxSize + 1];
+ int len = SNPRINTF(buffer, sizeof(buffer), gFormat, value);
+ memcpy(string, buffer, len);
+ return string + len;
#else
SkFixed x = SkScalarToFixed(value);
{ SK_Scalar1, "1" },
{ -SK_Scalar1, "-1" },
{ SK_Scalar1/2, "0.5" },
+#ifdef SK_SCALAR_IS_FLOAT
+ { 3.4028234e38f, "3.4028235e+38" },
+ { -3.4028234e38f, "-3.4028235e+38" },
+#endif
};
for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
a.reset();
a.appendScalar(gRec[i].fValue);
+ REPORTER_ASSERT(reporter, a.size() <= SkStrAppendScalar_MaxSize);
+// SkDebugf(" received <%s> expected <%s>\n", a.c_str(), gRec[i].fString);
REPORTER_ASSERT(reporter, a.equals(gRec[i].fString));
}
}