mirror of
https://github.com/janet-lang/janet
synced 2024-12-25 07:50:27 +00:00
Try harder to avoid string copying with janet_getcbytes.
This commit is contained in:
parent
f6248369fe
commit
3f7a2c2197
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user