Issue #81 - Support allocation of large strings

This commit is contained in:
Justin Ethier 2018-01-01 18:59:39 -05:00
parent e95f29aaf8
commit 0951be3d2b
2 changed files with 17 additions and 13 deletions

View file

@ -2,6 +2,10 @@
## 0.7.2 - TBD ## 0.7.2 - TBD
Bug Fixes
- Fixed `utf8->string` and `list->string` to handle strings that exceed the maximum stack object size by allocating them on the heap.
## 0.7.1 - December 21, 2017 ## 0.7.1 - December 21, 2017
Features Features

View file

@ -1859,7 +1859,7 @@ object Cyc_string2symbol(void *data, object str)
object Cyc_list2string(void *data, object cont, object lst) object Cyc_list2string(void *data, object cont, object lst)
{ {
char *buf, cbuf[5]; char *buf, cbuf[5];
int i = 0, len = 0; int i = 0, len = 0, num_cp = 0;
object cbox, tmp = lst; object cbox, tmp = lst;
char_type ch; char_type ch;
@ -1877,16 +1877,19 @@ object Cyc_list2string(void *data, object cont, object lst)
} }
if (!ch) { if (!ch) {
len++; len++;
num_cp++; // Failsafe?
} else { } else {
Cyc_utf8_encode_char(cbuf, 5, ch); Cyc_utf8_encode_char(cbuf, 5, ch);
len += strlen(cbuf); len += strlen(cbuf);
num_cp++;
} }
tmp = cdr(tmp); tmp = cdr(tmp);
} }
{ {
make_string_noalloc(str, NULL, len); object str;
str.str = buf = alloca(sizeof(char) * (len + 1)); alloc_string(data, str, len, num_cp);
buf = ((string_type *)str)->str;
while ((lst != NULL)) { while ((lst != NULL)) {
cbox = car(lst); cbox = car(lst);
ch = obj_obj2char(cbox); // Already validated, can assume chars now ch = obj_obj2char(cbox); // Already validated, can assume chars now
@ -1899,7 +1902,7 @@ object Cyc_list2string(void *data, object cont, object lst)
lst = cdr(lst); lst = cdr(lst);
} }
buf[i] = '\0'; buf[i] = '\0';
_return_closcall1(data, cont, &str); _return_closcall1(data, cont, str);
} }
} }
@ -2656,15 +2659,12 @@ object Cyc_utf82string(void *data, object cont, object bv, object start,
} }
{ {
make_string_noalloc(st, NULL, len); object st;
st.str = alloca(sizeof(char) * (len + 1)); alloc_string(data, st, len, len);
memcpy(st.str, &buf[s], len); memcpy(((string_type *)st)->str, &buf[s], len);
st.str[len] = '\0'; ((string_type *)st)->str[len] = '\0';
st.num_cp = Cyc_utf8_count_code_points((uint8_t *)(st.str)); ((string_type *)st)->num_cp = Cyc_utf8_count_code_points((uint8_t *)(((string_type *)st)->str));
if (st.num_cp < 0) { _return_closcall1(data, cont, st);
Cyc_rt_raise2(data, "utf8->string - error decoding UTF 8", bv);
}
_return_closcall1(data, cont, &st);
} }
} }