lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nwelln...@apache.org
Subject [lucy-commits] [13/50] [abbrv] git commit: refs/heads/master - Introduce Str_To_Utf8
Date Fri, 11 Oct 2013 13:31:51 GMT
Introduce Str_To_Utf8


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

Branch: refs/heads/master
Commit: c3b1106de563a27fbf648e090b71f94c6ed6ecba
Parents: c3ccd8c
Author: Nick Wellnhofer <wellnhofer@aevum.de>
Authored: Sat Sep 14 16:45:35 2013 +0200
Committer: Nick Wellnhofer <wellnhofer@aevum.de>
Committed: Fri Oct 11 15:28:16 2013 +0200

----------------------------------------------------------------------
 clownfish/runtime/core/Clownfish/String.c          |  9 ++++++---
 clownfish/runtime/core/Clownfish/String.cfh        |  8 ++++----
 clownfish/runtime/core/Clownfish/Test/TestString.c | 15 ++++++++++++++-
 3 files changed, 24 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/c3b1106d/clownfish/runtime/core/Clownfish/String.c
----------------------------------------------------------------------
diff --git a/clownfish/runtime/core/Clownfish/String.c b/clownfish/runtime/core/Clownfish/String.c
index d68aacf..0fb4c5f 100644
--- a/clownfish/runtime/core/Clownfish/String.c
+++ b/clownfish/runtime/core/Clownfish/String.c
@@ -237,9 +237,12 @@ Str_To_F64_IMP(String *self) {
     return value;
 }
 
-String*
-Str_To_CB8_IMP(String *self) {
-    return Str_new_from_trusted_utf8(self->ptr, self->size);
+char*
+Str_To_Utf8_IMP(String *self) {
+    char *buf = (char*)MALLOCATE(self->size + 1);
+    memcpy(buf, self->ptr, self->size);
+    buf[self->size] = '\0'; // NULL-terminate.
+    return buf;
 }
 
 String*

http://git-wip-us.apache.org/repos/asf/lucy/blob/c3b1106d/clownfish/runtime/core/Clownfish/String.cfh
----------------------------------------------------------------------
diff --git a/clownfish/runtime/core/Clownfish/String.cfh b/clownfish/runtime/core/Clownfish/String.cfh
index 22138db..41ca029 100644
--- a/clownfish/runtime/core/Clownfish/String.cfh
+++ b/clownfish/runtime/core/Clownfish/String.cfh
@@ -173,11 +173,11 @@ class Clownfish::String cnick Str
     uint8_t*
     Get_Ptr8(String *self);
 
-    /** Return a fresh copy of the string data in a String with an internal
-     * encoding of UTF-8.
+    /** Return a NULL-terminated copy of the string data in UTF-8 encoding.
+     * The buffer must be freed by the caller.
      */
-    String*
-    To_CB8(String *self);
+    char*
+    To_Utf8(String *self);
 
     public incremented String*
     Clone(String *self);

http://git-wip-us.apache.org/repos/asf/lucy/blob/c3b1106d/clownfish/runtime/core/Clownfish/Test/TestString.c
----------------------------------------------------------------------
diff --git a/clownfish/runtime/core/Clownfish/Test/TestString.c b/clownfish/runtime/core/Clownfish/Test/TestString.c
index 2b7e911..851ff81 100644
--- a/clownfish/runtime/core/Clownfish/Test/TestString.c
+++ b/clownfish/runtime/core/Clownfish/Test/TestString.c
@@ -31,8 +31,10 @@
 #include "Clownfish/Test.h"
 #include "Clownfish/TestHarness/TestBatchRunner.h"
 #include "Clownfish/TestHarness/TestUtils.h"
+#include "Clownfish/Util/Memory.h"
 #include "Clownfish/VTable.h"
 
+#define SMILEY "\xE2\x98\xBA"
 static char smiley[] = { (char)0xE2, (char)0x98, (char)0xBA, 0 };
 static uint32_t smiley_len = 3;
 static uint32_t smiley_cp  = 0x263A;
@@ -263,6 +265,16 @@ test_To_I64(TestBatchRunner *runner) {
 }
 
 static void
+test_To_Utf8(TestBatchRunner *runner) {
+    String *string = Str_newf("a%s%sb%sc", smiley, smiley, smiley);
+    char *buf = Str_To_Utf8(string);
+    TEST_TRUE(runner, strcmp(buf, "a" SMILEY SMILEY "b" SMILEY "c") == 0,
+              "To_Utf8");
+    FREEMEM(buf);
+    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");
@@ -495,7 +507,7 @@ test_iterator_substring(TestBatchRunner *runner) {
 
 void
 TestStr_Run_IMP(TestString *self, TestBatchRunner *runner) {
-    TestBatchRunner_Plan(runner, (TestBatch*)self, 100);
+    TestBatchRunner_Plan(runner, (TestBatch*)self, 101);
     test_Cat(runner);
     test_Clone(runner);
     test_Code_Point_At_and_From(runner);
@@ -504,6 +516,7 @@ TestStr_Run_IMP(TestString *self, TestBatchRunner *runner) {
     test_Trim(runner);
     test_To_F64(runner);
     test_To_I64(runner);
+    test_To_Utf8(runner);
     test_Length(runner);
     test_Compare_To(runner);
     test_Swap_Chars(runner);


Mime
View raw message