diff --git a/builtins/text.c b/builtins/text.c index 800c555..32e9609 100644 --- a/builtins/text.c +++ b/builtins/text.c @@ -442,10 +442,16 @@ static void u8_buf_append(Text_t text, char **buf, size_t *capacity, int64_t *i) public const char *Text$as_c_string(Text_t text) { - size_t capacity = text.length; + size_t capacity = text.length + 1; char *buf = GC_MALLOC_ATOMIC(capacity); int64_t i = 0; u8_buf_append(text, &buf, &capacity, &i); + + if (i + 1 > (int64_t)capacity) { + capacity = i + 1; + buf = GC_REALLOC(buf, capacity); + } + buf[i] = '\0'; return buf; } diff --git a/test/extern.tm b/test/extern.tm index da3b182..4823b21 100644 --- a/test/extern.tm +++ b/test/extern.tm @@ -1,5 +1,5 @@ -extern CORD_cat:func(a:Text, b:Text)->Text +extern sqrt:func(n:Num)->Num func main(): - >> CORD_cat("hello ", "world") - = "hello world" + >> sqrt(4) + = 2