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.
This commit is contained in:
Justin Ethier 2016-11-15 18:56:25 -05:00
parent 12770291b7
commit 7e0c7a1435

View file

@ -1594,9 +1594,11 @@ object Cyc_list2string(void *data, object cont, object lst)
object len; object len;
Cyc_check_pair_or_null(data, lst); Cyc_check_pair_or_null(data, lst);
len = Cyc_length(data, lst); // Inefficient, walks whole list len = Cyc_length(data, lst); // Inefficient, walks whole list
buf = alloca(sizeof(char) * (obj_obj2int(len) + 1));
{
make_string_noalloc(str, NULL, (obj_obj2int(len)));
str.str = buf = alloca(sizeof(char) * (obj_obj2int(len) + 1));
while ((lst != NULL)) { while ((lst != NULL)) {
if (!obj_is_char(car(lst))) { if (!obj_is_char(car(lst))) {
Cyc_rt_raise2(data, "Expected character but received", car(lst)); Cyc_rt_raise2(data, "Expected character but received", car(lst));
@ -1605,10 +1607,6 @@ object Cyc_list2string(void *data, object cont, object lst)
lst = cdr(lst); lst = cdr(lst);
} }
buf[i] = '\0'; buf[i] = '\0';
//{ make_string_noalloc(str, buf, i);
{
make_string(str, buf);
_return_closcall1(data, cont, &str); _return_closcall1(data, cont, &str);
} }
} }