lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nwelln...@apache.org
Subject [lucy-commits] [01/20] git commit: refs/heads/cfish-string-prep1 - Allow NULL arguments to StrIter_substring
Date Sat, 07 Sep 2013 16:53:22 GMT
Updated Branches:
  refs/heads/cfish-string-prep1 1f51cae0f -> ee3c8ed8f


Allow NULL arguments to StrIter_substring


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

Branch: refs/heads/cfish-string-prep1
Commit: e3a2f9280851e957f678752300a98595236eec02
Parents: 0d0cb67
Author: Nick Wellnhofer <wellnhofer@aevum.de>
Authored: Sat Sep 7 12:39:22 2013 +0200
Committer: Nick Wellnhofer <wellnhofer@aevum.de>
Committed: Sat Sep 7 18:12:45 2013 +0200

----------------------------------------------------------------------
 clownfish/runtime/core/Clownfish/String.c       | 39 +++++++++++++++-----
 clownfish/runtime/core/Clownfish/String.cfh     |  4 +-
 .../runtime/core/Clownfish/Test/TestString.c    | 20 +++++++++-
 3 files changed, 51 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/e3a2f928/clownfish/runtime/core/Clownfish/String.c
----------------------------------------------------------------------
diff --git a/clownfish/runtime/core/Clownfish/String.c b/clownfish/runtime/core/Clownfish/String.c
index 01a7939..ce10b7f 100644
--- a/clownfish/runtime/core/Clownfish/String.c
+++ b/clownfish/runtime/core/Clownfish/String.c
@@ -790,20 +790,41 @@ StrIter_new(String *string, size_t byte_offset) {
 
 String*
 StrIter_substring(StringIterator *top, StringIterator *tail) {
-    String *string = top->string;
+    String *string;
+    size_t  top_offset;
+    size_t  tail_offset;
 
-    if (string != tail->string) {
-        THROW(ERR, "StrIter_substring: strings don't match");
+    if (tail == NULL) {
+        if (top == NULL) {
+            THROW(ERR, "StrIter_substring: Both top and tail are NULL");
+        }
+        string      = top->string;
+        tail_offset = string->size;
+    }
+    else {
+        string = tail->string;
+        if (top != NULL && string != top->string) {
+            THROW(ERR, "StrIter_substring: strings don't match");
+        }
+
+        tail_offset = tail->byte_offset;
+        if (tail_offset > string->size) {
+            THROW(ERR, "Invalid StringIterator offset");
+        }
     }
-    if (top->byte_offset > tail->byte_offset) {
-        THROW(ERR, "StrIter_substring: top is behind tail");
+
+    if (top == NULL) {
+        top_offset = 0;
     }
-    if (tail->byte_offset > string->size) {
-        THROW(ERR, "Invalid StringIterator offset");
+    else {
+        top_offset = top->byte_offset;
+        if (top_offset > tail_offset) {
+            THROW(ERR, "StrIter_substring: top is behind tail");
+        }
     }
 
-    return Str_new_from_trusted_utf8(string->ptr + top->byte_offset,
-                                     tail->byte_offset - top->byte_offset);
+    return Str_new_from_trusted_utf8(string->ptr + top_offset,
+                                     tail_offset - top_offset);
 }
 
 StringIterator*

http://git-wip-us.apache.org/repos/asf/lucy/blob/e3a2f928/clownfish/runtime/core/Clownfish/String.cfh
----------------------------------------------------------------------
diff --git a/clownfish/runtime/core/Clownfish/String.cfh b/clownfish/runtime/core/Clownfish/String.cfh
index bf9a4a9..b49d957 100644
--- a/clownfish/runtime/core/Clownfish/String.cfh
+++ b/clownfish/runtime/core/Clownfish/String.cfh
@@ -386,8 +386,8 @@ class Clownfish::StringIterator cnick StrIter
     new(String *string, size_t byte_offset);
 
     /** Return the substring between the top and tail iterators.
-     * @param offset Offset from the top, in code points.
-     * @param len The desired length of the substring, in code points.
+     * @param offset Top iterator. Use start of string if NULL.
+     * @param len Tail iterator. Use end of string if NULL.
      */
     inert incremented String*
     substring(StringIterator *top, StringIterator *tail);

http://git-wip-us.apache.org/repos/asf/lucy/blob/e3a2f928/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 4b937f8..df04794 100644
--- a/clownfish/runtime/core/Clownfish/Test/TestString.c
+++ b/clownfish/runtime/core/Clownfish/Test/TestString.c
@@ -464,6 +464,24 @@ test_iterator_substring(TestBatchRunner *runner) {
         DECREF(substring);
     }
 
+    {
+        String *substring = StrIter_substring(end, NULL);
+        String *wanted = Str_newf("%sd", smiley);
+        TEST_TRUE(runner, Str_Equals(substring, (Obj*)wanted),
+                  "StrIter_substring with NULL tail");
+        DECREF(wanted);
+        DECREF(substring);
+    }
+
+    {
+        String *substring = StrIter_substring(NULL, start);
+        String *wanted = Str_newf("a%s", smiley);
+        TEST_TRUE(runner, Str_Equals(substring, (Obj*)wanted),
+                  "StrIter_substring with NULL top");
+        DECREF(wanted);
+        DECREF(substring);
+    }
+
     DECREF(start);
     DECREF(end);
     DECREF(string);
@@ -471,7 +489,7 @@ test_iterator_substring(TestBatchRunner *runner) {
 
 void
 TestStr_Run_IMP(TestString *self, TestBatchRunner *runner) {
-    TestBatchRunner_Plan(runner, (TestBatch*)self, 102);
+    TestBatchRunner_Plan(runner, (TestBatch*)self, 104);
     test_Cat(runner);
     test_Mimic_and_Clone(runner);
     test_Code_Point_At_and_From(runner);


Mime
View raw message