Implement Str_To_ByteBuf

Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/2ba218ef
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/2ba218ef
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/2ba218ef

Branch: refs/heads/master
Commit: 2ba218ef6c7b7fd4a0136f4906f42db85888343a
Parents: 10f04e6
Author: Nick Wellnhofer <wellnho...@aevum.de>
Authored: Thu Nov 12 13:50:05 2015 +0100
Committer: Nick Wellnhofer <wellnho...@aevum.de>
Committed: Thu Nov 12 14:08:17 2015 +0100

----------------------------------------------------------------------
 runtime/core/Clownfish/String.c          |  6 ++++++
 runtime/core/Clownfish/String.cfh        |  5 +++++
 runtime/core/Clownfish/Test/TestString.c | 13 ++++++++++++-
 3 files changed, 23 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/2ba218ef/runtime/core/Clownfish/String.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/String.c b/runtime/core/Clownfish/String.c
index 4daaa83..7c2c0fa 100644
--- a/runtime/core/Clownfish/String.c
+++ b/runtime/core/Clownfish/String.c
@@ -26,6 +26,7 @@
 #include "Clownfish/Class.h"
 #include "Clownfish/String.h"
 
+#include "Clownfish/ByteBuf.h"
 #include "Clownfish/CharBuf.h"
 #include "Clownfish/Err.h"
 #include "Clownfish/Util/Memory.h"
@@ -290,6 +291,11 @@ Str_To_Utf8_IMP(String *self) {
     return buf;
 }
 
+ByteBuf*
+Str_To_ByteBuf_IMP(String *self) {
+    return BB_new_bytes(self->ptr, self->size);
+}
+
 String*
 Str_Clone_IMP(String *self) {
     return (String*)INCREF(self);

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/2ba218ef/runtime/core/Clownfish/String.cfh
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/String.cfh 
b/runtime/core/Clownfish/String.cfh
index 284e6ec..a2d6337 100644
--- a/runtime/core/Clownfish/String.cfh
+++ b/runtime/core/Clownfish/String.cfh
@@ -212,6 +212,11 @@ public final class Clownfish::String nickname Str
     public char*
     To_Utf8(String *self);
 
+    /** Return a ByteBuf which holds a copy of the String.
+     */
+    public incremented ByteBuf*
+    To_ByteBuf(String *self);
+
     public incremented String*
     Clone(String *self);
 

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/2ba218ef/runtime/core/Clownfish/Test/TestString.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/Test/TestString.c 
b/runtime/core/Clownfish/Test/TestString.c
index c32dbd1..86103e2 100644
--- a/runtime/core/Clownfish/Test/TestString.c
+++ b/runtime/core/Clownfish/Test/TestString.c
@@ -24,6 +24,7 @@
 
 #include "Clownfish/String.h"
 #include "Clownfish/Boolean.h"
+#include "Clownfish/ByteBuf.h"
 #include "Clownfish/CharBuf.h"
 #include "Clownfish/Test.h"
 #include "Clownfish/TestHarness/TestBatchRunner.h"
@@ -374,6 +375,15 @@ test_To_Utf8(TestBatchRunner *runner) {
 }
 
 static void
+test_To_ByteBuf(TestBatchRunner *runner) {
+    String     *string = Str_newf("foo");
+    ByteBuf    *bb     = Str_To_ByteBuf(string);
+    TEST_TRUE(runner, BB_Equals_Bytes(bb, "foo", 3), "To_ByteBuf");
+    DECREF(bb);
+    DECREF(string);
+}
+
+static void
 test_Length(TestBatchRunner *runner) {
     String *string = Str_newf("a%s%sb%sc", smiley, smiley, smiley);
     TEST_INT_EQ(runner, Str_Length(string), 6, "Length");
@@ -693,7 +703,7 @@ test_iterator_substring(TestBatchRunner *runner) {
 
 void
 TestStr_Run_IMP(TestString *self, TestBatchRunner *runner) {
-    TestBatchRunner_Plan(runner, (TestBatch*)self, 138);
+    TestBatchRunner_Plan(runner, (TestBatch*)self, 139);
     test_new(runner);
     test_Cat(runner);
     test_Clone(runner);
@@ -705,6 +715,7 @@ TestStr_Run_IMP(TestString *self, TestBatchRunner *runner) {
     test_To_I64(runner);
     test_To_String(runner);
     test_To_Utf8(runner);
+    test_To_ByteBuf(runner);
     test_Length(runner);
     test_Compare_To(runner);
     test_Starts_Ends_With(runner);

Reply via email to