lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nwelln...@apache.org
Subject [lucy-commits] [12/15] git commit: refs/heads/cfish-string-prep1 - Remove Str_Cat and variants
Date Mon, 02 Sep 2013 19:16:43 GMT
Remove Str_Cat and variants


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

Branch: refs/heads/cfish-string-prep1
Commit: 3948f7876a1e32456273d157cc2d84a3a259e243
Parents: 6b1837e
Author: Nick Wellnhofer <wellnhofer@aevum.de>
Authored: Mon Sep 2 21:07:37 2013 +0200
Committer: Nick Wellnhofer <wellnhofer@aevum.de>
Committed: Mon Sep 2 21:12:30 2013 +0200

----------------------------------------------------------------------
 clownfish/runtime/core/Clownfish/String.c       | 197 +------------------
 clownfish/runtime/core/Clownfish/String.cfh     |  45 +----
 .../runtime/core/Clownfish/Test/TestString.c    | 167 +---------------
 3 files changed, 7 insertions(+), 402 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/3948f787/clownfish/runtime/core/Clownfish/String.c
----------------------------------------------------------------------
diff --git a/clownfish/runtime/core/Clownfish/String.c b/clownfish/runtime/core/Clownfish/String.c
index 92cc1c4..499c79e 100644
--- a/clownfish/runtime/core/Clownfish/String.c
+++ b/clownfish/runtime/core/Clownfish/String.c
@@ -30,6 +30,7 @@
 #include "Clownfish/VTable.h"
 #include "Clownfish/String.h"
 
+#include "Clownfish/CharBuf.h"
 #include "Clownfish/Err.h"
 #include "Clownfish/Util/Memory.h"
 #include "Clownfish/Util/StringHelper.h"
