mirror of
https://github.com/janet-lang/janet
synced 2025-01-17 02:42:51 +00:00
Improve error message in slice functions.
Check the first argument before trying to do range checks.
This commit is contained in:
parent
cf2d3861d6
commit
026f26f05f
@ -158,8 +158,8 @@ static Janet cfun_array_ensure(int32_t argc, Janet *argv) {
|
||||
}
|
||||
|
||||
static Janet cfun_array_slice(int32_t argc, Janet *argv) {
|
||||
JanetRange range = janet_getslice(argc, argv);
|
||||
JanetView view = janet_getindexed(argv, 0);
|
||||
JanetRange range = janet_getslice(argc, argv);
|
||||
JanetArray *array = janet_array(range.end - range.start);
|
||||
if (array->data)
|
||||
memcpy(array->data, view.items + range.start, sizeof(Janet) * (range.end - range.start));
|
||||
|
@ -242,8 +242,8 @@ static Janet cfun_buffer_popn(int32_t argc, Janet *argv) {
|
||||
}
|
||||
|
||||
static Janet cfun_buffer_slice(int32_t argc, Janet *argv) {
|
||||
JanetRange range = janet_getslice(argc, argv);
|
||||
JanetByteView view = janet_getbytes(argv, 0);
|
||||
JanetRange range = janet_getslice(argc, argv);
|
||||
JanetBuffer *buffer = janet_buffer(range.end - range.start);
|
||||
if (buffer->data)
|
||||
memcpy(buffer->data, view.bytes + range.start, range.end - range.start);
|
||||
|
@ -343,12 +343,14 @@ static Janet janet_core_array(int32_t argc, Janet *argv) {
|
||||
}
|
||||
|
||||
static Janet janet_core_slice(int32_t argc, Janet *argv) {
|
||||
JanetRange range = janet_getslice(argc, argv);
|
||||
JanetRange range;
|
||||
JanetByteView bview;
|
||||
JanetView iview;
|
||||
if (janet_bytes_view(argv[0], &bview.bytes, &bview.len)) {
|
||||
range = janet_getslice(argc, argv);
|
||||
return janet_stringv(bview.bytes + range.start, range.end - range.start);
|
||||
} else if (janet_indexed_view(argv[0], &iview.items, &iview.len)) {
|
||||
range = janet_getslice(argc, argv);
|
||||
return janet_wrap_tuple(janet_tuple_n(iview.items + range.start, range.end - range.start));
|
||||
} else {
|
||||
janet_panic_type(argv[0], 0, JANET_TFLAG_BYTES | JANET_TFLAG_INDEXED);
|
||||
|
@ -170,8 +170,8 @@ static int32_t kmp_next(struct kmp_state *state) {
|
||||
/* CFuns */
|
||||
|
||||
static Janet cfun_string_slice(int32_t argc, Janet *argv) {
|
||||
JanetRange range = janet_getslice(argc, argv);
|
||||
JanetByteView view = janet_getbytes(argv, 0);
|
||||
JanetRange range = janet_getslice(argc, argv);
|
||||
return janet_stringv(view.bytes + range.start, range.end - range.start);
|
||||
}
|
||||
|
||||
|
@ -100,8 +100,8 @@ static Janet cfun_tuple_brackets(int32_t argc, Janet *argv) {
|
||||
}
|
||||
|
||||
static Janet cfun_tuple_slice(int32_t argc, Janet *argv) {
|
||||
JanetRange range = janet_getslice(argc, argv);
|
||||
JanetView view = janet_getindexed(argv, 0);
|
||||
JanetRange range = janet_getslice(argc, argv);
|
||||
return janet_wrap_tuple(janet_tuple_n(view.items + range.start, range.end - range.start));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user