2017-11-01 21:53:43 +00:00
|
|
|
/*
|
2019-01-06 08:23:03 +00:00
|
|
|
* Copyright (c) 2019 Calvin Rose
|
2017-11-01 21:53:43 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
#ifndef JANET_AMALG
|
2018-09-06 02:18:42 +00:00
|
|
|
#include <janet/janet.h>
|
2017-12-21 04:03:34 +00:00
|
|
|
#include "gc.h"
|
2019-01-06 06:49:56 +00:00
|
|
|
#include "util.h"
|
2019-01-24 05:15:58 +00:00
|
|
|
#endif
|
2017-11-01 21:53:43 +00:00
|
|
|
|
|
|
|
/* Initialize a buffer */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetBuffer *janet_buffer_init(JanetBuffer *buffer, int32_t capacity) {
|
2017-11-06 14:44:10 +00:00
|
|
|
uint8_t *data = NULL;
|
|
|
|
if (capacity > 0) {
|
|
|
|
data = malloc(sizeof(uint8_t) * capacity);
|
|
|
|
if (NULL == data) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2017-11-06 14:44:10 +00:00
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
buffer->count = 0;
|
|
|
|
buffer->capacity = capacity;
|
2017-11-06 14:44:10 +00:00
|
|
|
buffer->data = data;
|
2017-11-01 21:53:43 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
/* Deinitialize a buffer (free data memory) */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_buffer_deinit(JanetBuffer *buffer) {
|
2017-11-06 03:05:47 +00:00
|
|
|
free(buffer->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize a buffer */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetBuffer *janet_buffer(int32_t capacity) {
|
|
|
|
JanetBuffer *buffer = janet_gcalloc(JANET_MEMORY_BUFFER, sizeof(JanetBuffer));
|
|
|
|
return janet_buffer_init(buffer, capacity);
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Ensure that the buffer has enough internal capacity */
|
2018-11-08 03:27:06 +00:00
|
|
|
void janet_buffer_ensure(JanetBuffer *buffer, int32_t capacity, int32_t growth) {
|
2017-11-06 14:44:10 +00:00
|
|
|
uint8_t *new_data;
|
2017-11-01 21:53:43 +00:00
|
|
|
uint8_t *old = buffer->data;
|
|
|
|
if (capacity <= buffer->capacity) return;
|
2019-01-09 18:25:51 +00:00
|
|
|
int64_t big_capacity = capacity * growth;
|
2019-01-14 05:12:25 +00:00
|
|
|
capacity = big_capacity > INT32_MAX ? INT32_MAX : (int32_t) big_capacity;
|
2017-11-06 14:44:10 +00:00
|
|
|
new_data = realloc(old, capacity * sizeof(uint8_t));
|
|
|
|
if (NULL == new_data) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
2017-11-06 14:44:10 +00:00
|
|
|
buffer->data = new_data;
|
2017-11-01 21:53:43 +00:00
|
|
|
buffer->capacity = capacity;
|
|
|
|
}
|
|
|
|
|
2018-04-30 00:13:04 +00:00
|
|
|
/* Ensure that the buffer has enough internal capacity */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_buffer_setcount(JanetBuffer *buffer, int32_t count) {
|
2018-04-30 00:13:04 +00:00
|
|
|
if (count < 0)
|
|
|
|
return;
|
|
|
|
if (count > buffer->count) {
|
|
|
|
int32_t oldcount = buffer->count;
|
2018-11-08 03:27:06 +00:00
|
|
|
janet_buffer_ensure(buffer, count, 1);
|
2018-04-30 00:13:04 +00:00
|
|
|
memset(buffer->data + oldcount, 0, count - oldcount);
|
|
|
|
}
|
|
|
|
buffer->count = count;
|
|
|
|
}
|
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Adds capacity for enough extra bytes to the buffer. Ensures that the
|
|
|
|
* next n bytes pushed to the buffer will not cause a reallocation */
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_buffer_extra(JanetBuffer *buffer, int32_t n) {
|
2018-01-06 18:25:45 +00:00
|
|
|
/* Check for buffer overflow */
|
|
|
|
if ((int64_t)n + buffer->count > INT32_MAX) {
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panic("buffer overflow");
|
2018-01-06 18:25:45 +00:00
|
|
|
}
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t new_size = buffer->count + n;
|
2017-11-06 14:44:10 +00:00
|
|
|
if (new_size > buffer->capacity) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t new_capacity = new_size * 2;
|
2017-11-06 14:44:10 +00:00
|
|
|
uint8_t *new_data = realloc(buffer->data, new_capacity * sizeof(uint8_t));
|
|
|
|
if (NULL == new_data) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
2017-11-06 14:44:10 +00:00
|
|
|
buffer->data = new_data;
|
|
|
|
buffer->capacity = new_capacity;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Push a cstring to buffer */
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_buffer_push_cstring(JanetBuffer *buffer, const char *cstring) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t len = 0;
|
2017-11-01 21:53:43 +00:00
|
|
|
while (cstring[len]) ++len;
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_buffer_push_bytes(buffer, (const uint8_t *) cstring, len);
|
2018-01-06 18:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Push multiple bytes into the buffer */
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_buffer_push_bytes(JanetBuffer *buffer, const uint8_t *string, int32_t length) {
|
|
|
|
janet_buffer_extra(buffer, length);
|
2018-01-06 18:25:45 +00:00
|
|
|
memcpy(buffer->data + buffer->count, string, length);
|
|
|
|
buffer->count += length;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_buffer_push_string(JanetBuffer *buffer, const uint8_t *string) {
|
|
|
|
janet_buffer_push_bytes(buffer, string, janet_string_length(string));
|
2018-04-28 22:10:57 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Push a single byte to the buffer */
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_buffer_push_u8(JanetBuffer *buffer, uint8_t byte) {
|
|
|
|
janet_buffer_extra(buffer, 1);
|
2017-11-01 21:53:43 +00:00
|
|
|
buffer->data[buffer->count] = byte;
|
2018-01-06 18:25:45 +00:00
|
|
|
buffer->count++;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Push a 16 bit unsigned integer to the buffer */
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_buffer_push_u16(JanetBuffer *buffer, uint16_t x) {
|
|
|
|
janet_buffer_extra(buffer, 2);
|
2017-11-01 21:53:43 +00:00
|
|
|
buffer->data[buffer->count] = x & 0xFF;
|
|
|
|
buffer->data[buffer->count + 1] = (x >> 8) & 0xFF;
|
2018-01-06 18:25:45 +00:00
|
|
|
buffer->count += 2;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Push a 32 bit unsigned integer to the buffer */
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_buffer_push_u32(JanetBuffer *buffer, uint32_t x) {
|
|
|
|
janet_buffer_extra(buffer, 4);
|
2017-11-01 21:53:43 +00:00
|
|
|
buffer->data[buffer->count] = x & 0xFF;
|
|
|
|
buffer->data[buffer->count + 1] = (x >> 8) & 0xFF;
|
|
|
|
buffer->data[buffer->count + 2] = (x >> 16) & 0xFF;
|
|
|
|
buffer->data[buffer->count + 3] = (x >> 24) & 0xFF;
|
2018-01-06 18:25:45 +00:00
|
|
|
buffer->count += 4;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Push a 64 bit unsigned integer to the buffer */
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_buffer_push_u64(JanetBuffer *buffer, uint64_t x) {
|
|
|
|
janet_buffer_extra(buffer, 8);
|
2017-11-01 21:53:43 +00:00
|
|
|
buffer->data[buffer->count] = x & 0xFF;
|
|
|
|
buffer->data[buffer->count + 1] = (x >> 8) & 0xFF;
|
|
|
|
buffer->data[buffer->count + 2] = (x >> 16) & 0xFF;
|
|
|
|
buffer->data[buffer->count + 3] = (x >> 24) & 0xFF;
|
|
|
|
buffer->data[buffer->count + 4] = (x >> 32) & 0xFF;
|
|
|
|
buffer->data[buffer->count + 5] = (x >> 40) & 0xFF;
|
|
|
|
buffer->data[buffer->count + 6] = (x >> 48) & 0xFF;
|
|
|
|
buffer->data[buffer->count + 7] = (x >> 56) & 0xFF;
|
2018-01-06 18:25:45 +00:00
|
|
|
buffer->count += 8;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
2018-01-18 22:25:45 +00:00
|
|
|
|
|
|
|
/* C functions */
|
2018-05-09 21:01:58 +00:00
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_buffer_new(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2019-01-06 01:09:03 +00:00
|
|
|
int32_t cap = janet_getinteger(argv, 0);
|
|
|
|
JanetBuffer *buffer = janet_buffer(cap);
|
|
|
|
return janet_wrap_buffer(buffer);
|
2018-05-09 21:01:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_buffer_new_filled(int32_t argc, Janet *argv) {
|
2019-01-09 18:25:51 +00:00
|
|
|
janet_arity(argc, 1, 2);
|
|
|
|
int32_t count = janet_getinteger(argv, 0);
|
|
|
|
int32_t byte = 0;
|
|
|
|
if (argc == 2) {
|
|
|
|
byte = janet_getinteger(argv, 1) & 0xFF;
|
|
|
|
}
|
|
|
|
JanetBuffer *buffer = janet_buffer(count);
|
|
|
|
memset(buffer->data, byte, count);
|
|
|
|
buffer->count = count;
|
|
|
|
return janet_wrap_buffer(buffer);
|
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_buffer_u8(int32_t argc, Janet *argv) {
|
2018-01-18 22:25:45 +00:00
|
|
|
int32_t i;
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_arity(argc, 1, -1);
|
|
|
|
JanetBuffer *buffer = janet_getbuffer(argv, 0);
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
janet_buffer_push_u8(buffer, (uint8_t) (janet_getinteger(argv, i) & 0xFF));
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
return argv[0];
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_buffer_word(int32_t argc, Janet *argv) {
|
2018-01-18 22:25:45 +00:00
|
|
|
int32_t i;
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_arity(argc, 1, -1);
|
|
|
|
JanetBuffer *buffer = janet_getbuffer(argv, 0);
|
|
|
|
for (i = 1; i < argc; i++) {
|
2019-01-26 01:31:08 +00:00
|
|
|
double number = janet_getnumber(argv, i);
|
2019-01-06 01:09:03 +00:00
|
|
|
uint32_t word = (uint32_t) number;
|
|
|
|
if (word != number)
|
2019-01-26 01:31:08 +00:00
|
|
|
janet_panicf("cannot convert %v to machine word", argv[i]);
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_buffer_push_u32(buffer, word);
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
return argv[0];
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_buffer_chars(int32_t argc, Janet *argv) {
|
2018-01-18 22:25:45 +00:00
|
|
|
int32_t i;
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_arity(argc, 1, -1);
|
|
|
|
JanetBuffer *buffer = janet_getbuffer(argv, 0);
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
JanetByteView view = janet_getbytes(argv, i);
|
|
|
|
janet_buffer_push_bytes(buffer, view.bytes, view.len);
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
return argv[0];
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_buffer_clear(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetBuffer *buffer = janet_getbuffer(argv, 0);
|
2018-01-28 20:29:47 +00:00
|
|
|
buffer->count = 0;
|
2019-01-06 01:09:03 +00:00
|
|
|
return argv[0];
|
2018-01-28 20:29:47 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_buffer_popn(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 2);
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetBuffer *buffer = janet_getbuffer(argv, 0);
|
|
|
|
int32_t n = janet_getinteger(argv, 1);
|
|
|
|
if (n < 0) janet_panic("n must be non-negative");
|
2018-04-30 00:13:04 +00:00
|
|
|
if (buffer->count < n) {
|
2018-03-26 17:36:58 +00:00
|
|
|
buffer->count = 0;
|
|
|
|
} else {
|
2018-04-30 00:13:04 +00:00
|
|
|
buffer->count -= n;
|
2018-03-26 17:36:58 +00:00
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
return argv[0];
|
2018-03-26 17:36:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_buffer_slice(int32_t argc, Janet *argv) {
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetRange range = janet_getslice(argc, argv);
|
|
|
|
JanetByteView view = janet_getbytes(argv, 0);
|
|
|
|
JanetBuffer *buffer = janet_buffer(range.end - range.start);
|
|
|
|
memcpy(buffer->data, view.bytes + range.start, range.end - range.start);
|
|
|
|
buffer->count = range.end - range.start;
|
|
|
|
return janet_wrap_buffer(buffer);
|
2018-02-07 18:19:34 +00:00
|
|
|
}
|
|
|
|
|
2019-01-09 18:25:51 +00:00
|
|
|
static void bitloc(int32_t argc, Janet *argv, JanetBuffer **b, int32_t *index, int *bit) {
|
|
|
|
janet_fixarity(argc, 2);
|
|
|
|
JanetBuffer *buffer = janet_getbuffer(argv, 0);
|
|
|
|
double x = janet_getnumber(argv, 1);
|
|
|
|
int64_t bitindex = (int64_t) x;
|
|
|
|
int64_t byteindex = bitindex >> 3;
|
|
|
|
int which_bit = bitindex & 7;
|
|
|
|
if (bitindex != x || bitindex < 0 || byteindex >= buffer->count)
|
|
|
|
janet_panicf("invalid bit index %v", argv[1]);
|
|
|
|
*b = buffer;
|
2019-01-14 05:12:25 +00:00
|
|
|
*index = (int32_t) byteindex;
|
2019-01-09 18:25:51 +00:00
|
|
|
*bit = which_bit;
|
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_buffer_bitset(int32_t argc, Janet *argv) {
|
2019-01-09 18:25:51 +00:00
|
|
|
int bit;
|
|
|
|
int32_t index;
|
|
|
|
JanetBuffer *buffer;
|
|
|
|
bitloc(argc, argv, &buffer, &index, &bit);
|
|
|
|
buffer->data[index] |= 1 << bit;
|
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_buffer_bitclear(int32_t argc, Janet *argv) {
|
2019-01-09 18:25:51 +00:00
|
|
|
int bit;
|
|
|
|
int32_t index;
|
|
|
|
JanetBuffer *buffer;
|
|
|
|
bitloc(argc, argv, &buffer, &index, &bit);
|
|
|
|
buffer->data[index] &= ~(1 << bit);
|
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_buffer_bitget(int32_t argc, Janet *argv) {
|
2019-01-09 18:25:51 +00:00
|
|
|
int bit;
|
|
|
|
int32_t index;
|
|
|
|
JanetBuffer *buffer;
|
|
|
|
bitloc(argc, argv, &buffer, &index, &bit);
|
|
|
|
return janet_wrap_boolean(buffer->data[index] & (1 << bit));
|
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_buffer_bittoggle(int32_t argc, Janet *argv) {
|
2019-01-09 18:25:51 +00:00
|
|
|
int bit;
|
|
|
|
int32_t index;
|
|
|
|
JanetBuffer *buffer;
|
|
|
|
bitloc(argc, argv, &buffer, &index, &bit);
|
|
|
|
buffer->data[index] ^= (1 << bit);
|
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_buffer_blit(int32_t argc, Janet *argv) {
|
2019-01-09 18:25:51 +00:00
|
|
|
janet_arity(argc, 2, 5);
|
|
|
|
JanetBuffer *dest = janet_getbuffer(argv, 0);
|
|
|
|
JanetByteView src = janet_getbytes(argv, 1);
|
|
|
|
int32_t offset_dest = 0;
|
|
|
|
int32_t offset_src = 0;
|
|
|
|
if (argc > 2)
|
|
|
|
offset_dest = janet_gethalfrange(argv, 2, dest->count, "dest-start");
|
|
|
|
if (argc > 3)
|
|
|
|
offset_src = janet_gethalfrange(argv, 3, src.len, "src-start");
|
|
|
|
int32_t length_src;
|
|
|
|
if (argc > 4) {
|
|
|
|
int32_t src_end = janet_gethalfrange(argv, 4, src.len, "src-end");
|
|
|
|
length_src = src_end - offset_src;
|
|
|
|
if (length_src < 0) length_src = 0;
|
|
|
|
} else {
|
|
|
|
length_src = src.len - offset_src;
|
|
|
|
}
|
|
|
|
int64_t last = ((int64_t) offset_dest - offset_src) + length_src;
|
|
|
|
if (last > INT32_MAX)
|
|
|
|
janet_panic("buffer blit out of range");
|
2019-01-14 05:12:25 +00:00
|
|
|
janet_buffer_ensure(dest, (int32_t) last, 2);
|
|
|
|
if (last > dest->count) dest->count = (int32_t) last;
|
2019-01-09 18:25:51 +00:00
|
|
|
memcpy(dest->data + offset_dest, src.bytes + offset_src, length_src);
|
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static const JanetReg buffer_cfuns[] = {
|
|
|
|
{"buffer/new", cfun_buffer_new,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(buffer/new capacity)\n\n"
|
|
|
|
"Creates a new, empty buffer with enough memory for capacity bytes. "
|
|
|
|
"Returns a new buffer.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-01-24 05:15:58 +00:00
|
|
|
{"buffer/new-filled", cfun_buffer_new_filled,
|
2019-01-09 18:25:51 +00:00
|
|
|
JDOC("(buffer/new-filled count [, byte=0])\n\n"
|
|
|
|
"Creates a new buffer of length count filled with byte. "
|
|
|
|
"Returns the new buffer.")
|
|
|
|
},
|
2019-01-24 05:15:58 +00:00
|
|
|
{"buffer/push-byte", cfun_buffer_u8,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(buffer/push-byte buffer x)\n\n"
|
|
|
|
"Append a byte to a buffer. Will expand the buffer as necessary. "
|
|
|
|
"Returns the modified buffer. Will throw an error if the buffer overflows.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-01-24 05:15:58 +00:00
|
|
|
{"buffer/push-word", cfun_buffer_word,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(buffer/push-word buffer x)\n\n"
|
|
|
|
"Append a machine word to a buffer. The 4 bytes of the integer are appended "
|
|
|
|
"in twos complement, big endian order, unsigned. Returns the modified buffer. Will "
|
|
|
|
"throw an error if the buffer overflows.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-01-24 05:15:58 +00:00
|
|
|
{"buffer/push-string", cfun_buffer_chars,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(buffer/push-string buffer str)\n\n"
|
|
|
|
"Push a string onto the end of a buffer. Non string values will be converted "
|
|
|
|
"to strings before being pushed. Returns the modified buffer. "
|
|
|
|
"Will throw an error if the buffer overflows.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-01-24 05:15:58 +00:00
|
|
|
{"buffer/popn", cfun_buffer_popn,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(buffer/popn buffer n)\n\n"
|
|
|
|
"Removes the last n bytes from the buffer. Returns the modified buffer.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-01-24 05:15:58 +00:00
|
|
|
{"buffer/clear", cfun_buffer_clear,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(buffer/clear buffer)\n\n"
|
|
|
|
"Sets the size of a buffer to 0 and empties it. The buffer retains "
|
|
|
|
"its memory so it can be efficiently refilled. Returns the modified buffer.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-01-24 05:15:58 +00:00
|
|
|
{"buffer/slice", cfun_buffer_slice,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(buffer/slice bytes [, start=0 [, end=(length bytes)]])\n\n"
|
|
|
|
"Takes a slice of a byte sequence from start to end. The range is half open, "
|
|
|
|
"[start, end). Indexes can also be negative, indicating indexing from the end of the "
|
|
|
|
"end of the array. By default, start is 0 and end is the length of the buffer. "
|
|
|
|
"Returns a new buffer.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-01-24 05:15:58 +00:00
|
|
|
{"buffer/bit-set", cfun_buffer_bitset,
|
2019-01-09 18:25:51 +00:00
|
|
|
JDOC("(buffer/bit-set buffer index)\n\n"
|
|
|
|
"Sets the bit at the given bit-index. Returns the buffer.")
|
|
|
|
},
|
2019-01-24 05:15:58 +00:00
|
|
|
{"buffer/bit-clear", cfun_buffer_bitclear,
|
2019-01-09 18:25:51 +00:00
|
|
|
JDOC("(buffer/bit-clear buffer index)\n\n"
|
|
|
|
"Clears the bit at the given bit-index. Returns the buffer.")
|
|
|
|
},
|
2019-01-24 05:15:58 +00:00
|
|
|
{"buffer/bit", cfun_buffer_bitget,
|
2019-01-09 18:25:51 +00:00
|
|
|
JDOC("(buffer/bit buffer index)\n\n"
|
|
|
|
"Gets the bit at the given bit-index. Returns true if the bit is set, false if not.")
|
|
|
|
},
|
2019-01-24 05:15:58 +00:00
|
|
|
{"buffer/bit-toggle", cfun_buffer_bittoggle,
|
2019-01-09 18:25:51 +00:00
|
|
|
JDOC("(buffer/bit-toggle buffer index)\n\n"
|
|
|
|
"Toggles the bit at the given bit index in buffer. Returns the buffer.")
|
|
|
|
},
|
2019-01-24 05:15:58 +00:00
|
|
|
{"buffer/blit", cfun_buffer_blit,
|
2019-01-09 18:25:51 +00:00
|
|
|
JDOC("(buffer/blit dest src [, dest-start=0 [, src-start=0 [, src-end=-1]]])\n\n"
|
|
|
|
"Insert the contents of src into dest. Can optionally take indices that "
|
|
|
|
"indicate which part of src to copy into which part of dest. Indices can be "
|
|
|
|
"negative to index from the end of src or dest. Returns dest.")
|
|
|
|
},
|
2018-11-15 20:45:41 +00:00
|
|
|
{NULL, NULL, NULL}
|
2018-01-28 20:29:47 +00:00
|
|
|
};
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_lib_buffer(JanetTable *env) {
|
2019-01-24 05:15:58 +00:00
|
|
|
janet_cfuns(env, NULL, buffer_cfuns);
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|