lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nwelln...@apache.org
Subject [lucy-commits] [06/17] git commit: refs/heads/cfish-string-prep1 - Immutable versions of String Trim methods
Date Fri, 13 Sep 2013 18:31:38 GMT
Immutable versions of String Trim methods


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

Branch: refs/heads/cfish-string-prep1
Commit: e409e848a137a8478967a296c603eb97c7034b24
Parents: b0f3052
Author: Nick Wellnhofer <wellnhofer@aevum.de>
Authored: Sat Sep 7 19:18:35 2013 +0200
Committer: Nick Wellnhofer <wellnhofer@aevum.de>
Committed: Mon Sep 9 20:17:51 2013 +0200

----------------------------------------------------------------------
 clownfish/runtime/core/Clownfish/String.c       | 80 +++++---------------
 clownfish/runtime/core/Clownfish/String.cfh     |  9 +--
 .../runtime/core/Clownfish/Test/TestString.c    | 62 +++++++++++----
 core/Lucy/Test/Util/TestJson.c                  | 19 +++--
 4 files changed, 79 insertions(+), 91 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/e409e848/clownfish/runtime/core/Clownfish/String.c
----------------------------------------------------------------------
diff --git a/clownfish/runtime/core/Clownfish/String.c b/clownfish/runtime/core/Clownfish/String.c
index d1d36d1..319f54d 100644
--- a/clownfish/runtime/core/Clownfish/String.c
+++ b/clownfish/runtime/core/Clownfish/String.c
@@ -430,52 +430,32 @@ Str_Find_Str_IMP(String *self, const char *ptr, size_t size) {
     return -1;
 }
 
-uint32_t
+String*
 Str_Trim_IMP(String *self) {
-    return Str_Trim_Top(self) + Str_Trim_Tail(self);
-}
+    StackStringIterator *top = STR_STACKTOP(self);
+    SStrIter_Skip_Next_Whitespace(top);
 
-uint32_t
-Str_Trim_Top_IMP(String *self) {
-    char     *ptr   = self->ptr;
-    char     *end   = ptr + self->size;
-    uint32_t  count = 0;
-
-    while (ptr < end) {
-        uint32_t code_point = StrHelp_decode_utf8_char(ptr);
-        if (!StrHelp_is_whitespace(code_point)) { break; }
-        ptr += StrHelp_UTF8_COUNT[*(uint8_t*)ptr];
-        count++;
-    }
-    if (ptr > end) {
-        DIE_INVALID_UTF8(self->ptr, self->size);
+    StackStringIterator *tail = NULL;
+    if (top->byte_offset < self->size) {
+        tail = STR_STACKTAIL(self);
+        SStrIter_Skip_Prev_Whitespace(tail);
     }
 
-    if (count) {
-        // Copy string backwards.
-        self->size = end - ptr;
-        memmove(self->ptr, ptr, self->size);
-    }
+    return StrIter_substring((StringIterator*)top, (StringIterator*)tail);
+}
 
-    return count;
+String*
+Str_Trim_Top_IMP(String *self) {
+    StackStringIterator *top = STR_STACKTOP(self);
+    SStrIter_Skip_Next_Whitespace(top);
+    return StrIter_substring((StringIterator*)top, NULL);
 }
 
-uint32_t
+String*
 Str_Trim_Tail_IMP(String *self) {
-    uint32_t      count    = 0;
-    char *const   top      = self->ptr;
-    const char   *ptr      = top + self->size;
-    size_t        new_size = self->size;
-
-    while (NULL != (ptr = StrHelp_back_utf8_char(ptr, top))) {
-        uint32_t code_point = StrHelp_decode_utf8_char(ptr);
-        if (!StrHelp_is_whitespace(code_point)) { break; }
-        new_size = ptr - top;
-        count++;
-    }
-    self->size = new_size;
-
-    return count;
+    StackStringIterator *tail = STR_STACKTAIL(self);
+    SStrIter_Skip_Prev_Whitespace(tail);
+    return StrIter_substring(NULL, (StringIterator*)tail);
 }
 
 size_t
@@ -633,30 +613,6 @@ ViewCB_Assign_Trusted_Str_IMP(ViewCharBuf *self, const char *utf8, size_t
size)
     self->size = size;
 }
 
