mirror of
https://github.com/janet-lang/janet
synced 2024-11-24 17:27:18 +00:00
Merge pull request #1224 from primo-ppcg/array-remove
This commit is contained in:
commit
cd3573a4d2
@ -211,7 +211,7 @@ JANET_CORE_FN(cfun_array_slice,
|
|||||||
"Takes a slice of array or tuple from `start` to `end`. The range is half open, "
|
"Takes a slice of array or tuple from `start` to `end`. The range is half open, "
|
||||||
"[start, end). Indexes can also be negative, indicating indexing from the "
|
"[start, end). Indexes can also be negative, indicating indexing from the "
|
||||||
"end of the array. By default, `start` is 0 and `end` is the length of the array. "
|
"end of the array. By default, `start` is 0 and `end` is the length of the array. "
|
||||||
"Note that index -1 is synonymous with index `(length arrtup)` to allow a full "
|
"Note that if the range is negative, it is taken as (start, end] to allow a full "
|
||||||
"negative slice range. Returns a new array.") {
|
"negative slice range. Returns a new array.") {
|
||||||
JanetView view = janet_getindexed(argv, 0);
|
JanetView view = janet_getindexed(argv, 0);
|
||||||
JanetRange range = janet_getslice(argc, argv);
|
JanetRange range = janet_getslice(argc, argv);
|
||||||
@ -259,8 +259,8 @@ JANET_CORE_FN(cfun_array_insert,
|
|||||||
"(array/insert arr at & xs)",
|
"(array/insert arr at & xs)",
|
||||||
"Insert all `xs` into array `arr` at index `at`. `at` should be an integer between "
|
"Insert all `xs` into array `arr` at index `at`. `at` should be an integer between "
|
||||||
"0 and the length of the array. A negative value for `at` will index backwards from "
|
"0 and the length of the array. A negative value for `at` will index backwards from "
|
||||||
"the end of the array, such that inserting at -1 appends to the array. "
|
"the end of the array, inserting after the index such that inserting at -1 appends to "
|
||||||
"Returns the array.") {
|
"the array. Returns the array.") {
|
||||||
size_t chunksize, restsize;
|
size_t chunksize, restsize;
|
||||||
janet_arity(argc, 2, -1);
|
janet_arity(argc, 2, -1);
|
||||||
JanetArray *array = janet_getarray(argv, 0);
|
JanetArray *array = janet_getarray(argv, 0);
|
||||||
@ -297,7 +297,7 @@ JANET_CORE_FN(cfun_array_remove,
|
|||||||
int32_t at = janet_getinteger(argv, 1);
|
int32_t at = janet_getinteger(argv, 1);
|
||||||
int32_t n = 1;
|
int32_t n = 1;
|
||||||
if (at < 0) {
|
if (at < 0) {
|
||||||
at = array->count + at + 1;
|
at = array->count + at;
|
||||||
}
|
}
|
||||||
if (at < 0 || at > array->count)
|
if (at < 0 || at > array->count)
|
||||||
janet_panicf("removal index %d out of range [0,%d]", at, array->count);
|
janet_panicf("removal index %d out of range [0,%d]", at, array->count);
|
||||||
|
@ -175,8 +175,9 @@ JANET_CORE_FN(cfun_string_slice,
|
|||||||
"Returns a substring from a byte sequence. The substring is from "
|
"Returns a substring from a byte sequence. The substring is from "
|
||||||
"index `start` inclusive to index `end`, exclusive. All indexing "
|
"index `start` inclusive to index `end`, exclusive. All indexing "
|
||||||
"is from 0. `start` and `end` can also be negative to indicate indexing "
|
"is from 0. `start` and `end` can also be negative to indicate indexing "
|
||||||
"from the end of the string. Note that index -1 is synonymous with "
|
"from the end of the string. Note that if `start` is negative it is "
|
||||||
"index `(length bytes)` to allow a full negative slice range. ") {
|
"exclusive, and if `end` is negative it is inclusive, to allow a full "
|
||||||
|
"negative slice range.") {
|
||||||
JanetByteView view = janet_getbytes(argv, 0);
|
JanetByteView view = janet_getbytes(argv, 0);
|
||||||
JanetRange range = janet_getslice(argc, argv);
|
JanetRange range = janet_getslice(argc, argv);
|
||||||
return janet_stringv(view.bytes + range.start, range.end - range.start);
|
return janet_stringv(view.bytes + range.start, range.end - range.start);
|
||||||
|
@ -69,9 +69,9 @@ JANET_CORE_FN(cfun_tuple_slice,
|
|||||||
"inclusive to index `end` exclusive. If `start` or `end` are not provided, "
|
"inclusive to index `end` exclusive. If `start` or `end` are not provided, "
|
||||||
"they default to 0 and the length of `arrtup`, respectively. "
|
"they default to 0 and the length of `arrtup`, respectively. "
|
||||||
"`start` and `end` can also be negative to indicate indexing "
|
"`start` and `end` can also be negative to indicate indexing "
|
||||||
"from the end of the input. Note that index -1 is synonymous with "
|
"from the end of the input. Note that if `start` is negative it is "
|
||||||
"index `(length arrtup)` to allow a full negative slice range. "
|
"exclusive, and if `end` is negative it is inclusive, to allow a full "
|
||||||
"Returns the new tuple.") {
|
"negative slice range. Returns the new tuple.") {
|
||||||
JanetView view = janet_getindexed(argv, 0);
|
JanetView view = janet_getindexed(argv, 0);
|
||||||
JanetRange range = janet_getslice(argc, argv);
|
JanetRange range = janet_getslice(argc, argv);
|
||||||
return janet_wrap_tuple(janet_tuple_n(view.items + range.start, range.end - range.start));
|
return janet_wrap_tuple(janet_tuple_n(view.items + range.start, range.end - range.start));
|
||||||
|
@ -44,7 +44,7 @@
|
|||||||
(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) @[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 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] 2 200) @[1 2]) "array/remove 3")
|
||||||
(assert (deep= (array/remove @[1 2 3 4 5] -3 200) @[1 2 3]) "array/remove 4")
|
(assert (deep= (array/remove @[1 2 3 4 5] -2 200) @[1 2 3]) "array/remove 4")
|
||||||
|
|
||||||
|
|
||||||
# array/peek
|
# array/peek
|
||||||
|
Loading…
Reference in New Issue
Block a user