@@ -135,11 +136,13 @@ Str_new_from_char(uint32_t code_point) {
 
 String*
 Str_newf(const char *pattern, ...) {
-    String *self = Str_new(strlen(pattern));
+    CharBuf *buf = CB_new(strlen(pattern));
     va_list args;
     va_start(args, pattern);
-    Str_VCatF(self, pattern, args);
+    CB_VCatF(buf, pattern, args);
     va_end(args);
+    String *self = CB_Yield_String(buf);
+    DECREF(buf);
     return self;
 }
 
@@ -199,169 +202,11 @@ S_die_invalid_pattern(const char *pattern) {
     THROW(ERR, "Invalid pattern.");
 }
 
-void
-Str_catf(String *self, const char *pattern, ...) {
-    va_list args;
-    va_start(args, pattern);
-    Str_VCatF(self, pattern, args);
-    va_end(args);
-}
-
-void
-Str_VCatF_IMP(String *self, const char *pattern, va_list args) {
-    size_t      pattern_len   = strlen(pattern);
-    const char *pattern_start = pattern;
-    const char *pattern_end   = pattern + pattern_len;
-    char        buf[64];
-
-    for (; pattern < pattern_end; pattern++) {
-        const char *slice_end = pattern;
-
-        // Consume all characters leading up to a '%'.
-        while (slice_end < pattern_end && *slice_end != '%') { slice_end++; }
-        if (pattern != slice_end) {
-            size_t size = slice_end - pattern;
-            Str_Cat_Trusted_Str(self, pattern, size);
-            pattern = slice_end;
-        }
-
-        if (pattern < pattern_end) {
-            pattern++; // Move past '%'.
-
-            switch (*pattern) {
-                case '%': {
-                        Str_Cat_Trusted_Str(self, "%", 1);
-                    }
-                    break;
-                case 'o': {
-                        Obj *obj = va_arg(args, Obj*);
-                        if (!obj) {
-                            Str_Cat_Trusted_Str(self, "[NULL]", 6);
-                        }
-                        else if (Obj_Is_A(obj, STRING)) {
-                            Str_Cat(self, (String*)obj);
-                        }
-                        else {
-                            String *string = Obj_To_String(obj);
-                            Str_Cat(self, string);
-                            DECREF(string);
-                        }
-                    }
-                    break;
-                case 'i': {
-                        int64_t val = 0;
-                        size_t size;
-                        if (pattern[1] == '8') {
-                            val = va_arg(args, int32_t);
-                            pattern++;
-                        }
-                        else if (pattern[1] == '3' && pattern[2] == '2') {
-                            val = va_arg(args, int32_t);
-                            pattern += 2;
-                        }
-                        else if (pattern[1] == '6' && pattern[2] == '4') {
-                            val = va_arg(args, int64_t);
-                            pattern += 2;
-                        }
-                        else {
-                            S_die_invalid_pattern(pattern_start);
-                        }
-                        size = sprintf(buf, "%" PRId64, val);
-                        Str_Cat_Trusted_Str(self, buf, size);
-                    }
-                    break;
-                case 'u': {
-                        uint64_t val = 0;
-                        size_t size;
-                        if (pattern[1] == '8') {
-                            val = va_arg(args, uint32_t);
-                            pattern += 1;
-                        }
-                        else if (pattern[1] == '3' && pattern[2] == '2') {
-                            val = va_arg(args, uint32_t);
-                            pattern += 2;
-                        }
-                        else if (pattern[1] == '6' && pattern[2] == '4') {
-                            val = va_arg(args, uint64_t);
-                            pattern += 2;
-                        }
-                        else {
-                            S_die_invalid_pattern(pattern_start);
-                        }
-                        size = sprintf(buf, "%" PRIu64, val);
-                        Str_Cat_Trusted_Str(self, buf, size);
-                    }
-                    break;
-                case 'f': {
-                        if (pattern[1] == '6' && pattern[2] == '4') {
-                            double num  = va_arg(args, double);
-                            char bigbuf[512];
-                            size_t size = sprintf(bigbuf, "%g", num);
-                            Str_Cat_Trusted_Str(self, bigbuf, size);
-                            pattern += 2;
-                        }
-                        else {
-                            S_die_invalid_pattern(pattern_start);
-                        }
-                    }
-                    break;
-                case 'x': {
-                        if (pattern[1] == '3' && pattern[2] == '2') {
-                            unsigned long val = va_arg(args, uint32_t);
-                            size_t size = sprintf(buf, "%.8lx", val);
-                            Str_Cat_Trusted_Str(self, buf, size);
-                            pattern += 2;
-                        }
-                        else {
-                            S_die_invalid_pattern(pattern_start);
-                        }
-                    }
-                    break;
-                case 's': {
-                        char *string = va_arg(args, char*);
-                        if (string == NULL) {
-                            Str_Cat_Trusted_Str(self, "[NULL]", 6);
-                        }
-                        else {
-                            size_t size = strlen(string);
-                            if (StrHelp_utf8_valid(string, size)) {
-                                Str_Cat_Trusted_Str(self, string, size);
-                            }
-                            else {
-                                Str_Cat_Trusted_Str(self, "[INVALID UTF8]", 14);
-                            }
-                        }
-                    }
-                    break;
-                default: {
-                        // Assume NULL-terminated pattern string, which
-                        // eliminates the need for bounds checking if '%' is
-                        // the last visible character.
-                        S_die_invalid_pattern(pattern_start);
-                    }
-            }
-        }
-    }
-}
-
 String*
 Str_To_String_IMP(String *self) {
     return Str_new_from_trusted_utf8(self->ptr, self->size);
 }
 
-void
-Str_Cat_Char_IMP(String *self, uint32_t code_point) {
-    const size_t MAX_UTF8_BYTES = 4;
-    if (self->size + MAX_UTF8_BYTES >= self->cap) {
-        S_grow(self, Memory_oversize(self->size + MAX_UTF8_BYTES,
-                                     sizeof(char)));
-    }
-    char *end = self->ptr + self->size;
-    size_t count = StrHelp_encode_utf8_char(code_point, (uint8_t*)end);
-    self->size += count;
-    *(end + count) = '\0';
-}
-
 int32_t
 Str_Swap_Chars_IMP(String *self, uint32_t match, uint32_t replacement) {
     int32_t num_swapped = 0;
@@ -500,38 +345,6 @@ Str_Immutable_Cat_Trusted_UTF8_IMP(String *self, const char* ptr, size_t
size) {
                                       result_size + 1);
 }
 
-void
-Str_Cat_Str_IMP(String *self, const char* ptr, size_t size) {
-    if (!StrHelp_utf8_valid(ptr, size)) {
-        DIE_INVALID_UTF8(ptr, size);
-    }
-    Str_Cat_Trusted_Str_IMP(self, ptr, size);
-}
-
-void
-Str_Cat_Trusted_Str_IMP(String *self, const char* ptr, size_t size) {
-    const size_t new_size = self->size + size;
-    if (new_size >= self->cap) {
-        size_t amount = Memory_oversize(new_size, sizeof(char));
-        S_grow(self, amount);
-    }
-    memcpy((self->ptr + self->size), ptr, size);
-    self->size = new_size;
-    self->ptr[new_size] = '\0';
-}
-
-void
-Str_Cat_IMP(String *self, const String *other) {
-    const size_t new_size = self->size + other->size;
-    if (new_size >= self->cap) {
-        size_t amount = Memory_oversize(new_size, sizeof(char));
-        S_grow(self, amount);
-    }
-    memcpy((self->ptr + self->size), other->ptr, other->size);
-    self->size = new_size;
-    self->ptr[new_size] = '\0';
-}
-
 bool
 Str_Starts_With_IMP(String *self, const String *prefix) {
     return Str_Starts_With_Str_IMP(self, prefix->ptr, prefix->size);

http://git-wip-us.apache.org/repos/asf/lucy/blob/3948f787/clownfish/runtime/core/Clownfish/String.cfh
----------------------------------------------------------------------
diff --git a/clownfish/runtime/core/Clownfish/String.cfh b/clownfish/runtime/core/Clownfish/String.cfh
index 8c4618f..edc52e3 100644
--- a/clownfish/runtime/core/Clownfish/String.cfh
+++ b/clownfish/runtime/core/Clownfish/String.cfh
@@ -76,7 +76,7 @@ class Clownfish::String cnick Str
     new_from_char(uint32_t code_point);
 
     /** Return a pointer to a new String which contains formatted data
-     * expanded according to Str_VCatF.
+     * expanded according to CB_VCatF.
      *
      * Note: a user-supplied <code>pattern</code> string is a security hole
      * and must not be allowed.
@@ -119,43 +119,6 @@ class Clownfish::String cnick Str
     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
-    Cat_Str(String *self, const char *ptr, size_t size);
-
-    /** Concatenate the contents of <code>other</code> onto the end of the
-     * caller.
-     */
-    void
-    Cat(String *self, const String *other);
-
-    /** Concatenate formatted arguments.  Similar to the printf family, but
-     * only accepts minimal options (just enough for decent error messages).
-     *
-     * Objects:  %o
-     * char*:    %s
-     * integers: %i8 %i32 %i64 %u8 %u32 %u64
-     * floats:   %f64
-     * hex:      %x32
-     *
-     * Note that all Clownfish Objects, including Strings, are printed via
-     * %o (which invokes Obj_To_String()).
-     */
-    void
-    VCatF(String *self, const char *pattern, va_list args);
-
-    /** Invokes Str_VCatF to concatenate formatted arguments.  Note that this
-     * is only a function and not a method.
-     */
-    inert void
-    catf(String *self, const char *pattern, ...);
-
-    /** Concatenate one Unicode character onto the end of the String.
-     */
-    void
-    Cat_Char(String *self, uint32_t code_point);
-
     /** Replace all instances of one character for the other.  For now, both
      * the source and replacement code points must be ASCII.
      */
@@ -307,12 +270,6 @@ class Clownfish::String cnick Str
      */
     incremented String*
     SubString(String *self, size_t offset, size_t len);
-
-    /** Concatenate the supplied text onto the end of the String.  Don't
-     * check for UTF-8 validity.
-     */
-    void
-    Cat_Trusted_Str(String *self, const char *ptr, size_t size);
 }
 
 class Clownfish::ViewCharBuf cnick ViewCB

http://git-wip-us.apache.org/repos/asf/lucy/blob/3948f787/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 5a594a4..cfc2968 100644
--- a/clownfish/runtime/core/Clownfish/Test/TestString.c
+++ b/clownfish/runtime/core/Clownfish/Test/TestString.c
@@ -261,174 +261,9 @@ test_To_I64(TestBatchRunner *runner) {
 }
 
 
-static void
-test_vcatf_s(TestBatchRunner *runner) {
-    String *wanted = S_get_str("foo bar bizzle baz");
-    String *got = S_get_str("foo ");
-    Str_catf(got, "bar %s baz", "bizzle");
-    TEST_TRUE(runner, Str_Equals(wanted, (Obj*)got), "%%s");
-    DECREF(wanted);
-    DECREF(got);
-}
-
-static void
-test_vcatf_null_string(TestBatchRunner *runner) {
-    String *wanted = S_get_str("foo bar [NULL] baz");
-    String *got = S_get_str("foo ");
-    Str_catf(got, "bar %s baz", NULL);
-    TEST_TRUE(runner, Str_Equals(wanted, (Obj*)got), "%%s NULL");
-    DECREF(wanted);
-    DECREF(got);
-}
-
-static void
-test_vcatf_str(TestBatchRunner *runner) {
-    String *wanted = S_get_str("foo bar ZEKE baz");
-    String *catworthy = S_get_str("ZEKE");
-    String *got = S_get_str("foo ");
-    Str_catf(got, "bar %o baz", catworthy);
-    TEST_TRUE(runner, Str_Equals(wanted, (Obj*)got), "%%o String");
-    DECREF(catworthy);
-    DECREF(wanted);
-    DECREF(got);
-}
-
-static void
-test_vcatf_obj(TestBatchRunner *runner) {
-    String    *wanted = S_get_str("ooga 20 booga");
-    Integer32 *i32 = Int32_new(20);
-    String    *got = S_get_str("ooga");
-    Str_catf(got, " %o booga", i32);
-    TEST_TRUE(runner, Str_Equals(wanted, (Obj*)got), "%%o Obj");
-    DECREF(i32);
-    DECREF(wanted);
-    DECREF(got);
-}
-
-static void
-test_vcatf_null_obj(TestBatchRunner *runner) {
-    String *wanted = S_get_str("foo bar [NULL] baz");
-    String *got = S_get_str("foo ");
-    Str_catf(got, "bar %o baz", NULL);
-    TEST_TRUE(runner, Str_Equals(wanted, (Obj*)got), "%%o NULL");
-    DECREF(wanted);
-    DECREF(got);
-}
-
-static void
-test_vcatf_i8(TestBatchRunner *runner) {
-    String *wanted = S_get_str("foo bar -3 baz");
-    int8_t num = -3;
-    String *got = S_get_str("foo ");
-    Str_catf(got, "bar %i8 baz", num);
-    TEST_TRUE(runner, Str_Equals(wanted, (Obj*)got), "%%i8");
-    DECREF(wanted);
-    DECREF(got);
-}
-
-static void
-test_vcatf_i32(TestBatchRunner *runner) {
-    String *wanted = S_get_str("foo bar -100000 baz");
-    int32_t num = -100000;
-    String *got = S_get_str("foo ");
-    Str_catf(got, "bar %i32 baz", num);
-    TEST_TRUE(runner, Str_Equals(wanted, (Obj*)got), "%%i32");
-    DECREF(wanted);
-    DECREF(got);
-}
-
-static void
-test_vcatf_i64(TestBatchRunner *runner) {
-    String *wanted = S_get_str("foo bar -5000000000 baz");
-    int64_t num = INT64_C(-5000000000);
-    String *got = S_get_str("foo ");
-    Str_catf(got, "bar %i64 baz", num);
-    TEST_TRUE(runner, Str_Equals(wanted, (Obj*)got), "%%i64");
-    DECREF(wanted);
-    DECREF(got);
-}
-
-static void
-test_vcatf_u8(TestBatchRunner *runner) {
-    String *wanted = S_get_str("foo bar 3 baz");
-    uint8_t num = 3;
-    String *got = S_get_str("foo ");
-    Str_catf(got, "bar %u8 baz", num);
-    TEST_TRUE(runner, Str_Equals(wanted, (Obj*)got), "%%u8");
-    DECREF(wanted);
-    DECREF(got);
-}
-
-static void
-test_vcatf_u32(TestBatchRunner *runner) {
-    String *wanted = S_get_str("foo bar 100000 baz");
-    uint32_t num = 100000;
-    String *got = S_get_str("foo ");
-    Str_catf(got, "bar %u32 baz", num);
-    TEST_TRUE(runner, Str_Equals(wanted, (Obj*)got), "%%u32");
-    DECREF(wanted);
-    DECREF(got);
-}
-
-static void
-test_vcatf_u64(TestBatchRunner *runner) {
-    String *wanted = S_get_str("foo bar 5000000000 baz");
-    uint64_t num = UINT64_C(5000000000);
-    String *got = S_get_str("foo ");
-    Str_catf(got, "bar %u64 baz", num);
-    TEST_TRUE(runner, Str_Equals(wanted, (Obj*)got), "%%u64");
-    DECREF(wanted);
-    DECREF(got);
-}
-
-static void
-test_vcatf_f64(TestBatchRunner *runner) {
-    String *wanted;
-    char buf[64];
-    float num = 1.3f;
-    String *got = S_get_str("foo ");
-    sprintf(buf, "foo bar %g baz", num);
-    wanted = Str_new_from_trusted_utf8(buf, strlen(buf));
-    Str_catf(got, "bar %f64 baz", num);
-    TEST_TRUE(runner, Str_Equals(wanted, (Obj*)got), "%%f64");
-    DECREF(wanted);
-    DECREF(got);
-}
-
-static void
-test_vcatf_x32(TestBatchRunner *runner) {
-    String *wanted;
-    char buf[64];
-    unsigned long num = INT32_MAX;
-    String *got = S_get_str("foo ");
-#if (SIZEOF_LONG == 4)
-    sprintf(buf, "foo bar %.8lx baz", num);
-#elif (SIZEOF_INT == 4)
-    sprintf(buf, "foo bar %.8x baz", (unsigned)num);
-#endif
-    wanted = Str_new_from_trusted_utf8(buf, strlen(buf));
-    Str_catf(got, "bar %x32 baz", (uint32_t)num);
-    TEST_TRUE(runner, Str_Equals(wanted, (Obj*)got), "%%x32");
-    DECREF(wanted);
-    DECREF(got);
-}
-
 void
 TestStr_Run_IMP(TestString *self, TestBatchRunner *runner) {
-    TestBatchRunner_Plan(runner, (TestBatch*)self, 53);
-    test_vcatf_s(runner);
-    test_vcatf_null_string(runner);
-    test_vcatf_str(runner);
-    test_vcatf_obj(runner);
-    test_vcatf_null_obj(runner);
-    test_vcatf_i8(runner);
-    test_vcatf_i32(runner);
-    test_vcatf_i64(runner);
-    test_vcatf_u8(runner);
-    test_vcatf_u32(runner);
-    test_vcatf_u64(runner);
-    test_vcatf_f64(runner);
-    test_vcatf_x32(runner);
+    TestBatchRunner_Plan(runner, (TestBatch*)self, 40);
     test_Cat(runner);
     test_Mimic_and_Clone(runner);
     test_Code_Point_At_and_From(runner);


Mime
View raw message