aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2012-06-28 15:40:09 +0000
committerGravatar reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2012-06-28 15:40:09 +0000
commitdbc936dff3357f74fc60e124d912a2179b909b0d (patch)
treebe896546a5e7489364f82892bf1b1ab3dc30cdfe
parentfe65943d309dc687f618d16e394dff3baa526426 (diff)
add SkData::NewFromCString()
git-svn-id: http://skia.googlecode.com/svn/trunk@4383 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r--include/core/SkData.h16
-rw-r--r--src/core/SkData.cpp16
-rw-r--r--tests/DataRefTest.cpp30
3 files changed, 51 insertions, 11 deletions
diff --git a/include/core/SkData.h b/include/core/SkData.h
index 8bbe3c89a9..f24cf9c92c 100644
--- a/include/core/SkData.h
+++ b/include/core/SkData.h
@@ -27,6 +27,8 @@ public:
*/
size_t size() const { return fSize; }
+ bool isEmpty() const { return 0 == fSize; }
+
/**
* Returns the ptr to the data.
*/
@@ -49,6 +51,12 @@ public:
size_t copyRange(size_t offset, size_t length, void* buffer) const;
/**
+ * Returns true if these two objects have the same length and contents,
+ * effectively returning 0 == memcmp(...)
+ */
+ bool equals(const SkData* other) const;
+
+ /**
* Function that, if provided, will be called when the SkData goes out
* of scope, allowing for custom allocation/freeing of the data.
*/
@@ -58,7 +66,13 @@ public:
* Create a new dataref by copying the specified data
*/
static SkData* NewWithCopy(const void* data, size_t length);
-
+
+ /**
+ * Create a new dataref by copying the specified c-string
+ * (a null-terminated array of bytes).
+ */
+ static SkData* NewWithCString(const char cstr[]);
+
/**
* Create a new dataref, taking the data ptr as is, and using the
* releaseproc to free it. The proc may be NULL.
diff --git a/src/core/SkData.cpp b/src/core/SkData.cpp
index 495da8886e..2653f327c0 100644
--- a/src/core/SkData.cpp
+++ b/src/core/SkData.cpp
@@ -25,6 +25,14 @@ SkData::~SkData() {
}
}
+bool SkData::equals(const SkData* other) const {
+ if (NULL == other) {
+ return false;
+ }
+
+ return fSize == other->fSize && !memcmp(fPtr, other->fPtr, fSize);
+}
+
size_t SkData::copyRange(size_t offset, size_t length, void* buffer) const {
size_t available = fSize;
if (offset >= available || 0 == length) {
@@ -103,3 +111,11 @@ SkData* SkData::NewSubset(const SkData* src, size_t offset, size_t length) {
const_cast<SkData*>(src));
}
+SkData* SkData::NewWithCString(const char cstr[]) {
+ if (NULL == cstr || 0 == cstr[0]) {
+ return NewEmpty();
+ } else {
+ return NewWithCopy(cstr, strlen(cstr));
+ }
+}
+
diff --git a/tests/DataRefTest.cpp b/tests/DataRefTest.cpp
index 56c5d11725..0b72c4a93a 100644
--- a/tests/DataRefTest.cpp
+++ b/tests/DataRefTest.cpp
@@ -26,21 +26,29 @@ static void assert_data(skiatest::Reporter* reporter, SkData* ref,
REPORTER_ASSERT(reporter, !memcmp(ref->data(), data, len));
}
+static void test_cstring(skiatest::Reporter* reporter) {
+ const char str[] = "Hello world";
+ size_t len = strlen(str);
+
+ SkAutoTUnref<SkData> r0(SkData::NewWithCopy(str, len));
+ SkAutoTUnref<SkData> r1(SkData::NewWithCString(str));
+
+ REPORTER_ASSERT(reporter, r0->equals(r1));
+
+ SkAutoTUnref<SkData> r2(SkData::NewWithCString(NULL));
+ REPORTER_ASSERT(reporter, r2->isEmpty());
+}
+
static void TestDataRef(skiatest::Reporter* reporter) {
const char* str = "We the people, in order to form a more perfect union.";
const int N = 10;
- SkData* r0 = SkData::NewEmpty();
- SkData* r1 = SkData::NewWithCopy(str, strlen(str));
- SkData* r2 = SkData::NewWithProc(new int[N], N*sizeof(int),
- delete_int_proc, gGlobal);
- SkData* r3 = SkData::NewSubset(r1, 7, 6);
+ SkAutoTUnref<SkData> r0(SkData::NewEmpty());
+ SkAutoTUnref<SkData> r1(SkData::NewWithCopy(str, strlen(str)));
+ SkAutoTUnref<SkData> r2(SkData::NewWithProc(new int[N], N*sizeof(int),
+ delete_int_proc, gGlobal));
+ SkAutoTUnref<SkData> r3(SkData::NewSubset(r1, 7, 6));
- SkAutoUnref aur0(r0);
- SkAutoUnref aur1(r1);
- SkAutoUnref aur2(r2);
- SkAutoUnref aur3(r3);
-
assert_len(reporter, r0, 0);
assert_len(reporter, r1, strlen(str));
assert_len(reporter, r2, N * sizeof(int));
@@ -55,6 +63,8 @@ static void TestDataRef(skiatest::Reporter* reporter) {
tmp = SkData::NewSubset(r1, 0, 0);
assert_len(reporter, tmp, 0);
tmp->unref();
+
+ test_cstring(reporter);
}
#include "TestClassDef.h"