1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-26 08:20:27 +00:00

Add symbol/slice and keyword/slice

This commit is contained in:
Calvin Rose 2020-06-27 15:21:17 -05:00
parent 9c5e97144d
commit d033412b1f
3 changed files with 26 additions and 1 deletions

View File

@ -2,6 +2,9 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## Unreleased - ??? ## Unreleased - ???
- Add `symbol/slice`
- Add `keyword/slice`
- Allow cross compilation with Makefile.
- Change `compare-primitve` to `cmp` and make it more efficient. - Change `compare-primitve` to `cmp` and make it more efficient.
- Change `reverse` to `reversed`, reverse now mutates the backing array - Change `reverse` to `reversed`, reverse now mutates the backing array
- `janet_dobytes` and `janet_dostring` return parse errors in \*out - `janet_dobytes` and `janet_dostring` return parse errors in \*out

View File

@ -176,6 +176,18 @@ static Janet cfun_string_slice(int32_t argc, Janet *argv) {
return janet_stringv(view.bytes + range.start, range.end - range.start); return janet_stringv(view.bytes + range.start, range.end - range.start);
} }
static Janet cfun_symbol_slice(int32_t argc, Janet *argv) {
JanetByteView view = janet_getbytes(argv, 0);
JanetRange range = janet_getslice(argc, argv);
return janet_symbolv(view.bytes + range.start, range.end - range.start);
}
static Janet cfun_keyword_slice(int32_t argc, Janet *argv) {
JanetByteView view = janet_getbytes(argv, 0);
JanetRange range = janet_getslice(argc, argv);
return janet_keywordv(view.bytes + range.start, range.end - range.start);
}
static Janet cfun_string_repeat(int32_t argc, Janet *argv) { static Janet cfun_string_repeat(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2); janet_fixarity(argc, 2);
JanetByteView view = janet_getbytes(argv, 0); JanetByteView view = janet_getbytes(argv, 0);
@ -529,6 +541,16 @@ static const JanetReg string_cfuns[] = {
"from the end of the string. Note that index -1 is synonymous with " "from the end of the string. Note that index -1 is synonymous with "
"index (length bytes) to allow a full negative slice range. ") "index (length bytes) to allow a full negative slice range. ")
}, },
{
"keyword/slice", cfun_keyword_slice,
JDOC("(keyword/slice bytes &opt start end)\n\n"
"Same a string/slice, but returns a keyword.")
},
{
"symbol/slice", cfun_symbol_slice,
JDOC("(symbol/slice bytes &opt start end)\n\n"
"Same a string/slice, but returns a symbol.")
},
{ {
"string/repeat", cfun_string_repeat, "string/repeat", cfun_string_repeat,
JDOC("(string/repeat bytes n)\n\n" JDOC("(string/repeat bytes n)\n\n"

View File

@ -1399,7 +1399,7 @@ int janet_init(void) {
* a collection pretty much every cycle, which is * a collection pretty much every cycle, which is
* incredibly horrible for performance, but can help ensure * incredibly horrible for performance, but can help ensure
* there are no memory bugs during development */ * there are no memory bugs during development */
janet_vm_gc_interval = 0x10000; janet_vm_gc_interval = 0x400000;
janet_symcache_init(); janet_symcache_init();
/* Initialize gc roots */ /* Initialize gc roots */
janet_vm_roots = NULL; janet_vm_roots = NULL;