-uint32_t
-ViewCB_Trim_Top_IMP(ViewCharBuf *self) {
-    uint32_t  count = 0;
-    char     *ptr   = self->ptr;
-    char     *end   = ptr + self->size;
-
-    while (ptr < end) {
-        uint32_t code_point = StrHelp_decode_utf8_char(ptr);
-        if (!StrHelp_is_whitespace(code_point)) { break; }
-        ptr += StrHelp_UTF8_COUNT[*(uint8_t*)ptr];
-        count++;
-    }
-
-    if (count) {
-        if (ptr > end) {
-            DIE_INVALID_UTF8(self->ptr, self->size);
-        }
-        self->size = end - ptr;
-        self->ptr  = ptr;
-    }
-
-    return count;
-}
-
 char*
 ViewCB_Grow_IMP(ViewCharBuf *self, size_t size) {
     UNUSED_VAR(size);

http://git-wip-us.apache.org/repos/asf/lucy/blob/e409e848/clownfish/runtime/core/Clownfish/String.cfh
----------------------------------------------------------------------
diff --git a/clownfish/runtime/core/Clownfish/String.cfh b/clownfish/runtime/core/Clownfish/String.cfh
index bf56e22..bce0b36 100644
--- a/clownfish/runtime/core/Clownfish/String.cfh
+++ b/clownfish/runtime/core/Clownfish/String.cfh
@@ -226,17 +226,17 @@ class Clownfish::String cnick Str
 
     /** Remove Unicode whitespace characters from both top and tail.
      */
-    uint32_t
+    String*
     Trim(String *self);
 
     /** Remove leading Unicode whitespace.
      */
-    uint32_t
+    String*
     Trim_Top(String *self);
 
     /** Remove trailing Unicode whitespace.
      */
-    uint32_t
+    String*
     Trim_Tail(String *self);
 
     /** Return the Unicode code point at the specified number of code points
@@ -304,9 +304,6 @@ class Clownfish::ViewCharBuf cnick ViewCB
     void
     Assign_Trusted_Str(ViewCharBuf *self, const char *utf8, size_t size);
 
-    uint32_t
-    Trim_Top(ViewCharBuf *self);
-
     /** Throws an error. */
     char*
     Grow(ViewCharBuf *self, size_t size);

http://git-wip-us.apache.org/repos/asf/lucy/blob/e409e848/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 75616fd..ec37a2b 100644
--- a/clownfish/runtime/core/Clownfish/Test/TestString.c
+++ b/clownfish/runtime/core/Clownfish/Test/TestString.c
@@ -175,25 +175,57 @@ test_SubString(TestBatchRunner *runner) {
 
 static void
 test_Trim(TestBatchRunner *runner) {
+    String *ws_smiley = S_smiley_with_whitespace(NULL);
+    String *ws_foo    = S_get_str("  foo  ");
+    String *ws_only   = S_get_str("  \t  \r\n");
+    String *trimmed   = S_get_str("a     b");
     String *got;
 
-    got = S_smiley_with_whitespace(NULL);
-    TEST_TRUE(runner, Str_Trim_Top(got), "Trim_Top returns true on success");
-    TEST_FALSE(runner, Str_Trim_Top(got),
-               "Trim_Top returns false on failure");
-    TEST_TRUE(runner, Str_Trim_Tail(got), "Trim_Tail returns true on success");
-    TEST_FALSE(runner, Str_Trim_Tail(got),
-               "Trim_Tail returns false on failure");
-    TEST_TRUE(runner, Str_Equals_Str(got, smiley, smiley_len),
-              "Trim_Top and Trim_Tail worked");
+    got = Str_Trim(ws_smiley);
+    TEST_TRUE(runner, Str_Equals_Str(got, smiley, smiley_len), "Trim");
     DECREF(got);
 
-    got = S_smiley_with_whitespace(NULL);
-    TEST_TRUE(runner, Str_Trim(got), "Trim returns true on success");
-    TEST_FALSE(runner, Str_Trim(got), "Trim returns false on failure");
-    TEST_TRUE(runner, Str_Equals_Str(got, smiley, smiley_len),
-              "Trim worked");
+    got = Str_Trim_Top(ws_foo);
+    TEST_TRUE(runner, Str_Equals_Str(got, "foo  ", 5), "Trim_Top");
     DECREF(got);
+
+    got = Str_Trim_Tail(ws_foo);
+    TEST_TRUE(runner, Str_Equals_Str(got, "  foo", 5), "Trim_Tail");
+    DECREF(got);
+
+    got = Str_Trim(ws_only);
+    TEST_TRUE(runner, Str_Equals_Str(got, "", 0), "Trim with only whitespace");
+    DECREF(got);
+
+    got = Str_Trim_Top(ws_only);
+    TEST_TRUE(runner, Str_Equals_Str(got, "", 0),
+              "Trim_Top with only whitespace");
+    DECREF(got);
+
+    got = Str_Trim_Tail(ws_only);
+    TEST_TRUE(runner, Str_Equals_Str(got, "", 0),
+              "Trim_Tail with only whitespace");
+    DECREF(got);
+
+    got = Str_Trim(trimmed);
+    TEST_TRUE(runner, Str_Equals(got, (Obj*)trimmed),
+              "Trim doesn't change trimmed string");
+    DECREF(got);
+
+    got = Str_Trim_Top(trimmed);
+    TEST_TRUE(runner, Str_Equals(got, (Obj*)trimmed),
+              "Trim_Top doesn't change trimmed string");
+    DECREF(got);
+
+    got = Str_Trim_Tail(trimmed);
+    TEST_TRUE(runner, Str_Equals(got, (Obj*)trimmed),
+              "Trim_Tail doesn't change trimmed string");
+    DECREF(got);
+
+    DECREF(trimmed);
+    DECREF(ws_only);
+    DECREF(ws_foo);
+    DECREF(ws_smiley);
 }
 
 static void
@@ -456,7 +488,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, 103);
     test_Cat(runner);
     test_Mimic_and_Clone(runner);
     test_Code_Point_At_and_From(runner);

http://git-wip-us.apache.org/repos/asf/lucy/blob/e409e848/core/Lucy/Test/Util/TestJson.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Test/Util/TestJson.c b/core/Lucy/Test/Util/TestJson.c
index 3872a1d..3b17a6d 100644
--- a/core/Lucy/Test/Util/TestJson.c
+++ b/core/Lucy/Test/Util/TestJson.c
@@ -118,11 +118,11 @@ test_escapes(TestBatchRunner *runner) {
         String     *string  = Str_new_from_char(i);
         const char *escaped = control_escapes[i];
         String     *json    = Json_to_json((Obj*)string);
+        String     *trimmed = Str_Trim(json);
         String     *decoded = (String*)Json_from_json(json);
 
         String *json_wanted = Str_newf("\"%s\"", escaped);
-        Str_Trim(json);
-        TEST_TRUE(runner, json != NULL && Str_Equals(json_wanted, (Obj*)json),
+        TEST_TRUE(runner, Str_Equals(json_wanted, (Obj*)trimmed),
                   "encode control escape: %s", escaped);
 
         TEST_TRUE(runner, decoded != NULL && Str_Equals(string, (Obj*)decoded),
@@ -130,6 +130,7 @@ test_escapes(TestBatchRunner *runner) {
 
         DECREF(string);
         DECREF(json);
+        DECREF(trimmed);
         DECREF(decoded);
         DECREF(json_wanted);
     }
@@ -139,11 +140,11 @@ test_escapes(TestBatchRunner *runner) {
         const char *escaped = quote_escapes_json[i];
         String *string  = Str_new_from_utf8(source, strlen(source));
         String *json    = Json_to_json((Obj*)string);
+        String *trimmed = Str_Trim(json);
         String *decoded = (String*)Json_from_json(json);
 
         String *json_wanted = Str_newf("\"%s\"", escaped);
-        Str_Trim(json);
-        TEST_TRUE(runner, json != NULL && Str_Equals(json_wanted, (Obj*)json),
+        TEST_TRUE(runner, Str_Equals(json_wanted, (Obj*)trimmed),
                   "encode quote/backslash escapes: %s", source);
 
         TEST_TRUE(runner, decoded != NULL && Str_Equals(string, (Obj*)decoded),
@@ -151,6 +152,7 @@ test_escapes(TestBatchRunner *runner) {
 
         DECREF(string);
         DECREF(json);
+        DECREF(trimmed);
         DECREF(decoded);
         DECREF(json_wanted);
     }
@@ -158,11 +160,12 @@ test_escapes(TestBatchRunner *runner) {
 
 static void
 test_numbers(TestBatchRunner *runner) {
-    Integer64 *i64  = Int64_new(33);
-    String    *json = Json_to_json((Obj*)i64);
-    Str_Trim(json);
-    TEST_TRUE(runner, json && Str_Equals_Str(json, "33", 2), "Integer");
+    Integer64 *i64     = Int64_new(33);
+    String    *json    = Json_to_json((Obj*)i64);
+    String    *trimmed = Str_Trim(json);
+    TEST_TRUE(runner, Str_Equals_Str(trimmed, "33", 2), "Integer");
     DECREF(json);
+    DECREF(trimmed);
 
     Float64 *f64 = Float64_new(33.33);
     json = Json_to_json((Obj*)f64);


Mime
View raw message