Skip to content

Commit

Permalink
slight refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
primo-ppcg committed Aug 3, 2023
1 parent d8b45ec commit 381891f
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions src/core/corelib.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,23 +433,19 @@ 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;
}
JanetArray *array = janet_array(count);
for (int32_t i = 0; i < count; i++) {
Expand Down

0 comments on commit 381891f

Please sign in to comment.