New stack string macro SSTR_WRAP_C Creates a stack string from a null-terminated C string. Also useful for string literals. (strlen of a literal should be constant folded by the compiler.)
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/6fadaae2 Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/6fadaae2 Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/6fadaae2 Branch: refs/heads/master Commit: 6fadaae2b17c776dac6387147b7d3d8c64f982d2 Parents: 4ea023c Author: Nick Wellnhofer <wellnho...@aevum.de> Authored: Sat Oct 24 16:03:23 2015 +0200 Committer: Nick Wellnhofer <wellnho...@aevum.de> Committed: Wed Oct 28 16:10:35 2015 +0100 ---------------------------------------------------------------------- runtime/core/Clownfish/Class.c | 4 ++-- runtime/core/Clownfish/String.cfh | 8 ++++++++ runtime/core/Clownfish/Test/TestHash.c | 12 ++++++------ runtime/core/Clownfish/Test/TestObj.c | 2 +- runtime/core/Clownfish/Test/TestVector.c | 2 +- 5 files changed, 18 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/6fadaae2/runtime/core/Clownfish/Class.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Class.c b/runtime/core/Clownfish/Class.c index 62d9841..ba83928 100644 --- a/runtime/core/Clownfish/Class.c +++ b/runtime/core/Clownfish/Class.c @@ -206,7 +206,7 @@ Class_bootstrap(const cfish_ClassSpec *specs, size_t num_specs, // Only store novel methods for now. for (size_t i = 0; i < spec->num_novel_meths; ++i) { const NovelMethSpec *mspec = &novel_specs[num_novel++]; - String *name = SSTR_WRAP_UTF8(mspec->name, strlen(mspec->name)); + String *name = SSTR_WRAP_C(mspec->name); Method *method = Method_new(name, mspec->callback_func, *mspec->offset); klass->methods[i] = method; @@ -406,7 +406,7 @@ Class_Add_Host_Method_Alias_IMP(Class *self, const char *alias, fprintf(stderr, "Method %s not found\n", meth_name); abort(); } - String *string = SSTR_WRAP_UTF8(alias, strlen(alias)); + String *string = SSTR_WRAP_C(alias); Method_Set_Host_Alias(method, string); } http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/6fadaae2/runtime/core/Clownfish/String.cfh ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/String.cfh b/runtime/core/Clownfish/String.cfh index 530db6d..a063095 100644 --- a/runtime/core/Clownfish/String.cfh +++ b/runtime/core/Clownfish/String.cfh @@ -18,6 +18,9 @@ parcel Clownfish; __C__ +// For strlen +#include <string.h> + // For CFISH_ALLOCA_OBJ. #include "Clownfish/Class.h" @@ -385,6 +388,10 @@ __C__ #define CFISH_SSTR_BLANK() \ cfish_Str_init_stack_string(CFISH_ALLOCA_OBJ(CFISH_STRING), "", 0) +#define CFISH_SSTR_WRAP_C(ptr) \ + cfish_Str_init_stack_string(CFISH_ALLOCA_OBJ(CFISH_STRING), ptr, \ + strlen(ptr)) + #define CFISH_SSTR_WRAP_UTF8(ptr, size) \ cfish_Str_init_stack_string(CFISH_ALLOCA_OBJ(CFISH_STRING), ptr, size) @@ -393,6 +400,7 @@ __C__ #ifdef CFISH_USE_SHORT_NAMES #define SSTR_BLANK CFISH_SSTR_BLANK + #define SSTR_WRAP_C CFISH_SSTR_WRAP_C #define SSTR_WRAP_UTF8 CFISH_SSTR_WRAP_UTF8 #define STR_OOB CFISH_STR_OOB #define STRITER_DONE CFISH_STRITER_DONE http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/6fadaae2/runtime/core/Clownfish/Test/TestHash.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Test/TestHash.c b/runtime/core/Clownfish/Test/TestHash.c index d3e96f2..e3fb0d7 100644 --- a/runtime/core/Clownfish/Test/TestHash.c +++ b/runtime/core/Clownfish/Test/TestHash.c @@ -40,7 +40,7 @@ static void test_Equals(TestBatchRunner *runner) { Hash *hash = Hash_new(0); Hash *other = Hash_new(0); - String *stuff = SSTR_WRAP_UTF8("stuff", 5); + String *stuff = SSTR_WRAP_C("stuff"); TEST_TRUE(runner, Hash_Equals(hash, (Obj*)other), "Empty hashes are equal"); @@ -68,9 +68,9 @@ test_Store_and_Fetch(TestBatchRunner *runner) { const size_t starting_cap = Hash_Get_Capacity(hash); Vector *expected = Vec_new(100); Vector *got = Vec_new(100); - String *twenty = SSTR_WRAP_UTF8("20", 2); - String *forty = SSTR_WRAP_UTF8("40", 2); - String *foo = SSTR_WRAP_UTF8("foo", 3); + String *twenty = SSTR_WRAP_C("20"); + String *forty = SSTR_WRAP_C("40"); + String *foo = SSTR_WRAP_C("foo"); for (int32_t i = 0; i < 100; i++) { String *str = Str_newf("%i32", i); @@ -161,8 +161,8 @@ test_Keys_Values(TestBatchRunner *runner) { Vec_Clear(values); { - String *forty = SSTR_WRAP_UTF8("40", 2); - String *nope = SSTR_WRAP_UTF8("nope", 4); + String *forty = SSTR_WRAP_C("40"); + String *nope = SSTR_WRAP_C("nope"); TEST_TRUE(runner, Hash_Has_Key(hash, forty), "Has_Key"); TEST_FALSE(runner, Hash_Has_Key(hash, nope), "Has_Key returns false for non-existent key"); http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/6fadaae2/runtime/core/Clownfish/Test/TestObj.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Test/TestObj.c b/runtime/core/Clownfish/Test/TestObj.c index b37ad71..6f9b695 100644 --- a/runtime/core/Clownfish/Test/TestObj.c +++ b/runtime/core/Clownfish/Test/TestObj.c @@ -36,7 +36,7 @@ TestObj_new() { static Obj* S_new_testobj() { - String *class_name = SSTR_WRAP_UTF8("TestObj", 7); + String *class_name = SSTR_WRAP_C("TestObj"); Obj *obj; Class *klass = Class_fetch_class(class_name); if (!klass) { http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/6fadaae2/runtime/core/Clownfish/Test/TestVector.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Test/TestVector.c b/runtime/core/Clownfish/Test/TestVector.c index 30621f0..7183504 100644 --- a/runtime/core/Clownfish/Test/TestVector.c +++ b/runtime/core/Clownfish/Test/TestVector.c @@ -61,7 +61,7 @@ static void test_Equals(TestBatchRunner *runner) { Vector *array = Vec_new(0); Vector *other = Vec_new(0); - String *stuff = SSTR_WRAP_UTF8("stuff", 5); + String *stuff = SSTR_WRAP_C("stuff"); TEST_TRUE(runner, Vec_Equals(array, (Obj*)array), "Array equal to self");