You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by nw...@apache.org on 2013/09/01 22:17:06 UTC

[lucy-commits] [18/24] git commit: refs/heads/cfish-string-prep1 - Immutable versions of Str_Cat

Immutable versions of Str_Cat

These will be renamed later.


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

Branch: refs/heads/cfish-string-prep1
Commit: e6150f48b0426484aa3e3579b1cd35f08f455e55
Parents: 0806c59
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Sun Sep 1 20:47:18 2013 +0200
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Sun Sep 1 22:05:02 2013 +0200

----------------------------------------------------------------------
 clownfish/runtime/core/Clownfish/String.c       | 25 +++++++++++++++++
 clownfish/runtime/core/Clownfish/String.cfh     | 16 +++++++++++
 .../runtime/core/Clownfish/Test/TestString.c    | 28 ++++++++++----------
 3 files changed, 55 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/e6150f48/clownfish/runtime/core/Clownfish/String.c
----------------------------------------------------------------------
diff --git a/clownfish/runtime/core/Clownfish/String.c b/clownfish/runtime/core/Clownfish/String.c
index 3cf3668..a2f0a3f 100644
--- a/clownfish/runtime/core/Clownfish/String.c
+++ b/clownfish/runtime/core/Clownfish/String.c
@@ -459,6 +459,31 @@ Str_Mimic_IMP(String *self, Obj *other) {
     self->ptr[twin->size] = '\0';
 }
 
+String*
+Str_Immutable_Cat_IMP(String *self, const String *other) {
+    return Str_Immutable_Cat_Trusted_UTF8(self, other->ptr, other->size);
+}
+
+String*
+Str_Immutable_Cat_UTF8_IMP(String *self, const char* ptr, size_t size) {
+    if (!StrHelp_utf8_valid(ptr, size)) {
+        DIE_INVALID_UTF8(ptr, size);
+    }
+    return Str_Immutable_Cat_Trusted_UTF8(self, ptr, size);
+}
+
+String*
+Str_Immutable_Cat_Trusted_UTF8_IMP(String *self, const char* ptr, size_t size) {
+    size_t  result_size = self->size + size;
+    char   *result_ptr  = (char*)MALLOCATE(result_size + 1);
+    memcpy(result_ptr, self->ptr, self->size);
+    memcpy(result_ptr + self->size, ptr, size);
+    result_ptr[result_size] = '\0';
+    String *result = (String*)VTable_Make_Obj(STRING);
+    return Str_init_steal_trusted_str(result, result_ptr, result_size,
+                                      result_size + 1);
+}
+
 void
 Str_Cat_Str_IMP(String *self, const char* ptr, size_t size) {
     if (!StrHelp_utf8_valid(ptr, size)) {

http://git-wip-us.apache.org/repos/asf/lucy/blob/e6150f48/clownfish/runtime/core/Clownfish/String.cfh
----------------------------------------------------------------------
diff --git a/clownfish/runtime/core/Clownfish/String.cfh b/clownfish/runtime/core/Clownfish/String.cfh
index 2cddf4a..529cc70 100644
--- a/clownfish/runtime/core/Clownfish/String.cfh
+++ b/clownfish/runtime/core/Clownfish/String.cfh
@@ -92,6 +92,22 @@ class Clownfish::String cnick Str
     void
     Mimic_Str(String *self, const char *ptr, size_t size);
 
+    /** Return the concatenation of the String and <code>other</code>.
+     */
+    incremented String*
+    Immutable_Cat(String *self, const String *other);
+
+    /** Return the concatenation of the String and the passed-in raw UTF-8.
+     */
+    incremented String*
+    Immutable_Cat_UTF8(String *self, const char *ptr, size_t size);
+
+    /** Return the concatenation of the String and the passed-in raw UTF-8.
+     * Don't check for UTF-8 validity.
+     */
+    incremented String*
+    Immutable_Cat_Trusted_UTF8(String *self, const char *ptr, size_t size);
+
     /** Concatenate the passed-in string onto the end of the String.
      */
     void

http://git-wip-us.apache.org/repos/asf/lucy/blob/e6150f48/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 3a4bd54..ba8076f 100644
--- a/clownfish/runtime/core/Clownfish/Test/TestString.c
+++ b/clownfish/runtime/core/Clownfish/Test/TestString.c
@@ -48,26 +48,26 @@ S_get_cb(const char *string) {
 static void
 test_Cat(TestBatchRunner *runner) {
     String *wanted = Str_newf("a%s", smiley);
-    String *got    = S_get_cb("");
+    String *source;
+    String *got;
 
-    Str_Cat(got, wanted);
+    source = S_get_cb("");
+    got = Str_Immutable_Cat(source, wanted);
     TEST_TRUE(runner, Str_Equals(wanted, (Obj*)got), "Cat");
     DECREF(got);
+    DECREF(source);
 
-    got = S_get_cb("a");
-    Str_Cat_Char(got, 0x263A);
-    TEST_TRUE(runner, Str_Equals(wanted, (Obj*)got), "Cat_Char");
-    DECREF(got);
-
-    got = S_get_cb("a");
-    Str_Cat_Str(got, smiley, smiley_len);
-    TEST_TRUE(runner, Str_Equals(wanted, (Obj*)got), "Cat_Str");
+    source = S_get_cb("a");
+    got = Str_Immutable_Cat_UTF8(source, smiley, smiley_len);
+    TEST_TRUE(runner, Str_Equals(wanted, (Obj*)got), "Cat_UTF8");
     DECREF(got);
+    DECREF(source);
 
-    got = S_get_cb("a");
-    Str_Cat_Trusted_Str(got, smiley, smiley_len);
-    TEST_TRUE(runner, Str_Equals(wanted, (Obj*)got), "Cat_Trusted_Str");
+    source = S_get_cb("a");
+    got = Str_Immutable_Cat_Trusted_UTF8(source, smiley, smiley_len);
+    TEST_TRUE(runner, Str_Equals(wanted, (Obj*)got), "Cat_Trusted_UTF8");
     DECREF(got);
+    DECREF(source);
 
     DECREF(wanted);
 }
@@ -415,7 +415,7 @@ test_vcatf_x32(TestBatchRunner *runner) {
 
 void
 TestStr_Run_IMP(TestString *self, TestBatchRunner *runner) {
-    TestBatchRunner_Plan(runner, (TestBatch*)self, 54);
+    TestBatchRunner_Plan(runner, (TestBatch*)self, 53);
     test_vcatf_s(runner);
     test_vcatf_null_string(runner);
     test_vcatf_cb(runner);