Add SkString::appendU32() and SkString::appendU64()
as needed to re-land r9682
R=reed@google.com
Review URL: https://codereview.chromium.org/17448012
git-svn-id: http://skia.googlecode.com/svn/trunk@9686 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/core/SkString.h b/include/core/SkString.h
index b98e2c8..1ed2ccf 100644
--- a/include/core/SkString.h
+++ b/include/core/SkString.h
@@ -59,9 +59,14 @@
-#define SkStrAppendS32_MaxSize 11
+#define SkStrAppendU32_MaxSize 10
+char* SkStrAppendU32(char buffer[], int32_t);
+#define SkStrAppendU64_MaxSize 20
+char* SkStrAppendU64(char buffer[], int64_t, int minDigits);
+
+#define SkStrAppendS32_MaxSize (SkStrAppendU32_MaxSize + 1)
char* SkStrAppendS32(char buffer[], int32_t);
-#define SkStrAppendS64_MaxSize 20
+#define SkStrAppendS64_MaxSize (SkStrAppendU64_MaxSize + 1)
char* SkStrAppendS64(char buffer[], int64_t, int minDigits);
/**
@@ -164,6 +169,8 @@
void insertUnichar(size_t offset, SkUnichar);
void insertS32(size_t offset, int32_t value);
void insertS64(size_t offset, int64_t value, int minDigits = 0);
+ void insertU32(size_t offset, uint32_t value);
+ void insertU64(size_t offset, uint64_t value, int minDigits = 0);
void insertHex(size_t offset, uint32_t value, int minDigits = 0);
void insertScalar(size_t offset, SkScalar);
@@ -173,6 +180,8 @@
void appendUnichar(SkUnichar uni) { this->insertUnichar((size_t)-1, uni); }
void appendS32(int32_t value) { this->insertS32((size_t)-1, value); }
void appendS64(int64_t value, int minDigits = 0) { this->insertS64((size_t)-1, value, minDigits); }
+ void appendU32(uint32_t value) { this->insertU32((size_t)-1, value); }
+ void appendU64(uint64_t value, int minDigits = 0) { this->insertU64((size_t)-1, value, minDigits); }
void appendHex(uint32_t value, int minDigits = 0) { this->insertHex((size_t)-1, value, minDigits); }
void appendScalar(SkScalar value) { this->insertScalar((size_t)-1, value); }
diff --git a/src/core/SkString.cpp b/src/core/SkString.cpp
index d9d74c4..4e5e204 100644
--- a/src/core/SkString.cpp
+++ b/src/core/SkString.cpp
@@ -68,47 +68,39 @@
return -1;
}
-char* SkStrAppendS32(char string[], int32_t dec) {
+char* SkStrAppendU32(char string[], uint32_t dec) {
SkDEBUGCODE(char* start = string;)
- char buffer[SkStrAppendS32_MaxSize];
+ char buffer[SkStrAppendU32_MaxSize];
char* p = buffer + sizeof(buffer);
- bool neg = false;
-
- if (dec < 0) {
- neg = true;
- dec = -dec;
- }
do {
*--p = SkToU8('0' + dec % 10);
dec /= 10;
} while (dec != 0);
- if (neg) {
- *--p = '-';
- }
-
SkASSERT(p >= buffer);
char* stop = buffer + sizeof(buffer);
while (p < stop) {
*string++ = *p++;
}
- SkASSERT(string - start <= SkStrAppendS32_MaxSize);
+ SkASSERT(string - start <= SkStrAppendU32_MaxSize);
return string;
}
-char* SkStrAppendS64(char string[], int64_t dec, int minDigits) {
- SkDEBUGCODE(char* start = string;)
-
- char buffer[SkStrAppendS64_MaxSize];
- char* p = buffer + sizeof(buffer);
- bool neg = false;
-
+char* SkStrAppendS32(char string[], int32_t dec) {
if (dec < 0) {
- neg = true;
+ *string++ = '-';
dec = -dec;
}
+ return SkStrAppendU32(string, static_cast<uint32_t>(dec));
+}
+
+char* SkStrAppendU64(char string[], uint64_t dec, int minDigits) {
+ SkDEBUGCODE(char* start = string;)
+
+ char buffer[SkStrAppendU64_MaxSize];
+ char* p = buffer + sizeof(buffer);
do {
*--p = SkToU8('0' + (int32_t) (dec % 10));
@@ -121,18 +113,23 @@
minDigits--;
}
- if (neg) {
- *--p = '-';
- }
SkASSERT(p >= buffer);
size_t cp_len = buffer + sizeof(buffer) - p;
memcpy(string, p, cp_len);
string += cp_len;
- SkASSERT(string - start <= SkStrAppendS64_MaxSize);
+ SkASSERT(string - start <= SkStrAppendU64_MaxSize);
return string;
}
+char* SkStrAppendS64(char string[], int64_t dec, int minDigits) {
+ if (dec < 0) {
+ *string++ = '-';
+ dec = -dec;
+ }
+ return SkStrAppendU64(string, static_cast<uint64_t>(dec), minDigits);
+}
+
char* SkStrAppendFloat(char string[], float value) {
// since floats have at most 8 significant digits, we limit our %g to that.
static const char gFormat[] = "%.8g";
@@ -518,6 +515,18 @@
this->insert(offset, buffer, stop - buffer);
}
+void SkString::insertU32(size_t offset, uint32_t dec) {
+ char buffer[SkStrAppendU32_MaxSize];
+ char* stop = SkStrAppendU32(buffer, dec);
+ this->insert(offset, buffer, stop - buffer);
+}
+
+void SkString::insertU64(size_t offset, uint64_t dec, int minDigits) {
+ char buffer[SkStrAppendU64_MaxSize];
+ char* stop = SkStrAppendU64(buffer, dec, minDigits);
+ this->insert(offset, buffer, stop - buffer);
+}
+
void SkString::insertHex(size_t offset, uint32_t hex, int minDigits) {
minDigits = SkPin32(minDigits, 0, 8);
diff --git a/tests/StringTest.cpp b/tests/StringTest.cpp
index 2c5026c..3076719 100644
--- a/tests/StringTest.cpp
+++ b/tests/StringTest.cpp
@@ -102,20 +102,53 @@
a.set("abcd");
a.set("");
- a.appendS64(72036854775808LL, 0);
- REPORTER_ASSERT(reporter, a.equals("72036854775808"));
+ a.appendS32(0x7FFFFFFFL);
+ REPORTER_ASSERT(reporter, a.equals("2147483647"));
+ a.set("");
+ a.appendS32(0x80000001L);
+ REPORTER_ASSERT(reporter, a.equals("-2147483647"));
+ a.set("");
+ a.appendS32(0x80000000L);
+ REPORTER_ASSERT(reporter, a.equals("-2147483648"));
a.set("");
- a.appendS64(-1844674407370LL, 0);
- REPORTER_ASSERT(reporter, a.equals("-1844674407370"));
+ a.appendU32(0x7FFFFFFFUL);
+ REPORTER_ASSERT(reporter, a.equals("2147483647"));
+ a.set("");
+ a.appendU32(0x80000001UL);
+ REPORTER_ASSERT(reporter, a.equals("2147483649"));
+ a.set("");
+ a.appendU32(0xFFFFFFFFUL);
+ REPORTER_ASSERT(reporter, a.equals("4294967295"));
a.set("");
- a.appendS64(73709551616LL, 15);
- REPORTER_ASSERT(reporter, a.equals("000073709551616"));
+ a.appendS64(0x7FFFFFFFFFFFFFFFLL, 0);
+ REPORTER_ASSERT(reporter, a.equals("9223372036854775807"));
+ a.set("");
+ a.appendS64(0x8000000000000001LL, 0);
+ REPORTER_ASSERT(reporter, a.equals("-9223372036854775807"));
+ a.set("");
+ a.appendS64(0x8000000000000000LL, 0);
+ REPORTER_ASSERT(reporter, a.equals("-9223372036854775808"));
+ a.set("");
+ a.appendS64(0x0000000001000000LL, 15);
+ REPORTER_ASSERT(reporter, a.equals("000000016777216"));
+ a.set("");
+ a.appendS64(0xFFFFFFFFFF000000LL, 15);
+ REPORTER_ASSERT(reporter, a.equals("-000000016777216"));
a.set("");
- a.appendS64(-429496729612LL, 15);
- REPORTER_ASSERT(reporter, a.equals("-000429496729612"));
+ a.appendU64(0x7FFFFFFFFFFFFFFFULL, 0);
+ REPORTER_ASSERT(reporter, a.equals("9223372036854775807"));
+ a.set("");
+ a.appendU64(0x8000000000000001ULL, 0);
+ REPORTER_ASSERT(reporter, a.equals("9223372036854775809"));
+ a.set("");
+ a.appendU64(0xFFFFFFFFFFFFFFFFULL, 0);
+ REPORTER_ASSERT(reporter, a.equals("18446744073709551615"));
+ a.set("");
+ a.appendU64(0x0000000001000000ULL, 15);
+ REPORTER_ASSERT(reporter, a.equals("000000016777216"));
static const struct {
SkScalar fValue;