1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-26 00:10:27 +00:00

Add array/remove and update CHANGELOG.

This commit is contained in:
Calvin Rose 2019-03-08 10:17:57 -05:00
parent 951aa0d8cd
commit 687a3c91f5
4 changed files with 44 additions and 2 deletions

View File

@ -1,6 +1,9 @@
# Changelog
All notable changes to this project will be documented in this file.
## 0.4.1 latest - ??
- Add array/remove function
## 0.4.0 - 2019-03-08
- Fix a number of smaller bugs
- Added :export option to import and require

View File

@ -212,7 +212,32 @@ static Janet cfun_array_insert(int32_t argc, Janet *argv) {
restsize);
memcpy(array->data + at, argv + 2, chunksize);
array->count += (argc - 2);
return janet_wrap_array(array);
return argv[0];
}
static Janet cfun_array_remove(int32_t argc, Janet *argv) {
janet_arity(argc, 2, 3);
JanetArray *array = janet_getarray(argv, 0);
int32_t at = janet_getinteger(argv, 1);
int32_t n = 1;
if (at < 0) {
at = array->count + at + 1;
}
if (at < 0 || at > array->count)
janet_panicf("removal index %d out of range [0,%d]", at, array->count);
if (argc == 3) {
n = janet_getinteger(argv, 2);
if (n < 0)
janet_panicf("expected non-negative integer for argument n, got %v", argv[2]);
}
if (at + n > array->count) {
n = array->count - at;
}
memmove(array->data + at,
array->data + at + n,
(array->count - at - n) * sizeof(Janet));
array->count -= n;
return argv[0];
}
static const JanetReg array_cfuns[] = {
@ -270,6 +295,13 @@ static const JanetReg array_cfuns[] = {
"the end of the array, such that inserting at -1 appends to the array. "
"Returns the array.")
},
{
"array/remove", cfun_array_remove,
JDOC("(array/remove arr at [, n=1])\n\n"
"Remove up to n elements starting at index at in array arr. at can index from "
"the end of the array with a negative index, and n must be a non-negative integer. "
"Returns the array.")
},
{NULL, NULL, NULL}
};

View File

@ -29,7 +29,7 @@ extern "C" {
/***** START SECTION CONFIG *****/
#define JANET_VERSION "0.4.0"
#define JANET_VERSION "0.4.1"
#ifndef JANET_BUILD
#define JANET_BUILD "local"

View File

@ -57,5 +57,12 @@
(assert (= ((unmarshal (marshal b)) 3) (b 3)) "marshal")
# Array remove
(assert (deep= (array/remove @[1 2 3 4 5] 2) @[1 2 4 5]) "array/remove 1")
(assert (deep= (array/remove @[1 2 3 4 5] 2 2) @[1 2 5]) "array/remove 2")
(assert (deep= (array/remove @[1 2 3 4 5] 2 200) @[1 2]) "array/remove 3")
(assert (deep= (array/remove @[1 2 3 4 5] -3 200) @[1 2 3]) "array/remove 4")
(end-suite)