1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-24 17:27:18 +00:00

Try harder to avoid string copying with janet_getcbytes.

This commit is contained in:
Calvin Rose 2023-06-28 08:30:09 -05:00
parent f6248369fe
commit 3f7a2c2197

View File

@ -219,8 +219,9 @@ const char *janet_getcbytes(const Janet *argv, int32_t n) {
/* Ensure buffer 0-padded */
if (janet_checktype(argv[n], JANET_BUFFER)) {
JanetBuffer *b = janet_unwrap_buffer(argv[n]);
if (b->gc.flags & JANET_BUFFER_FLAG_NO_REALLOC) {
/* Make a copy with janet_smalloc in the rare case we have a buffer that cannot be realloced */
if ((b->gc.flags & JANET_BUFFER_FLAG_NO_REALLOC) && b->count == b->capacity) {
/* Make a copy with janet_smalloc in the rare case we have a buffer that
* cannot be realloced and pushing a 0 byte would panic. */
char *new_string = janet_smalloc(b->count + 1);
memcpy(new_string, b->data, b->count);
new_string[b->count] = 0;