1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-25 09:47:17 +00:00

Fix string/check-set.

Also change external unification identifier in match macro
to @. This means we can more easily match symbol literals.
This commit is contained in:
Calvin Rose 2019-06-10 14:00:51 -04:00
parent 6225f8d334
commit 647139cdf9
2 changed files with 7 additions and 12 deletions

View File

@ -1130,8 +1130,8 @@
(put seen pattern true) (put seen pattern true)
~(if (= nil (def ,pattern ,expr)) ,sentinel ,(onmatch)))) ~(if (= nil (def ,pattern ,expr)) ,sentinel ,(onmatch))))
(tuple? pattern) (and (tuple? pattern) (= :parens (tuple/type pattern)))
(if (and (= (pattern 0) 'quote) (symbol? (pattern 1))) (if (and (= (pattern 0) '@) (symbol? (pattern 1)))
# Unification with external values # Unification with external values
~(if (= ,(pattern 1) ,expr) ,(onmatch) ,sentinel) ~(if (= ,(pattern 1) ,expr) ,(onmatch) ,sentinel)
(match-1 (match-1
@ -1139,7 +1139,7 @@
(fn [] (fn []
~(if (and ,;(tuple/slice pattern 1)) ,(onmatch) ,sentinel)) seen)) ~(if (and ,;(tuple/slice pattern 1)) ,(onmatch) ,sentinel)) seen))
(array? pattern) (indexed? pattern)
(do (do
(def len (length pattern)) (def len (length pattern))
(var i -1) (var i -1)

View File

@ -393,25 +393,20 @@ static Janet cfun_string_split(int32_t argc, Janet *argv) {
static Janet cfun_string_checkset(int32_t argc, Janet *argv) { static Janet cfun_string_checkset(int32_t argc, Janet *argv) {
uint32_t bitset[8] = {0, 0, 0, 0, 0, 0, 0, 0}; uint32_t bitset[8] = {0, 0, 0, 0, 0, 0, 0, 0};
janet_arity(argc, 2, 3); janet_fixarity(argc, 2);
JanetByteView set = janet_getbytes(argv, 0); JanetByteView set = janet_getbytes(argv, 0);
JanetByteView str = janet_getbytes(argv, 1); JanetByteView str = janet_getbytes(argv, 1);
/* Populate set */ /* Populate set */
for (int32_t i = 0; i < set.len; i++) { for (int32_t i = 0; i < set.len; i++) {
int index = set.bytes[i] >> 5; int index = set.bytes[i] >> 5;
uint32_t mask = 1 << (set.bytes[i] & 7); uint32_t mask = 1 << (set.bytes[i] & 0x1F);
bitset[index] |= mask; bitset[index] |= mask;
} }
if (argc == 3) {
if (janet_getboolean(argv, 2)) {
for (int i = 0; i < 8; i++)
bitset[i] = ~bitset[i];
}
}
/* Check set */ /* Check set */
if (str.len == 0) return janet_wrap_false();
for (int32_t i = 0; i < str.len; i++) { for (int32_t i = 0; i < str.len; i++) {
int index = str.bytes[i] >> 5; int index = str.bytes[i] >> 5;
uint32_t mask = 1 << (str.bytes[i] & 7); uint32_t mask = 1 << (str.bytes[i] & 0x1F);
if (!(bitset[index] & mask)) { if (!(bitset[index] & mask)) {
return janet_wrap_false(); return janet_wrap_false();
} }