1
0
mirror of https://github.com/janet-lang/janet synced 2025-01-13 17:10:27 +00:00

Add buffer/fill. Address #221

This commit is contained in:
Calvin Rose 2019-12-14 10:54:29 -06:00
parent 56784a34a1
commit 76342540dc

View File

@ -182,6 +182,19 @@ static Janet cfun_buffer_new_filled(int32_t argc, Janet *argv) {
return janet_wrap_buffer(buffer); return janet_wrap_buffer(buffer);
} }
static Janet cfun_buffer_fill(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
JanetBuffer *buffer = janet_getbuffer(argv, 0);
int32_t byte = 0;
if (argc == 2) {
byte = janet_getinteger(argv, 1) & 0xFF;
}
if (buffer->count) {
memset(buffer->data, byte, buffer->count);
}
return argv[0];
}
static Janet cfun_buffer_u8(int32_t argc, Janet *argv) { static Janet cfun_buffer_u8(int32_t argc, Janet *argv) {
int32_t i; int32_t i;
janet_arity(argc, 1, -1); janet_arity(argc, 1, -1);
@ -345,8 +358,8 @@ static const JanetReg buffer_cfuns[] = {
{ {
"buffer/new", cfun_buffer_new, "buffer/new", cfun_buffer_new,
JDOC("(buffer/new capacity)\n\n" JDOC("(buffer/new capacity)\n\n"
"Creates a new, empty buffer with enough memory for capacity bytes. " "Creates a new, empty buffer with enough backing memory for capacity bytes. "
"Returns a new buffer.") "Returns a new buffer of length 0.")
}, },
{ {
"buffer/new-filled", cfun_buffer_new_filled, "buffer/new-filled", cfun_buffer_new_filled,
@ -354,6 +367,12 @@ static const JanetReg buffer_cfuns[] = {
"Creates a new buffer of length count filled with byte. By default, byte is 0. " "Creates a new buffer of length count filled with byte. By default, byte is 0. "
"Returns the new buffer.") "Returns the new buffer.")
}, },
{
"buffer/fill", cfun_buffer_fill,
JDOC("(buffer/fill buffer &opt byte)\n\n"
"Fill up a buffer with bytes, defaulting to 0s. Does not change the buffer's length. "
"Returns the modified buffer.")
},
{ {
"buffer/push-byte", cfun_buffer_u8, "buffer/push-byte", cfun_buffer_u8,
JDOC("(buffer/push-byte buffer x)\n\n" JDOC("(buffer/push-byte buffer x)\n\n"