From 7e0c7a1435cc64c9d757b864b1b0f06aa31e4c0f Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Tue, 15 Nov 2016 18:56:25 -0500 Subject: [PATCH] Issue #132 - list->string enhancements - More efficient string construction, only do one alloca/copy - Allows null characters within a created string without truncating string length. --- runtime.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/runtime.c b/runtime.c index 8edaccb5..d45cbc25 100644 --- a/runtime.c +++ b/runtime.c @@ -1594,21 +1594,19 @@ object Cyc_list2string(void *data, object cont, object lst) object len; Cyc_check_pair_or_null(data, lst); - len = Cyc_length(data, lst); // Inefficient, walks whole list - buf = alloca(sizeof(char) * (obj_obj2int(len) + 1)); - while ((lst != NULL)) { - if (!obj_is_char(car(lst))) { - Cyc_rt_raise2(data, "Expected character but received", car(lst)); - } - buf[i++] = obj_obj2char(car(lst)); - lst = cdr(lst); - } - buf[i] = '\0'; - //{ make_string_noalloc(str, buf, i); { - make_string(str, buf); + make_string_noalloc(str, NULL, (obj_obj2int(len))); + str.str = buf = alloca(sizeof(char) * (obj_obj2int(len) + 1)); + while ((lst != NULL)) { + if (!obj_is_char(car(lst))) { + Cyc_rt_raise2(data, "Expected character but received", car(lst)); + } + buf[i++] = obj_obj2char(car(lst)); + lst = cdr(lst); + } + buf[i] = '\0'; _return_closcall1(data, cont, &str); } }