mirror of
https://github.com/janet-lang/janet
synced 2025-02-04 11:19:10 +00:00
Make slice a c function.
This will allow future integration into the compiler for more general destructuring.
This commit is contained in:
parent
6ceaf9d28d
commit
cf2d3861d6
@ -1169,11 +1169,6 @@
|
||||
(if (not= i len) (array/push ret (slicer ind i)))
|
||||
ret)
|
||||
|
||||
(defn slice
|
||||
"Extract a sub-range of an indexed data strutrue or byte sequence."
|
||||
[ind &opt start end]
|
||||
((if (bytes? ind) string/slice tuple/slice) ind start end))
|
||||
|
||||
###
|
||||
###
|
||||
### IO Helpers
|
||||
|
@ -342,6 +342,19 @@ static Janet janet_core_array(int32_t argc, Janet *argv) {
|
||||
return janet_wrap_array(array);
|
||||
}
|
||||
|
||||
static Janet janet_core_slice(int32_t argc, Janet *argv) {
|
||||
JanetRange range = janet_getslice(argc, argv);
|
||||
JanetByteView bview;
|
||||
JanetView iview;
|
||||
if (janet_bytes_view(argv[0], &bview.bytes, &bview.len)) {
|
||||
return janet_stringv(bview.bytes + range.start, range.end - range.start);
|
||||
} else if (janet_indexed_view(argv[0], &iview.items, &iview.len)) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
static Janet janet_core_table(int32_t argc, Janet *argv) {
|
||||
int32_t i;
|
||||
if (argc & 1)
|
||||
@ -665,6 +678,11 @@ static const JanetReg corelib_cfuns[] = {
|
||||
JDOC("(nat? x)\n\n"
|
||||
"Check if x can be exactly represented as a non-negative 32 bit signed two's complement integer.")
|
||||
},
|
||||
{
|
||||
"slice", janet_core_slice,
|
||||
JDOC("(slice x &opt start end)\n\n"
|
||||
"Extract a sub-range of an indexed data strutrue or byte sequence.")
|
||||
},
|
||||
{NULL, NULL, NULL}
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user