add SkData::NewFromCString()
git-svn-id: http://skia.googlecode.com/svn/trunk@4383 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
parent
fe65943d30
commit
dbc936dff3
@ -27,6 +27,8 @@ public:
|
||||
*/
|
||||
size_t size() const { return fSize; }
|
||||
|
||||
bool isEmpty() const { return 0 == fSize; }
|
||||
|
||||
/**
|
||||
* Returns the ptr to the data.
|
||||
*/
|
||||
@ -48,6 +50,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.
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user