1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-23 06:50:26 +00:00

slight refactoring

This commit is contained in:
primo-ppcg 2023-08-03 21:48:59 +07:00
parent d8b45ecd61
commit 8a70fb95b5
2 changed files with 15 additions and 17 deletions

View File

@ -433,24 +433,21 @@ JANET_CORE_FN(janet_core_range,
"a range [start, end). With three, returns a range with optional step size.") {
janet_arity(argc, 1, 3);
int32_t start = 0, stop = 0, step = 1, count = 0;
switch (argc) {
case 1:
stop = janet_getinteger(argv, 0);
count = stop;
break;
case 2:
start = janet_getinteger(argv, 0);
stop = janet_getinteger(argv, 1);
count = stop - start;
break;
case 3:
start = janet_getinteger(argv, 0);
stop = janet_getinteger(argv, 1);
step = janet_getinteger(argv, 2);
count = (step > 0) ? (stop - start - 1) / step + 1 :
((step < 0) ? (stop - start + 1) / step + 1 : 0);
break;
if (argc == 3) {
start = janet_getinteger(argv, 0);
stop = janet_getinteger(argv, 1);
step = janet_getinteger(argv, 2);
count = (step > 0) ? (stop - start - 1) / step + 1 :
((step < 0) ? (stop - start + 1) / step + 1 : 0);
} else if (argc == 2) {
start = janet_getinteger(argv, 0);
stop = janet_getinteger(argv, 1);
count = stop - start;
} else {
stop = janet_getinteger(argv, 0);
count = stop;
}
count = (count > 0) ? count : 0;
JanetArray *array = janet_array(count);
for (int32_t i = 0; i < count; i++) {
array->data[i] = janet_wrap_number(start + i * step);

View File

@ -169,6 +169,7 @@
(assert (deep= (range 17 0 -4) @[17 13 9 5 1]) "(range 17 0 -4)")
(assert (= (length (range 10)) 10) "(range 10)")
(assert (= (length (range -10)) 0) "(range -10)")
(assert (= (length (range 1 10)) 9) "(range 1 10)")
(end-suite)