add SkData::NewFromCString()



git-svn-id: http://skia.googlecode.com/svn/trunk@4383 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/core/SkData.h b/include/core/SkData.h
index 8bbe3c8..f24cf9c 100644
--- a/include/core/SkData.h
+++ b/include/core/SkData.h
@@ -27,6 +27,8 @@
      */
     size_t size() const { return fSize; }
 
+    bool isEmpty() const { return 0 == fSize; }
+
     /**
      *  Returns the ptr to the data.
      */
@@ -49,6 +51,12 @@
     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 @@
      *  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 495da88..2653f32 100644
--- a/src/core/SkData.cpp
+++ b/src/core/SkData.cpp
@@ -25,6 +25,14 @@
     }
 }
 
+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 @@
                          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 56c5d11..0b72c4a 100644
--- a/tests/DataRefTest.cpp
+++ b/tests/DataRefTest.cpp
@@ -26,21 +26,29 @@
     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 @@
     tmp = SkData::NewSubset(r1, 0, 0);
     assert_len(reporter, tmp, 0);
     tmp->unref();
+    
+    test_cstring(reporter);
 }
 
 #include "TestClassDef.h"